-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vagrantfile
115 lines (99 loc) · 3.52 KB
/
Vagrantfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# Loading Settings from settings.yml file
require 'yaml'
settings = YAML.load_file("settings.yml")
ansibleProvisioning = settings['ansible_provisioning']
ansibleExtraVars = settings['ansible_settings'] || {}
#if ansibleProvisioning
# system("
# if [ '#{ARGV[0]}' = 'up' ] || [ '#{ARGV[0]}' = 'provision' ]; then
# ansible-galaxy collection install community.kubernetes -p provisioning/ansible_collections/
# fi
# ")
#end
dictNodeInfo = settings['node_info']
defaultVagrantBox = settings['vagrant_box']
defaultGuestOSType = settings['vagrant_guest_os'] || "linux"
ansibleFirewallGroup = []
Vagrant.configure(2) do |config|
#Define the number of nodes to spin up
dictNodeInfo.each_with_index do |(hostname), index|
nodeInfo = dictNodeInfo[hostname]
config.vm.define hostname do |node|
node.vm.box = nodeInfo['vagrant_box'] || defaultVagrantBox
node.vm.provider "virtualbox" do |vb|
vb.memory = nodeInfo['memory'] || 1096
vb.cpus = nodeInfo['cpus'] || 1
end
node.ssh.forward_agent = true
node.vm.guest = nodeInfo['vagrant_guest_os'] || defaultGuestOSType
node.vm.hostname = hostname
# Loop through networks and add NICs
nodeInfo['networks'].each do |net|
if net['mode'] == "public"
node.vm.network :public_network,
ip: net['ip'],
mac: net['mac'] || nil,
netmask: net['netmask'] || "255.255.255.0"
elsif net['mode'] == "bridge"
node.vm.network :public_network,
ip: net['ip'] || nil,
mac: net['mac'] || nil,
netmask: net['netmask'] || nil,
bridge: net['net_bridge_order'] || nil
else
node.vm.network :private_network,
name: net['name'],
ip: net['ip'],
mac: net['mac'] || nil,
netmask: net['netmask'] || "255.255.255.0"
end
end
$script = ''
# VM Provision Shell Script for client only (override default router to eth1)
# - basically removes default GW to default NAT network set up by VirtualBox
if nodeInfo['role'] == 'client'
# use only the default router defined on the first private net
defaultRouter = nodeInfo['networks'][0]['default_router']
nameServers = nodeInfo['networks'][0]['nameservers']
$script = <<-SCRIPT
ip route delete default
ip route add default via #{defaultRouter}
cat <<EOF>/etc/netplan/99-route-overrides.yaml
network:
version: 2
ethernets:
eth0:
dhcp4-overrides:
use-routes: false
use-dns: false
nameservers:
addresses: []
eth1:
gateway4: #{defaultRouter}
nameservers:
addresses: #{nameServers}
EOF
SCRIPT
node.vm.provision :shell, :inline => $script
end
# Ansible stuff
if nodeInfo['role'] == "firewall"
ansibleFirewallGroup.append(hostname)
end
if index == dictNodeInfo.keys.length() - 1 and ansibleProvisioning
node.vm.provision "ansible" do |ansible|
ansible.limit = "all"
ansible.verbose = "vv"
ansible.playbook = "provisioning/playbook.yml"
ansible.galaxy_role_file = "provisioning/requirements.yaml"
ansible.galaxy_command = 'ansible-galaxy install --role-file=%{role_file} --roles-path=%{roles_path}'
ansible.config_file = "provisioning/ansible.cfg"
ansible.groups = {
"firewall" => ansibleFirewallGroup,
}
ansible.extra_vars = ansibleExtraVars
end
end
end
end
end