Merge "Change metadata.yaml structure"
[multicloud/k8s.git] / vagrant / Vagrantfile
1 # -*- mode: ruby -*-
2 # vi: set ft=ruby :
3
4 box = {
5   :virtualbox => { :name => 'elastic/ubuntu-16.04-x86_64', :version => '20180708.0.0' },
6   :libvirt => { :name => 'elastic/ubuntu-16.04-x86_64', :version=> '20180210.0.0'}
7 }
8
9 require 'yaml'
10 pdf = File.dirname(__FILE__) + '/config/default.yml'
11 if File.exist?(File.dirname(__FILE__) + '/config/pdf.yml')
12   pdf = File.dirname(__FILE__) + '/config/pdf.yml'
13 end
14 nodes = YAML.load_file(pdf)
15
16 # Inventory file creation
17 File.open(File.dirname(__FILE__) + "/inventory/hosts.ini", "w") do |inventory_file|
18   inventory_file.puts("[all:vars]\nansible_connection=ssh\nansible_ssh_user=vagrant\nansible_ssh_pass=vagrant\n\n[all]")
19   nodes.each do |node|
20     inventory_file.puts("#{node['name']}\tansible_ssh_host=#{node['ip']} ansible_ssh_port=22")
21   end
22   ['kube-master', 'kube-node', 'etcd', 'ovn-central', 'ovn-controller', 'virtlet'].each do|group|
23     inventory_file.puts("\n[#{group}]")
24     nodes.each do |node|
25       if node['roles'].include?("#{group}")
26         inventory_file.puts(node['name'])
27       end
28     end
29   end
30   inventory_file.puts("\n[k8s-cluster:children]\nkube-node\nkube-master")
31 end
32
33 provider = (ENV['VAGRANT_DEFAULT_PROVIDER'] || :virtualbox).to_sym
34 puts "[INFO] Provider: #{provider} "
35
36 if ENV['no_proxy'] != nil or ENV['NO_PROXY']
37   $no_proxy = ENV['NO_PROXY'] || ENV['no_proxy'] || "127.0.0.1,localhost"
38   nodes.each do |node|
39     $no_proxy += "," + node['ip']
40   end
41   $subnet = "192.168.121"
42   if provider == :virtualbox
43     $subnet = "10.0.2"
44   end
45   # NOTE: This range is based on vagrant-libvirt network definition CIDR 192.168.121.0/27
46   (1..31).each do |i|
47     $no_proxy += ",#{$subnet}.#{i}"
48   end
49 end
50
51 Vagrant.configure("2") do |config|
52   config.vm.box =  box[provider][:name]
53   config.vm.box_version = box[provider][:version]
54
55   if ENV['http_proxy'] != nil and ENV['https_proxy'] != nil
56     if Vagrant.has_plugin?('vagrant-proxyconf')
57       config.proxy.http     = ENV['http_proxy'] || ENV['HTTP_PROXY'] || ""
58       config.proxy.https    = ENV['https_proxy'] || ENV['HTTPS_PROXY'] || ""
59       config.proxy.no_proxy = $no_proxy
60       config.proxy.enabled = { docker: false }
61     end
62   end
63
64   nodes.each do |node|
65     config.vm.define node['name'] do |nodeconfig|
66       nodeconfig.vm.hostname = node['name']
67       nodeconfig.vm.network :private_network, :ip => node['ip'], :type => :static
68       nodeconfig.vm.provider 'virtualbox' do |v|
69         v.customize ["modifyvm", :id, "--memory", node['memory']]
70         v.customize ["modifyvm", :id, "--cpus", node['cpus']]
71         if node.has_key? "volumes"
72           node['volumes'].each do |volume|
73             $volume_file = "#{node['name']}-#{volume['name']}.vdi"
74             unless File.exist?($volume_file)
75               v.customize ['createmedium', 'disk', '--filename', $volume_file, '--size', volume['size']]
76             end
77             v.customize ['storageattach', :id, '--storagectl', 'IDE Controller', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', $volume_file]
78           end
79         end
80       end
81       nodeconfig.vm.provider 'libvirt' do |v|
82         v.memory = node['memory']
83         v.cpus = node['cpus']
84         v.nested = true
85         v.cpu_mode = 'host-passthrough'
86         v.management_network_address = "192.168.121.0/27"
87         nodeconfig.vm.provision 'shell' do |sh|
88           sh.path =  "node.sh"
89           if node.has_key? "volumes"
90             $volume_mounts_dict = ''
91             node['volumes'].each do |volume|
92               $volume_mounts_dict += "#{volume['name']}=#{volume['mount']},"
93               $volume_file = "./#{node['name']}-#{volume['name']}.qcow2"
94               v.storage :file, :bus => 'sata', :device => volume['name'], :size => volume['size']
95             end
96             sh.args = ['-v', $volume_mounts_dict[0...-1]]
97           end
98         end
99       end
100     end
101   end
102   sync_type = "virtualbox"
103   if provider == :libvirt
104     sync_type = "nfs"
105   end
106   config.vm.define :installer, primary: true, autostart: false do |installer|
107     installer.vm.hostname = "multicloud"
108     installer.vm.network :private_network, :ip => "10.10.10.2", :type => :static
109     installer.vm.synced_folder '../', '/root/go/src/k8-plugin-multicloud/', type: sync_type
110     installer.vm.provision 'shell' do |sh|
111       sh.path =  "installer.sh"
112       sh.args = ['-p', '-v', '-w', '/root/go/src/k8-plugin-multicloud/vagrant']
113     end
114   end
115 end