From 768ddd89a77a37f8b1d4a0dd1339fd4c6ab590a9 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Sat, 22 Apr 2017 18:08:41 +0200 Subject: [PATCH] icinga2x-ha-cluster: Refactor Vagrantfile refs #17 refs #53 --- icinga2x-ha-cluster/Vagrantfile | 163 ++++++++++++++-------- icinga2x-ha-cluster/manifests/finalize.sh | 5 - icinga2x-ha-cluster/manifests/puppet.sh | 26 ---- 3 files changed, 105 insertions(+), 89 deletions(-) delete mode 100644 icinga2x-ha-cluster/manifests/finalize.sh delete mode 100644 icinga2x-ha-cluster/manifests/puppet.sh diff --git a/icinga2x-ha-cluster/Vagrantfile b/icinga2x-ha-cluster/Vagrantfile index 57d2c832..38cfe376 100644 --- a/icinga2x-ha-cluster/Vagrantfile +++ b/icinga2x-ha-cluster/Vagrantfile @@ -3,6 +3,7 @@ VAGRANTFILE_API_VERSION = "2" VAGRANT_REQUIRED_VERSION = "1.6.5" +VAGRANT_REQUIRED_LINKED_CLONE_VERSION = "1.8.4" # Require 1.6.5 at least if ! defined? Vagrant.require_version @@ -14,86 +15,132 @@ else Vagrant.require_version ">= " + VAGRANT_REQUIRED_VERSION end -Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| - - # ip addresses are used in /etc/icinga2/cluster/cluster.conf (!) - servers = { 'icinga2a' => '192.168.33.101', - 'icinga2b' => '192.168.33.102', - 'icinga2c' => '192.168.33.103' +# inspired by https://github.com/lbetz/vagrant-icinga-book/blob/master/Vagrantfile +nodes = { 'icinga2a' => { + :box => 'centos/7', + :mac => '020027000102', + :net => 'icinga2a.demo.local', + :hostonly => '192.168.33.101', + :memory => '1024', + :cpu => '1', + :fowarded => { + '443' => '8445', + '80' => '8085', + '22' => '2085' } - http_port = { 'icinga2a' => 8085, - 'icinga2b' => 8086, - 'icinga2c' => 8087 - } - ssh_port = { 'icinga2a' => 2085, - 'icinga2b' => 2086, - 'icinga2c' => 2087 - } - - servers.each do |server_name, server_ip| - config.vm.define server_name do |app_config| - app_config.vm.hostname = "#{server_name.to_s}" - app_config.vm.network :forwarded_port, guest: 22, host: ssh_port[server_name], auto_correct: true - app_config.vm.network :forwarded_port, guest: 80, host: http_port[server_name], auto_correct: true - app_config.vm.network :private_network, ip: "#{server_ip}" - - app_config.vm.provision :shell, :path => "manifests/puppet.sh" - - # workaround for Vagrant >1.8.4-1.9.1 not bringing up eth1 properly - # https://github.com/mitchellh/vagrant/issues/8096 - app_config.vm.provision "shell", inline: "service network restart", run: "always" - - # parallels - app_config.vm.provider :parallels do |p, override| - override.vm.box = "parallels/centos-7.2" + }, + 'icinga2b' => { + :box => 'centos/7', + :mac => '020027000103', + :net => 'icinga2b.demo.local', + :hostonly => '192.168.33.102', + :memory => '1024', + :cpu => '1', + :fowarded => { + '443' => '8446', + '80' => '8086', + '22' => '2086' + } + }, + 'icinga2c' => { + :box => 'centos/7', + :mac => '020027000104', + :net => 'icinga2c.demo.local', + :hostonly => '192.168.33.103', + :memory => '1024', + :cpu => '1', + :fowarded => { + '443' => '8447', + '80' => '8087', + '22' => '2087' + } + } +} - p.name = "Icinga 2: #{server_name.to_s}" +Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| + nodes.each_pair do |name, options| + config.vm.define name do |node_config| + node_config.vm.box = options[:box] + node_config.vm.hostname = name + node_config.vm.box_url = options[:url] if options[:url] + if options[:forwarded] + options[:forwarded].each_pair do |guest, local| + node_config.vm.network "forwarded_port", guest: guest, host: local + end + end - # Update Parallels Tools automatically - p.update_guest_tools = true + node_config.vm.network :private_network, ip: options[:hostonly] if options[:hostonly] + # provider: parallels + node_config.vm.provider :parallels do |p, override| + override.vm.box = "parallels/centos-7.2" override.vm.boot_timeout = 600 - # save storage - p.linked_clone = true + p.name = "Icinga 2: #{name.to_s}" + p.update_guest_tools = false + p.linked_clone = true if Gem::Version.new(Vagrant::VERSION) >= Gem::Version.new(VAGRANT_REQUIRED_LINKED_CLONE_VERSION) # Set power consumption mode to "Better Performance" p.customize ["set", :id, "--longer-battery-life", "off"] - p.memory = 1024 - p.cpus = 1 + p.memory = options[:memory] if options[:memory] + p.cpus = options[:cpus] if options[:cpus] end - # virtualbox - app_config.vm.provider :virtualbox do |v, override| - override.vm.box = "centos-71-x64-vbox" - override.vm.box_url = "http://boxes.icinga.com/centos-71-x64-vbox.box" - - v.customize ["modifyvm", :id, "--memory", "1024"] - v.customize ["modifyvm", :id, "--cpus", "2"] - # Using the host's resolver as a DNS proxy in NAT mode - v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"] + # provider: virtualbox + node_config.vm.provider :virtualbox do |vb, override| + unless Vagrant.has_plugin?("vagrant-vbguest") + raise 'vagrant-vbguest is not installed! Run "vagrant plugin install vagrant-vbguest"' + end + node_config.vbguest.auto_update = true + vb.linked_clone = true if Gem::Version.new(Vagrant::VERSION) >= Gem::Version.new(VAGRANT_REQUIRED_LINKED_CLONE_VERSION) + vb.name = name + vb.gui = options[:gui] if options[:gui] + vb.customize ["modifyvm", :id, + "--groups", "/Icinga Vagrant/" + options[:net], + "--memory", "512", + "--cpus", "1", + "--audio", "none", + "--usb", "on", + "--usbehci", "off", + "--natdnshostresolver1", "on", + #"--nic2", "intnet", + #"--intnet2", options[:net], + #"--macaddress2". options[:mac] + ] + vb.memory = options[:memory] if options[:memory] + vb.cpus = options[:cpus] if options[:cpus] + end - # save storage - v.linked_clone = true if Vagrant::VERSION =~ /^1.8/ + # provider: libvirt + node_config.vm.provider :libvirt do |lv, override| + lv.memory = options[:memory] if options[:memory] + lv.cpus = options[:cpus] if options[:cpus] end - app_config.vm.provider :libvirt do |l, override| - override.vm.box = "centos/7" + # provisioner: ensure that hostonly network is up + # + # workaround for Vagrant >1.8.4-1.9.1 not bringing up eth1 properly + # https://github.com/mitchellh/vagrant/issues/8096 + node_config.vm.provision "shell", inline: "service network restart", run: "always" - l.memory = 1024 - l.cpus = 1 - end + # provisioner: install requirements + node_config.vm.provision :shell, :path => "../scripts/shell_provisioner.sh" - # provisioner - app_config.vm.provision :puppet do |puppet| + # provisioner: install box using puppet manifest + node_config.vm.provision :puppet do |puppet| puppet.module_path = "../modules" puppet.manifests_path = "manifests" puppet.hiera_config_path = "hiera.yaml" - #puppet.options = "--verbose --debug" + #puppet.options = "--verbose --debug --parser=future" puppet.options = "--parser=future" end - app_config.vm.provision :shell, :path => "manifests/finalize.sh" + + # print a friendly message + node_config.vm.provision "shell", inline: <<-SHELL + echo "Finished installing the Vagrant box '#{name}'." + echo "Navigate to http://#{options[:hostonly]}" + SHELL end end end diff --git a/icinga2x-ha-cluster/manifests/finalize.sh b/icinga2x-ha-cluster/manifests/finalize.sh deleted file mode 100644 index 9773d9fa..00000000 --- a/icinga2x-ha-cluster/manifests/finalize.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -echo "The Icinga 2 Vagrant VM has finished installing." -echo "Navigate to http://192.168.33.10{1,2,3}" -echo "and log into Icinga Web 2 using icingaadmin/icinga as credentials." - diff --git a/icinga2x-ha-cluster/manifests/puppet.sh b/icinga2x-ha-cluster/manifests/puppet.sh deleted file mode 100644 index 01463be5..00000000 --- a/icinga2x-ha-cluster/manifests/puppet.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/bash - -set -e - -if which puppet >/dev/null 2>&1; then - exit 0 -fi - -RELEASEVER=$(rpm -q --qf "%{VERSION}" $(rpm -q --whatprovides redhat-release)) - -case $RELEASEVER in - 6|7) - PUPPET="http://yum.puppetlabs.com/puppetlabs-release-el-${RELEASEVER}.noarch.rpm" - ;; - *) - echo "Unknown release version: $RELEASEVER" >&2 - exit 1 - ;; -esac - -echo "Adding puppet repository.." -rpm --import "https://yum.puppetlabs.com/RPM-GPG-KEY-puppetlabs" -rpm -Uvh $PUPPET >/dev/null - -echo "Installing puppet.." -yum install -y puppet >/dev/null