vagrant-box-updater 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in vagrant-box-updater.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 spil-ruslan
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # vagrant-box-updater
2
+
3
+ vagrant-box-updater - Vagrant (version 1.1+) plugin save details about box images, checks remote box images for updates, notify user and perform interactive update
4
+
5
+ By default Vagrant just store box image to localdisk (during box add) and never checks if there are updates for that image, so users may end up working with outdated boxes.
6
+
7
+ This plugin meant to notify user about new versions of remote box images available, and provide interactive prompt to download updates.
8
+
9
+
10
+ vagrant plugin hooks into :
11
+
12
+ "vagrant box add" - save details about remote box image: image creation timestamp, image source path (box data stored in yaml format inside ~/.vagrant.d/$box_name.stat);
13
+
14
+ "vagrant up" - checks source box url for updates (use remote file modification date to define whether image updated or not),
15
+ when update found - print message and optionally perform interactive download.
16
+
17
+
18
+ ## Installation
19
+
20
+ Note: plugin is written for Vagrant version 1.1 and higher
21
+
22
+ To install from gem repository:
23
+
24
+ vagrant plugin install vagrant-box-updater
25
+
26
+ To install from source code:
27
+
28
+ 1. Clone project
29
+ 2. run "rake build" - should create gem file
30
+ 3. install plugin to vagrant environment "sudo vagrant plugin install pkg/vagrant-box-updater-0.0.1.gem"
31
+
32
+ ## Usage
33
+
34
+ After plugin installed it's recommended to re-add box images (vagrant box add -f ) so plugin can collect necessary information about box (such as source url and creation timestamp).
35
+
36
+ It is possible to disable plugin by adding configuration parameter to Vagrantfile:
37
+
38
+ config.box_updater.disable = true
39
+
40
+ ## Contributing
41
+
42
+ 1. Fork it
43
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
44
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
45
+ 4. Push to the branch (`git push origin my-new-feature`)
46
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,2 @@
1
+ require "vagrant"
2
+ require "vagrant-box-updater/plugin"
@@ -0,0 +1,45 @@
1
+ require 'yaml'
2
+
3
+ module VagrantPlugins
4
+ module BoxUpdater
5
+ module Action
6
+ class AddBox
7
+
8
+ def initialize(app, env)
9
+ @app = app
10
+ end
11
+
12
+ def call(env)
13
+ box_url = env[:box_url]
14
+ box_name = env[:box_name]
15
+
16
+ begin
17
+ remote_modification_date = get_remote_modification_date?(box_url)
18
+ rescue
19
+ env[:ui].error("Unable access: #{box_url}")
20
+ env[:ui].error("Can not collect image status, please check box url and repeat action")
21
+ @app.call(env)
22
+ return 0
23
+ end
24
+
25
+ @box_attributes = {"modification_date" => remote_modification_date, "url" => box_url}
26
+ env[:ui].info("Box details: #{@box_attributes.to_yaml}")
27
+ stat_file = env[:home_path].join(box_name + ".stat")
28
+ File.open(stat_file, 'w+') {|f| f.write(@box_attributes.to_yaml) }
29
+ @app.call(env)
30
+ end
31
+
32
+ def get_remote_modification_date?(url)
33
+ require 'open-uri'
34
+ require 'net/http'
35
+ url = URI.parse(url)
36
+ Net::HTTP.start(url.host, url.port) do |http|
37
+ response = http.head(url.request_uri)
38
+ return response['Last-Modified']
39
+ end
40
+ end
41
+
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,127 @@
1
+ require 'yaml'
2
+
3
+ module VagrantPlugins
4
+ module BoxUpdater
5
+ module Action
6
+ class UpBox
7
+
8
+ def initialize(app, env)
9
+ @app = app
10
+ end
11
+
12
+ def call(env)
13
+ require 'time'
14
+ box_name = env[:machine].config.vm.box
15
+ box_url = env[:machine].config.vm.box_url
16
+
17
+ disable_plugin_flag = env[:machine].config.box_updater.disable
18
+ if disable_plugin_flag == true
19
+ env[:ui].warn("Update disabled")
20
+ @app.call(env)
21
+ return 0
22
+ end
23
+
24
+ stat_file = env[:home_path].join(box_name + ".stat")
25
+
26
+ # Create empty stat file if missing
27
+ if !File.file?(stat_file)
28
+ env[:ui].info("Local stat file not found: #{stat_file}")
29
+ @box_attributes = {"modification_date" => nil, "url" => box_url}
30
+ puts @box_attributes.to_yaml
31
+ File.open(stat_file, 'w+') {|f| f.write(@box_attributes.to_yaml) }
32
+ end
33
+
34
+ box_stats = YAML.load_file(stat_file)
35
+
36
+ present_modification_date = box_stats["modification_date"]
37
+ box_url = box_stats["url"]
38
+
39
+ if present_modification_date == nil
40
+ env[:ui].warn("Local box image \"modification_date\" is not set")
41
+ env[:ui].warn("Unable to check remote box")
42
+ env[:ui].warn("Please, add box again \"vagrant box add\" so it'll update image data")
43
+ @app.call(env)
44
+ return 0
45
+ end
46
+
47
+ if box_url == nil
48
+ env[:ui].warn("Local box url is not set")
49
+ env[:ui].warn("Unable to check remote box")
50
+ env[:ui].warn("Please, add box again \"vagrant box add\" so it'll update image data")
51
+ @app.call(env)
52
+ return 0
53
+ end
54
+
55
+ begin
56
+ env[:ui].info("Verify remote box modification date: #{box_url}")
57
+ remote_modification_date = get_remote_modification_date?(box_url)
58
+ rescue
59
+ env[:ui].warn("Unable access: #{box_url}")
60
+ env[:ui].warn("Skip remote box check")
61
+ @app.call(env)
62
+ return 0
63
+ end
64
+
65
+ if remote_modification_date == nil
66
+ env[:ui].warn("Can not retrieve 'Last-Modified' attribute for: #{box_url}")
67
+ env[:ui].warn("Skip remote box check")
68
+ @app.call(env)
69
+ return 0
70
+ end
71
+
72
+ remote_modification_timestamp = Time.parse(remote_modification_date)
73
+ present_modification_timestamp = Time.parse(present_modification_date)
74
+
75
+ env[:ui].info("Remote box timestamp #{remote_modification_timestamp}")
76
+ env[:ui].info("Local box timestamp #{present_modification_timestamp}")
77
+
78
+ if present_modification_timestamp < remote_modification_timestamp
79
+ env[:ui].warn("Updated image detected!!!!")
80
+ if ask_confirm(env,"Would you like to update the box? (Y/N)")
81
+ env[:ui].info("Going to update and replace box \"#{box_name}\" now!")
82
+ provider = nil
83
+ env[:action_runner].run(Vagrant::Action.action_box_add, {
84
+ :box_name => box_name,
85
+ :box_provider => provider,
86
+ :box_url => box_url,
87
+ :box_force => true,
88
+ :box_download_insecure => true,
89
+ })
90
+ else
91
+ env[:ui].warn("Update disabled")
92
+ @app.call(env)
93
+ end
94
+ end
95
+
96
+ env[:ui].info("Box is uptodate")
97
+ @app.call(env)
98
+ end
99
+
100
+ private
101
+
102
+ def ask_confirm(env, message)
103
+ choice = nil
104
+ # If we have a force key set and we're forcing, then set
105
+ # the result to "Y"
106
+ choice = "Y" if @force_key && env[@force_key]
107
+ # If we haven't chosen yes, then ask the user via TTY
108
+ choice = env[:ui].ask(message) if !choice
109
+ # The result is only true if the user said "Y"
110
+ result = choice && choice.upcase == "Y"
111
+ return result
112
+ end
113
+
114
+ def get_remote_modification_date?(url)
115
+ require 'open-uri'
116
+ require 'net/http'
117
+ url = URI.parse(url)
118
+ Net::HTTP.start(url.host, url.port) do |http|
119
+ response = http.head(url.request_uri)
120
+ return response['Last-Modified']
121
+ end
122
+ end
123
+
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,19 @@
1
+ module VagrantPlugins
2
+ module BoxUpdater
3
+ class Config < Vagrant.plugin('2', :config)
4
+ attr_accessor :disable
5
+
6
+ def initialize
7
+ @disable = false
8
+ end
9
+
10
+ def finalize!
11
+ @disable = false if @disable == UNSET_VALUE
12
+ end
13
+
14
+ def validate(machine)
15
+ {}
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,24 @@
1
+ module VagrantPlugins
2
+ module BoxUpdater
3
+ class Plugin < Vagrant.plugin('2')
4
+ name 'Vagrant BoxUpdater'
5
+ description 'Plugin allows to check source box for updates and interactively prompt user when update is available.'
6
+
7
+ config(:box_updater) do
8
+ require_relative 'config'
9
+ Config
10
+ end
11
+
12
+ action_hook 'do-before-boot' do |hook|
13
+ require_relative 'action/up_box'
14
+ hook.before ::Vagrant::Action::Builtin::ConfigValidate, VagrantPlugins::BoxUpdater::Action::UpBox
15
+ end
16
+
17
+ action_hook 'on_update' do |hook|
18
+ require_relative 'action/add_box'
19
+ hook.after ::Vagrant::Action::Builtin::BoxAdd, VagrantPlugins::BoxUpdater::Action::AddBox
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ module Vagrant
2
+ module BoxUpdater
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require 'vagrant-box-updater'
@@ -0,0 +1,53 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'vagrant-box-updater/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "vagrant-box-updater"
8
+ gem.version = Vagrant::BoxUpdater::VERSION
9
+ gem.authors = ["Ruslan Lutsenko"]
10
+ gem.email = ["ruslan.lutcenko@gmail.com"]
11
+ gem.description = "vagrant plugin which save details about added box image (image creation timestamp image source path), during start of virtual machine checks the source url of a box image for updates (use 'Last-Modified' header to detect changes), notify user and perform interactive download of the box image if update detected"
12
+ gem.summary = "vagrant plugin to monitor and notify about updates of remote box images"
13
+ gem.homepage = ""
14
+
15
+ #gem.files = `git ls-files`.split($/)
16
+ #gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ #gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ #gem.require_paths = ["lib"]
19
+ # The following block of code determines the files that should be included
20
+ # in the gem. It does this by reading all the files in the directory where
21
+ # this gemspec is, and parsing out the ignored files from the gitignore.
22
+ # Note that the entire gitignore(5) syntax is not supported, specifically
23
+ # the "!" syntax, but it should mostly work correctly.
24
+ root_path = File.dirname(__FILE__)
25
+ all_files = Dir.chdir(root_path) { Dir.glob("**/{*,.*}") }
26
+ all_files.reject! { |file| [".", ".."].include?(File.basename(file)) }
27
+ gitignore_path = File.join(root_path, ".gitignore")
28
+ gitignore = File.readlines(gitignore_path)
29
+ gitignore.map! { |line| line.chomp.strip }
30
+ gitignore.reject! { |line| line.empty? || line =~ /^(#|!)/ }
31
+
32
+ unignored_files = all_files.reject do |file|
33
+ # Ignore any directories, the gemspec only cares about files
34
+ next true if File.directory?(file)
35
+
36
+ # Ignore any paths that match anything in the gitignore. We do
37
+ # two tests here:
38
+ #
39
+ # - First, test to see if the entire path matches the gitignore.
40
+ # - Second, match if the basename does, this makes it so that things
41
+ # like '.DS_Store' will match sub-directories too (same behavior
42
+ # as git).
43
+ #
44
+ gitignore.any? do |ignore|
45
+ File.fnmatch(ignore, file, File::FNM_PATHNAME) ||
46
+ File.fnmatch(ignore, File.basename(file), File::FNM_PATHNAME)
47
+ end
48
+ end
49
+
50
+ gem.files = unignored_files
51
+ gem.executables = unignored_files.map { |f| f[/^bin\/(.*)/, 1] }.compact
52
+ gem.require_path = 'lib'
53
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vagrant-box-updater
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ruslan Lutsenko
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-15 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: vagrant plugin which save details about added box image (image creation
15
+ timestamp image source path), during start of virtual machine checks the source
16
+ url of a box image for updates (use 'Last-Modified' header to detect changes), notify
17
+ user and perform interactive download of the box image if update detected
18
+ email:
19
+ - ruslan.lutcenko@gmail.com
20
+ executables: []
21
+ extensions: []
22
+ extra_rdoc_files: []
23
+ files:
24
+ - Gemfile
25
+ - vagrant-box-updater.gemspec
26
+ - lib/vagrant-box-updater/version.rb
27
+ - lib/vagrant-box-updater/action/up_box.rb
28
+ - lib/vagrant-box-updater/action/add_box.rb
29
+ - lib/vagrant-box-updater/config.rb
30
+ - lib/vagrant-box-updater/plugin.rb
31
+ - lib/vagrant_init.rb
32
+ - lib/vagrant-box-updater.rb
33
+ - Rakefile
34
+ - LICENSE.txt
35
+ - README.md
36
+ - .gitignore
37
+ homepage: ''
38
+ licenses: []
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 1.8.23
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: vagrant plugin to monitor and notify about updates of remote box images
61
+ test_files: []