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