-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVagrantfile
174 lines (158 loc) · 5.45 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
require './hyperv/tools.rb'
require 'json'
ENV["VAGRANT_DEFAULT_PROVIDER"] = "hyperv"
PUBLIC_SWITCH = "Ceph (Public)"
CLUSTER_SUBNET = "192.168.10"
CLUSTER_SWITCH = "Ceph (Cluster)"
N_NODES = 5
ssh_pub_key = File.readlines("ssh/id_rsa_vagrant.pub").first.strip
netcfg_shell = "cat <<EOL > /etc/netplan/02-netcfg.yaml
network:
version: 2
ethernets:
eth1:
dhcp4: no
dhcp6: no
addresses:
- ADDRESS
EOL
"
# TRY to create Hyper-V virtual Switches if not exists
# Comment this section if you have trouble or you want to use existing switches
if ARGV[0] == "up"
begin
physical_adapters = list_physical_net_adapter
physical_adapter = physical_adapters.find { |adapter| adapter["status"] == "Up" }
unless switch_index(PUBLIC_SWITCH, "External", physical_adapter["description"])
create_switch(PUBLIC_SWITCH, "External", physical_adapter["description"])
end
unless switch_index(CLUSTER_SWITCH, "Private")
create_switch(CLUSTER_SWITCH, "Private")
end
rescue StandardError
puts StandardError
return
end
end
Vagrant.configure("2") do |config|
config.vm.box = "generic/ubuntu2004"
config.vm.synced_folder ".", "/vagrant", disabled: true
config.vm.provider :hyperv do |hv|
hv.cpus = 4
hv.enable_virtualization_extensions = true
hv.memory = 4096
hv.maxmemory = 4096
hv.linked_clone = true
end
config.hostmanager.enabled = true
config.hostmanager.manage_host = false # put true if you use your host to run ansible
config.hostmanager.manage_guest = false # put true if you use vm to run ansible
config.hostmanager.ignore_private_ip = false
config.hostmanager.include_offline = true
(1..N_NODES).each do |i|
name = "node-#{i}"
cluster_ip = "#{CLUSTER_SUBNET}.1#{i}/24"
config.vm.define name do |node|
node.vm.hostname = name
digit = i - 1
# Hyper-V
node.vm.provider :hyperv do |hv|
hv.vmname = name
# CHECK YOUR VIRTUAL SWITCH MANAGER RANGE IN YOUR HYPER-V
hv.mac = "00155D38010#{digit}"
end
node.vm.network :public_network, bridge: PUBLIC_SWITCH, type: "dhcp"
# AFTER VagrantPlugins::HyperV::Action::Configure
node.trigger.before :"VagrantPlugins::HyperV::Action::StartInstance", type: :action do |trigger|
trigger.ruby do |env, machine|
if "#{machine.provider_name}" == "hyperv"
# Add CLUSTER_SWITCH adapter
unless list_net_adapter(name).any? { |adapter| adapter["switch_name"] == CLUSTER_SWITCH }
# CHECK YOUR VIRTUAL SWITCH MANAGER RANGE IN YOUR HYPER-V
add_net_adapter(name, CLUSTER_SWITCH, "00155D38011#{digit}")
end
end
end
end
# AFTER PROVISION
node.trigger.after :provisioner_run, type: :hook do |trigger|
trigger.ruby do |env, machine|
if "#{machine.provider_name}" == "hyperv"
(1..2).each do |d|
disk_path = "./.vagrant/machines/#{name}/hyperv/Virtual Hard Disks/disk#{d}.vhdx"
unless File.exist?(disk_path)
create_vhd(disk_path, 20)
end
unless get_disks(name).any? { |disk| File.absolute_path(disk["path"]) == File.absolute_path(disk_path) }
add_vhd(name, disk_path, "SCSI")
end
end
end
end
end
node.vm.provision "shell" do |shell|
shell.inline = <<-SHELL
if ! grep -q "#{ssh_pub_key}" "/home/vagrant/.ssh/authorized_keys"; then
echo #{ssh_pub_key} >> /home/vagrant/.ssh/authorized_keys
fi
SHELL
end
netcfg = netcfg_shell.gsub("ADDRESS", cluster_ip)
node.vm.provision "shell", inline: netcfg
node.vm.provision "shell", inline: "netplan apply"
node.vm.provision "shell", inline: "sysctl -w net.ipv6.conf.all.disable_ipv6=1"
end
end
# uncomment if you want to use vm to run ansible
# config.vm.define "ansible" do |ansible|
#
# ansible.vm.hostname = "ansible"
# # Hyper-V
# ansible.vm.provider :hyperv do |hv|
# hv.vmname = "ansible"
# # CHECK YOUR VIRTUAL SWITCH MANAGER RANGE IN YOUR HYPER-V
# hv.mac = "00155D380120"
# end
# # disable ipv6
# ansible.vm.provision "shell", inline: "sysctl -w net.ipv6.conf.all.disable_ipv6=1"
#
# # add ssh keys ans config
# ansible.vm.provision "shell" do |shell|
# ssh_priv_key = File.read("ssh/id_rsa_vagrant").strip
# config = File.read("ssh/config").strip
# ansible_nodes = File.read("ssh/config.d/ansible_nodes").strip
# shell.inline = <<-SHELL
# mkdir -p /home/vagrant/.ssh/config.d
# cat <<EOL > /home/vagrant/.ssh/id_rsa_vagrant.pub
# #{ssh_pub_key}
# EOL
#
# cat <<EOL > /home/vagrant/.ssh/id_rsa_vagrant
# #{ssh_priv_key}
# EOL
#
# chmod 600 /home/vagrant/.ssh/id_rsa_vagrant.pub
# chmod 600 /home/vagrant/.ssh/id_rsa_vagrant
# chown vagrant: /home/vagrant/.ssh/id_rsa_vagrant.pub
# chown vagrant: /home/vagrant/.ssh/id_rsa_vagrant
#
# cat <<EOL > /home/vagrant/.ssh/config
# #{config}
# EOL
#
# cat <<EOL > /home/vagrant/.ssh/config.d/ansible_nodes
# #{ansible_nodes}
# EOL
# SHELL
# end
# # install pip
# ansible.vm.provision "shell" do |shell|
# # install apt install python3-pip
# shell.inline = <<-SHELL
# add-apt-repository -y ppa:ansible/ansible
# apt update
# apt install -y python3-pip
# SHELL
# end
# end
end