vagrant-state-change-listener 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 250dac67bd8c4ec0ab8956a24897a327e0464ae7
4
+ data.tar.gz: d2bb6f4fe11d65518488802c7d2732ed03adca34
5
+ SHA512:
6
+ metadata.gz: 9b3202668593d0146a25d82fefff502a082984a1680e0e5e9aa975e51a06fe3bee6c7b267a0d1d4c57a59143eb82aaae9ca498679563118c8bb1ebc752e90032
7
+ data.tar.gz: 393b4afdce38de278e874228132208bd8fa5120737f42dd1e555f37a53e26c5843a9365eb799c259973f11af2cd05440a12857d8b7dd42ccabef695bf97c7b60
@@ -0,0 +1,50 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ Gemfile.lock
46
+ .ruby-version
47
+ .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :development do
4
+ gem 'vagrant', git: 'https://github.com/mitchellh/vagrant.git'
5
+ end
6
+
7
+ group :plugins do
8
+ gem 'vagrant-state-change-listener', path: '.'
9
+ end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Alexey Chirukhin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,2 @@
1
+ # vagrant-state-change-listener
2
+ Vagrant plugin for listening vm state changes and executing actions on changes.
@@ -0,0 +1,5 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ # This installs the tasks that help with gem creation and publishing
5
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,16 @@
1
+ require 'pathname'
2
+ require 'vagrant-state-change-listener/plugin'
3
+
4
+ module VagrantPlugins
5
+ module StateChangeListener
6
+ lib_path = Pathname.new(File.expand_path('../vagrant-state-change-listener', __FILE__))
7
+
8
+ # Returns the path to the source of this plugins
9
+ def self.source_root
10
+ @source_root ||= Pathname.new(File.expand_path('../../', __FILE__))
11
+ end
12
+
13
+ I18n.load_path << File.expand_path('locales/en.yml', source_root)
14
+ I18n.reload!
15
+ end
16
+ end
@@ -0,0 +1,119 @@
1
+ require "vagrant/util/busy"
2
+
3
+ module VagrantPlugins
4
+ module StateChangeListener
5
+ module Command
6
+ class Listen < Vagrant.plugin("2", :command)
7
+
8
+ def execute
9
+ options = {}
10
+
11
+ opts = OptionParser.new do |o|
12
+ o.banner = 'Usage: vagrant listen-state-changes <vm name> <actions> [options]'
13
+ o.separator ''
14
+ o.separator '<vm name> - name of virtual machine'
15
+ o.separator '<actions> - shell commands'
16
+ o.separator ''
17
+ o.separator 'Options:'
18
+ o.on('-f', '--first-state FSTATE', "If vm changes it's state from FSTATE, plugin executes <actions>") do |s|
19
+ options[:first_state] = s
20
+ end
21
+ o.on('-s', '--second-state SSTATE', "If vm changes it's state to SSTATE, plugin executes <actions>") do |s|
22
+ options[:second_state] = s
23
+ end
24
+ o.on('-l', '--latency SECONDS', Integer, 'Delay (in seconds) between checking for changes. Default: 1 sec') do |l|
25
+ options[:latency] = l
26
+ end
27
+ o.on('-a', '--auto-stop', Integer, 'Stop listening after first state change (after executing <actions>)') do |l|
28
+ options[:auto_stop] = true
29
+ end
30
+ o.separator ''
31
+ end
32
+ opts.parse!
33
+
34
+ # ARGV shoul be [command, vm name, actions]
35
+ if ARGV.length != 3
36
+ puts opts
37
+ exit
38
+ end
39
+
40
+ if (!options[:first_state].nil? && !options[:second_state].nil? && options[:first_state] == options[:second_state])
41
+ puts opts
42
+ @env.ui.error(I18n.t('vagrant-state-change-listener.command.error.states-should-be-different'))
43
+ exit
44
+ end
45
+
46
+ first_state = options[:first_state]
47
+ second_state = options[:second_state]
48
+ latency = options[:latency].nil? ? 1 : options[:latency]
49
+ auto_stop = !options[:auto_stop].nil?
50
+ machine_name = ARGV[1]
51
+ actions = ARGV[2]
52
+ machine = nil
53
+
54
+ with_target_vms(machine_name) do |m|
55
+ machine = m
56
+ end
57
+ if machine.state.id == :not_created
58
+ @env.ui.error(I18n.t('vagrant-state-change-listener.command.error.vm-not-created',
59
+ vm_name: machine_name
60
+ ))
61
+ exit
62
+ end
63
+
64
+ # The callback that lets us know when we have been interrupted
65
+ queue = Queue.new
66
+ interrupt_callback = lambda do
67
+ # This needs to execute in another thread because Thread
68
+ # synchronization can't happen in a trap context
69
+ Thread.new { queue << true }
70
+ end
71
+
72
+ # Run the code in a busy block so that we can cleanly
73
+ # exit once we receive an interrupt
74
+ Vagrant::Util::Busy.busy(interrupt_callback) do
75
+ Thread.new do
76
+ prev_state = nil
77
+ loop do
78
+ curr_state = machine.state.short_description
79
+
80
+ if (!prev_state.nil? && prev_state != curr_state && # state changed
81
+ (first_state.nil? || prev_state == first_state) && # see if prev state match option FSTATE
82
+ (second_state.nil? || second_state == curr_state) # see if curr state match option SSTATE
83
+ )
84
+ @env.ui.info(I18n.t('vagrant-state-change-listener.command.info.state-changed',
85
+ f_state: prev_state,
86
+ s_state: curr_state
87
+ ))
88
+ @env.ui.info(I18n.t('vagrant-state-change-listener.command.info.executing-actions',
89
+ actions: actions
90
+ ))
91
+
92
+ status = system(actions)
93
+
94
+ @env.ui.info(I18n.t('vagrant-state-change-listener.command.info.executed-actions-status',
95
+ status: status
96
+ ))
97
+
98
+ if (auto_stop)
99
+ @env.ui.info(I18n.t('vagrant-state-change-listener.command.info.auto-stop'))
100
+ interrupt_callback.call()
101
+ break
102
+ end
103
+
104
+ @env.ui.info("")
105
+ end
106
+
107
+ prev_state = curr_state
108
+ sleep latency
109
+ end
110
+ end
111
+ # This will block main thread untill callback is called
112
+ queue.pop
113
+ end
114
+
115
+ end # def execute
116
+ end
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,22 @@
1
+ begin
2
+ require 'vagrant'
3
+ rescue LoadError
4
+ raise 'This plugin must run within Vagrant.'
5
+ end
6
+
7
+ module VagrantPlugins
8
+ module StateChangeListener
9
+ class Plugin < Vagrant.plugin('2')
10
+ name 'vagrant-state-change-listener'
11
+
12
+ description <<-DESC
13
+ This plugin is listening vm state changes and executes actions on changes
14
+ DESC
15
+
16
+ command 'listen-state-changes' do
17
+ require_relative "command"
18
+ Command::Listen
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ module VagrantPlugins
2
+ module StateChangeListener
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,11 @@
1
+ en:
2
+ vagrant-state-change-listener:
3
+ command:
4
+ info:
5
+ state-changed: "VM state changed from '%{f_state}' to '%{s_state}'"
6
+ executing-actions: "Executing actions: '%{actions}'"
7
+ executed-actions-status: "Actions executed successfully: %{status}"
8
+ auto-stop: "Stopping listening ..."
9
+ error:
10
+ states-should-be-different: "FSTATE and SSTATE should be different"
11
+ vm-not-created: "The machine with the name '%{vm_name}' is not created"
@@ -0,0 +1,25 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'vagrant-state-change-listener/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'vagrant-state-change-listener'
7
+ s.version = VagrantPlugins::StateChangeListener::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.date = '2018-10-01'
10
+ s.description = 'Vagrant plugin for listening vm state changes and executing actions on changes'
11
+ s.summary = s.description
12
+ s.homepage = 'https://github.com/pr3sto/vagrant-state-change-listener'
13
+ s.license = 'MIT'
14
+
15
+ s.authors = ['Alexey Chirukhin']
16
+ s.email = 'pr3sto1377@gmail.com'
17
+
18
+ s.files = `git ls-files`.split($\)
19
+ s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
20
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
21
+ s.require_paths = ['lib']
22
+
23
+ s.add_development_dependency 'rake'
24
+ s.add_development_dependency 'rspec'
25
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vagrant-state-change-listener
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexey Chirukhin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-10-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Vagrant plugin for listening vm state changes and executing actions on
42
+ changes
43
+ email: pr3sto1377@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE
51
+ - README.md
52
+ - Rakefile
53
+ - lib/vagrant-state-change-listener.rb
54
+ - lib/vagrant-state-change-listener/command.rb
55
+ - lib/vagrant-state-change-listener/plugin.rb
56
+ - lib/vagrant-state-change-listener/version.rb
57
+ - locales/en.yml
58
+ - vagrant-state-change-listener.gemspec
59
+ homepage: https://github.com/pr3sto/vagrant-state-change-listener
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.6.14
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Vagrant plugin for listening vm state changes and executing actions on changes
83
+ test_files: []