Class: UpdateChecker

Inherits:
Object
  • Object
show all
Includes:
Thor::Shell
Defined in:
lib/docker-sync/update_check.rb

Instance Method Summary collapse

Constructor Details

#initializeUpdateChecker

Returns a new instance of UpdateChecker.



8
9
10
# File 'lib/docker-sync/update_check.rb', line 8

def initialize
  @config = DockerSyncConfig::global_config
end

Instance Method Details

#check_and_warn(update_enforced = true) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/docker-sync/update_check.rb', line 59

def check_and_warn(update_enforced = true)
  # update the timestamp
  now = DateTime.now
  @config['update_last_check'] = now.iso8601(9)
  DockerSyncConfig::global_config_save(@config)

  check = docker_sync_update_check
  if check.update_available
    say_status 'warning',"There is an update (#{check.latest_version}) available (current version #{check.current_version}). Please update before you continue",:yellow
    if yes?("Shall i update docker-sync to #{check.latest_version} for you?")
      system('gem update docker-sync')
      say_status 'success','Successfully updated, please restart docker-sync and check the changelog at https://github.com/EugenMayer/docker-sync/wiki/5.-Changelog',:green
      exit 0
    else
      exit 1 if update_enforced
    end
  end
end

#check_rsync_imageObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/docker-sync/update_check.rb', line 35

def check_rsync_image
  say_status 'ok','Checking if a newer rsync image is available'

  if system("docker pull eugenmayer/rsync | grep 'Downloaded newer image for'")
    say_status 'warning', 'Downloaded newer image for rsync', :red
    say_status 'warning', 'Please use "docker-sync clean" before you start docker-sync again', :red

    exit 0
  end
  say_status 'success','Image is (now) up to date'
end

#docker_sync_update_checkObject



52
53
54
55
56
57
# File 'lib/docker-sync/update_check.rb', line 52

def docker_sync_update_check
  gem_name = 'docker-sync'
  current_version = get_current_version
  checker = GemUpdateChecker::Client.new(gem_name, current_version)
  return checker
end

#get_current_versionObject



47
48
49
50
# File 'lib/docker-sync/update_check.rb', line 47

def get_current_version
  path = File.expand_path('../../../', __FILE__)
  return File.read("#{path}/VERSION")
end

#runObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/docker-sync/update_check.rb', line 12

def run
  unless @config['update_check']
    say_status 'hint','Skipping up-to-date check since it has been disabled in your ~/.docker-sync-global.yml configuration',:yellow
    return
  end
  unless should_run
    return
  end
  check_rsync_image unless DockerSyncConfig::is_first_run # do not check the image if its the first run - since this it will be downloaded anyway
  check_and_warn(@config['update_enforce'])
end

#should_runObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/docker-sync/update_check.rb', line 24

def should_run
  now = DateTime.now
  last_check = DateTime.iso8601(@config['update_last_check'])
  check_after_days = 2
  if now - last_check > check_after_days
    return true
  end

  return false
end