Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- mode: ruby -*-
- # vi: set ft=ruby :
- # vim: noai:ts=2:sw=2:et
- # Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
- VAGRANTFILE_API_VERSION = "2"
- UBUNTU_BOX='ubuntu/trusty64'
- CENTOS_BOX='centos/7'
- DOCKER_MEM = 1024
- DOCKER_CPUS = 2
- # to find our provisioning scripts
- dir = Dir.pwd
- vagrant_dir = File.expand_path(File.dirname(__FILE__))
- servers = {
- "ansible01" => { :ip => "172.30.5.60", :bridge => "eth1",
- :mem => DOCKER_MEM, :cpus => DOCKER_CPUS, :box => CENTOS_BOX,
- :scripts => "provision/ansible-install.sh" },
- "docker-engine01" => { :ip => "172.30.5.61", :bridge => "eth1",
- :mem => DOCKER_MEM, :cpus => DOCKER_CPUS, :box => UBUNTU_BOX,
- :scripts => "provision/docker-install.sh" },
- "docker-engine02" => { :ip => "172.30.5.62", :bridge => "eth1",
- :mem => DOCKER_MEM, :cpus => DOCKER_CPUS, :box => UBUNTU_BOX,
- :scripts => "provision/docker-install.sh" },
- "docker-engine03" => { :ip => "172.30.5.63", :bridge => "eth1",
- :mem => DOCKER_MEM, :cpus => DOCKER_CPUS, :box => UBUNTU_BOX,
- :scripts => "provision/docker-install.sh" },
- "docker-engine04" => { :ip => "172.30.5.64", :bridge => "eth1",
- :mem => DOCKER_MEM, :cpus => DOCKER_CPUS, :box => UBUNTU_BOX,
- :scripts => "provision/docker-install.sh" }
- }
- Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
- config.vm.box_check_update = false
- # enable logging in via ssh with a password
- #config.ssh.username = "vagrant"
- #config.ssh.password = "vagrant"
- ################################################################################
- servers.each do |hostname, info|
- #
- # build a vm - from the server dict
- #
- config.vm.define hostname do |cfg| # a define per hostname
- cfg.vm.box = info[:box]
- cfg.vm.hostname = hostname
- # note the public network
- cfg.vm.network "public_network", ip: info[:ip], bridge: info[:bridge]
- cfg.vm.provider "virtualbox" do |v|
- v.name = hostname
- v.memory = info[:mem]
- v.cpus = info[:cpus]
- v.customize ["modifyvm", :id, "--hwvirtex", "on"]
- end
- #
- # do some provisioning
- #
- ssh_prv_key = ""
- ssh_pub_key = ""
- if not File.file?("#{Dir.home}/.ssh/vagrant")
- puts "No SSH key found. You will need to remedy this before pushing to the repository."
- else
- ssh_prv_key = File.read("#{Dir.home}/.ssh/vagrant")
- ssh_pub_key = File.readlines("#{Dir.home}/.ssh/vagrant.pub").first.strip
- cfg.vm.provision "shell", inline: <<-SHELL
- if grep -sq "#{ssh_pub_key}" /home/vagrant/.ssh/authorized_keys; then
- echo "SSH keys already provisioned."
- exit 0;
- fi
- echo "SSH key provisioning."
- mkdir -p /home/vagrant/.ssh/
- touch /home/vagrant/.ssh/authorized_keys
- echo #{ssh_pub_key} >> /home/vagrant/.ssh/authorized_keys
- echo #{ssh_pub_key} > /home/vagrant/.ssh/id_rsa.pub
- chmod 644 /home/vagrant/.ssh/id_rsa.pub
- echo "#{ssh_prv_key}" > /home/vagrant/.ssh/id_rsa
- chmod 600 /home/vagrant/.ssh/id_rsa
- chown -R vagrant:vagrant /home/vagrant
- exit 0
- SHELL
- end
- #
- # this fixes the centos boxes not enabling their eth1 IPs
- #
- puts "box #{info[:box]} #{hostname}"
- if info[:box] =~ /redhat|centos/i
- puts "RedHat check and network fix - #{info[:box]}"
- cfg.vm.provision "shell", inline: <<-SHELL
- if [ -e /etc/redhat-release ] ; then
- echo "Redhat release found on #{info[:box]}"
- if [ $(ip a s dev eth1 | grep -c "inet #{info[:ip]}") -eq 0 ] ; then
- echo "Restarting network"
- touch /tmp/network-setup
- sudo nmcli connection reload
- sudo systemctl restart network.service
- else
- echo "Network is already up"
- fi
- else
- echo "Not redhat"
- fi
- exit 0
- SHELL
- end
- #
- # per box provisioning - by hostname
- #
- provision_filename = hostname + "-provision.sh"
- cfg.vm.provision "shell", inline: "echo #{provision_filename}"
- if File.exists?(File.join(vagrant_dir,'provision',hostname + "-provision.sh")) then
- cfg.vm.provision "shell", inline: "echo +++exists+++"
- cfg.vm.provision "shell", :path => File.join( "provision", hostname + "-provision.sh" )
- else
- cfg.vm.provision "shell", inline: "echo PROVISION FILE DOES NOT EXIST!"
- end
- end # config.vm.define hostname
- end # servers.each
- ################################################################################
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement