Skip to content

Commit

Permalink
Replace old crufty /etc/hosts management with vagrant-hostmanager.
Browse files Browse the repository at this point in the history
  • Loading branch information
purpleidea committed Jul 25, 2014
1 parent ad0f567 commit 55f636b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 151 deletions.
4 changes: 4 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Dependencies:
* my puppet-puppet module (optional)
* my puppet-nfs module (optional)

Vagrant (optional):
* The vagrant integration works with the vagrant-libvirt provider.
* This requires the vagrant-hostmanager plugin for /etc/hosts management.


Happy hacking,
James Shubin <[email protected]>, https://ttboj.wordpress.com/
Expand Down
179 changes: 28 additions & 151 deletions vagrant/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,17 @@ offset = 100 # start hosts after here
#puts range[2].to_s # puppetmaster
#puts range[3].to_s # vip

network2 = IPAddr.new '192.168.145.0/24'
range2 = network2.to_range.to_a
cidr2 = (32-(Math.log(range2.length)/Math.log(2))).to_i
offset2 = 2
netmask2 = IPAddr.new('255.255.255.255').mask(cidr2).to_s
#
# puppetmaster
#
puppet_ip = range[2].to_s
puppet_hostname = 'puppet'

#
# vip
#
vip_ip = range[3].to_s
vip_hostname = 'ipa'

# mutable by ARGV and settings file
count = 1 # default number of ipa hosts to build
Expand Down Expand Up @@ -190,17 +196,6 @@ else
snoop = []
end

# figure out which hosts are getting destroyed
destroy = ARGV.select { |x| !x.start_with?('-') }
if destroy.length > 0 and destroy[0] == 'destroy'
destroy.shift # left over array destroy should be list of hosts or []
if destroy.length == 0
destroy = true # destroy everything
end
else
destroy = false # destroy nothing
end

# figure out which hosts are getting provisioned
provision = ARGV.select { |x| !x.start_with?('-') }
if provision.length > 0 and ['up', 'provision'].include?(provision[0])
Expand All @@ -224,8 +219,6 @@ end

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

#config.landrush.enable # TODO ?

#
# box (pre-built base image)
#
Expand All @@ -240,6 +233,23 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
#
config.vm.synced_folder './', '/vagrant', type: sync # nfs, rsync

#
# hostmanager
#
config.hostmanager.enabled = true
config.hostmanager.manage_host = false # don't manage local /etc/hosts
config.hostmanager.ignore_private_ip = false
config.hostmanager.include_offline = true # manage all the hosts!
config.hostmanager.fqdn_friendly = true # fqdns need to work...
config.hostmanager.domain_name = domain # use this domain name!
config.hostmanager.extra_hosts = [
{
:host => "#{vip_hostname}.#{domain}",
:ip => "#{vip_ip}",
:aliases => ["#{vip_hostname}"],
}
]

#
# cache
#
Expand All @@ -263,75 +273,23 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
end
end

#
# vip
#
vip_ip = range[3].to_s
vip_hostname = 'ipa'

#
# puppetmaster
#
puppet_ip = range[2].to_s
puppet_hostname = 'puppet'
fv = File.join(projectdir, '.vagrant', "#{puppet_hostname}-hosts.done")
if destroy.is_a?(TrueClass) or (destroy.is_a?(Array) and destroy.include?(puppet_hostname))
if File.exists?(fv) # safety
puts "Unlocking shell provisioning for: #{puppet_hostname}..."
File.delete(fv) # delete hosts token
end
end

#puppet_fqdn = "#{puppet_hostname}.#{domain}"
config.vm.define :puppet, :primary => true do |vm|
vm.vm.hostname = puppet_hostname
# red herring network so that management happens here...
vm.vm.network :private_network,
:ip => range2[2].to_s,
:libvirt__netmask => netmask2,
#:libvirt__dhcp_enabled => false, # XXX: not allowed here
:libvirt__network_name => 'default'

# this is the real network that we'll use...
vm.vm.network :private_network,
:ip => puppet_ip,
:libvirt__dhcp_enabled => false,
:libvirt__network_name => 'ipa'

#vm.landrush.host puppet_hostname, puppet_ip # TODO ?

# ensure the ipa module is present for provisioning...
if provision.is_a?(TrueClass) or (provision.is_a?(Array) and provision.include?(puppet_hostname))
cwd = `pwd`
mod = File.join(projectdir, 'puppet', 'modules')
`cd #{mod} && make ipa &> /dev/null; cd #{cwd}`
end

#
# shell
#
if not File.exists?(fv) # only modify /etc/hosts once
if provision.is_a?(TrueClass) or (provision.is_a?(Array) and provision.include?(puppet_hostname))
File.open(fv, 'w') {} # touch
end
vm.vm.provision 'shell', inline: 'puppet resource host localhost.localdomain ip=127.0.0.1 host_aliases=localhost'
vm.vm.provision 'shell', inline: "puppet resource host #{puppet_hostname} ensure=absent" # so that fqdn works

vm.vm.provision 'shell', inline: "puppet resource host #{vip_hostname}.#{domain} ip=#{vip_ip} host_aliases=#{vip_hostname} ensure=present"
vm.vm.provision 'shell', inline: "puppet resource host #{puppet_hostname}.#{domain} ip=#{puppet_ip} host_aliases=#{puppet_hostname} ensure=present"
(1..count).each do |i|
h = "ipa#{i}"
ip = range[offset+i].to_s
vm.vm.provision 'shell', inline: "puppet resource host #{h}.#{domain} ip=#{ip} host_aliases=#{h} ensure=present"
end

# hosts entries for all clients
(1..clients).each do |i|
h = "client#{i}"
ip = range[offset+count+i].to_s
vm.vm.provision 'shell', inline: "puppet resource host #{h}.#{domain} ip=#{ip} host_aliases=#{h} ensure=present"
end
end
#
# puppet (apply)
#
Expand Down Expand Up @@ -361,13 +319,6 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
h = "ipa#{i}"
ip = range[offset+i].to_s
#fqdn = "#{h}.#{domain}"
fvx = File.join(projectdir, '.vagrant', "#{h}-hosts.done")
if destroy.is_a?(TrueClass) or (destroy.is_a?(Array) and destroy.include?(h))
if File.exists?(fvx) # safety
puts "Unlocking shell provisioning for: #{h}..."
File.delete(fvx) # delete hosts token
end
end

if snoop.include?(h) # should we clean this machine?
cmd = "puppet cert clean #{h}.#{domain}"
Expand All @@ -380,11 +331,6 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

config.vm.define h.to_sym do |vm|
vm.vm.hostname = h
# red herring network so that management happens here...
vm.vm.network :private_network,
:ip => range2[offset2+i].to_s,
:libvirt__netmask => netmask2,
:libvirt__network_name => 'default'

# this is the real network that we'll use...
vm.vm.network :private_network,
Expand All @@ -402,35 +348,6 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
vm.vm.network 'forwarded_port', guest: 80, host: 80
vm.vm.network 'forwarded_port', guest: 443, host: 443

#vm.landrush.host h, ip # TODO ?

#
# shell
#
if not File.exists?(fvx) # only modify /etc/hosts once
if provision.is_a?(TrueClass) or (provision.is_a?(Array) and provision.include?(h))
File.open(fvx, 'w') {} # touch
end
vm.vm.provision 'shell', inline: 'puppet resource host localhost.localdomain ip=127.0.0.1 host_aliases=localhost'
vm.vm.provision 'shell', inline: "puppet resource host #{h} ensure=absent" # so that fqdn works

vm.vm.provision 'shell', inline: "puppet resource host #{vip_hostname}.#{domain} ip=#{vip_ip} host_aliases=#{vip_hostname} ensure=present"
vm.vm.provision 'shell', inline: "puppet resource host #{puppet_hostname}.#{domain} ip=#{puppet_ip} host_aliases=#{puppet_hostname} ensure=present"
#vm.vm.provision 'shell', inline: "[ ! -e /root/puppet-cert-is-clean ] && ssh -o 'StrictHostKeyChecking=no' #{puppet_hostname} puppet cert clean #{h}.#{domain} ; touch /root/puppet-cert-is-clean"
# hosts entries for all hosts
(1..count).each do |j|
oh = "ipa#{j}"
oip = range[offset+j].to_s # eg: "192.168.142.#{100+i}"
vm.vm.provision 'shell', inline: "puppet resource host #{oh}.#{domain} ip=#{oip} host_aliases=#{oh} ensure=present"
end

# hosts entries for all clients
(1..clients).each do |j|
oh = "client#{j}"
oip = range[offset+count+j].to_s
vm.vm.provision 'shell', inline: "puppet resource host #{oh}.#{domain} ip=#{oip} host_aliases=#{oh} ensure=present"
end
end
#
# puppet (agent)
#
Expand Down Expand Up @@ -473,13 +390,6 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
h = "client#{i}"
ip = range[offset+count+i].to_s
#fqdn = "ipa#{i}.#{domain}"
fvy = File.join(projectdir, '.vagrant', "#{h}-hosts.done")
if destroy.is_a?(TrueClass) or (destroy.is_a?(Array) and destroy.include?(h))
if File.exists?(fvy) # safety
puts "Unlocking shell provisioning for: #{h}..."
File.delete(fvy) # delete hosts token
end
end

if snoop.include?(h) # should we clean this machine?
cmd = "puppet cert clean #{h}.#{domain}"
Expand All @@ -492,46 +402,13 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

config.vm.define h.to_sym do |vm|
vm.vm.hostname = h
# red herring network so that management happens here...
vm.vm.network :private_network,
:ip => range2[offset2+count+i].to_s,
:libvirt__netmask => netmask2,
:libvirt__network_name => 'default'

# this is the real network that we'll use...
vm.vm.network :private_network,
:ip => ip,
:libvirt__dhcp_enabled => false,
:libvirt__network_name => 'ipa'

#vm.landrush.host h, ip # TODO ?

#
# shell
#
if not File.exists?(fvy) # only modify /etc/hosts once
if provision.is_a?(TrueClass) or (provision.is_a?(Array) and provision.include?(h))
File.open(fvy, 'w') {} # touch
end
vm.vm.provision 'shell', inline: 'puppet resource host localhost.localdomain ip=127.0.0.1 host_aliases=localhost'
vm.vm.provision 'shell', inline: "puppet resource host #{h} ensure=absent" # so that fqdn works

vm.vm.provision 'shell', inline: "puppet resource host #{vip_hostname}.#{domain} ip=#{vip_ip} host_aliases=#{vip_hostname} ensure=present"
vm.vm.provision 'shell', inline: "puppet resource host #{puppet_hostname}.#{domain} ip=#{puppet_ip} host_aliases=#{puppet_hostname} ensure=present"
# hosts entries for all hosts
(1..count).each do |j|
oh = "ipa#{j}"
oip = range[offset+j].to_s # eg: "192.168.142.#{100+i}"
vm.vm.provision 'shell', inline: "puppet resource host #{oh}.#{domain} ip=#{oip} host_aliases=#{oh} ensure=present"
end

# hosts entries for all clients
(1..clients).each do |j|
oh = "client#{j}"
oip = range[offset+count+j].to_s
vm.vm.provision 'shell', inline: "puppet resource host #{oh}.#{domain} ip=#{oip} host_aliases=#{oh} ensure=present"
end
end
#
# puppet (agent)
#
Expand Down

0 comments on commit 55f636b

Please sign in to comment.