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.



10
11
12
13
# File 'lib/docker-sync/update_check.rb', line 10

def initialize
  @config = DockerSync::GlobalConfig.load
  @newer_image_found = false
end

Instance Method Details

#check_and_warn(update_enforced = true) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/docker-sync/update_check.rb', line 110

def check_and_warn(update_enforced = true)
  return if ENV['DOCKER_SYNC_SKIP_UPDATE']
  # update the timestamp
  @config.update! 'update_last_check' => DateTime.now.iso8601(9)

  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



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

def check_rsync_image
  return if ENV['DOCKER_SYNC_SKIP_UPDATE']
  say_status 'ok','Checking if a newer rsync image is available'

  if system("docker pull eugenmayer/rsync | grep 'Downloaded newer image for'")
    say_status 'ok', 'Downloaded newer image for rsync', :green
    @newer_image_found = true
  else
    say_status 'ok', 'No newer image found - current image is up to date.'
  end

end

#check_unison_hostsync_image(silent = false) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/docker-sync/update_check.rb', line 72

def check_unison_hostsync_image(silent = false)
  return if ENV['DOCKER_SYNC_SKIP_UPDATE']
  say_status 'ok','Checking if a newer native_osx (unison:hostsync_0.2) image is available' unless silent

  if system("docker pull eugenmayer/unison:hostsync_0.2 | grep 'Downloaded newer image for'")
    say_status 'ok', 'Downloaded newer image for native_osx', :green unless silent
    @newer_image_found = true
  else
    say_status 'ok', 'No newer image found - current image is up to date.' unless silent
  end

end

#check_unison_imageObject



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/docker-sync/update_check.rb', line 85

def check_unison_image
  return if ENV['DOCKER_SYNC_SKIP_UPDATE']
  say_status 'ok','Checking if a newer unison image is available'

  if system("docker pull eugenmayer/unison | grep 'Downloaded newer image for'")
    say_status 'ok', 'Downloaded newer image for unison', :green
    @newer_image_found = true
  else
    say_status 'ok', 'No newer image found - current image is up to date.'
  end

end

#docker_sync_update_checkObject



103
104
105
106
107
108
# File 'lib/docker-sync/update_check.rb', line 103

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



98
99
100
101
# File 'lib/docker-sync/update_check.rb', line 98

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

#has_internet?Boolean

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/docker-sync/update_check.rb', line 42

def has_internet?
  `ping -c1 -t 1 8.8.8.8 > /dev/null 2>&1`
  return $?.success?
end

#runObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/docker-sync/update_check.rb', line 15

def run
  return if ENV['DOCKER_SYNC_SKIP_UPDATE']
  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

  # do not check the image if its the first run - since this it will be downloaded anyway
  unless @config.first_run?
    unless has_internet?
      check_unison_hostsync_image
      check_unison_image
      check_rsync_image
      # stop if there was an update
      if @newer_image_found
        say_status 'warning', 'One or more images have been updated. Please use "docker-sync clean" before you start docker-sync again', :red
        exit 0
      end
    end
  end

  check_and_warn(@config['update_enforce'])
end

#should_runObject



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/docker-sync/update_check.rb', line 47

def should_run
  return false unless has_internet?
  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