vagrant-hosts 0.0.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -6,14 +6,18 @@ Manage vagrant guest local DNS resolution.
6
6
  Synopsis
7
7
  --------
8
8
 
9
- Vagrant::Config.run do |config|
9
+ Manually specify addresses:
10
+
11
+ Vagrant.configure('2') do |config|
10
12
  config.vm.box = "ubuntu-12.04-server-i386"
11
13
 
12
14
  config.vm.provision :hosts do |provisioner|
13
15
  # Add a single hostname
14
16
  provisioner.add_host '10.0.2.2', ['myhost.vagrantup.internal']
17
+
15
18
  # Or a fqdn and a short hostname
16
- provisioner.add_host '10.0.2.3', ['myotherhost.vagrantup.internal', 'myotherhost']
19
+ provisioner.add_host '10.0.2.3', ['myotherhost.vagrantup.internal', 'myotherhost']
20
+
17
21
  # Or as many aliases as you like!
18
22
  provisioner.add_host '10.0.2.5', [
19
23
  'mypuppetmaster.vagrantup.internal',
@@ -24,17 +28,64 @@ Synopsis
24
28
  end
25
29
  end
26
30
 
27
- Supported Guest Platforms
28
- -------------------------
31
+ - - -
32
+
33
+ Autodetect internal network addresses and autoconfigure hosts:
34
+
35
+ # Autoconfigure hosts. This will copy the private network addresses from
36
+ # each VM and update hosts entries on all other machines. No further
37
+ # configuration is needed.
38
+ Vagrant.configure('2') do |config|
39
+
40
+ config.vm.define :first do |node|
41
+ node.vm.box = "ubuntu-12.04-server-i386"
42
+ node.vm.network :private_network, :ip => '10.20.1.2'
43
+ node.vm.provision :hosts
44
+ end
45
+
46
+ config.vm.define :second do |node|
47
+ node.vm.box = "ubuntu-12.04-server-i386"
48
+ node.vm.network :private_network, :ip => '10.20.1.3'
49
+ node.vm.provision :hosts
50
+ end
51
+ end
52
+
53
+ - - -
54
+
55
+ Use autodetection with manual entries
56
+
57
+ Vagrant.configure('2') do |config|
58
+
59
+ config.vm.define :first do |node|
60
+ node.vm.box = "ubuntu-12.04-server-i386"
61
+ node.vm.network :private_network, :ip => '10.20.1.2'
62
+ node.vm.provision :hosts do |provisioner|
63
+ provisioner.autoconfigure = true
64
+ provisioner.add_host 172.16.3.10, ['yum.mirror.local']
65
+ end
66
+
67
+ end
68
+
69
+ config.vm.define :second do |node|
70
+ node.vm.box = "ubuntu-12.04-server-i386"
71
+ node.vm.network :private_network, :ip => '10.20.1.3'
72
+ node.vm.provision :hosts do |provisioner|
73
+ provisioner.autoconfigure = true
74
+ provisioner.add_host 172.16.3.11, ['apt.mirror.local']
75
+ end
76
+ end
77
+ end
78
+
79
+ Supported Platforms
80
+ -------------------
81
+
82
+ As of version 1.0.0 or later Vagrant 1.1 is required.
83
+
84
+ Supported guests:
29
85
 
30
86
  * Linux
31
- * (soon) Windows (As soon as I get a chance to incorporate the driver)
32
87
 
33
88
  Installation
34
89
  ------------
35
90
 
36
- # For full releases
37
- gem install vagrant-hosts
38
- # For pre releases
39
- gem install --pre vagrant-hosts
40
- # Vagrant hosts magic!
91
+ vagrant plugin install vagrant-hosts
@@ -1,12 +1,20 @@
1
1
  require 'vagrant'
2
- require 'vagrant-hosts'
3
2
 
4
- class VagrantHosts::Config < Vagrant::Config::Base
3
+ module VagrantHosts
4
+ class Config < Vagrant.plugin('2', :config)
5
5
 
6
+ # @!attribute hosts
7
+ # @return [Array<Array<String, Array<String>>>] A list of IP addresses and their aliases
6
8
  attr_reader :hosts
7
9
 
10
+ # @!attribute autoconfigure
11
+ # @return [TrueClass, FalseClass] If hosts should be generated from the
12
+ # other vagrant machines
13
+ attr_accessor :autoconfigure
14
+
8
15
  def initialize
9
16
  @hosts = []
17
+ @autoconfigure = UNSET_VALUE
10
18
  end
11
19
 
12
20
  # Register a host for entry
@@ -17,6 +25,7 @@ class VagrantHosts::Config < Vagrant::Config::Base
17
25
  @hosts << [address, aliases]
18
26
  end
19
27
 
28
+ # All IPv6 multicast addresses
20
29
  def add_ipv6_multicast
21
30
  add_host '::1', ['ip6-localhost', 'ip6-loopback']
22
31
  add_host 'fe00::0', ['ip6-localnet']
@@ -25,11 +34,29 @@ class VagrantHosts::Config < Vagrant::Config::Base
25
34
  add_host 'ff02::2', ['ip6-allrouters']
26
35
  end
27
36
 
28
- def validate(env, errors)
37
+ def finalize!
38
+ if @autoconfigure == UNSET_VALUE or @hosts.empty?
39
+ @autoconfigure = true
40
+ end
41
+ end
42
+
43
+ # @param other [VagrantHosts::Config]
44
+ # @return [VagrantHosts::Config] The merged results
45
+ def merge(other)
46
+ super.tap do |result|
47
+ result.hosts += other.hosts
48
+ end
49
+ end
50
+
51
+ def validate(machine)
52
+ errors = []
29
53
  @hosts.each do |(address, aliases)|
30
54
  unless aliases.is_a? Array
31
- errors.add("#{address} should have an array of aliases, got #{aliases.inspect}:#{aliases.class}")
55
+ errors << "#{address} should have an array of aliases, got #{aliases.inspect}:#{aliases.class}"
32
56
  end
33
57
  end
58
+
59
+ {"Vagrant Hosts" => errors}
34
60
  end
35
61
  end
62
+ end
@@ -0,0 +1,26 @@
1
+ require 'vagrant'
2
+ require 'vagrant-hosts'
3
+ require 'vagrant-hosts/version'
4
+
5
+ if Vagrant::VERSION < "1.1.0"
6
+ raise "vagrant-hosts version #{VagrantHosts::VERSION} requires Vagrant 1.1 or later"
7
+ end
8
+
9
+ class VagrantHosts::Plugin < Vagrant.plugin(2)
10
+ name 'hosts'
11
+
12
+ description <<-DESC
13
+ This plugin adds commands and provisioners to manage static host entries on
14
+ Vagrant guests.
15
+ DESC
16
+
17
+ provisioner(:hosts) do
18
+ require_relative 'provisioner'
19
+ VagrantHosts::Provisioner
20
+ end
21
+
22
+ config(:hosts, :provisioner) do
23
+ require_relative 'config'
24
+ VagrantHosts::Config
25
+ end
26
+ end
@@ -0,0 +1,90 @@
1
+ require 'vagrant'
2
+ require 'tempfile'
3
+
4
+
5
+ module VagrantHosts
6
+ class Provisioner < Vagrant.plugin('2', :provisioner)
7
+
8
+ def initialize(machine, config)
9
+ @machine, @config = machine, config
10
+ p
11
+ end
12
+
13
+ def provision
14
+ # too tired to do this. detect target platform, select according provider,
15
+ # add entries that are specified in the config and are not on the client
16
+
17
+ driver = Linux.new(@machine, @config)
18
+ driver.sync!
19
+ end
20
+
21
+ class Linux
22
+
23
+ def initialize(machine, config)
24
+ @machine, @config = machine, config
25
+ end
26
+
27
+ def sync!
28
+ upload_tmphosts
29
+ update_hosts
30
+ end
31
+
32
+ private
33
+
34
+ def upload_tmphosts
35
+ cache = Tempfile.new('tmp-hosts')
36
+ cache.write(format_hosts)
37
+ cache.flush
38
+ @machine.communicate.upload(cache.path, '/tmp/hosts')
39
+ end
40
+
41
+ def update_hosts
42
+ @machine.guest.change_host_name(@machine.name.to_s)
43
+ @machine.communicate.sudo('install -m 644 /tmp/hosts /etc/hosts')
44
+ end
45
+
46
+ # Generates content appropriate for a linux hosts file
47
+ #
48
+ # @return [String] All hosts in the config joined into hosts records
49
+ def format_hosts
50
+ all_hosts.inject('') do |str, (address, aliases)|
51
+ str << "#{address} #{aliases.join(' ')}\n"
52
+ end
53
+ end
54
+
55
+ def all_hosts
56
+ all_hosts = []
57
+
58
+ if @config.autoconfigure
59
+ all_hosts += vagrant_hosts
60
+ else
61
+ all_hosts += @config.hosts
62
+ end
63
+
64
+ all_hosts.unshift(['127.0.0.1', ['localhost']])
65
+ all_hosts.unshift(['127.0.1.1', [@machine.name]])
66
+ end
67
+
68
+ def vagrant_hosts
69
+ hosts = []
70
+ env = @machine.env
71
+ names = env.machine_names
72
+
73
+ # Assume that all VMs are using the current provider
74
+ provider = @machine.provider
75
+
76
+ names.each do |name|
77
+ network_settings = env.machine(name, :virtualbox).config.vm.networks
78
+ network_settings.each do |entry|
79
+ if entry[0] == :private_network
80
+ ipaddr = entry[1][:ip]
81
+ hosts << [ipaddr, [name]]
82
+ end
83
+ end
84
+ end
85
+
86
+ hosts
87
+ end
88
+ end
89
+ end
90
+ end
@@ -1,3 +1,3 @@
1
1
  module VagrantHosts
2
- VERSION = '0.0.2'
2
+ VERSION = '1.0.0'
3
3
  end
data/lib/vagrant-hosts.rb CHANGED
@@ -2,6 +2,4 @@
2
2
  module VagrantHosts; end
3
3
 
4
4
  require 'vagrant-hosts/version'
5
- require 'vagrant-hosts/provisioners/hosts'
6
-
7
- Vagrant.provisioners.register(:hosts) { VagrantHosts::Provisioner }
5
+ require 'vagrant-hosts/plugin'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-hosts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-22 00:00:00.000000000 Z
12
+ date: 2013-03-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: vagrant
@@ -39,10 +39,9 @@ files:
39
39
  - README.markdown
40
40
  - lib/vagrant-hosts.rb
41
41
  - lib/vagrant-hosts/config.rb
42
- - lib/vagrant-hosts/provisioners/hosts.rb
43
- - lib/vagrant-hosts/ssh.rb
42
+ - lib/vagrant-hosts/plugin.rb
43
+ - lib/vagrant-hosts/provisioner.rb
44
44
  - lib/vagrant-hosts/version.rb
45
- - lib/vagrant_init.rb
46
45
  - vagrant-hosts.gemspec
47
46
  homepage: https://github.com/adrienthebo/vagrant-hosts
48
47
  licenses:
@@ -1,64 +0,0 @@
1
- require 'vagrant'
2
- require 'vagrant-hosts'
3
- require 'vagrant-hosts/ssh' # Guerrilla patch ssh download
4
- require 'vagrant-hosts/config'
5
- require 'tempfile'
6
-
7
-
8
- class VagrantHosts::Provisioner < Vagrant::Provisioners::Base
9
-
10
- def self.config_class
11
- ::VagrantHosts::Config
12
- end
13
-
14
- def provision!
15
- # too tired to do this. detect target platform, select according provider,
16
- # add entries that are specified in the config and are not on the client
17
-
18
- driver = Linux.new(@env, config)
19
- driver.sync!
20
- end
21
-
22
- class Linux
23
-
24
- def initialize(env, config)
25
- @env, @config = env, config
26
- end
27
-
28
- def sync!
29
- upload_tmphosts
30
- atomic_sync
31
- end
32
-
33
- def upload_tmphosts
34
- cache = Tempfile.new('tmp-hosts')
35
- cache.write(format_hosts)
36
- cache.flush
37
- @env[:vm].channel.upload(cache.path, '/tmp/hosts')
38
- end
39
-
40
- def atomic_sync
41
- script = <<-ATOMIC
42
- hostname #{@env[:vm].name}
43
- domainname vagrantup.internal
44
- install -m 644 /tmp/hosts /etc/hosts
45
- ATOMIC
46
-
47
- sync = Tempfile.new('sync')
48
- sync.write(script)
49
- sync.flush
50
- @env[:vm].channel.upload(sync.path, '/tmp/sync')
51
-
52
- @env[:vm].channel.sudo('bash /tmp/sync')
53
- end
54
-
55
- # Generates content appropriate for a linux hosts file
56
- #
57
- # @return [String] All hosts in the config joined into hosts records
58
- def format_hosts
59
- @config.hosts.inject('') do |str, (address, aliases)|
60
- str << "#{address} #{aliases.join(' ')}\n"
61
- end
62
- end
63
- end
64
- end
@@ -1,27 +0,0 @@
1
- require 'vagrant'
2
- require 'vagrant/errors'
3
- require 'vagrant/communication/ssh'
4
-
5
- # Helloooooo monkey patching.
6
-
7
- class Vagrant::Communication::SSH
8
-
9
- # Download a remote file
10
- #
11
- # @param [String] from the path on the remote end
12
- # @param [String] to the path on the local end
13
- #
14
- #
15
- def download(from, to)
16
- @logger.debug("Downlaoding: #{from} to #{to}")
17
-
18
- connect do |connection|
19
- scp = Net::SCP.new(connection)
20
- scp.download!(from, to)
21
- end
22
-
23
- rescue Net::SCP::Error => e
24
- raise Vagrant::Errors::SCPUnavailable if e.message =~ /\(127\)/
25
- raise
26
- end
27
- end
data/lib/vagrant_init.rb DELETED
@@ -1 +0,0 @@
1
- require 'vagrant-hosts'