Index: Vagrantfile |
diff --git a/Vagrantfile b/Vagrantfile |
index 7290a77ea9de45f0080b8bb4880a4c087163ab52..9c22004e5d1cf7e35db0cea728d3e8d4a4714067 100644 |
--- a/Vagrantfile |
+++ b/Vagrantfile |
@@ -1,73 +1,119 @@ |
+# coding: utf-8 |
+# vi: set fenc=utf-8 ft=ruby ts=8 sw=2 sts=2 et: |
+require 'shellwords' |
require 'yaml' |
-VAGRANTFILE_API_VERSION = "2" |
-REPOSITORY_DIR = File.dirname(__FILE__) |
-DEPENDENCY_SCRIPT = File.join(REPOSITORY_DIR, "ensure_dependencies.py") |
- |
-if !system("python", DEPENDENCY_SCRIPT) |
- error = Vagrant::Errors::VagrantError |
- error.error_message("Failed to ensure dependencies being up-to-date!") |
- raise error |
+# https://issues.adblockplus.org/ticket/170 |
+if !system('python', File.expand_path('../ensure_dependencies.py', __FILE__)) |
+ message = 'Failed to ensure dependencies being up-to-date!' |
+ raise Vagrant::Errors::VagrantError, message |
end |
-def define_standard_vm(config, host_name, ip, role=nil) |
- config.vm.define host_name do |config| |
- config.vm.box = 'precise64' |
- config.vm.box_url = 'http://cloud-images.ubuntu.com/vagrant/precise/current/precise-server-cloudimg-amd64-vagrant-disk1.box' |
- config.vm.host_name = "#{host_name}.adblockplus.org" |
- config.vm.network :private_network, ip: ip |
- config.vm.provider :virtualbox do |vb| |
- vb.customize ["modifyvm", :id, "--cpus", 1] |
+# https://www.vagrantup.com/docs/vagrantfile/version.html |
+Vagrant.configure('2') do |config| |
+ |
+ # The repository location in the production system's puppet master |
+ sync_path = '/etc/puppet/infrastructure' |
+ sync_type = system('which', 'rsync', :out => File::NULL) ? 'rsync' : nil |
+ |
+ # See also modules/adblockplus/manifests/host.pp |
+ hosts_file = File.expand_path('../modules/private/hiera/hosts.yaml', __FILE__) |
+ hosts_data = YAML.load_file(hosts_file) |
+ hosts_data.fetch('adblockplus::hosts', {}).each_pair do |name, record| |
+ |
+ # Formerly present hosts not destroyed yet require manual intervention |
+ next if record['ensure'] == 'absent' |
+ |
+ # https://docs.puppet.com/puppet/latest/man/apply.html |
+ puppet_options = Shellwords.split ENV.fetch('PUPPET_OPTIONS', '--verbose') |
+ puppet_options << '--debug' unless ENV.fetch('PUPPET_DEBUG', '').empty? |
+ puppet_options << '--environment=development' |
+ puppet_options << "--external_nodes=#{sync_path}/hiera/puppet_node_classifier.rb" |
+ puppet_options << '--node_terminus=exec' |
+ |
+ # https://www.vagrantup.com/docs/multi-machine/ |
+ config.vm.define name do |host| |
+ |
+ if record.fetch('os', 'ubuntu-precise') == 'ubuntu-precise' |
- # Work around https://www.virtualbox.org/ticket/11649 |
- vb.customize ['modifyvm', :id, '--natdnshostresolver1', 'on'] |
+ # http://cloud-images.ubuntu.com/vagrant/precise/current/ |
+ host.vm.box = 'precise64' |
+ host.vm.box_url = 'http://cloud-images.ubuntu.com/vagrant/precise/current/precise-server-cloudimg-amd64-vagrant-disk1.box' |
- setup_path = File.join(REPOSITORY_DIR, "hiera", "roles", "#{role}.yaml") |
- setup = YAML.load_file(setup_path) rescue {} |
- requirements = setup.fetch("requirements", {}) |
+ # https://www.vagrantup.com/docs/provisioning/shell.html |
+ host.vm.provision :shell, :privileged => true, :inline => <<-end |
+ python /etc/puppet/infrastructure/hiera/install_precise.py |
+ end |
- requirements.each do |key, value| |
- vb.customize ['modifyvm', :id, "--#{key}", "#{value}"] |
+ # https://www.vagrantup.com/docs/synced-folders/ |
+ host.vm.synced_folder '.', sync_path, type: sync_type |
+ |
+ elsif record['os'] == 'debian-jessie' |
+ |
+ # https://www.vagrantup.com/docs/boxes.html |
+ host.vm.box = 'debian/contrib-jessie64' |
+ host.vm.box_url = 'https://atlas.hashicorp.com/debian/boxes/contrib-jessie64' |
+ |
+ # https://packages.debian.org/jessie/puppet |
+ host.vm.provision :shell, :privileged => true, :inline => <<-end |
+ set -e -- '#{sync_path}' /etc/puppet/hiera.yaml |
+ if ! which puppet >/dev/null; then |
+ apt-get -y update |
+ apt-get -y install puppet |
+ fi |
+ test -e "$1" || ln -s /vagrant "$1" |
+ test -e "$2" || ln -s infrastructure/hiera/hiera.yaml "$2" |
+ puppet agent --enable |
+ end |
+ |
+ # https://docs.puppet.com/puppet/latest/configuration.html#hieraconfig |
+ puppet_options << "--hiera_config=#{sync_path}/hiera/hiera.yaml" |
+ |
+ else |
+ message = "Unrecognized OS '#{record['os']}' for host '#{name}'" |
+ raise Vagrant::Errors::VagrantError, message |
end |
- end |
+ # https://www.vagrantup.com/docs/vagrantfile/machine_settings.html |
+ host.vm.hostname = record.fetch('fqdn', "#{name}.test") |
- # The repository location in the production system's puppet master |
- if system('which', 'rsync', :out => File::NULL) |
- config.vm.synced_folder '.', '/etc/puppet/infrastructure', type: 'rsync' |
- else |
- config.vm.synced_folder '.', '/etc/puppet/infrastructure' |
- end |
+ # https://www.vagrantup.com/docs/networking/ |
+ host.vm.network :private_network, ip: record['ips'][0] |
+ |
+ # https://www.vagrantup.com/docs/virtualbox/configuration.html |
+ host.vm.provider :virtualbox do |virtualbox| |
+ |
+ # The GUI would be just annoying to pop up by default every time |
+ virtualbox.gui = !ENV.fetch('VIRTUALBOX_GUI', '').empty? |
+ |
+ # Individual box configuration may increase the number of CPUs |
+ virtualbox.customize ['modifyvm', :id, '--cpus', 1] |
+ # Work around https://www.virtualbox.org/ticket/11649 |
+ virtualbox.customize ['modifyvm', :id, '--natdnshostresolver1', 'on'] |
+ |
+ # Role-specific requirements, optional |
+ role_name = record.fetch('role', 'default') |
+ role_file = File.expand_path("../hiera/roles/#{role_name}.yaml", __FILE__) |
+ role_data = File.exists?(role_file) ? YAML.load_file(role_file) : {} |
+ role_data.fetch('requirements', {}).each do |key, value| |
+ virtualbox.customize ['modifyvm', :id, "--#{key}", value.to_s] |
+ end |
+ |
+ end |
+ |
+ # https://www.vagrantup.com/docs/provisioning/puppet_apply.html |
+ host.vm.provision :puppet do |puppet| |
+ puppet.manifests_path = 'manifests' |
+ puppet.manifest_file = 'site.pp' |
+ puppet.module_path = 'modules' |
+ puppet.options = puppet_options |
+ end |
+ |
+ # https://github.com/mitchellh/vagrant/issues/1673 |
+ host.ssh.shell = "sh -c 'BASH_ENV=/etc/profile exec bash'" |
- config.vm.provision :shell, :inline => ' |
- sudo /etc/puppet/infrastructure/hiera/install_precise.py |
- ' |
- |
- config.vm.provision :puppet do |puppet| |
- puppet.options = [ |
- '--environment=development', |
- '--external_nodes=/etc/puppet/infrastructure/hiera/puppet_node_classifier.rb', |
- '--node_terminus=exec', |
- '--verbose', |
- '--debug', |
- ] |
- puppet.manifests_path = 'manifests' |
- puppet.manifest_file = 'site.pp' |
- puppet.module_path = 'modules' |
end |
- yield(config) if block_given? |
end |
-end |
-Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| |
- config_path = File.join(REPOSITORY_DIR, "modules/private/hiera/hosts.yaml") |
- config_data = YAML.load_file(config_path) |
- servers = config_data["adblockplus::hosts"] |
- servers.each do |server, items| |
- next if items['ensure'] == 'absent' |
- ip = items["ips"][0] |
- role = items.fetch("role", "default") |
- define_standard_vm(config, server, ip, role) |
- end |
end |