vagrant-ghost 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3f7324291014420cc8974f67bb919306781d56f0
4
+ data.tar.gz: b0731e3ac2351f218d35db97edf5bb813c361cd3
5
+ SHA512:
6
+ metadata.gz: 8089840cbdc356d96e42bce47c1afe082f7112f128581199757ab5f900349edca85820bc3eee5ff696e9e728472d6d76593878170638b67681b9c11dd0fdf8b7
7
+ data.tar.gz: dc16f9a522dfb027f1a81cbc46641f76d1bd22c1df6146689ac6bc351a1395b3d248f2b81e3c8307468934979bb7689e04f87e56ea918177fe4ad3e9c7304015
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ .idea
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
19
+ *.bundle
20
+ *.so
21
+ *.o
22
+ *.a
23
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ #gemspec
4
+
5
+ group :development do
6
+ gem "vagrant", git: "https://github.com/mitchellh/vagrant.git"
7
+ end
8
+
9
+ group :plugins do
10
+ gem "vagrant-ghost", path: "."
11
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Eric Mann
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,29 @@
1
+ # Vagrant::Ghost
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'vagrant-ghost'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install vagrant-ghost
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/vagrant-ghost/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'bundler/gem_tasks'
4
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,32 @@
1
+ module VagrantPlugins
2
+ module Ghost
3
+ module Command
4
+ class Base < Vagrant.plugin( 2, :command )
5
+ def execute
6
+ sub_command = self.class.name.split('::').last.downcase
7
+
8
+ parser = OptionParser.new do |opts|
9
+ opts.banner = "Usage: vagrant ghost #{sub_command} [vm-name]"
10
+ end
11
+
12
+ # Parse the options
13
+ argv = parse_options(parser)
14
+ return if !argv
15
+
16
+ with_target_vms(argv) do |vm|
17
+ if vm.state == :running
18
+ Ghost::VM.new(vm).send sub_command.to_sym
19
+ elsif vm.created?
20
+ vm.ui.info I18n.t("vagrant.commands.common.vm_not_running")
21
+ else
22
+ vm.ui.info I18n.t("vagrant.commands.common.vm_not_created")
23
+ end
24
+ end
25
+
26
+ # Success, exit status 0
27
+ 0
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,8 @@
1
+ module VagrantPlugins
2
+ module Ghost
3
+ module Command
4
+ class List < Ghost::Command::Base
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module VagrantPlugins
2
+ module Ghost
3
+ module Command
4
+ class Remove < Ghost::Command::Base
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,72 @@
1
+ module VagrantPlugins
2
+ module Ghost
3
+ module Command
4
+ class Root < Vagrant.plugin( 2, :command )
5
+ def self.synopsis
6
+ "manages hosts file: addition, removal, etc."
7
+ end
8
+
9
+ def initialize(argv, env)
10
+ super
11
+
12
+ @main_args, @sub_command, @sub_args = split_main_and_subcommand(argv)
13
+
14
+ @subcommands = Vagrant::Registry.new
15
+ @subcommands.register(:list) do
16
+ require File.expand_path("../list", __FILE__)
17
+ List
18
+ end
19
+
20
+ @subcommands.register(:remove) do
21
+ require File.expand_path("../remove", __FILE__)
22
+ Remove
23
+ end
24
+
25
+ @subcommands.register(:update) do
26
+ require File.expand_path("../update", __FILE__)
27
+ Update
28
+ end
29
+ end
30
+
31
+ def execute
32
+ if @main_args.include?("-h") || @main_args.include?("--help")
33
+ # Print the help for all the hosts commands.
34
+ return help
35
+ end
36
+
37
+ # If we reached this far then we must have a subcommand. If not,
38
+ # then we also just print the help and exit.
39
+ command_class = @subcommands.get(@sub_command.to_sym) if @sub_command
40
+ return help if !command_class || !@sub_command
41
+ @logger.debug("Invoking command class: #{command_class} #{@sub_args.inspect}")
42
+
43
+ # Initialize and execute the command class
44
+ command_class.new(@sub_args, @env).execute
45
+ end
46
+
47
+ # Prints the help out for this command
48
+ def help
49
+ opts = OptionParser.new do |opts|
50
+ opts.banner = "Usage: vagrant ghost <command> [<args>]"
51
+ opts.separator ""
52
+ opts.separator "Available subcommands:"
53
+
54
+ # Add the available subcommands as separators in order to print them
55
+ # out as well.
56
+ keys = []
57
+ @subcommands.each { |key, value| keys << key.to_s }
58
+
59
+ keys.sort.each do |key|
60
+ opts.separator " #{key}"
61
+ end
62
+
63
+ opts.separator ""
64
+ opts.separator "For help on any individual command run `vagrant ghost COMMAND -h`"
65
+ end
66
+
67
+ @env.ui.info(opts.help, :prefix => false)
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,8 @@
1
+ module VagrantPlugins
2
+ module Ghost
3
+ module Command
4
+ class Update < Ghost::Command::Base
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,10 @@
1
+ module VagrantPlugins
2
+ module Ghost
3
+ class Config < Vagrant.plugin( 2, :config )
4
+ attr_accessor :name
5
+ attr_accessor :aliases
6
+ attr_accessor :id
7
+ attr_accessor :remove_on_suspend
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,21 @@
1
+ module VagrantPlugins
2
+ module Ghost
3
+ module Middleware
4
+ class Remove
5
+ def initialize(app, env)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ remove env[:vm] if env["vm"].created?
11
+ @app.call(env)
12
+ end
13
+
14
+ protected
15
+ def remove(vm)
16
+ Ghost::VM.new(vm).remove(:guests => false)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module VagrantPlugins
2
+ module Ghost
3
+ module Middleware
4
+ class Update
5
+ def initialize(app, env)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ @app.call(env)
11
+ update env[:vm] if env["vm"].state == :running
12
+ end
13
+
14
+ protected
15
+ def update(vm)
16
+ Ghost::VM.new(vm).update
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ module VagrantPlugins
2
+ module Ghost
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,154 @@
1
+ require 'forwardable'
2
+
3
+ module VagrantPlugins
4
+ module Ghost
5
+ class VM
6
+ extend Forwardable
7
+
8
+ def_delegators :@vm, :channel, :config, :env, :name, :uuid
9
+
10
+ class << self
11
+ def expand_path(relative_path, relative_to)
12
+ File.expand_path(relative_path, relative_to)
13
+ end
14
+
15
+ def hosts_path
16
+ Util::Platform.windows? ? expand_path('system32/drivers/etc/hosts', ENV['SYSTEMROOT']) : '/etc/hosts'
17
+ end
18
+ end
19
+
20
+ def initialize( vm )
21
+ @vm = vm
22
+ end
23
+
24
+ def add(options = {})
25
+ if process_local?(options)
26
+ env.ui.info("Adding host entry for #{name} VM. Administrator privileges will be required...") unless options[:quiet]
27
+ sudo add_command
28
+ end
29
+
30
+ with_other_vms { |vm| channel.sudo vm.add_command(:uuid => uuid, :hosts_path => hosts_path) } if process_guests?(options)
31
+ end
32
+
33
+ def hosts_path
34
+ # TODO: if windows guests are supported, this will need to be smarter
35
+ "/etc/hosts"
36
+ end
37
+
38
+ def list(options = {})
39
+ if process_local?(options)
40
+ output = `#{list_command}`.chomp
41
+ env.ui.info("[local] #{output}\n\n", :prefix => false) unless output.empty?
42
+ end
43
+
44
+ if process_guests?(options)
45
+ entries = ""
46
+ with_other_vms do |vm|
47
+ channel.execute(vm.list_command(:uuid => uuid, :hosts_path => hosts_path), :error_check => false) do |type, data|
48
+ entries << data if type == :stdout
49
+ end
50
+ end
51
+ entries = entries.split($/).collect { |entry| "[#{name}] #{entry}" }.join("\n")
52
+ env.ui.info("#{entries}\n\n", :prefix => false) unless entries.empty?
53
+ end
54
+ end
55
+
56
+ def remove(options = {})
57
+ if process_local?(options)
58
+ env.ui.info("Removing host entry for #{name} VM. Administrator privileges will be required...") unless options[:quiet]
59
+ sudo remove_command
60
+ end
61
+ with_other_vms { |vm| channel.sudo vm.remove_command(:uuid => uuid, :hosts_path => hosts_path) } if process_guests?(options)
62
+ end
63
+
64
+ def update(options = {})
65
+ if process_local?(options)
66
+ env.ui.info("Updating host entry for #{name} VM. Administrator privileges will be required...") unless options[:quiet]
67
+ sudo(remove_command) && sudo(add_command)
68
+ end
69
+ with_other_vms { |vm| channel.sudo(vm.remove_command(:uuid => uuid, :hosts_path => hosts_path)) && channel.sudo(vm.add_command(:uuid => uuid, :hosts_path => hosts_path)) } if process_guests?(options)
70
+ end
71
+
72
+ protected
73
+ def add_command(options = {})
74
+ uuid = options[:uuid] || self.uuid
75
+ hosts_path = options[:hosts_path] || self.class.hosts_path
76
+ %Q(sh -c 'echo "#{host_entry(uuid)}" >>#{hosts_path}')
77
+ end
78
+
79
+ def address
80
+ # network parameters consist of an address and a hash of options
81
+ @address ||= (network_parameters && network_parameters.first)
82
+ end
83
+
84
+ def host_aliases
85
+ @host_aliases ||= Array(config.hosts.aliases)
86
+ end
87
+
88
+ def host_entry(uuid = self.uuid)
89
+ %Q(#{address} #{host_names.join(' ')} #{signature(uuid)})
90
+ end
91
+
92
+ def host_name
93
+ @host_name ||= (config.hosts.name || config.vm.host_name)
94
+ end
95
+
96
+ def host_names
97
+ @host_names ||= (Array(host_name) + host_aliases)
98
+ end
99
+
100
+ def process_guests?(options = {})
101
+ {:guests => true}.merge(options)[:guests]
102
+ end
103
+
104
+ def process_local?(options = {})
105
+ {:local => true}.merge(options)[:local]
106
+ end
107
+
108
+ def list_command(options = {})
109
+ uuid = options[:uuid] || self.uuid
110
+ hosts_path = options[:hosts_path] || self.class.hosts_path
111
+ %Q(grep '#{signature(uuid)}$' #{hosts_path})
112
+ end
113
+
114
+ def network
115
+ # hostonly networks are the only ones we're interested in
116
+ @network ||= networks.find { |type,network_parameters| type == :hostonly }
117
+ end
118
+
119
+ def network_parameters
120
+ # network is a pair of a network type and the network parameters
121
+ @network_parameters ||= (network && network.last)
122
+ end
123
+
124
+ def networks
125
+ @networks ||= config.vm.networks
126
+ end
127
+
128
+ def remove_command(options = {})
129
+ uuid = options[:uuid] || self.uuid
130
+ hosts_path = options[:hosts_path] || self.class.hosts_path
131
+ %Q(sed -e '/#{signature(uuid)}$/ d' -ibak #{hosts_path})
132
+ end
133
+
134
+ def signature(uuid = self.uuid)
135
+ %Q(# VAGRANT: #{uuid} (#{name}))
136
+ end
137
+
138
+ def sudo(command)
139
+ if Util::Platform.windows?
140
+ `#{command}`
141
+ else
142
+ `sudo #{command}`
143
+ end
144
+ end
145
+
146
+ def with_other_vms
147
+ env.vms.each do |name,vm|
148
+ yield Ghost::VM.new(vm) if vm.config.vm.networks.any? { |type,network_parameters| type == :hostonly } && vm.name != self.name
149
+ end
150
+ end
151
+
152
+ end
153
+ end
154
+ end
@@ -0,0 +1,49 @@
1
+ begin
2
+ require 'vagrant'
3
+ rescue LoadError
4
+ raise 'The Vagrant Ghost plugin must be run within Vagrant.'
5
+ end
6
+
7
+ require 'vagrant-ghost/version'
8
+ require 'vagrant-ghost/vm'
9
+ require 'vagrant-ghost/middleware/remove'
10
+ require 'vagrant-ghost/middleware/update'
11
+
12
+ module VagrantPlugins
13
+ module Ghost
14
+ class Plugin < Vagrant.plugin( 2 )
15
+ name 'Ghost'
16
+ description <<-DESC
17
+ Something something something
18
+ DESC
19
+
20
+ config( :ghost ) do
21
+ require 'vagrant-ghost/config'
22
+ Ghost::Config
23
+ end
24
+
25
+ action_hook( :ghost, :machine_action_destroy ) do |hook|
26
+ hook.append( Ghost::Middleware::Remove )
27
+ end
28
+
29
+ action_hook( :ghost, :machine_action_up ) do |hook|
30
+ hook.append( Vagrant::Action::VM::Provision, Ghost::Middleware::Update )
31
+ end
32
+
33
+ action_hook( :ghost, :machine_action_reload ) do |hook|
34
+ hook.append( Ghost::Middleware::Update )
35
+ end
36
+
37
+ action_hook( :ghost, :machine_action_resume ) do |hook|
38
+ hook.append( Ghost::Middleware::Update )
39
+ end
40
+
41
+ command( 'ghost' ) do
42
+ require 'vagrant-ghost/command/root'
43
+ require 'vagrant-ghost/command/base'
44
+
45
+ Command::Root
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,59 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'vagrant-ghost/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "vagrant-ghost"
8
+ spec.version = VagrantPlugins::Ghost::VERSION
9
+ spec.platform = Gem::Platform::RUBY
10
+ spec.authors = ["Eric Mann"]
11
+ spec.email = ["eric@eamann.com"]
12
+ spec.summary = %q{Vagrant plugin to manage /etc/hosts}
13
+ spec.description = %q{Vagrant plugin to manage /etc/hosts}
14
+ spec.homepage = "https://github.com/ericmann/vagrant-ghost"
15
+ spec.license = "MIT"
16
+
17
+ #spec.files = `git ls-files -z`.split("\x0")
18
+ #spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ #spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ #spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.6"
23
+ spec.add_development_dependency "rake"
24
+
25
+ # The following block of code determines the files that should be included
26
+ # in the gem. It does this by reading all the files in the directory where
27
+ # this gemspec is, and parsing out the ignored files from the gitignore.
28
+ # Note that the entire gitignore(5) syntax is not supported, specifically
29
+ # the "!" syntax, but it should mostly work correctly.
30
+ root_path = File.dirname(__FILE__)
31
+ all_files = Dir.chdir(root_path) { Dir.glob("**/{*,.*}") }
32
+ all_files.reject! { |file| [".", ".."].include?(File.basename(file)) }
33
+ gitignore_path = File.join(root_path, ".gitignore")
34
+ gitignore = File.readlines(gitignore_path)
35
+ gitignore.map! { |line| line.chomp.strip }
36
+ gitignore.reject! { |line| line.empty? || line =~ /^(#|!)/ }
37
+
38
+ unignored_files = all_files.reject do |file|
39
+ # Ignore any directories, the gemspec only cares about files
40
+ next true if File.directory?(file)
41
+
42
+ # Ignore any paths that match anything in the gitignore. We do
43
+ # two tests here:
44
+ #
45
+ # - First, test to see if the entire path matches the gitignore.
46
+ # - Second, match if the basename does, this makes it so that things
47
+ # like '.DS_Store' will match sub-directories too (same behavior
48
+ # as git).
49
+ #
50
+ gitignore.any? do |ignore|
51
+ File.fnmatch(ignore, file, File::FNM_PATHNAME) ||
52
+ File.fnmatch(ignore, File.basename(file), File::FNM_PATHNAME)
53
+ end
54
+ end
55
+
56
+ spec.files = unignored_files
57
+ spec.executables = unignored_files.map { |f| f[/^bin\/(.*)/, 1] }.compact
58
+ spec.require_path = 'lib'
59
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vagrant-ghost
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Eric Mann
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
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 to manage /etc/hosts
42
+ email:
43
+ - eric@eamann.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - Gemfile
49
+ - lib/vagrant-ghost/command/base.rb
50
+ - lib/vagrant-ghost/command/list.rb
51
+ - lib/vagrant-ghost/command/remove.rb
52
+ - lib/vagrant-ghost/command/root.rb
53
+ - lib/vagrant-ghost/command/update.rb
54
+ - lib/vagrant-ghost/config.rb
55
+ - lib/vagrant-ghost/middleware/remove.rb
56
+ - lib/vagrant-ghost/middleware/update.rb
57
+ - lib/vagrant-ghost/version.rb
58
+ - lib/vagrant-ghost/vm.rb
59
+ - lib/vagrant-ghost.rb
60
+ - LICENSE.txt
61
+ - Rakefile
62
+ - README.md
63
+ - vagrant-ghost.gemspec
64
+ - .gitignore
65
+ homepage: https://github.com/ericmann/vagrant-ghost
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.0.14
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Vagrant plugin to manage /etc/hosts
89
+ test_files: []