vagrant-notify 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4b92afa6ddcd75d898cddd0a6aa0d782b0402092
4
- data.tar.gz: 9bfa48cd8650a092d9daa36f3e8ddce83bcb32aa
3
+ metadata.gz: c08a1c58a9fcfa0470ad77a737f52f2b60cf7971
4
+ data.tar.gz: b2a3093c5a9dce8a52c28aa8dc90b2e95ef68f07
5
5
  SHA512:
6
- metadata.gz: baddaf5a7b7d1978c9ab1db6dfc61ba0b429231926d07135ea67d1b4434ae47be30cb3e7355ec5afa219adc9ab4b94b9a9e84e4080a4f2912ee7d22d017041df
7
- data.tar.gz: df8160d0754c78384a47c4623e14bdb9b5a7f39cf9a87dd82382b4b039a5ff167b3c94830697d872a07db470a6d861e21859ebf80f43097cf48624aef00f2754
6
+ metadata.gz: 479ab1f5e182e41f8e15ed05e3f91ecd66e044276c3eb6b60059f175babf5c42447ed2de497827abd3f482d83db67d543596ce73235adbfaeb1e5efd067c696e
7
+ data.tar.gz: e91676a8f4795be0f42f442091c7e7fe25902f45448355c33b7b96da0ea798c9a0e696cd66d873d4654cd89a1ad499185b72dec7761921579bbecad68041862f
data/.gitignore CHANGED
@@ -15,4 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
- /.vagrant
18
+ /development/.vagrant
data/Gemfile CHANGED
@@ -10,11 +10,9 @@ group :development do
10
10
  end
11
11
 
12
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'
13
+ gem 'vagrant', github: 'mitchellh/vagrant'
14
+ gem 'vagrant-lxc', github: 'fgrehm/vagrant-lxc'
17
15
  gem 'rake'
18
- gem 'rspec'
16
+ gem 'rspec', '~> 2.13.0'
19
17
  gem 'rspec-spies'
20
18
  end
data/README.md CHANGED
@@ -18,10 +18,10 @@ $ vagrant plugin install vagrant-notify
18
18
 
19
19
  ## Usage
20
20
 
21
- After installing the gem, whenever you run `vagrant up`, a Ruby
22
- [TCPServer](http://www.ruby-doc.org/stdlib-1.9.3/libdoc/socket/rdoc/TCPServer.html)
23
- will fire up on `8081` port and a [script](https://github.com/fgrehm/vagrant-notify/blob/master/files/notify-send.erb)
24
- will be installed on the guest machine to replace the original `notify-send`
21
+ Whenever you run `vagrant up`, a Ruby [TCPServer](http://www.ruby-doc.org/stdlib-1.9.3/libdoc/socket/rdoc/TCPServer.html)
22
+ will fire up on a port within the [usable port range](https://github.com/mitchellh/vagrant/blob/master/config/default.rb#L14)
23
+ and a [script](https://github.com/fgrehm/vagrant-notify/blob/master/files/notify-send.erb)
24
+ will be copied over to the guest machine to replace the original `notify-send`
25
25
  command.
26
26
 
27
27
  If by any chance your IP changes, you can run `vagrant provision` in order to
@@ -29,10 +29,7 @@ update the guest script with the new IP.
29
29
 
30
30
  ## Known issues
31
31
 
32
- * Does not work properly on multi vm environments, it used to work on a Vagrant
33
- < 1.1 version and I'm planning to fix that as soon as I have a chance.
34
-
35
- * vagrant's `suspend` does not stop the notification server
32
+ * `vagrant suspend` does not stop the notification server
36
33
 
37
34
 
38
35
  ## Contributing
@@ -0,0 +1,13 @@
1
+ # -*- mode: ruby -*-
2
+ # vi: set ft=ruby :
3
+
4
+ Vagrant.require_plugin 'vagrant-notify'
5
+ Vagrant.require_plugin 'vagrant-lxc'
6
+
7
+ Vagrant.configure("2") do |config|
8
+ config.vm.box = "base"
9
+ config.vm.box = "quantal64"
10
+
11
+ config.vm.define :vm1
12
+ config.vm.define :vm2
13
+ end
@@ -8,6 +8,7 @@
8
8
  require 'rubygems'
9
9
  require 'socket'
10
10
  require 'optparse'
11
+ require 'fileutils'
11
12
 
12
13
  options = {}
13
14
  OptionParser.new do |opts|
@@ -23,6 +24,12 @@ OptionParser.new do |opts|
23
24
 
24
25
  end.parse!
25
26
 
27
+ if options[:i]
28
+ new_filename = options[:i].gsub('/', '-')
29
+ FileUtils.cp options[:i], "<%= shared_folder %>/#{new_filename}"
30
+ options[:i] = new_filename
31
+ end
32
+
26
33
  cmd = options.map do |key, value|
27
34
  "-#{key} '#{value}'"
28
35
  end.join(' ')
@@ -8,6 +8,19 @@ require_relative 'action/stop_server'
8
8
  module Vagrant
9
9
  module Notify
10
10
  module Action
11
+ class SetSharedFolder
12
+ def initialize(app, env)
13
+ @app = app
14
+ end
15
+
16
+ def call(env)
17
+ host_dir = Pathname("/tmp/vagrant-notify/#{env[:machine].id}")
18
+ FileUtils.mkdir_p host_dir.to_s unless host_dir.exist?
19
+ env[:machine].config.vm.synced_folder host_dir, "/tmp/vagrant-notify", id: "vagrant-notify"
20
+ @app.call(env)
21
+ end
22
+ end
23
+
11
24
  class << self
12
25
  Call = Vagrant::Action::Builtin::Call
13
26
 
@@ -17,7 +17,7 @@ module Vagrant
17
17
 
18
18
  def compile_command(env)
19
19
  host_port = env[:notify_data][:port]
20
- template_binding = OpenStruct.new(:host_ip => local_ip, :host_port => host_port)
20
+ template_binding = OpenStruct.new(:host_ip => local_ip, :host_port => host_port, :shared_folder => '/tmp/vagrant-notify')
21
21
  command_template = ERB.new(Vagrant::Notify.files_path.join('notify-send.erb').read)
22
22
  command = command_template.result(template_binding.instance_eval { binding })
23
23
 
@@ -27,6 +27,7 @@ module Vagrant
27
27
  def install_command_on_guest(env, command_path)
28
28
  source = env[:tmp_path].join 'vagrant-notify-send'
29
29
  env[:machine].communicate.upload(source.to_s, '/tmp/notify-send')
30
+ env[:machine].communicate.sudo('mv /usr/bin/{notify-send,notify-send.bkp}; exit 0')
30
31
  env[:machine].communicate.sudo('mv /tmp/notify-send /usr/bin/notify-send && chmod +x /usr/bin/notify-send')
31
32
  end
32
33
 
@@ -1,23 +1,61 @@
1
+ require "vagrant/util/is_port_open"
2
+
1
3
  require_relative '../server'
2
4
 
3
5
  module Vagrant
4
6
  module Notify
5
7
  module Action
6
8
  class StartServer
7
- # TODO: This should come from vm configuration or be automatically
8
- # assigned
9
- PORT = '9999'
9
+ include Util::IsPortOpen
10
10
 
11
11
  def initialize(app, env)
12
12
  @app = app
13
13
  end
14
14
 
15
15
  def call(env)
16
- env[:notify_data][:pid] = Server.run(env, PORT)
17
- env[:notify_data][:port] = PORT
16
+ @env = env
17
+
18
+ port = next_available_port
19
+ env[:notify_data][:pid] = Server.run(env, port)
20
+ env[:notify_data][:port] = port
18
21
 
19
22
  @app.call env
20
23
  end
24
+
25
+ def next_available_port
26
+ # Determine a list of usable ports for us to use
27
+ usable_ports = Set.new(@env[:machine].config.vm.usable_port_range)
28
+
29
+ # Pass one, remove all defined host ports from usable ports
30
+ with_forwarded_ports do |options|
31
+ usable_ports.delete(options[:host])
32
+ end
33
+
34
+ # Pass two, remove ports used by other processes
35
+ with_forwarded_ports do |options|
36
+ host_port = options[:host]
37
+ usable_ports.delete(options[:host]) if is_port_open?("127.0.0.1", host_port)
38
+ end
39
+
40
+ # If we have no usable ports then we can't use the plugin
41
+ raise 'No usable ports available!' if usable_ports.empty?
42
+
43
+ # Set the port up to be the last one since vagrant's port collision handler
44
+ # will use the first as in:
45
+ # https://github.com/mitchellh/vagrant/blob/master/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb#L84
46
+ usable_ports.to_a.sort.reverse.find do |port|
47
+ return port unless is_port_open?("127.0.0.1", port)
48
+ end
49
+ end
50
+
51
+ def with_forwarded_ports
52
+ @env[:machine].config.vm.networks.each do |type, options|
53
+ # Ignore anything but forwarded ports
54
+ next if type != :forwarded_port
55
+
56
+ yield options
57
+ end
58
+ end
21
59
  end
22
60
  end
23
61
  end
@@ -7,6 +7,9 @@ module Vagrant
7
7
  end
8
8
 
9
9
  def call(env)
10
+ env[:machine].communicate.sudo('rm /usr/bin/notify-send')
11
+ env[:machine].communicate.sudo('mv /usr/bin/{notify-send.bkp,notify-send}; exit 0')
12
+
10
13
  @app.call env
11
14
 
12
15
  pid = env[:notify_data][:pid]
@@ -17,18 +17,21 @@ module Vagrant
17
17
  end
18
18
  end
19
19
 
20
- action_hook 'install-command-after-provisioning', :machine_action_provision do |hook|
21
- hook.after Vagrant::Action::Builtin::Provision, Vagrant::Notify::Action.action_start_server
20
+ share_folder_hook = lambda do |hook|
21
+ require_relative './action'
22
+ hook.after Vagrant::Action::Builtin::Provision, Vagrant::Notify::Action::SetSharedFolder
22
23
  end
24
+ action_hook 'set-shared-folder-and-start-notify-server-on-machine-up', :machine_action_up, &share_folder_hook
25
+ action_hook 'set-shared-folder-and-start-notify-server-on-machine-reload', :machine_action_reload, &share_folder_hook
23
26
 
24
- action_hook 'stop-server-after-halting', :machine_action_halt do |hook|
27
+ action_hook 'stop-server-after-halt' do |hook|
25
28
  require_relative './action'
26
29
  hook.before Vagrant::Action::Builtin::GracefulHalt, Vagrant::Notify::Action.action_stop_server
27
30
  end
28
31
 
29
32
  # TODO: This should be generic, we don't want to hard code every single
30
33
  # possible provider action class that Vagrant might have
31
- action_hook 'stop-server-before-destruction', :machine_action_destroy do |hook|
34
+ action_hook 'stop-server-before-destroy', :machine_action_destroy do |hook|
32
35
  require_relative './action'
33
36
  hook.before VagrantPlugins::ProviderVirtualBox::Action::Destroy, Vagrant::Notify::Action.action_stop_server
34
37
 
@@ -30,18 +30,22 @@ module Vagrant
30
30
  if http_request?(args)
31
31
  client.puts HTTP_RESPONSE
32
32
  else
33
- download_icon!(args)
33
+ fix_icon_path!(args)
34
34
  system("notify-send #{args}")
35
35
  end
36
36
  client.close
37
37
  rescue => ex
38
- File.open("/tmp/vagrant-notify-error-#{@id}.log", 'a+') do |log|
39
- log.puts "#{ex.message}"
40
- end
38
+ log ex.message
41
39
  end
42
40
 
43
41
  private
44
42
 
43
+ def log(message)
44
+ File.open("/tmp/vagrant-notify-error-#{@id}.log", 'a+') do |log|
45
+ log.puts "#{message}"
46
+ end
47
+ end
48
+
45
49
  def read_args(client)
46
50
  ''.tap do |args|
47
51
  while tmp = client.gets and tmp !~ /^\s*$/
@@ -54,24 +58,13 @@ module Vagrant
54
58
  args =~ /^GET/
55
59
  end
56
60
 
57
- def download_icon!(args)
61
+ def fix_icon_path!(args)
58
62
  return unless args =~ /-i '([^']+)'/
59
63
  icon = $1
60
64
  # TODO: Handle system icons
61
- host_file = "/tmp/vagrant-notify-#{@id}-#{icon.gsub('/', '-')}"
62
- download(icon, host_file) unless File.exists?(host_file)
65
+ host_file = "/tmp/vagrant-notify/#{@id}/#{icon.gsub('/', '-')}"
63
66
  args.gsub!(icon, host_file)
64
67
  end
65
-
66
- def download(icon, host_file)
67
- communicator.download(icon, host_file)
68
- end
69
-
70
- def communicator
71
- env = Vagrant::Environment.new
72
- machine = env.machine(@machine_name, @provider)
73
- machine.communicate
74
- end
75
68
  end
76
69
  end
77
70
  end
@@ -1,5 +1,5 @@
1
1
  module Vagrant
2
2
  module Notify
3
- VERSION = "0.2.2"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -34,6 +34,10 @@ describe Vagrant::Notify::Action::InstallCommand do
34
34
  communicator.should have_received(:upload).with(tmp_cmd_path.to_s, guest_tmp_path)
35
35
  end
36
36
 
37
+ it 'creates a backup for the available command' do
38
+ communicator.should have_received(:sudo).with("mv /usr/bin/{notify-send,notify-send.bkp}; exit 0")
39
+ end
40
+
37
41
  it 'installs command for all users' do
38
42
  communicator.should have_received(:sudo).with("mv #{guest_tmp_path} #{guest_path} && chmod +x #{guest_path}")
39
43
  end
@@ -6,12 +6,13 @@ describe Vagrant::Notify::Action::StartServer do
6
6
  let(:app) { lambda { |env| } }
7
7
  let(:env) { {notify_data: {}} }
8
8
  let(:pid) { '42' }
9
- let(:port) { described_class::PORT }
9
+ let(:port) { '1234' }
10
10
 
11
11
  subject { described_class.new(app, env) }
12
12
 
13
13
  before do
14
14
  Vagrant::Notify::Server.stub(:run => pid)
15
+ subject.stub(next_available_port: port)
15
16
  subject.call(env)
16
17
  end
17
18
 
@@ -26,4 +27,6 @@ describe Vagrant::Notify::Action::StartServer do
26
27
  it 'persists used port' do
27
28
  env[:notify_data][:port].should == port
28
29
  end
30
+
31
+ pending 'identifies the next available port'
29
32
  end
@@ -3,10 +3,12 @@ require 'spec_helper'
3
3
  require 'vagrant-notify/action/stop_server'
4
4
 
5
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 }
6
+ let(:app) { lambda { |env| } }
7
+ let(:communicator) { mock(sudo: true) }
8
+ let(:machine) { mock(communicate: communicator) }
9
+ let(:env) { {notify_data: {pid: pid, port: 1234}, machine: machine} }
10
+ let(:pid) { '42' }
11
+ let(:port) { described_class::PORT }
10
12
 
11
13
  subject { described_class.new(app, env) }
12
14
 
data/spec/server_spec.rb CHANGED
@@ -7,7 +7,7 @@ describe Vagrant::Notify::Server do
7
7
 
8
8
  subject { described_class.new(machine_id) }
9
9
 
10
- before { subject.stub(:system => true, :download => true) }
10
+ before { subject.stub(:system => true) }
11
11
 
12
12
  it 'runs notify-send with received data from client' do
13
13
  subject.should_receive(:system).with("notify-send #{arguments}")
@@ -22,27 +22,10 @@ describe Vagrant::Notify::Server do
22
22
  context 'notification with an icon' do
23
23
  let(:arguments) { "-i 'foo/bar.jpg'" }
24
24
 
25
- context 'icon hasnt been downloaded before' do
26
- before { subject.receive_data(client) }
27
-
28
- it 'downloads icon file to host machine' do
29
- subject.should have_received(:download)
30
- end
31
-
32
- it 'rewrites icon path before sending the notification' do
33
- subject.should have_received(:system).with("notify-send -i '/tmp/vagrant-notify-#{machine_id}-foo-bar.jpg'")
34
- end
35
- end
36
-
37
- context 'icon file cache is present' do
38
- before do
39
- File.stub(:exists? => true)
40
- subject.receive_data(client)
41
- end
25
+ before { subject.receive_data(client) }
42
26
 
43
- it 'does not download icon if was already downloaded' do
44
- subject.should_not have_received(:download)
45
- end
27
+ it 'rewrites icon path before sending the notification' do
28
+ subject.should have_received(:system).with("notify-send -i '/tmp/vagrant-notify/#{machine_id}/foo-bar.jpg'")
46
29
  end
47
30
  end
48
31
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-notify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fabio Rehm
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-21 00:00:00.000000000 Z
11
+ date: 2013-07-29 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A Vagrant plugin that redirects system notifications from guest to host
14
14
  machine.
@@ -27,6 +27,7 @@ files:
27
27
  - LICENSE.txt
28
28
  - README.md
29
29
  - Rakefile
30
+ - development/Vagrantfile
30
31
  - files/notify-send.erb
31
32
  - lib/vagrant-notify.rb
32
33
  - lib/vagrant-notify/action.rb