Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
Merge pull request #27 from alpha01/devel
Browse files Browse the repository at this point in the history
vagrant-notify 0.5.0 initial merge
  • Loading branch information
alpha01 committed Jun 22, 2016
2 parents 669a7cf + e1c859b commit 1a9ca8c
Show file tree
Hide file tree
Showing 25 changed files with 354 additions and 54 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ test/tmp
test/version_tmp
tmp
/development/.vagrant
.DS_Store
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: ruby
bundler_args: --without development
rvm:
- 1.9.3
- 2.0.0
- 2.3.0
22 changes: 21 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
## [0.4.1](https://github.com/fgrehm/vagrant-notify/compare/v0.3.0...master) (unreleased)


## [0.5.0](https://github.com/fgrehm/vagrant-notify/compare/v0.4.0...v0.5.0) (May 11, 2016)

IMPROVEMENTS

- Notification server runs as a daemon (fork() has been removed so it's compatible with Windows). [[GH-14]]
- Windows support (beta) [[GH-14]]
- Notification server is available from localhost.
- Notification server information is displayed each time vagrant is started and halted.
- New plugin command. Ability to view notification server status, and ability for manual restart. [CLI](https://gist.github.com/alpha01/9b81caca694a2735e658f978c41600b5)
- New notify-send example scripts for OS X and Windows

BUG FIXES

- `vagrant suspend/resume` stops/starts notification server respectively. [[GH-18]]
- Plugin causes VM boot to fail. [[GH-22]]

[GH-14]:https://github.com/fgrehm/vagrant-notify/issues/14
[GH-18]:https://github.com/fgrehm/vagrant-notify/issues/18
[GH-22]:https://github.com/fgrehm/vagrant-notify/pull/22


## [0.4.0](https://github.com/fgrehm/vagrant-notify/compare/v0.3.0...v0.4.0) (Feb 26, 2014)
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ group :development do
gem 'rb-inotify'
gem 'guard'
gem 'guard-rspec'
gem 'ocra'
end

group :development, :test do
Expand Down
7 changes: 6 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ GIT
PATH
remote: .
specs:
vagrant-notify (0.4.1.dev)
vagrant-notify (0.5.0)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -51,6 +51,7 @@ GEM
net-scp (1.1.2)
net-ssh (>= 2.6.5)
net-ssh (2.7.0)
ocra (1.3.6)
pry (0.9.12.2)
coderay (~> 1.0.5)
method_source (~> 0.8)
Expand Down Expand Up @@ -80,10 +81,14 @@ PLATFORMS
DEPENDENCIES
guard
guard-rspec
ocra
rake
rb-inotify
rspec (~> 2.13.0)
rspec-spies
vagrant!
vagrant-lxc!
vagrant-notify!

BUNDLED WITH
1.10.6
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2012-2014 Fabio Rehm
Copyright (c) 2012-2016 Fabio Rehm

MIT License

Expand Down
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
A Vagrant plugin that forwards `notify-send` from guest to host machine and
notifies provisioning status. [See it in action](#demo)

## Help Needed!

This project is looking for maintainers, please see [GH-24](https://github.com/fgrehm/vagrant-notify/issues/24) for more.

## Installation

Expand Down Expand Up @@ -58,14 +55,26 @@ A (too) primitive script integrating with Growl:
growlnotify -t "Vagrant VM" -m "$*"
```

See example [scripts](https://github.com/alpha01/vagrant-notify/tree/devel/examples).

### Windows (beta)

You can use the freeware application [notify-send for Windows](http://vaskovsky.net/notify-send/), make sure the notify-send binary is available on `Path`.

See example [scripts](https://github.com/alpha01/vagrant-notify/tree/devel/examples).


## Demo

![Demo](http://i.imgur.com/tzOLvGY.gif)
![Demo OS X](http://i.imgur.com/216NIlf.gif)
![Demo Windows](http://i.imgur.com/cJYqX4y.gif)


## Known issues

* `vagrant suspend` does not stop the notification server
* `vagrant destroy` on a running VM will not stop the notification server.
* On rare occasions the notification server may stop receiving notifications if the host is suspended/hibernates. The notification server may need to be manually restarted if that's the case. `vagrant notify --restart`


## Contributing
Expand Down
16 changes: 16 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Example notify-send wrapper scripts

## OS X

* [terminal-notifier](https://github.com/alloy/terminal-notifier) [wrapper script](https://github.com/alpha01/vagrant-notify/blob/devel/examples/osx/notify-send_terminal-notifier)


## Windows

* [Growl for Windows](http://www.growlforwindows.com/gfw/default.aspx) [wrapper script](https://github.com/alpha01/vagrant-notify/blob/devel/examples/windows/notify-send_growl_for_windows)

**IMPORTANT:** `notify-send` wrapper script has to be binary executable. Also as with `notify-send`, `growlnotify.exe` needs to be is in your `path`.

Compiling using ocra:

ocra --output notify-send windows/notify-send_growl_for_windows
33 changes: 33 additions & 0 deletions examples/osx/notify-send_terminal-notifier
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env ruby
# -*- mode: ruby -*-
# vi: set ft=ruby :

# Example OS X terminal-notifier notify-send wrapper script.

require 'optparse'


options = {}
OptionParser.new do |opts|
opts.on('-u', '--urgency LEVEL') { |v| options[:u] = v } # Option gets removed
opts.on('-t', '--expire-time TIME') { |v| options[:t] = v } # Option gets removed
opts.on('-a', '--app-name APP_NAME') { |v| options[:a] = v } # TO DO: Set to -title
opts.on('-i', '--icon ICON[,ICON...]') { |v| options[:i] = v } # Option gets removed
opts.on('-c', '--category TYPE[,TYPE...]') { |v| options[:c] = v } # Option gets removed
opts.on('-h', '--hint TYPE:NAME:VALUE') { |v| options[:h] = v } # Option gets removed
opts.on('-v', '--version') { |v| options[:v] = v } # Option gets removed
end.parse!


if ARGV.length == 0
puts "No summary specified"
exit 1
elsif ARGV.length == 1
message = "-message '\\#{ARGV[0]}'"
elsif ARGV.length == 2
message = "-title '\\#{ARGV[0]}' -message '\\#{ARGV[1]}'"
else
message = ARGV.map{|a| a =~ /-(title|message)/ ? a : "'\\#{a}'"}.join(' ')
end

system("terminal-notifier -sound default #{message}")
36 changes: 36 additions & 0 deletions examples/windows/notify-send_growl_for_windows
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env ruby
# -*- mode: ruby -*-
# vi: set ft=ruby :

# Example MS-Windows notify-send wrapper script. Assumes growlnotify.exe is in your 'path'.

require 'optparse'

options = {}
OptionParser.new do |opts|
opts.banner = "Usage: notify-send [options]"

opts.on('-u', '--urgency LEVEL') { |v| options[:u] = v } # TO DO: Set to /p:priority
opts.on('-t', '--expire-time TIME') { |v| options[:t] = v } # Option gets removed
opts.on('-a', '--app-name APP_NAME') { |v| options[:a] = v } # TO DO: Set to /a:application
opts.on('-i', '--icon ICON[,ICON...]') { |v| options[:i] = v } # TO DO: Set to /i:icon (url only)
opts.on('-c', '--category TYPE[,TYPE...]') { |v| options[:c] = v } # Option gets removed
opts.on('-h', '--hint TYPE:NAME:VALUE') { |v| options[:h] = v } # Option gets removed
opts.on('-v', '--version') { |v| options[:v] = v } # Option gets removed
end.parse!



if ARGV.length == 0
puts "No summary specified"
elsif ARGV.length == 1
message = "'#{ARGV[0]}'"
system("growlnotify.exe \"#{message}\"")
elsif ARGV.length == 2
title = "/t:'#{ARGV[0]}'"
message = "'#{ARGV[1]}'"
system("growlnotify.exe \"#{title}\" \"#{message}\"")
else
message = ARGV.map{|a| "'#{a}'"}.join(' ')
system("growlnotify.exe \"#{message}\"")
end
11 changes: 5 additions & 6 deletions files/notify-send.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env ruby
#!/usr/bin/ruby
# -*- mode: ruby -*-
# vi: set ft=ruby :

Expand All @@ -12,7 +12,7 @@ require 'fileutils'

options = {}
OptionParser.new do |opts|
opts.banner = "Usage: example.rb [options]"
opts.banner = "Usage: notify-send.rb [options]"

opts.on('-u', '--urgency LEVEL') { |v| options[:u] = v }
opts.on('-t', '--expire-time TIME') { |v| options[:t] = v }
Expand All @@ -36,11 +36,10 @@ end.join(' ')

# TODO: Need to escape values
unless ARGV.empty?
cmd << ' -- '
cmd << ARGV.map{|a| "'#{a}'"}.join(' ')
cmd << ARGV.map{|a| " '#{a}'"}.join(' ')
end

client_ip = ENV['SSH_CLIENT'].split(' ')[0]
TCPSocket.open ENV['SSH_CLIENT'], <%= host_port %> do |s|
client_ip = `ip route|grep default|awk '{print $3}'`
TCPSocket.open client_ip, <%= host_port %> do |s|
s.send cmd, 0
end
1 change: 0 additions & 1 deletion lib/vagrant-notify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
end

require 'vagrant-notify/plugin'
require 'vagrant-notify/server'
require "vagrant-notify/version"

module Vagrant
Expand Down
17 changes: 17 additions & 0 deletions lib/vagrant-notify/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ def action_stop_server
end
end
end

def action_status_server
Vagrant::Action::Builder.new.tap do |b|
b.use Call, CheckProvider do |env, b2|
next if !env[:result]

b2.use PrepareData
b2.use Call, ServerIsRunning do |env2, b3|
if env2[:result]
env[:machine].ui.success("vagrant-notify-server pid: #{env2[:notify_data][:pid]}")
else
env[:machine].ui.error("No vagrant-notify server detected.")
end
end
end
end
end
end
end
end
Expand Down
9 changes: 6 additions & 3 deletions lib/vagrant-notify/action/install_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ def initialize(app, env)
end

def call(env)
path = compile_command(env)
path = compile_command(env, 'notify-send.erb')
install_command_on_guest(env, path)

@app.call env
end

private

def compile_command(env)
def compile_command(env, template_file)
host_port = env[:notify_data][:port]
template_binding = OpenStruct.new(:host_port => host_port, :shared_folder => '/tmp/vagrant-notify')
command_template = ERB.new(Vagrant::Notify.files_path.join('notify-send.erb').read)
command_template = ERB.new(Vagrant::Notify.files_path.join(template_file).read)
command = command_template.result(template_binding.instance_eval { binding })

env[:tmp_path].join('vagrant-notify-send').open('w') { |f| f.write(command) }
Expand All @@ -29,6 +29,9 @@ def install_command_on_guest(env, command_path)
env[:machine].communicate.upload(source.to_s, '/tmp/notify-send')
env[:machine].communicate.sudo('mv /usr/bin/{notify-send,notify-send.bkp}; exit 0')
env[:machine].communicate.sudo('mv /tmp/notify-send /usr/bin/notify-send && chmod +x /usr/bin/notify-send')
if RUBY_PLATFORM =~ /mswin|msys|mingw|cygwin|bccwin|wince|emc/
env[:machine].communicate.sudo("sed 's/\r\$//' -i /usr/bin/notify-send") # dos2unix
end
end
end
end
Expand Down
12 changes: 9 additions & 3 deletions lib/vagrant-notify/action/server_is_running.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require_relative 'windows/process_info'

module Vagrant
module Notify
module Action
Expand All @@ -18,9 +20,13 @@ def call(env)
private

def valid_process?(pid)
Process.getpgid(pid.to_i) if pid
rescue Errno::ESRCH
false
if RUBY_PLATFORM =~ /mswin|msys|mingw|cygwin|bccwin|wince|emc/
Vagrant::Notify::Action::Windows::ProcessInfo.queryProcess(pid) if pid
else
Process.getpgid(pid.to_i) if pid
end
rescue Errno::ESRCH
false
end
end
end
Expand Down
29 changes: 26 additions & 3 deletions lib/vagrant-notify/action/start_server.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require "vagrant/util/is_port_open"

require_relative '../server'

module Vagrant
module Notify
Expand All @@ -16,8 +15,18 @@ def call(env)
@env = env

port = next_available_port
env[:notify_data][:pid] = Server.run(env, port)
env[:notify_data][:port] = port
id = env[:machine].id
dir = File.expand_path('../../', __FILE__)

if which('ruby')
env[:notify_data][:pid] = Process.spawn("ruby #{dir}/server.rb #{id} #{port}")
env[:notify_data][:port] = port

env[:machine].ui.success("Started vagrant-notify-server pid: #{env[:notify_data][:pid]}")
sleep 5
else
env[:machine].ui.error("Unable to spawn TCPServer daemon, ruby not found in $PATH")
end

@app.call env
end
Expand Down Expand Up @@ -56,6 +65,20 @@ def with_forwarded_ports
yield options
end
end

# http://stackoverflow.com/questions/2108727/which-in-ruby-checking-if-program-exists-in-path-from-ruby
# Cross-platform way of finding an executable in the $PATH.
# which('ruby') #=> /usr/bin/ruby
def which(cmd)
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
exts.each { |ext|
exe = File.join(path, "#{cmd}#{ext}")
return exe if File.executable?(exe) && !File.directory?(exe)
}
end
return nil
end
end
end
end
Expand Down
9 changes: 8 additions & 1 deletion lib/vagrant-notify/action/stop_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ def call(env)

pid = env[:notify_data][:pid]

Process.kill('KILL', pid.to_i) rescue nil
begin
Process.kill('KILL', pid.to_i)
env[:machine].ui.success("Stopped vagrant-notify-server pid: #{pid}")
rescue Errno::ESRCH
nil
rescue
env[:machine].ui.error("Failed to stop vagrant-notify-server pid: #{pid}")
end

env[:notify_data][:pid] = nil
env[:notify_data][:port] = nil
Expand Down
Loading

0 comments on commit 1a9ca8c

Please sign in to comment.