Merge "Use httptest instead of http in unit tests"
[multicloud/k8s.git] / kud / hosting_providers / 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]")
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'] || :libvirt).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   config.ssh.insert_key = false
63
64   if ENV['http_proxy'] != nil and ENV['https_proxy'] != nil
65     if Vagrant.has_plugin?('vagrant-proxyconf')
66       config.proxy.http     = ENV['http_proxy'] || ENV['HTTP_PROXY'] || ""
67       config.proxy.https    = ENV['https_proxy'] || ENV['HTTPS_PROXY'] || ""
68       config.proxy.no_proxy = $no_proxy
69       config.proxy.enabled = { docker: false }
70     end
71   end
72
73   nodes.each do |node|
74     config.vm.define node['name'] do |nodeconfig|
75       nodeconfig.vm.hostname = node['name']
76       nodeconfig.vm.network :private_network, :ip => node['ip'], :type => :static
77       nodeconfig.vm.provider 'virtualbox' do |v|
78         v.customize ["modifyvm", :id, "--memory", node['memory']]
79         v.customize ["modifyvm", :id, "--cpus", node['cpus']]
80         if node.has_key? "volumes"
81           node['volumes'].each do |volume|
82             $volume_file = "#{node['name']}-#{volume['name']}.vdi"
83             unless File.exist?($volume_file)
84               v.customize ['createmedium', 'disk', '--filename', $volume_file, '--size', volume['size']]
85             end
86             v.customize ['storageattach', :id, '--storagectl', 'IDE Controller', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', $volume_file]
87           end
88         end
89       end
90       nodeconfig.vm.provider 'libvirt' do |v|
91         v.memory = node['memory']
92         v.cpus = node['cpus']
93         v.nested = true
94         v.cpu_mode = 'host-passthrough'
95         v.management_network_address = "192.168.121.0/27"
96         nodeconfig.vm.provision 'shell' do |sh|
97           sh.path =  "node.sh"
98           if node.has_key? "volumes"
99             $volume_mounts_dict = ''
100             node['volumes'].each do |volume|
101               $volume_mounts_dict += "#{volume['name']}=#{volume['mount']},"
102               $volume_file = "./#{node['name']}-#{volume['name']}.qcow2"
103               v.storage :file, :bus => 'sata', :device => volume['name'], :size => volume['size']
104             end
105             sh.args = ['-v', $volume_mounts_dict[0...-1]]
106           end
107         end
108       end
109     end
110   end
111   sync_type = "virtualbox"
112   if provider == :libvirt
113     sync_type = "nfs"
114   end
115   config.vm.define :installer, primary: true, autostart: false do |installer|
116     installer.vm.hostname = "multicloud"
117     installer.vm.network :private_network, :ip => "10.10.10.2", :type => :static
118     installer.vm.synced_folder '../../../', '/home/vagrant/multicloud-k8s/', type: sync_type
119     installer.vm.provision 'shell', privileged: false do |sh|
120       sh.env = {'KUD_PLUGIN_ENABLED': 'true'}
121       sh.inline = <<-SHELL
122         cp /vagrant/insecure_keys/key.pub /home/vagrant/.ssh/id_rsa.pub
123         cp /vagrant/insecure_keys/key /home/vagrant/.ssh/id_rsa
124         chown vagrant /home/vagrant/.ssh/id_rsa
125         chmod 400 /home/vagrant/.ssh/id_rsa
126         cd /home/vagrant/multicloud-k8s/kud/hosting_providers/vagrant/ && ./installer.sh | tee kud_installer.log
127       SHELL
128     end
129   end
130 end