-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVagrantfile
320 lines (284 loc) · 12.7 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'json'
require 'nokogiri'
require 'tempfile'
VAGRANTFILE_API_VERSION = 2
SHELL_VARIABLE_REGEX = /\$([a-zA-Z_]\w*)$|\$\{{1}(\w+)\}{1}/
VAGRANT_NETWORK_CONFIGS_PATH = "./.vagrant/network_configs.yml"
VAGRANT_LIBVIRT_HOMELAB_DOMAIN = "homelab.staging.cavcrosby.net"
VAGRANT_CONFIG_JSON = JSON.parse(
File.read("#{ENV['PROJECT_VAGRANT_CONFIGURATION_FILE']}")
)
ansible_host_vars = VAGRANT_CONFIG_JSON["ansible_host_vars"]
ansible_groups = VAGRANT_CONFIG_JSON["ansible_groups"]
if VAGRANT_CONFIG_JSON.key?("vms_include")
VMS_INCLUDE = VAGRANT_CONFIG_JSON["vms_include"]
end
vagrant_homelab_network_configs = {
"homelab_network_domain" => VAGRANT_LIBVIRT_HOMELAB_DOMAIN,
"homelab_poseidon_k8s_network_domain" => "poseidon.#{VAGRANT_LIBVIRT_HOMELAB_DOMAIN}",
"homelab_poseidon_vrrp_server_vip" => "192.168.2.2",
"homelab_network_subnet" => "192.168.1.0/24",
"homelab_network_gateway_ipv4_addr" => "192.168.1.1",
"homelab_network_subnet_mask" => "255.255.255.0",
"homelab_network_lower_bound" => "192.168.1.50",
"homelab_network_upper_bound" => "192.168.1.254"
}
# exported constants
VAGRANT_LIBVIRT_HOMELAB_NETWORK_IPV4_ADDR = vagrant_homelab_network_configs["homelab_network_gateway_ipv4_addr"]
VAGRANT_LIBVIRT_POSEIDON_K8S_NETWORK_IPV4_ADDR = "192.168.2.1"
def eval_config_ref(machine_attrs, config)
# A config=>config_ref is a JSON key=>value pair whose value is a key in
# machine_attrs.
has_children = true
if config.kind_of?(Array)
nil
else
# lets at first assume that all JSON key values do not have children
has_children = false
config.each do |config_name, config_ref|
if !(config_ref.kind_of?(Array) || config_ref.kind_of?(Hash))
# eval each JSON's key value
if machine_attrs.key?(config_ref)
config[config_name] = machine_attrs[config_ref]
elsif SHELL_VARIABLE_REGEX.match?(config_ref)
match = SHELL_VARIABLE_REGEX.match(config_ref)
config[config_name] = eval(match[1].nil? ? match[2] : match[1])
end
elsif config_ref.kind_of?(Hash) && config_ref.key?("join")
# If an a JSON's key value is another JSON with a sole 'join' element, then the
# "joins" value will be combined into one string. For example:
# {
# "filename": {
# "join": [
# "eth0",
# ".link"
# ]
# }
# }
#
# Will become:
# {
# "filename": "eth0.link"
# }
config_ref["join"].each_index do |str_index|
if machine_attrs.key?(config_ref["join"][str_index])
config_ref["join"][str_index] = machine_attrs[config_ref["join"][str_index]]
end
end
config[config_name] = config_ref["join"].join()
else
has_children = true
end
end
end
return has_children
end
def traverse_configs(func, machine_attrs, config_node)
if config_node.class == String || !(func.call(machine_attrs, config_node))
return
end
if config_node.kind_of?(Array)
config_node.each do |config_node_child|
traverse_configs(func, machine_attrs, config_node_child)
end
else
config_node.values().each do |config_node_child|
traverse_configs(func, machine_attrs, config_node_child)
end
end
end
ansible_host_vars.each do |machine_name, machine_attrs|
# hostnames may use dashes but Ansible variables cannot
machine_name_dash_replaced = machine_name.gsub("-", "_")
vagrant_homelab_network_configs["#{machine_name_dash_replaced}_homelab_mac_addr"] = machine_attrs["vagrant_vm_homelab_mac_addr"]
vagrant_homelab_network_configs["#{machine_name_dash_replaced}_homelab_ipv4_addr"] = machine_attrs["vagrant_vm_homelab_ipv4_addr"]
if machine_attrs.key?("vagrant_vm_poseidon_k8s_mac_addr")
vagrant_homelab_network_configs["#{machine_name_dash_replaced}_poseidon_k8s_mac_addr"] = machine_attrs["vagrant_vm_poseidon_k8s_mac_addr"]
vagrant_homelab_network_configs["#{machine_name_dash_replaced}_poseidon_k8s_ipv4_addr"] = machine_attrs["vagrant_vm_poseidon_k8s_ipv4_addr"]
end
if (defined? VMS_INCLUDE) && !VMS_INCLUDE.include?(machine_name)
ansible_host_vars.delete(machine_name)
next
end
# Takes each key value pair in vagrant_config_refs and inserts it into
# machine_attrs but with the value eval'd based on machine_attrs[value].
if machine_attrs.key?("vagrant_config_refs")
vagrant_config_refs = machine_attrs["vagrant_config_refs"]
# passing a function as a argument was inspired by:
# https://stackoverflow.com/questions/522720/passing-a-method-as-a-parameter-in-ruby#answer-4094968
traverse_configs(method(:eval_config_ref), machine_attrs, vagrant_config_refs)
vagrant_config_refs.keys().each do |config_name|
config_ref = vagrant_config_refs[config_name]
if config_ref.kind_of?(Array) || config_ref.kind_of?(Hash)
machine_attrs[config_name] = "'#{config_ref.to_json}'"
else
machine_attrs[config_name] = config_ref
end
end
# Since all the key values pairs are resolved and put at the machine_attrs level,
# for now the config_refs json will be discarded.
machine_attrs.delete("vagrant_config_refs")
end
# Takes each key value pair in vagrant_external_config_refs and inserts it into
# machine_attrs but with the value eval'd based on
# ansible_host_vars[machine_name][value].
if machine_attrs.key?("vagrant_external_config_refs")
machine_attrs["vagrant_external_config_refs"].each do |machine_name, vagrant_external_config_refs|
if (!defined? VMS_INCLUDE) || VMS_INCLUDE.include?(machine_name)
traverse_configs(method(:eval_config_ref), ansible_host_vars[machine_name], vagrant_external_config_refs)
vagrant_external_config_refs.keys().each do |config_name|
machine_attrs[config_name] = vagrant_external_config_refs[config_name]
end
end
end
machine_attrs.delete("vagrant_external_config_refs")
end
# Evals each key value pair's value in domain_config based on
# ansible_host_vars[domain_config[name]].
if machine_attrs.key?("libvirt_poseidon_k8s_controller_domain_configs")
domain_configs = machine_attrs["libvirt_poseidon_k8s_controller_domain_configs"]
domain_configs.each do |domain_config|
traverse_configs(method(:eval_config_ref), ansible_host_vars[domain_config["name"]], domain_config)
end
machine_attrs["libvirt_poseidon_k8s_controller_domain_configs"] = "'#{domain_configs.to_json}'"
end
if machine_attrs.key?("libvirt_poseidon_k8s_worker_domain_configs")
domain_configs = machine_attrs["libvirt_poseidon_k8s_worker_domain_configs"]
domain_configs.each do |domain_config|
traverse_configs(method(:eval_config_ref), ansible_host_vars[domain_config["name"]], domain_config)
end
machine_attrs["libvirt_poseidon_k8s_worker_domain_configs"] = "'#{domain_configs.to_json}'"
end
end
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
VAGRANT_LIBVIRT_MANAGEMENT_NETWORK_NAME = "mgmt-homelab-cm"
TRUTHY_VALUES = [
"1",
"true"
]
# general VM configuration
config.vm.synced_folder ".", "/vagrant", disabled: true
# general VM provider configuration
config.vm.provider "libvirt" do |domains|
domains.default_prefix = "#{ENV['LIBVIRT_PREFIX']}"
domains.management_network_name = VAGRANT_LIBVIRT_MANAGEMENT_NETWORK_NAME
domains.management_network_address = "192.168.3.0/24"
end
@libvirt_management_network_xml = Nokogiri::XML.parse(<<-_EOF_)
<network ipv6='yes'>
<name>#{VAGRANT_LIBVIRT_MANAGEMENT_NETWORK_NAME}</name>
<uuid>206e8c36-866a-4723-a262-011a8152febb</uuid>
<forward mode='nat'>
<nat>
<port start='1024' end='65535'/>
</nat>
</forward>
<mac address='52:54:00:4c:7a:ea'/>
<ip address='192.168.3.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.3.3' end='192.168.3.254'/>
</dhcp>
</ip>
</network>
_EOF_
counter = 0
management_network_defined = system("virsh net-info --network #{VAGRANT_LIBVIRT_MANAGEMENT_NETWORK_NAME} > /dev/null 2>&1")
ansible_host_vars.each do |machine_name, machine_attrs|
# specific VM configuration
config.vm.define "#{machine_name}" do |machine|
machine.vm.hostname = "#{machine_name}"
machine.vm.box = machine_attrs["vagrant_vm_box"]
machine.vm.network "private_network",
mac: machine_attrs["vagrant_vm_homelab_mac_addr"],
libvirt__network_name: "homelab-cm",
libvirt__host_ip: vagrant_homelab_network_configs["homelab_network_gateway_ipv4_addr"],
libvirt__dhcp_enabled: false
if machine_attrs.key?("vagrant_vm_poseidon_k8s_mac_addr")
machine.vm.network "private_network",
mac: machine_attrs["vagrant_vm_poseidon_k8s_mac_addr"],
ip: machine_attrs["vagrant_vm_poseidon_k8s_ipv4_addr"],
libvirt__network_name: "poseidon-k8s-homelab-cm",
libvirt__host_ip: VAGRANT_LIBVIRT_POSEIDON_K8S_NETWORK_IPV4_ADDR,
libvirt__dhcp_enabled: false
end
# A domain is an instance of an operating system running on a VM. At least
# according to libvirt. For reference: https://libvirt.org/goals.html
#
# VM provider configuration
machine.vm.provider "libvirt" do |domain|
domain.cpus = machine_attrs["vagrant_vm_cpus"]
domain.memory = machine_attrs["vagrant_vm_memory"]
domain.machine_virtual_size = machine_attrs["vagrant_vm_libvirt_disk_size"] # GBs
domain.management_network_mac = machine_attrs["vagrant_vm_mgmt_mac_addr"]
if !management_network_defined and @libvirt_management_network_xml.xpath("//host[@name='#{machine_name}']").empty?
host_entry = Nokogiri::XML::Node.new("host", @libvirt_management_network_xml)
host_entry["mac"] = machine_attrs["vagrant_vm_mgmt_mac_addr"]
host_entry["name"] = "#{machine_name}"
host_entry["ip"] = machine_attrs["vagrant_vm_mgmt_ipv4_addr"]
dhcp = @libvirt_management_network_xml.at_css("dhcp")
dhcp << host_entry
end
end
counter += 1
# As of Vagrant 2.2.9, the documentation recommends a specific implementation for
# taking advantage of Ansible's parallelism. Modified to my liking, for reference:
# https://www.vagrantup.com/docs/provisioning/ansible#ansible-parallel-execution
if counter == ansible_host_vars.length
if !management_network_defined
# associates the vagrant management network xml with libvirt
xml = Tempfile.new("mgmt-homelab-libvirt.xml")
xml.write(@libvirt_management_network_xml.to_xml)
xml.close()
system("virsh net-define #{xml.path}")
xml.unlink()
end
ansible_extra_vars = {}
if !ENV["ANSIBLE_EXTRA_VARS"].empty?
ENV["ANSIBLE_EXTRA_VARS"].split(/ |=/).each_slice(2) do |var_name, var_value|
ansible_extra_vars[var_name] = var_value
end
end
# write out the vagrant network configuration to be consumed by the playbooks
File.write(VAGRANT_NETWORK_CONFIGS_PATH, vagrant_homelab_network_configs.to_yaml)
ansible_groups["on_prem:vars"] = {
"preferred_nameserver" => ansible_host_vars["staging-node1"]["vagrant_vm_homelab_ipv4_addr"]
}
if TRUTHY_VALUES.include? ENV["USE_MAINTENANCE_PLAYBOOK"]
machine.vm.provision "ansible" do |ansible|
ansible.playbook = "./playbooks/maintenance.yml"
ansible.compatibility_mode = "2.0"
ansible.limit = ENV.fetch("ANSIBLE_LIMIT", "all")
ansible.ask_become_pass = true
ansible.tags = ENV["ANSIBLE_TAGS"]
ansible.host_vars = ansible_host_vars
ansible.groups = ansible_groups
end
else
machine.vm.provision "ansible" do |ansible|
ansible.playbook = "./playbooks/vagrant_customizations.yml"
ansible.compatibility_mode = "2.0"
ansible.limit = ENV.fetch("ANSIBLE_LIMIT", "all")
ansible.ask_become_pass = true
ansible.tags = ENV["ANSIBLE_TAGS"]
ansible.host_vars = ansible_host_vars
ansible.groups = ansible_groups
end
machine.vm.provision "ansible" do |ansible|
ansible.playbook = "./playbooks/site.yml"
ansible.compatibility_mode = "2.0"
ansible.limit = ENV.fetch("ANSIBLE_LIMIT", "all")
ansible.ask_become_pass = true
ansible.tags = ENV["ANSIBLE_TAGS"]
ansible.host_vars = ansible_host_vars
ansible.groups = ansible_groups
ansible.extra_vars = {
network_configs_path: File.join("..", VAGRANT_NETWORK_CONFIGS_PATH[1..VAGRANT_NETWORK_CONFIGS_PATH.length])
}.merge(ansible_extra_vars)
end
end
end
end
end
end