-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vagrantfile
317 lines (290 loc) · 10.5 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
require 'fileutils'
IMAGE_NAME = "generic/debian12"
IMAGE_VERSION = "4.3.10"
MASTERS = 1
SLAVES = 2
EXTRA_DISKS = 2
MTU = 1342
$script1 = <<-SCRIPT
sudo ip link set eth0 mtu #{MTU}
sudo ip link set eth1 mtu #{MTU}
SCRIPT
# sudo sed -i.bak \
# -e 's#deb.debian.org#mirror.sjtu.edu.cn#g' \
# -e 's#security.debian.org#mirror.sjtu.edu.cn#g' \
# /etc/apt/sources.list
# sudo apt-get update && sudo apt-get install -y gpg parted
$script2 = <<-SCRIPT
echo "Set default time zone to Asia/Shanghai..."
sudo cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
sudo echo "Asia/Shanghai" > /etc/timezone
SCRIPT
# Disable parallel provisioning to avoid failure
ENV['VAGRANT_NO_PARALLEL'] = 'yes'
Vagrant.configure("2") do |config|
if ARGV[0] == 'up'
# vailidate and save mock test env meta info into file
validate_meta_info()
end
vagrant_provider = get_provider(ARGV)
cert_type, mock_set, question, cluster = get_quartet(read_meta_info())
print_banner(cert_type, mock_set, question, cluster, 66)
if ARGV[0] == 'destroy'
# remove mock test env meta info file
remove_meta_info()
end
# set MTU when vm need cross GFW
config.vm.provision "shell", inline: $script1, run: "always"
# apply local settings such as time zone
config.vm.provision "shell", inline: $script2
# You may need copy the private key manually from existing env
config.ssh.private_key_path = ["~/.vagrant.d/insecure_private_keys/vagrant.key.rsa"]
config.ssh.insert_key = false
config.vm.box_check_update = false
# take care of /etc/hosts in both host and guest
config.hostmanager.enabled = true
config.hostmanager.manage_host = true
config.hostmanager.manage_guest = true
(1..SLAVES + MASTERS).each do |i|
prefix = "#{cert_type}#{mock_set}-c#{cluster}"
# node_id = i <= MASTERS ? "#{prefix}-master" : "#{prefix}-slave-#{i - MASTERS}"
node_id = i <= MASTERS ? "master" : "slave-#{i - MASTERS}"
config.vm.define node_id do |node|
node.vm.box = IMAGE_NAME
node.vm.box_version = IMAGE_VERSION
if vagrant_provider == 'libvirt'
node.vm.network "private_network",
ip: "192.168.122.#{i + 9}",
:libvirt__network_name => "kubernetes",
:libvirt__mtu => MTU,
:autostart => true
else
node.vm.network "private_network", ip: "192.168.56.#{i + 9}"
end
node.vm.hostname = node_id
node.vm.provision :hostmanager
node.hostmanager.aliases = "#{node_id}.kube.vn"
node.vm.provider "libvirt" do |v|
v.management_network_mtu = MTU
# the default pool is full
# v.storage_pool_name = "home-pool"
v.title = node_id
v.description = "Kubernetes node: " + node_id
v.memory = 2048
v.cpus = 2
if i > MASTERS
(1..EXTRA_DISKS).each do |j|
v.storage :file, :size => '30G'
end
end
end
node.vm.provider "virtualbox" do |v|
# vm name must be assigned w/ node id
# otherwise, extra disks won't be created
v.name = node_id
v.memory = 2048
v.cpus = 2
v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
if i > MASTERS
(1..EXTRA_DISKS).each do |j|
v.storage :file, :size => '30G'
end
end
end
dir = "#{cert_type}/mock/#{mock_set}/.infra/cluster#{cluster}"
if i == MASTERS
node.vm.provision "ansible" do |ansible|
ansible.playbook = "#{dir}/playbook-master.yml"
ansible.config_file = "ansible.cfg"
ansible.limit = "master"
ansible.extra_vars = {
hypervisor: vagrant_provider
}
end
end
if i == SLAVES + MASTERS
node.vm.provision "ansible" do |ansible|
ansible.playbook = "#{dir}/playbook-node.yml"
ansible.config_file = "ansible.cfg"
ansible.limit = "slave-*"
ansible.extra_vars = {
hypervisor: vagrant_provider
}
end
if File.exists?("#{dir}/playbook-setup.yml")
node.vm.provision "ansible" do |ansible|
ansible.playbook = "#{dir}/playbook-setup.yml"
ansible.config_file = "ansible.cfg"
ansible.limit = "master"
ansible.extra_vars = {
hypervisor: vagrant_provider
}
end
end
end
end
end
end
def get_provider_from_state()
# fix here for multi-master cluster
libvirt_dir = File.join(".vagrant", "machines", "master", "libvirt")
if File.exists?(libvirt_dir)
return "libvirt"
end
for i in 1..SLAVES
libvirt_dir = File.join(".vagrant", "machines", "slave-#{i}", "libvirt")
if File.exists?(libvirt_dir)
return "libvirt"
end
end
return nil
end
def get_provider_from_args(args)
provider = nil
for i in 0..args.length-1
if args[i] == '--provider'
provider = args[i+1]
break
elsif args[i].start_with?("--provider=")
provider = args[i].split(/=/, 2)[1]
break
end
end
return provider || ENV['VAGRANT_DEFAULT_PROVIDER'] || "virtualbox"
end
def get_provider(args)
return get_provider_from_state() || get_provider_from_args(args)
end
def format_cluster(cert_type, mock_set, question, cluster)
cert_type ||= "XXX"
mock_set ||= "NN"
cluster ||= "NN"
if question
return "#{cert_type}-M#{mock_set}-C#{cluster}(Q#{question})"
else
return "#{cert_type}-M#{mock_set}-C#{cluster}"
end
end
def extract_from_array(args)
cert_type, mock_set, question = nil, nil, nil
for i in 0..args.length-1
if args[i] == '--cert-type'
cert_type = args[i+1]
elsif args[i].start_with?("--cert-type=")
cert_type = args[i].split(/=/, 2)[1]
elsif args[i] == '--mock-set'
mock_set = args[i+1]
elsif args[i].start_with?("--mock-set=")
mock_set = args[i].split(/=/, 2)[1]
elsif args[i] == '--question'
question = args[i+1]
elsif args[i].start_with?("--question=")
question = args[i].split(/=/, 2)[1]
end
end
return cert_type, mock_set, question
end
def get_cluster_by_triple(cert_type, mock_set, question)
question = question.to_i
question_dict = {
'01' => {
1 => { 'cluster' => 1 },
2 => { 'cluster' => 1 },
3 => { 'cluster' => 2 },
4 => { 'cluster' => 3 },
5 => { 'cluster' => 6 },
6 => { 'cluster' => 1 },
7 => { 'cluster' => 5 },
8 => { 'cluster' => 6 },
9 => { 'cluster' => 6 },
10 => { 'cluster' => 6 },
11 => { 'cluster' => 6 },
12 => { 'cluster' => 7 },
13 => { 'cluster' => 8 },
14 => { 'cluster' => 1 },
15 => { 'cluster' => 6 },
16 => { 'cluster' => 1 },
17 => { 'cluster' => 9 },
18 => { 'cluster' => 10 },
99 => { 'cluster' => 99 },
}
}
if !question_dict.has_key?(mock_set)
mock_set = "01"
end
if question > question_dict[mock_set].keys.max() || question < 1
question = 1
end
return question_dict[mock_set][question]['cluster']
end
def get_quartet(args)
cert_type, mock_set, question = extract_from_array(args)
cert_type ||= ENV['CERTIFICATION_TYPE'] || "CKS"
mock_set ||= ENV['MOCK_SET'] || "01"
question ||= ENV['QUESTION']
question = question.to_i
cluster = get_cluster_by_triple(cert_type, mock_set, question)
return cert_type.upcase, mock_set, question, cluster
end
def persist_meta_info(cert_type, mock_set, question)
meta_dir = File.join(".vagrant", "cert")
FileUtils.mkpath(meta_dir) unless File.exists?(meta_dir)
meta_info = File.join(".vagrant", "cert", "meta-info.txt")
open(meta_info, 'w') do |f|
f.puts "--cert-type=#{cert_type}"
f.puts "--mock-set=#{mock_set}"
f.puts "--question=#{question}"
end
end
def validate_meta_info()
cert_type = ENV['CERTIFICATION_TYPE']
mock_set = ENV['MOCK_SET']
question = ENV['QUESTION']
meta_info = File.join(".vagrant", "cert", "meta-info.txt")
if File.exists?(meta_info)
if cert_type == nil and mock_set == nil and question == nil
return
end
cert_type_f, mock_set_f, question_f = extract_from_array(read_meta_info())
if cert_type_f == cert_type and mock_set_f == mock_set
cluster = get_cluster_by_triple(cert_type, mock_set, question)
cluster_f = get_cluster_by_triple(cert_type, mock_set, question_f)
if question_f == question
abort("Kubernetes cluster #{format_cluster(cert_type, mock_set, question, cluster)} is already provisioned!")
elsif cluster == cluster_f
# update question parameter in meta-info.txt
persist_meta_info(cert_type, mock_set, question)
abort("Question Q#{question} and Q#{question_f} share same kubernetes cluster #{format_cluster(cert_type, mock_set, nil, cluster)} which is already provisioned!")
else
abort("You are requesting cluster #{format_cluster(cert_type, mock_set, question, cluster)}, you have to destroy the existing cluster #{format_cluster(cert_type_f, mock_set_f, question_f, cluster_f)} first!")
end
end
else
if !question
abort("You must specify question number as QUESTION=n followed by vagrant up!")
end
persist_meta_info(cert_type || 'CKS', mock_set || '01', question)
end
end
def read_meta_info()
meta_info = File.join(".vagrant", "cert", "meta-info.txt")
if File.exists?(meta_info)
return File.read(meta_info).split
end
return ARGV
end
def remove_meta_info()
meta_info = File.join(".vagrant", "cert", "meta-info.txt")
if File.exists?(meta_info)
return File.delete(meta_info)
end
end
def print_banner(cert_type, mock_set, question, cluster, banner_len)
banner_text = "Kubernetes cluster #{format_cluster(cert_type, mock_set, question, cluster)}"
puts "#" * banner_len
puts "#" + " " * (banner_len - 2) + "#"
puts "#" + banner_text.center(banner_len-2) + "#"
puts "#" + " " * (banner_len - 2) + "#"
puts "#" * banner_len
end
# vim:set nu ai expandtab sw=4 ts=4 syntax=ruby: