Vagrantfile 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. # -*- mode: ruby -*-
  2. # vi: set ft=ruby :
  3. VAGRANTFILE_API_VERSION = "2"
  4. NETWORK = "192.168.56."
  5. NETMASK = "255.255.255.0"
  6. # Put the MAIN ip <-> NCDOM domain below to host /etc/hosts
  7. MAIN = NETWORK+"10"
  8. NCDOM = "nextcloud.test"
  9. # VM machines configuration
  10. HOSTS = [
  11. #VM_NAME IP_ADDRESS RAM(mb) CPU BOX GROUP
  12. { :hostname => "db1", :ip => NETWORK+"11", :ram => 1024, :cpu => 1, :box => "centos/7", :group => "db_servers", }, #:guestport => 3306, :hostport => 13306 }, #:user => "" :pass => "" },
  13. #{ :hostname => "db2", :ip => NETWORK+"12", :ram => 1024, :cpu => 1, :box => "centos/7", :group => "db_servers", }, #:guestport => 3306, :hostport => 23306 }, #:user => "" :pass => "" },
  14. #{ :hostname => "db3", :ip => NETWORK+"13", :ram => 1024, :cpu => 1, :box => "centos/7", :group => "db_servers", }, #:guestport => 3306, :hostport => 33306 }, #:user => "" :pass => "" },
  15. #{ :hostname => "lbsql1", :ip => NETWORK+"19", :ram => 1024, :cpu => 1, :box => "centos/7", :group => "db_lbal_servers", :state => "MASTER", :priority => 101, :vip => NETWORK+"20" },
  16. #{ :hostname => "lbsql2", :ip => NETWORK+"18", :ram => 1024, :cpu => 1, :box => "centos/7", :group => "db_lbal_servers", :state => "BACKUP", :priority => 100, :vip => NETWORK+"20" },
  17. #{ :hostname => "redis", :ip => NETWORK+"21", :ram => 512, :cpu => 1, :box => "centos/7", :group => "redis_servers" },
  18. #{ :hostname => "redis2", :ip => NETWORK+"22", :ram => 512, :cpu => 1, :box => "centos/7", :group => "redis_servers" },
  19. #{ :hostname => "gl", :ip => NETWORK+"31", :ram => 1024, :cpu => 1, :box => "centos/7", :group => "gluster_servers" },
  20. #{ :hostname => "gl2", :ip => NETWORK+"32", :ram => 1024, :cpu => 1, :box => "centos/7", :group => "gluster_servers" },
  21. { :hostname => "web.test", :ip => NETWORK+"41", :ram => 1024, :cpu => 1, :box => "centos/7", :group => "web_servers", :ipdb => NETWORK+"20", :redis_vip => NETWORK+"40", :priority => 101 },
  22. #{ :hostname => "web2.test", :ip => NETWORK+"42", :ram => 1024, :cpu => 1, :box => "centos/7", :group => "web_servers", :ipdb => NETWORK+"20", :redis_vip => NETWORK+"40", :priority => 100 },
  23. { :hostname => "lb.test", :ip => NETWORK+"51", :ram => 1024, :cpu => 1, :box => "ubuntu/focal64", :group => "lbal_servers", :state => "MASTER", :priority => 101 },
  24. #{ :hostname => "lb2.test", :ip => NETWORK+"52", :ram => 512, :cpu => 1, :box => "ubuntu/focal64", :group => "lbal_servers", :state => "BACKUP", :priority => 100 },
  25. #{ :hostname => "prome", :ip => NETWORK+"61", :ram => 512, :cpu => 1, :box => "centos/7", :group => "monitor_servers" },
  26. #{ :hostname => "node", :ip => NETWORK+"62", :ram => 512, :cpu => 1, :box => "centos/7", :group => "monitor_servers" },
  27. #{ :hostname => "grafa", :ip => NETWORK+"63", :ram => 512, :cpu => 1, :box => "centos/7", :group => "monitor_servers" },
  28. #{ :hostname => "essai1.test", :ip => NETWORK+"98", :ram => 1024, :cpu => 1, :box => "centos/7", :group => "test_servers", :priority => 101, :vip => NETWORK+"100" },
  29. #{ :hostname => "essai2.test", :ip => NETWORK+"99", :ram => 1024, :cpu => 1, :box => "centos/7", :group => "test_servers", :priority => 100, :vip => NETWORK+"100" },
  30. #{ :hostname => "web.test", :ip => NETWORK+"41", :ram => 1024, :cpu => 1, :box => "centos/7", :group => "web_servers" },
  31. #{ :hostname => "web2.test", :ip => NETWORK+"42", :ram => 1024, :cpu => 1, :box => "centos/7", :group => "web_servers" },
  32. ]
  33. # Defined ansible playbook
  34. vagrant_root = File.expand_path(File.dirname(__FILE__))
  35. # If empty, will skip the ansible provisioner block
  36. ansible_playbook = "provisioning/ansible/playbook.yml"
  37. # Ansible inventory. The path supports nested directories or a single file
  38. ansible_inventory_path = "provisioning/ansible/hosts"
  39. Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  40. if Vagrant.has_plugin?("vagrant-hostmanager")
  41. config.vm.box_check_update = false
  42. # To enable the hostmanager plugin
  43. config.hostmanager.enabled = true
  44. # To enable add records to host /etc/hosts
  45. config.hostmanager.manage_host = false
  46. # To enable add records to guest /etc/hosts
  47. config.hostmanager.manage_guest = true
  48. # Not use private ip addresses for the hosts file, set to false
  49. config.hostmanager.ignore_private_ip = false
  50. config.hostmanager.include_offline = false
  51. end
  52. # Create groups to be used in ansible inventory
  53. groups = {"all" => []}
  54. HOSTS.each do |cfg|
  55. if ! groups.has_key?(cfg[:group])
  56. groups[cfg[:group]] = [cfg[:hostname]]
  57. else
  58. groups[cfg[:group]].push(cfg[:hostname])
  59. end
  60. groups["all"].push(cfg[:hostname])
  61. end
  62. # Create inventory for ansible provision
  63. # The inventory will hold servers details and groups per each server.
  64. if File.dirname(ansible_inventory_path) != "."
  65. Dir.mkdir(File.dirname(ansible_inventory_path)) unless Dir.exist?(File.dirname(ansible_inventory_path))
  66. end
  67. File.delete(ansible_inventory_path) if File.exist?(ansible_inventory_path)
  68. File.open(ansible_inventory_path, 'w') do |f|
  69. HOSTS.each do |cfg|
  70. ssh_key = vagrant_root+"/.vagrant/machines/"+cfg[:hostname]+"/virtualbox/private_key"
  71. f.write "#{cfg[:hostname]} ansible_host=#{cfg[:ip]} ansible_ssh_private_key_file=#{ssh_key}\n"
  72. end
  73. groups.keys.each_with_index do |g, index|
  74. f.write "\n"
  75. f.write "[#{g}]\n"
  76. groups[g].each do |h|
  77. f.write "#{h}"
  78. network = NETWORK+"0/24"
  79. f.write " network_allowed="+network
  80. if g == "lbal_servers"
  81. HOSTS.each do |cfg|
  82. if cfg[:hostname] == h
  83. priority = (cfg[:priority].to_s || "UNKNOWN" )
  84. state = (cfg[:state].to_s || "UNKNOWN" )
  85. f.write " ssl_name="+NCDOM+" keepalived_vip="+MAIN+" keepalived_priority="+priority+" keepalived_state="+state
  86. end
  87. end
  88. end
  89. if g == "web_servers"
  90. HOSTS.each do |cfg|
  91. if cfg[:hostname] == h
  92. ipdb = (cfg[:ipdb].to_s || "" )
  93. priority = (cfg[:priority].to_s || "UNKNOWN" )
  94. redis = (cfg[:redis_vip].to_s || "UNKNOWN" )
  95. if groups.count("web_servers") > 2
  96. f.write " nc_global_name="+NCDOM+" db_host="+ipdb+" redis_host="+redis+" keepalived_vip="+redis+" keepalived_priority="+priority
  97. else
  98. f.write " nc_global_name="+NCDOM+" db_host="+ipdb
  99. end
  100. end
  101. end
  102. end
  103. if g == "db_lbal_servers"
  104. HOSTS.each do |cfg|
  105. if cfg[:hostname] == h
  106. priority = (cfg[:priority].to_s || "UNKNOWN" )
  107. state = (cfg[:state].to_s || "UNKNOWN" )
  108. vip = (cfg[:vip].to_s || "UNKNOWN" )
  109. f.write " keepalived_vip="+vip+" keepalived_priority="+priority+" keepalived_state="+state
  110. end
  111. end
  112. end
  113. if g == "test_servers"
  114. HOSTS.each do |cfg|
  115. if cfg[:hostname] == h
  116. priority = (cfg[:priority].to_s || "UNKNOWN" )
  117. vip = (cfg[:vip].to_s || "UNKNOWN" )
  118. f.write " keepalived_vip="+vip+" keepalived_priority="+priority
  119. end
  120. end
  121. end
  122. f.write "\n"
  123. end
  124. end
  125. end
  126. # VM DEFINITIONS
  127. HOSTS.each_with_index do |server, index|
  128. config.vm.define server[:hostname] do |conf|
  129. conf.vm.box = server[:box]
  130. conf.vm.hostname = server[:hostname]
  131. conf.vm.boot_timeout = 360
  132. # Set system options
  133. cpu = server[:cpu] ? server[:cpu] : 1;
  134. memory = server[:ram] ? server[:ram] : 512;
  135. name = server[:hostname] ? server[:hostname] : "linux";
  136. conf.vm.provider "virtualbox" do |vbox|
  137. vbox.cpus = cpu.to_s
  138. vbox.memory = memory.to_s
  139. vbox.name = name
  140. end
  141. # Set network options
  142. netmask = server[:netmask] || NETMASK
  143. conf.vm.network :private_network, ip: server[:ip], netmask: netmask
  144. # Set port forwarding if defined
  145. if !server[:port_guest].nil? && !server[:port_host].nil?
  146. conf.vm.network "forwarded_port", guest: server[:port_guest], host: server[:port_host]
  147. end
  148. # Set synced folders if defined
  149. if !server[:folder_guest].nil? && !server[:folder_host].nil?
  150. conf.vm.synced_folder server[:folder_host], server[:folder_guest]
  151. end
  152. # Set common provision
  153. conf.vm.provision "shell" do |s|
  154. s.path = "provisioning/bash/common.sh"
  155. s.args = [server[:box]]
  156. end
  157. # Provision nodes with Ansible.
  158. # The index used here in order to execute the provision just after all
  159. # the servers are up and running.
  160. if index == HOSTS.size - 1
  161. if ansible_playbook != ""
  162. conf.vm.provision :ansible do |ansible|
  163. ansible.limit = "all"
  164. ansible.compatibility_mode = "2.0"
  165. ansible.inventory_path = ansible_inventory_path
  166. ansible.playbook = ansible_playbook
  167. ansible.become = true
  168. #ansible.verbose = "vvvv"
  169. end
  170. end
  171. end
  172. end
  173. end
  174. # VM PROVISIONING
  175. # #Prometheus
  176. # config.vm.define "prometheus" do |prometheus|
  177. # prometheus.vm.box = 'centos/7'
  178. # prometheus.vm.hostname = "prometheus"
  179. # prometheus.vm.network :private_network, ip: "192.168.56.41"
  180. # prometheus.vm.provision "shell", path: "provisioning/install/Centos_7.sh"
  181. #
  182. # #Provision prometheus-grafana with Ansible
  183. # prometheus.vm.provision "ansible" do |ansible|
  184. # ansible.compatibility_mode = "2.0"
  185. # ansible.playbook="provisioning/ansible/prometheus.yml"
  186. # ansible.become = true
  187. # ansible.extra_vars = {
  188. # ansible_python_interpreter: "/usr/bin/python2",
  189. # }
  190. # end
  191. # end
  192. #
  193. # #Node Exporter
  194. # config.vm.define "node" do |node|
  195. # node.vm.box = 'centos/7'
  196. # node.vm.hostname = "nodexporter"
  197. # node.vm.network :private_network, ip: "192.168.56.42"
  198. # node.vm.provision "shell", path: "provisioning/install/Centos_7.sh"
  199. #
  200. # #Provision prometheus-grafana with Ansible
  201. # node.vm.provision "ansible" do |ansible|
  202. # ansible.compatibility_mode = "2.0"
  203. # ansible.playbook="provisioning/ansible/node_exporter.yml"
  204. # ansible.become = true
  205. # ansible.extra_vars = {
  206. # ansible_python_interpreter: "/usr/bin/python2",
  207. # }
  208. # end
  209. # end
  210. #
  211. # #Grafana
  212. # config.vm.define "grafana" do |grafana|
  213. # grafana.vm.box = 'centos/7'
  214. # grafana.vm.hostname = "grafana"
  215. # grafana.vm.network :private_network, ip: "192.168.56.43"
  216. # grafana.vm.provision "shell", path: "provisioning/install/Centos_7.sh"
  217. #
  218. # #Provision prometheus-grafana with Ansible
  219. # grafana.vm.provision "ansible" do |ansible|
  220. # ansible.compatibility_mode = "2.0"
  221. # ansible.playbook="provisioning/ansible/grafana.yml"
  222. # ansible.become = true
  223. # ansible.extra_vars = {
  224. # ansible_python_interpreter: "/usr/bin/python2",
  225. # }
  226. # end
  227. # end
  228. #end
  229. end