vagrant-notify 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -0
  3. data/.travis.yml +2 -4
  4. data/Gemfile +15 -3
  5. data/Gemfile.lock +51 -36
  6. data/README.md +4 -8
  7. data/burn/old_stack.rb +11 -0
  8. data/lib/vagrant-notify/action/check_provider.rb +19 -0
  9. data/lib/vagrant-notify/action/install_command.rb +58 -0
  10. data/lib/vagrant-notify/action/prepare_data.rb +40 -0
  11. data/lib/vagrant-notify/action/server_is_running.rb +28 -0
  12. data/lib/vagrant-notify/action/start_server.rb +24 -0
  13. data/lib/vagrant-notify/action/stop_server.rb +22 -0
  14. data/lib/vagrant-notify/action.rb +56 -0
  15. data/lib/vagrant-notify/data.rb +23 -0
  16. data/lib/vagrant-notify/plugin.rb +29 -0
  17. data/lib/vagrant-notify/server.rb +25 -9
  18. data/lib/vagrant-notify/version.rb +1 -1
  19. data/lib/vagrant-notify.rb +2 -15
  20. data/spec/action/install_command_spec.rb +40 -0
  21. data/spec/action/prepare_data_spec.rb +22 -0
  22. data/spec/action/server_is_running_spec.rb +44 -0
  23. data/spec/action/start_server_spec.rb +29 -0
  24. data/spec/action/stop_server_spec.rb +29 -0
  25. data/spec/data_spec.rb +35 -0
  26. data/spec/server_spec.rb +7 -4
  27. data/spec/spec_helper.rb +5 -3
  28. data/vagrant-notify.gemspec +0 -5
  29. metadata +36 -92
  30. data/lib/vagrant-notify/middleware.rb +0 -3
  31. data/lib/vagrant-notify/vagrant_ssh_ext.rb +0 -22
  32. data/lib/vagrant_init.rb +0 -4
  33. /data/{lib/vagrant-notify → burn}/middleware/install_command.rb +0 -0
  34. /data/{lib/vagrant-notify → burn}/middleware/start_server.rb +0 -0
  35. /data/{lib/vagrant-notify → burn}/middleware/stop_server.rb +0 -0
  36. /data/{spec → burn/spec}/middleware/install_command_spec.rb +0 -0
  37. /data/{spec → burn/spec}/middleware/start_server_spec.rb +0 -0
  38. /data/{spec → burn/spec}/middleware/stop_server_spec.rb +0 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a1074a71950090d90ae0b29f1a84c15ceb70ba2e
4
+ data.tar.gz: 9da0d636fd6ebe6f8023ed14d2338574a90c5868
5
+ SHA512:
6
+ metadata.gz: 4aa952173645b5a6e165b180fbd4111c8e06dac056673813d3a658b09a4c19fb284e675233a414d605a92fe63b32aae2d7daec2c57e2ab5596e86b7ee8643723
7
+ data.tar.gz: 7858e8b543ff5236af42f2c32c894bf3980814979fb94d5eb8c6d41329e9fe4410809a19961173fb5f6f37167a66f546bd8f82aea33af5b967ef56372d2d390c
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ /.vagrant
data/.travis.yml CHANGED
@@ -1,7 +1,5 @@
1
1
  language: ruby
2
- before_script:
3
- - sudo apt-get -y install linux-headers-$(uname -r)
4
- - sudo apt-get install virtualbox
2
+ bundler_args: --without development
5
3
  rvm:
6
4
  - 1.9.3
7
- - 1.8.7
5
+ - 2.0.0
data/Gemfile CHANGED
@@ -3,6 +3,18 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in vagrant-notify.gemspec
4
4
  gemspec
5
5
 
6
- gem 'rb-inotify'
7
- gem 'guard'
8
- gem 'guard-rspec'
6
+ group :development do
7
+ gem 'rb-inotify'
8
+ gem 'guard'
9
+ gem 'guard-rspec'
10
+ end
11
+
12
+ group :development, :test do
13
+ # We depend on Vagrant for development, but we don't add it as a
14
+ # gem dependency because we expect to be installed within the
15
+ # Vagrant environment itself using `vagrant plugin`.
16
+ gem 'vagrant', github: 'mitchellh/vagrant'
17
+ gem 'rake'
18
+ gem 'rspec'
19
+ gem 'rspec-spies'
20
+ end
data/Gemfile.lock CHANGED
@@ -1,53 +1,68 @@
1
+ GIT
2
+ remote: git://github.com/mitchellh/vagrant.git
3
+ revision: 11ad0392758b6b3f4b8a44332c66a79e94cef1eb
4
+ specs:
5
+ vagrant (1.1.3.dev)
6
+ childprocess (~> 0.3.7)
7
+ erubis (~> 2.7.0)
8
+ i18n (~> 0.6.0)
9
+ json (>= 1.5.1, < 1.8.0)
10
+ log4r (~> 1.1.9)
11
+ net-scp (~> 1.1.0)
12
+ net-ssh (~> 2.6.6)
13
+
1
14
  PATH
2
15
  remote: .
3
16
  specs:
4
- vagrant-notify (0.1.0)
17
+ vagrant-notify (0.2.0)
5
18
 
6
19
  GEM
7
20
  remote: https://rubygems.org/
8
21
  specs:
9
- archive-tar-minitar (0.5.2)
10
- childprocess (0.3.6)
11
- ffi (~> 1.0, >= 1.0.6)
12
- diff-lcs (1.1.3)
22
+ childprocess (0.3.9)
23
+ ffi (~> 1.0, >= 1.0.11)
24
+ coderay (1.0.9)
25
+ diff-lcs (1.2.1)
13
26
  erubis (2.7.0)
14
- ffi (1.1.5)
15
- guard (1.4.0)
16
- listen (>= 0.4.2)
27
+ ffi (1.5.0)
28
+ guard (1.6.2)
29
+ listen (>= 0.6.0)
30
+ lumberjack (>= 1.0.2)
31
+ pry (>= 0.9.10)
32
+ terminal-table (>= 1.4.3)
17
33
  thor (>= 0.14.6)
18
- guard-rspec (2.1.0)
34
+ guard-rspec (2.5.1)
19
35
  guard (>= 1.1)
20
36
  rspec (~> 2.11)
21
- i18n (0.6.1)
22
- json (1.5.4)
23
- listen (0.5.3)
37
+ i18n (0.6.4)
38
+ json (1.7.7)
39
+ listen (0.7.3)
24
40
  log4r (1.1.10)
25
- net-scp (1.0.4)
26
- net-ssh (>= 1.99.1)
27
- net-ssh (2.2.2)
28
- rake (0.9.2.2)
29
- rb-inotify (0.8.8)
41
+ lumberjack (1.0.3)
42
+ method_source (0.8.1)
43
+ net-scp (1.1.0)
44
+ net-ssh (>= 2.6.5)
45
+ net-ssh (2.6.6)
46
+ pry (0.9.12)
47
+ coderay (~> 1.0.5)
48
+ method_source (~> 0.8)
49
+ slop (~> 3.4)
50
+ rake (10.0.3)
51
+ rb-inotify (0.9.0)
30
52
  ffi (>= 0.5.0)
31
- rspec (2.11.0)
32
- rspec-core (~> 2.11.0)
33
- rspec-expectations (~> 2.11.0)
34
- rspec-mocks (~> 2.11.0)
35
- rspec-core (2.11.1)
36
- rspec-expectations (2.11.3)
37
- diff-lcs (~> 1.1.3)
38
- rspec-mocks (2.11.3)
53
+ rspec (2.13.0)
54
+ rspec-core (~> 2.13.0)
55
+ rspec-expectations (~> 2.13.0)
56
+ rspec-mocks (~> 2.13.0)
57
+ rspec-core (2.13.1)
58
+ rspec-expectations (2.13.0)
59
+ diff-lcs (>= 1.1.3, < 2.0)
60
+ rspec-mocks (2.13.0)
39
61
  rspec-spies (2.1.3)
40
62
  rspec (~> 2.0)
41
- thor (0.16.0)
42
- vagrant (1.0.5)
43
- archive-tar-minitar (= 0.5.2)
44
- childprocess (~> 0.3.1)
45
- erubis (~> 2.7.0)
46
- i18n (~> 0.6.0)
47
- json (~> 1.5.1)
48
- log4r (~> 1.1.9)
49
- net-scp (~> 1.0.4)
50
- net-ssh (~> 2.2.2)
63
+ slop (3.4.4)
64
+ terminal-table (1.4.5)
65
+ thor (0.17.0)
51
66
 
52
67
  PLATFORMS
53
68
  ruby
@@ -59,5 +74,5 @@ DEPENDENCIES
59
74
  rb-inotify
60
75
  rspec
61
76
  rspec-spies
62
- vagrant
77
+ vagrant!
63
78
  vagrant-notify!
data/README.md CHANGED
@@ -8,17 +8,13 @@ using Ubuntu as guest and host machine.
8
8
 
9
9
  ## Installation
10
10
 
11
- If you use the gem version of Vagrant, use:
11
+ Make sure you have Vagrant >= 1.1 around and run:
12
12
 
13
13
  ```terminal
14
- $ gem install vagrant-notify
14
+ $ vagrant plugin install vagrant-notify
15
15
  ```
16
16
 
17
- otherwise, use:
18
17
 
19
- ```terminal
20
- $ vagrant gem install vagrant-notify
21
- ```
22
18
 
23
19
  ## Usage
24
20
 
@@ -31,8 +27,8 @@ command.
31
27
  If by any chance your IP changes, you can run `vagrant provision` in order to
32
28
  update the guest script with the new IP.
33
29
 
34
-
35
-
30
+ As of now, the plugin is not supported on multi vm environments, it used to be
31
+ for the Vagrant < 1.1 version and I'm planning to fix that as soon as I have a chance.
36
32
 
37
33
 
38
34
  ## Contributing
data/burn/old_stack.rb ADDED
@@ -0,0 +1,11 @@
1
+ Vagrant.actions[:start].tap do |start|
2
+ start.insert_after Vagrant::Action::VM::Boot, Vagrant::Notify::Middleware::StartServer
3
+ start.insert_after Vagrant::Notify::Middleware::StartServer, Vagrant::Notify::Middleware::InstallCommand
4
+ end
5
+ Vagrant.actions[:resume].tap do |start|
6
+ start.use Vagrant::Notify::Middleware::StartServer
7
+ start.use Vagrant::Notify::Middleware::InstallCommand
8
+ end
9
+ Vagrant.actions[:halt].use Vagrant::Notify::Middleware::StopServer
10
+ Vagrant.actions[:suspend].use Vagrant::Notify::Middleware::StopServer
11
+ Vagrant.actions[:provision].use Vagrant::Notify::Middleware::InstallCommand
@@ -0,0 +1,19 @@
1
+ module Vagrant
2
+ module Notify
3
+ module Action
4
+ class CheckProvider
5
+ def initialize(app, env)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ env[:result] = true
11
+
12
+ # Call the next if we have one (but we shouldn't, since this
13
+ # middleware is built to run with the Call-type middlewares)
14
+ @app.call env
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,58 @@
1
+ module Vagrant
2
+ module Notify
3
+ module Action
4
+ class InstallCommand
5
+ def initialize(app, env)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ path = compile_command(env)
11
+ install_command_on_guest(env, path)
12
+
13
+ @app.call env
14
+ end
15
+
16
+ private
17
+
18
+ def compile_command(env)
19
+ host_port = env[:notify_data][:port]
20
+ template_binding = OpenStruct.new(:host_ip => local_ip, :host_port => host_port)
21
+ command_template = ERB.new(Vagrant::Notify.files_path.join('notify-send.erb').read)
22
+ command = command_template.result(template_binding.instance_eval { binding })
23
+
24
+ env[:tmp_path].join('vagrant-notify-send').open('w') { |f| f.write(command) }
25
+ end
26
+
27
+ def install_command_on_guest(env, command_path)
28
+ source = env[:tmp_path].join 'vagrant-notify-send'
29
+ env[:machine].communicate.upload(source.to_s, '/tmp/notify-send')
30
+ env[:machine].communicate.sudo('mv /tmp/notify-send /usr/bin/notify-send && chmod +x /usr/bin/notify-send')
31
+ end
32
+
33
+ ##
34
+ # Returns the local IP address of the host running the vagrant VMs.
35
+ #
36
+ # Thanks to:
37
+ # https://github.com/fnichol/vagrant-butter/blob/master/lib/vagrant/butter/helpers.rb
38
+ # https://github.com/jedi4ever/veewee/blob/c75a5b175c5b8ac7e5aa3341e93493923d0c7af0/lib/veewee/session.rb#L622
39
+ #
40
+ # @return [String] the local IP address
41
+ def local_ip
42
+ @local_ip ||= begin
43
+ # turn off reverse DNS resolution temporarily
44
+ orig, Socket.do_not_reverse_lookup =
45
+ Socket.do_not_reverse_lookup, true
46
+
47
+ UDPSocket.open do |s|
48
+ s.connect '64.233.187.99', 1
49
+ s.addr.last
50
+ end
51
+ ensure
52
+ Socket.do_not_reverse_lookup = orig
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,40 @@
1
+ require_relative '../data'
2
+
3
+ module Vagrant
4
+ module Notify
5
+ module Action
6
+ class PrepareData
7
+ def initialize(app, env)
8
+ @app = app
9
+ @logger = Log4r::Logger.new("vagrant::notify::action::prepare_data")
10
+ end
11
+
12
+ def call(env)
13
+ @env = env
14
+ @data_dir = @env[:machine].data_dir.join('vagrant-notify')
15
+
16
+ ensure_dir_exists
17
+ assign_data_object
18
+
19
+ @app.call env
20
+ end
21
+
22
+ private
23
+
24
+ def ensure_dir_exists
25
+ return if @data_dir.directory?
26
+
27
+ @logger.debug 'Creating data directory'
28
+ @data_dir.mkdir
29
+ end
30
+
31
+ def assign_data_object
32
+ return if @env[:notify_data]
33
+
34
+ @logger.debug 'Assigning data object'
35
+ @env[:notify_data] = Data.new(@data_dir)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,28 @@
1
+ module Vagrant
2
+ module Notify
3
+ module Action
4
+ class ServerIsRunning
5
+ def initialize(app, env)
6
+ @app = app
7
+ @logger = Log4r::Logger.new("vagrant::notify::action::prepare_data_dir")
8
+ end
9
+
10
+ def call(env)
11
+ env[:result] = valid_process?(env[:notify_data][:pid])
12
+
13
+ # Call the next if we have one (but we shouldn't, since this
14
+ # middleware is built to run with the Call-type middlewares)
15
+ @app.call env
16
+ end
17
+
18
+ private
19
+
20
+ def valid_process?(pid)
21
+ Process.getpgid(pid.to_i) if pid
22
+ rescue Errno::ESRCH
23
+ false
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,24 @@
1
+ require_relative '../server'
2
+
3
+ module Vagrant
4
+ module Notify
5
+ module Action
6
+ class StartServer
7
+ # TODO: This should come from vm configuration or be automatically
8
+ # assigned
9
+ PORT = '9999'
10
+
11
+ def initialize(app, env)
12
+ @app = app
13
+ end
14
+
15
+ def call(env)
16
+ env[:notify_data][:pid] = Server.run(env, PORT)
17
+ env[:notify_data][:port] = PORT
18
+
19
+ @app.call env
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,22 @@
1
+ module Vagrant
2
+ module Notify
3
+ module Action
4
+ class StopServer
5
+ def initialize(app, env)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ @app.call env
11
+
12
+ pid = env[:notify_data][:pid]
13
+
14
+ Process.kill('KILL', pid.to_i) rescue nil
15
+
16
+ env[:notify_data][:pid] = nil
17
+ env[:notify_data][:port] = nil
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,56 @@
1
+ require_relative 'action/check_provider'
2
+ require_relative 'action/install_command'
3
+ require_relative 'action/prepare_data'
4
+ require_relative 'action/server_is_running'
5
+ require_relative 'action/start_server'
6
+ require_relative 'action/stop_server'
7
+
8
+ module Vagrant
9
+ module Notify
10
+ module Action
11
+ class << self
12
+ Call = Vagrant::Action::Builtin::Call
13
+
14
+ def action_start_server
15
+ Vagrant::Action::Builder.new.tap do |b|
16
+ b.use Call, CheckProvider do |env, b2|
17
+ next if !env[:result]
18
+
19
+ b2.use PrepareData
20
+ b2.use Call, ServerIsRunning do |env2, b3|
21
+ if env2[:result]
22
+ # TODO: b3.use MessageServerRunning
23
+ else
24
+ # TODO: b3.use CheckServerPortCollision
25
+ b3.use StartServer
26
+ # TODO: b3.use BackupCommand
27
+ end
28
+ # Always install the command to make sure we can fix stale ips
29
+ # on the guest machine
30
+ b3.use InstallCommand
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ def action_stop_server
37
+ Vagrant::Action::Builder.new.tap do |b|
38
+ b.use Call, CheckProvider do |env, b2|
39
+ next if !env[:result]
40
+
41
+ b2.use PrepareData
42
+ b2.use Call, ServerIsRunning do |env2, b3|
43
+ if env2[:result]
44
+ b3.use StopServer
45
+ # TODO: b3.use RestoreCommandBackup
46
+ else
47
+ # TODO: b3.use MessageServerStopped
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,23 @@
1
+ module Vagrant
2
+ module Notify
3
+ class Data
4
+ def initialize(data_dir)
5
+ @data_dir = data_dir
6
+ end
7
+
8
+ def []=(key, value)
9
+ file = @data_dir.join(key.to_s)
10
+ if value.nil?
11
+ file.delete
12
+ else
13
+ file.open("w+") { |f| f.write(value) }
14
+ end
15
+ end
16
+
17
+ def [](key)
18
+ file = @data_dir.join(key.to_s)
19
+ file.read if file.file?
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,29 @@
1
+ module Vagrant
2
+ module Notify
3
+ class Plugin < Vagrant.plugin('2')
4
+ name 'vagrant notify'
5
+ description 'Forwards notify-send from guest to host machine'
6
+
7
+ action_hook 'start-server-after-boot' do |hook|
8
+ require_relative './action'
9
+ # TODO: This should be generic
10
+ hook.after VagrantPlugins::ProviderVirtualBox::Action::Boot, Vagrant::Notify::Action.action_start_server
11
+
12
+ if defined?(Vagrant::LXC)
13
+ # TODO: Require just the boot file once its "require dependencies" are sorted out
14
+ require 'vagrant-lxc/action'
15
+ hook.after Vagrant::LXC::Action::Boot, Vagrant::Notify::Action.action_start_server
16
+ end
17
+ end
18
+
19
+ action_hook 'install-command-after-provisioning', :machine_action_provision do |hook|
20
+ hook.after Vagrant::Action::Builtin::Provision, Vagrant::Notify::Action.action_start_server
21
+ end
22
+
23
+ action_hook 'stop-server-after-halting' do |hook|
24
+ require_relative './action'
25
+ hook.after Vagrant::Action::Builtin::GracefulHalt, Vagrant::Notify::Action.action_stop_server
26
+ end
27
+ end
28
+ end
29
+ end
@@ -4,22 +4,25 @@ module Vagrant
4
4
  HTTP_RESPONSE = "Hi! You just reached the vagrant notification server"
5
5
 
6
6
  def self.run(env, port)
7
- uuid = env[:vm].uuid
7
+ id = env[:machine].id
8
+ machine_name = env[:machine].name
9
+ provider = env[:machine].provider_name
8
10
  fork do
9
11
  $0 = "vagrant-notify-server (#{port})"
10
12
  tcp_server = TCPServer.open(port)
11
- server = self.new(uuid)
13
+ server = self.new(id, machine_name, provider)
12
14
  loop {
13
- Thread.start(tcp_server.accept) do |client|
15
+ Thread.start(tcp_server.accept) { |client|
14
16
  server.receive_data(client)
15
- end
17
+ }
16
18
  }
17
19
  end
18
20
  end
19
21
 
20
- def initialize(uuid, env = Vagrant::Environment.new)
21
- @uuid = uuid
22
- @env = env
22
+ def initialize(id, machine_name = :default, provider = :virtualbox)
23
+ @id = id
24
+ @machine_name = machine_name
25
+ @provider = provider
23
26
  end
24
27
 
25
28
  def receive_data(client)
@@ -31,6 +34,10 @@ module Vagrant
31
34
  system("notify-send #{args}")
32
35
  end
33
36
  client.close
37
+ rescue => ex
38
+ File.open("/tmp/vagrant-notify-error-#{@id}.log", 'a+') do |log|
39
+ log.puts "#{ex.message}"
40
+ end
34
41
  end
35
42
 
36
43
  private
@@ -51,13 +58,22 @@ module Vagrant
51
58
  return unless args =~ /-i '([^']+)'/
52
59
  icon = $1
53
60
  # TODO: Handle system icons
54
- host_file = "/tmp/vagrant-notify-#{@uuid}-#{icon.gsub('/', '-')}"
61
+ host_file = "/tmp/vagrant-notify-#{@id}-#{icon.gsub('/', '-')}"
55
62
  download(icon, host_file) unless File.exists?(host_file)
56
63
  args.gsub!(icon, host_file)
57
64
  end
58
65
 
59
66
  def download(icon, host_file)
60
- @env.vms[:default].channel.download(icon, host_file)
67
+ communicator.download(icon, host_file)
68
+ end
69
+
70
+ def communicator
71
+ @communicator ||=
72
+ begin
73
+ env = Vagrant::Environment.new
74
+ machine = env.machine(@machine_name, @provider)
75
+ machine.communicate
76
+ end
61
77
  end
62
78
  end
63
79
  end
@@ -1,5 +1,5 @@
1
1
  module Vagrant
2
2
  module Notify
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -7,29 +7,16 @@ if File.exists?(File.join(File.expand_path('../../', __FILE__), '.git'))
7
7
  $:.unshift(File.expand_path('../../lib', __FILE__))
8
8
  end
9
9
 
10
- require 'vagrant-notify/middleware'
10
+ require 'vagrant-notify/plugin'
11
11
  require 'vagrant-notify/server'
12
- require 'vagrant-notify/vagrant_ssh_ext'
13
12
  require "vagrant-notify/version"
14
13
 
15
14
  module Vagrant
16
15
  module Notify
17
16
  class << self
18
17
  def files_path
19
- @file_path ||= File.expand_path(File.dirname(__FILE__) + '/../files')
18
+ @file_path ||= Pathname.new(File.dirname(__FILE__)).join('..', 'files').expand_path
20
19
  end
21
20
  end
22
21
  end
23
22
  end
24
-
25
- Vagrant.actions[:start].tap do |start|
26
- start.insert_after Vagrant::Action::VM::Boot, Vagrant::Notify::Middleware::StartServer
27
- start.insert_after Vagrant::Notify::Middleware::StartServer, Vagrant::Notify::Middleware::InstallCommand
28
- end
29
- Vagrant.actions[:resume].tap do |start|
30
- start.use Vagrant::Notify::Middleware::StartServer
31
- start.use Vagrant::Notify::Middleware::InstallCommand
32
- end
33
- Vagrant.actions[:halt].use Vagrant::Notify::Middleware::StopServer
34
- Vagrant.actions[:suspend].use Vagrant::Notify::Middleware::StopServer
35
- Vagrant.actions[:provision].use Vagrant::Notify::Middleware::InstallCommand
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ require 'vagrant-notify'
4
+ require 'vagrant-notify/action/install_command'
5
+
6
+ describe Vagrant::Notify::Action::InstallCommand do
7
+ let(:app) { lambda { |env| } }
8
+ let(:env) { {notify_data: {port: host_port}, machine: machine, tmp_path: tmp_path} }
9
+ let(:host_port) { 12345 }
10
+ let(:machine) { mock(communicate: communicator) }
11
+ let(:communicator) { mock(upload: true, sudo: true) }
12
+ let(:host_ip) { '192.168.1.2' }
13
+ let(:tmp_path) { Pathname.new(Dir.mktmpdir) }
14
+ let(:tmp_cmd_path) { tmp_path.join('vagrant-notify-send') }
15
+ let(:guest_tmp_path) { '/tmp/notify-send' }
16
+ let(:guest_path) { '/usr/bin/notify-send' }
17
+ let(:stubbed_template) { ERB.new('<%= host_ip %> <%= host_port %>') }
18
+
19
+ subject { described_class.new(app, env) }
20
+
21
+ before do
22
+ ERB.stub(:new => stubbed_template)
23
+ subject.stub(local_ip: host_ip)
24
+ subject.call(env)
25
+ end
26
+
27
+ after { FileUtils.rm_rf tmp_path.to_s }
28
+
29
+ it 'compiles command script passing host ip and server port' do
30
+ tmp_cmd_path.read.should == "#{host_ip} #{host_port}"
31
+ end
32
+
33
+ it 'uploads compiled command script over to guest machine' do
34
+ communicator.should have_received(:upload).with(tmp_cmd_path.to_s, guest_tmp_path)
35
+ end
36
+
37
+ it 'installs command for all users' do
38
+ communicator.should have_received(:sudo).with("mv #{guest_tmp_path} #{guest_path} && chmod +x #{guest_path}")
39
+ end
40
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ require 'vagrant-notify/action/prepare_data'
4
+
5
+ describe Vagrant::Notify::Action::PrepareData do
6
+ let(:data_dir) { Pathname.new(Dir.mktmpdir) }
7
+ let(:app) { lambda { |env| } }
8
+ let(:env) { {machine: mock(data_dir: data_dir)} }
9
+
10
+ subject { described_class.new(app, env) }
11
+
12
+ before { subject.call(env) }
13
+ after { FileUtils.rm_rf data_dir.to_s }
14
+
15
+ it 'creates the directory to keep vagrant notify data' do
16
+ data_dir.join('vagrant-notify').should be_directory
17
+ end
18
+
19
+ it 'assigns a data object to the environment' do
20
+ env[:notify_data].should be_a(Vagrant::Notify::Data)
21
+ end
22
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ require 'vagrant-notify/action/server_is_running'
4
+
5
+ describe Vagrant::Notify::Action::ServerIsRunning do
6
+ let(:app) { lambda { |env| } }
7
+ let(:env) { {notify_data: {pid: pid}} }
8
+
9
+ subject { described_class.new(app, env) }
10
+
11
+ context 'when pid is not set' do
12
+ let(:pid) { nil }
13
+
14
+ it 'sets the result to false' do
15
+ env[:result].should be_false
16
+ end
17
+ end
18
+
19
+ context 'when pid is set' do
20
+ let(:pid) { '12345' }
21
+
22
+ context 'and is valid' do
23
+ before do
24
+ Process.stub(:getpgid => true)
25
+ subject.call(env)
26
+ end
27
+
28
+ it 'sets the result to true' do
29
+ env[:result].should be_true
30
+ end
31
+ end
32
+
33
+ context 'and is invalid' do
34
+ before do
35
+ Process.stub(:getpgid).and_raise(Errno::ESRCH)
36
+ subject.call(env)
37
+ end
38
+
39
+ it 'sets the result to false' do
40
+ env[:result].should be_false
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ require 'vagrant-notify/action/start_server'
4
+
5
+ describe Vagrant::Notify::Action::StartServer do
6
+ let(:app) { lambda { |env| } }
7
+ let(:env) { {notify_data: {}} }
8
+ let(:pid) { '42' }
9
+ let(:port) { described_class::PORT }
10
+
11
+ subject { described_class.new(app, env) }
12
+
13
+ before do
14
+ Vagrant::Notify::Server.stub(:run => pid)
15
+ subject.call(env)
16
+ end
17
+
18
+ it 'starts the server' do
19
+ Vagrant::Notify::Server.should have_received(:run).with(env, port)
20
+ end
21
+
22
+ it 'persists pid number' do
23
+ env[:notify_data][:pid].should == pid
24
+ end
25
+
26
+ it 'persists used port' do
27
+ env[:notify_data][:port].should == port
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ require 'vagrant-notify/action/stop_server'
4
+
5
+ describe Vagrant::Notify::Action::StopServer do
6
+ let(:app) { lambda { |env| } }
7
+ let(:env) { {notify_data: {pid: pid, port: 1234}} }
8
+ let(:pid) { '42' }
9
+ let(:port) { described_class::PORT }
10
+
11
+ subject { described_class.new(app, env) }
12
+
13
+ before do
14
+ Process.stub(kill: true)
15
+ subject.call(env)
16
+ end
17
+
18
+ it 'kills the notification server' do
19
+ Process.should have_received(:kill).with('KILL', pid.to_i)
20
+ end
21
+
22
+ it "removes server PID from notify data" do
23
+ env[:notify_data][:pid].should be_nil
24
+ end
25
+
26
+ it "removes server port from notify data" do
27
+ env[:notify_data][:port].should be_nil
28
+ end
29
+ end
data/spec/data_spec.rb ADDED
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ require 'vagrant-notify/data'
4
+
5
+ describe Vagrant::Notify::Data do
6
+ let!(:data_dir) { Pathname.new(Dir.mktmpdir) }
7
+ let(:key) { :pid }
8
+ let(:value) { '123456' }
9
+
10
+ subject { described_class.new(data_dir) }
11
+
12
+ after { FileUtils.rm_rf data_dir.to_s }
13
+
14
+ it 'writes data out to a file' do
15
+ subject[key] = value
16
+
17
+ data_dir.join(key.to_s).read.should eq value
18
+ end
19
+
20
+ it 'reads data from files' do
21
+ data_dir.join(key.to_s).open('w+') { |f| f.write(value) }
22
+
23
+ subject[key].should eq value
24
+ end
25
+
26
+ it 'handles unset keys' do
27
+ subject[key].should be_nil
28
+ end
29
+
30
+ it 'removes the file when a value is set to nil' do
31
+ subject.tap { |s| s[key] = value; s[key] = nil }
32
+
33
+ data_dir.join(key.to_s).should_not be_file
34
+ end
35
+ end
data/spec/server_spec.rb CHANGED
@@ -1,8 +1,11 @@
1
+ require 'vagrant-notify/server'
2
+
1
3
  describe Vagrant::Notify::Server do
2
- let(:arguments) { "-- '20 examples, 1 failure\n10 seconds'" }
3
- let(:client) { StringIO.new(arguments) }
4
+ let(:arguments) { "-- '20 examples, 1 failure\n10 seconds'" }
5
+ let(:client) { StringIO.new(arguments) }
6
+ let(:machine_id) { 'machine-id' }
4
7
 
5
- subject { described_class.new('uuid') }
8
+ subject { described_class.new(machine_id) }
6
9
 
7
10
  before { subject.stub(:system => true, :download => true) }
8
11
 
@@ -27,7 +30,7 @@ describe Vagrant::Notify::Server do
27
30
  end
28
31
 
29
32
  it 'rewrites icon path before sending the notification' do
30
- subject.should have_received(:system).with("notify-send -i '/tmp/vagrant-notify-uuid-foo-bar.jpg'")
33
+ subject.should have_received(:system).with("notify-send -i '/tmp/vagrant-notify-#{machine_id}-foo-bar.jpg'")
31
34
  end
32
35
  end
33
36
 
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,8 @@
1
- require 'vagrant-notify'
1
+ require 'vagrant'
2
+
3
+ require 'fileutils'
4
+ require 'tmpdir'
5
+
2
6
  require 'rspec-spies'
3
7
 
4
8
  RSpec.configure do |config|
@@ -11,6 +15,4 @@ RSpec.configure do |config|
11
15
  # the seed, which is printed after each run.
12
16
  # --seed 1234
13
17
  config.order = 'random'
14
-
15
- config.include Vagrant::TestHelpers
16
18
  end
@@ -16,9 +16,4 @@ Gem::Specification.new do |gem|
16
16
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
-
20
- gem.add_development_dependency 'vagrant'
21
- gem.add_development_dependency 'rake'
22
- gem.add_development_dependency 'rspec'
23
- gem.add_development_dependency 'rspec-spies'
24
19
  end
metadata CHANGED
@@ -1,80 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-notify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
5
- prerelease:
4
+ version: 0.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Fabio Rehm
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-02-04 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
15
- prerelease: false
16
- name: vagrant
17
- version_requirements: !ruby/object:Gem::Requirement
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
- none: false
23
- requirement: !ruby/object:Gem::Requirement
24
- requirements:
25
- - - ! '>='
26
- - !ruby/object:Gem::Version
27
- version: '0'
28
- none: false
29
- type: :development
30
- - !ruby/object:Gem::Dependency
31
- prerelease: false
32
- name: rake
33
- version_requirements: !ruby/object:Gem::Requirement
34
- requirements:
35
- - - ! '>='
36
- - !ruby/object:Gem::Version
37
- version: '0'
38
- none: false
39
- requirement: !ruby/object:Gem::Requirement
40
- requirements:
41
- - - ! '>='
42
- - !ruby/object:Gem::Version
43
- version: '0'
44
- none: false
45
- type: :development
46
- - !ruby/object:Gem::Dependency
47
- prerelease: false
48
- name: rspec
49
- version_requirements: !ruby/object:Gem::Requirement
50
- requirements:
51
- - - ! '>='
52
- - !ruby/object:Gem::Version
53
- version: '0'
54
- none: false
55
- requirement: !ruby/object:Gem::Requirement
56
- requirements:
57
- - - ! '>='
58
- - !ruby/object:Gem::Version
59
- version: '0'
60
- none: false
61
- type: :development
62
- - !ruby/object:Gem::Dependency
63
- prerelease: false
64
- name: rspec-spies
65
- version_requirements: !ruby/object:Gem::Requirement
66
- requirements:
67
- - - ! '>='
68
- - !ruby/object:Gem::Version
69
- version: '0'
70
- none: false
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ! '>='
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- none: false
77
- type: :development
11
+ date: 2013-03-21 00:00:00.000000000 Z
12
+ dependencies: []
78
13
  description: A Vagrant plugin that redirects system notifications from guest to host
79
14
  machine.
80
15
  email:
@@ -92,55 +27,64 @@ files:
92
27
  - LICENSE.txt
93
28
  - README.md
94
29
  - Rakefile
30
+ - burn/middleware/install_command.rb
31
+ - burn/middleware/start_server.rb
32
+ - burn/middleware/stop_server.rb
33
+ - burn/old_stack.rb
34
+ - burn/spec/middleware/install_command_spec.rb
35
+ - burn/spec/middleware/start_server_spec.rb
36
+ - burn/spec/middleware/stop_server_spec.rb
95
37
  - files/notify-send.erb
96
38
  - lib/vagrant-notify.rb
97
- - lib/vagrant-notify/middleware.rb
98
- - lib/vagrant-notify/middleware/install_command.rb
99
- - lib/vagrant-notify/middleware/start_server.rb
100
- - lib/vagrant-notify/middleware/stop_server.rb
39
+ - lib/vagrant-notify/action.rb
40
+ - lib/vagrant-notify/action/check_provider.rb
41
+ - lib/vagrant-notify/action/install_command.rb
42
+ - lib/vagrant-notify/action/prepare_data.rb
43
+ - lib/vagrant-notify/action/server_is_running.rb
44
+ - lib/vagrant-notify/action/start_server.rb
45
+ - lib/vagrant-notify/action/stop_server.rb
46
+ - lib/vagrant-notify/data.rb
47
+ - lib/vagrant-notify/plugin.rb
101
48
  - lib/vagrant-notify/server.rb
102
- - lib/vagrant-notify/vagrant_ssh_ext.rb
103
49
  - lib/vagrant-notify/version.rb
104
- - lib/vagrant_init.rb
105
- - spec/middleware/install_command_spec.rb
106
- - spec/middleware/start_server_spec.rb
107
- - spec/middleware/stop_server_spec.rb
50
+ - spec/action/install_command_spec.rb
51
+ - spec/action/prepare_data_spec.rb
52
+ - spec/action/server_is_running_spec.rb
53
+ - spec/action/start_server_spec.rb
54
+ - spec/action/stop_server_spec.rb
55
+ - spec/data_spec.rb
108
56
  - spec/server_spec.rb
109
57
  - spec/spec_helper.rb
110
58
  - vagrant-notify.gemspec
111
59
  homepage: https://github.com/fgrehm/vagrant-notify
112
60
  licenses: []
61
+ metadata: {}
113
62
  post_install_message:
114
63
  rdoc_options: []
115
64
  require_paths:
116
65
  - lib
117
66
  required_ruby_version: !ruby/object:Gem::Requirement
118
67
  requirements:
119
- - - ! '>='
68
+ - - '>='
120
69
  - !ruby/object:Gem::Version
121
70
  version: '0'
122
- segments:
123
- - 0
124
- hash: -2874098481974869093
125
- none: false
126
71
  required_rubygems_version: !ruby/object:Gem::Requirement
127
72
  requirements:
128
- - - ! '>='
73
+ - - '>='
129
74
  - !ruby/object:Gem::Version
130
75
  version: '0'
131
- segments:
132
- - 0
133
- hash: -2874098481974869093
134
- none: false
135
76
  requirements: []
136
77
  rubyforge_project:
137
- rubygems_version: 1.8.23
78
+ rubygems_version: 2.0.0
138
79
  signing_key:
139
- specification_version: 3
80
+ specification_version: 4
140
81
  summary: A Vagrant plugin that redirects system notifications from guest to host machine.
141
82
  test_files:
142
- - spec/middleware/install_command_spec.rb
143
- - spec/middleware/start_server_spec.rb
144
- - spec/middleware/stop_server_spec.rb
83
+ - spec/action/install_command_spec.rb
84
+ - spec/action/prepare_data_spec.rb
85
+ - spec/action/server_is_running_spec.rb
86
+ - spec/action/start_server_spec.rb
87
+ - spec/action/stop_server_spec.rb
88
+ - spec/data_spec.rb
145
89
  - spec/server_spec.rb
146
90
  - spec/spec_helper.rb
@@ -1,3 +0,0 @@
1
- Dir[File.dirname(__FILE__) + '/middleware/**/*.rb'].each do |file|
2
- require file
3
- end
@@ -1,22 +0,0 @@
1
- require 'vagrant/communication/ssh'
2
-
3
- module VagrantSshExt
4
- # Based on https://github.com/mitchellh/vagrant/blob/1-0-stable/lib/vagrant/communication/ssh.rb#L75-L90
5
- def download(from, to)
6
- @logger.debug("Downloading: #{from} to #{to}")
7
-
8
- connect do |connection|
9
- scp = Net::SCP.new(connection)
10
- scp.download!(from, to)
11
- end
12
- rescue Net::SCP::Error => e
13
- # If we get the exit code of 127, then this means SCP is unavailable.
14
- raise Errors::SCPUnavailable if e.message =~ /\(127\)/
15
- # Otherwise, just raise the error up
16
- raise
17
- end
18
- end
19
-
20
- unless Vagrant::Communication::SSH.public_methods.include?('download')
21
- Vagrant::Communication::SSH.send :include, VagrantSshExt
22
- end
data/lib/vagrant_init.rb DELETED
@@ -1,4 +0,0 @@
1
- # This file is automatically loaded by Vagrant to load any
2
- # plugins. This file kicks off this plugin.
3
-
4
- require 'vagrant-notify'
File without changes