Advertisement
Guest User

Untitled

a guest
Feb 6th, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. # -*- mode: ruby -*-
  2. # vi: set ft=ruby :
  3. # vim: noai:ts=2:sw=2:et
  4.  
  5. # Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
  6. VAGRANTFILE_API_VERSION = "2"
  7.  
  8. UBUNTU_BOX='ubuntu/trusty64'
  9. CENTOS_BOX='centos/7'
  10.  
  11. DOCKER_MEM = 1024
  12. DOCKER_CPUS = 2
  13.  
  14. # to find our provisioning scripts
  15. dir = Dir.pwd
  16. vagrant_dir = File.expand_path(File.dirname(__FILE__))
  17.  
  18. servers = {
  19. "ansible01" => { :ip => "172.30.5.60", :bridge => "eth1",
  20. :mem => DOCKER_MEM, :cpus => DOCKER_CPUS, :box => CENTOS_BOX,
  21. :scripts => "provision/ansible-install.sh" },
  22. "docker-engine01" => { :ip => "172.30.5.61", :bridge => "eth1",
  23. :mem => DOCKER_MEM, :cpus => DOCKER_CPUS, :box => UBUNTU_BOX,
  24. :scripts => "provision/docker-install.sh" },
  25. "docker-engine02" => { :ip => "172.30.5.62", :bridge => "eth1",
  26. :mem => DOCKER_MEM, :cpus => DOCKER_CPUS, :box => UBUNTU_BOX,
  27. :scripts => "provision/docker-install.sh" },
  28. "docker-engine03" => { :ip => "172.30.5.63", :bridge => "eth1",
  29. :mem => DOCKER_MEM, :cpus => DOCKER_CPUS, :box => UBUNTU_BOX,
  30. :scripts => "provision/docker-install.sh" },
  31. "docker-engine04" => { :ip => "172.30.5.64", :bridge => "eth1",
  32. :mem => DOCKER_MEM, :cpus => DOCKER_CPUS, :box => UBUNTU_BOX,
  33. :scripts => "provision/docker-install.sh" }
  34. }
  35.  
  36. Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  37.  
  38. config.vm.box_check_update = false
  39. # enable logging in via ssh with a password
  40. #config.ssh.username = "vagrant"
  41. #config.ssh.password = "vagrant"
  42.  
  43. ################################################################################
  44. servers.each do |hostname, info|
  45.  
  46. #
  47. # build a vm - from the server dict
  48. #
  49. config.vm.define hostname do |cfg| # a define per hostname
  50. cfg.vm.box = info[:box]
  51. cfg.vm.hostname = hostname
  52.  
  53. # note the public network
  54. cfg.vm.network "public_network", ip: info[:ip], bridge: info[:bridge]
  55.  
  56. cfg.vm.provider "virtualbox" do |v|
  57. v.name = hostname
  58. v.memory = info[:mem]
  59. v.cpus = info[:cpus]
  60. v.customize ["modifyvm", :id, "--hwvirtex", "on"]
  61. end
  62.  
  63. #
  64. # do some provisioning
  65. #
  66. ssh_prv_key = ""
  67. ssh_pub_key = ""
  68. if not File.file?("#{Dir.home}/.ssh/vagrant")
  69. puts "No SSH key found. You will need to remedy this before pushing to the repository."
  70. else
  71. ssh_prv_key = File.read("#{Dir.home}/.ssh/vagrant")
  72. ssh_pub_key = File.readlines("#{Dir.home}/.ssh/vagrant.pub").first.strip
  73. cfg.vm.provision "shell", inline: <<-SHELL
  74. if grep -sq "#{ssh_pub_key}" /home/vagrant/.ssh/authorized_keys; then
  75. echo "SSH keys already provisioned."
  76. exit 0;
  77. fi
  78. echo "SSH key provisioning."
  79. mkdir -p /home/vagrant/.ssh/
  80. touch /home/vagrant/.ssh/authorized_keys
  81. echo #{ssh_pub_key} >> /home/vagrant/.ssh/authorized_keys
  82. echo #{ssh_pub_key} > /home/vagrant/.ssh/id_rsa.pub
  83. chmod 644 /home/vagrant/.ssh/id_rsa.pub
  84. echo "#{ssh_prv_key}" > /home/vagrant/.ssh/id_rsa
  85. chmod 600 /home/vagrant/.ssh/id_rsa
  86. chown -R vagrant:vagrant /home/vagrant
  87. exit 0
  88. SHELL
  89. end
  90.  
  91.  
  92. #
  93. # this fixes the centos boxes not enabling their eth1 IPs
  94. #
  95. puts "box #{info[:box]} #{hostname}"
  96. if info[:box] =~ /redhat|centos/i
  97. puts "RedHat check and network fix - #{info[:box]}"
  98. cfg.vm.provision "shell", inline: <<-SHELL
  99. if [ -e /etc/redhat-release ] ; then
  100. echo "Redhat release found on #{info[:box]}"
  101. if [ $(ip a s dev eth1 | grep -c "inet #{info[:ip]}") -eq 0 ] ; then
  102. echo "Restarting network"
  103. touch /tmp/network-setup
  104. sudo nmcli connection reload
  105. sudo systemctl restart network.service
  106. else
  107. echo "Network is already up"
  108. fi
  109. else
  110. echo "Not redhat"
  111. fi
  112. exit 0
  113. SHELL
  114. end
  115.  
  116. #
  117. # per box provisioning - by hostname
  118. #
  119. provision_filename = hostname + "-provision.sh"
  120. cfg.vm.provision "shell", inline: "echo #{provision_filename}"
  121. if File.exists?(File.join(vagrant_dir,'provision',hostname + "-provision.sh")) then
  122. cfg.vm.provision "shell", inline: "echo +++exists+++"
  123. cfg.vm.provision "shell", :path => File.join( "provision", hostname + "-provision.sh" )
  124. else
  125. cfg.vm.provision "shell", inline: "echo PROVISION FILE DOES NOT EXIST!"
  126. end
  127.  
  128. end # config.vm.define hostname
  129. end # servers.each
  130. ################################################################################
  131. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement