-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVagrantfile
235 lines (197 loc) · 6.66 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
# -*- mode: ruby -*-
# # vi: set ft=ruby :
require 'fileutils'
Vagrant.require_version ">= 1.6.0"
# CoreOS doesn't support vboxsf annd guest-additions for virtualbox
# So we need to use NFS, and Vagrant NFS doesn't work without this
plugin_dependencies = [
"vagrant-winnfsd",
"vagrant-hostmanager"
]
needsRestart = false
# Install plugins if required
plugin_dependencies.each do |plugin_name|
unless Vagrant.has_plugin? plugin_name
system("vagrant plugin install #{plugin_name}")
needsRestart = true
puts "#{plugin_name} installed"
end
end
# Restart vagrant if new plugins were installed
if needsRestart === true
exec "vagrant #{ARGV.join(' ')}"
end
# Use old vb_xxx config variables when set
def vm_gui
$vb_gui.nil? ? $vm_gui : $vb_gui
end
def vm_memory
$vb_memory.nil? ? $vm_memory : $vb_memory
end
def vm_cpus
$vb_cpus.nil? ? $vm_cpus : $vb_cpus
end
$vm_configs = [
# Defaults for config options
etcd_config: {
num_instances: 1,
instance_name_prefix: "etcd",
enable_serial_logging: false,
vm_gui: false,
vm_memory: 512,
vm_cpus: 1,
vb_cpuexecutioncap: 80,
user_home_path: "/home/core",
forwarded_ports: [],
shared_folders: [
{
host_path: "./",
guest_path: "/vagrant"
}
]
},
kube_master_config: {
num_instances: 1,
instance_name_prefix: "kube-master",
enable_serial_logging: false,
vm_gui: false,
vm_memory: 2048,
vm_cpus: 2,
vb_cpuexecutioncap: 80,
user_home_path: "/home/core",
forwarded_ports: [],
shared_folders: [
{
host_path: "./",
guest_path: "/vagrant"
}
]
},
kube_worker_config: {
num_instances: 2,
instance_name_prefix: "kube-worker",
enable_serial_logging: false,
vm_gui: false,
vm_memory: 1024,
vm_cpus: 2,
vb_cpuexecutioncap: 80,
user_home_path: "/home/core",
forwarded_ports: [],
shared_folders: [
{
host_path: "./",
guest_path: "/vagrant"
}
]
}
]
Vagrant.configure("2") do |config|
# always use Vagrants insecure key
config.ssh.insert_key = true
# forward ssh agent to easily ssh into the different machines
config.ssh.forward_agent = false
# Hostmanager
config.hostmanager.enabled = true
config.hostmanager.manage_guest = true
config.hostmanager.ignore_private_ip = false
config.vm.box = "jaskaranbir/coreos-ansible"
config.vm.boot_timeout = 500
config.vm.provider :virtualbox do |vbox|
# On VirtualBox, we don't have guest additions or a functional vboxsf
# in CoreOS, so tell Vagrant that so it can be smarter.
vbox.check_guest_additions = false
vbox.functional_vboxsf = false
end
# plugin conflict
if Vagrant.has_plugin?("vagrant-vbguest") then
config.vbguest.auto_update = false
end
# This keeps track of total number of instances in all VMs
# It is dynamically incremented as the VM configs are iterated
vm_num_instances_offset = 0
# We need to know total number of instances so we run ansible
# only once, at last instance.
total_instances_count = 0
$vm_configs.each do | vm_config |
vm_config.each do |_, vc|
total_instances_count += vc[:num_instances]
end
end
# ================= VM-specific Configurations =================
$vm_configs.each do |vm_config|
vm_config.each do |vm_config_name, vc|
(1..vc[:num_instances]).each do |i|
config.vm.define vm_name = "%s-%02d" % [vc[:instance_name_prefix], i] do |config|
vm_num_instances_offset += 1
config.vm.hostname = vm_name
# Serial Logging
if vc[:enable_serial_logging]
logdir = File.join(File.dirname(__FILE__), "log")
FileUtils.mkdir_p(logdir)
serialFile = File.join(logdir, "%s-%s-serial.txt" % [vm_name, vc[:instance_name_prefix]])
FileUtils.touch(serialFile)
config.vm.provider :virtualbox do |vb, override|
vb.customize ["modifyvm", :id, "--uart1", "0x3F8", "4"]
vb.customize ["modifyvm", :id, "--uartmode1", serialFile]
end
end
# VM hardware resources configurations
config.vm.provider :virtualbox do |vb|
vb.gui = vc[:vm_gui]
vb.memory = vc[:vm_memory]
vb.cpus = vc[:vm_cpus]
vb.customize [
"modifyvm", :id,
"--cpuexecutioncap", "#{vc[:vb_cpuexecutioncap]}"
]
end
ip = "172.17.8.#{vm_num_instances_offset + 100}"
config.vm.network :private_network, ip: ip, auto_correct: true
# Port Forwarding
vc[:forwarded_ports].each do |port|
config.vm.network :forwarded_port,
host: port[:host_port],
guest: port[:guest_port],
auto_correct: true
end
# # Shared folders
vc[:shared_folders].each_with_index do |share, i|
config.vm.synced_folder share[:host_path], share[:guest_path],
id: "core-share%02d" % vm_num_instances_offset,
nfs: true,
mount_options: ['nolock,vers=3,udp']
end
# Automatically set current-dir to /vagrant on vagrant ssh
config.vm.provision :shell,
inline: "echo 'cd /vagrant' >> #{vc[:user_home_path]}/.bashrc"
# Ansible 2.6+ works only when SSH key is protected.
# So we manually copy the SSH key and set its permissions.
config.vm.provision :shell,
privileged: true, inline: <<-EOF
mkdir -p "#{vc[:user_home_path]}/.ssh"
cp "/vagrant/.vagrant/machines/#{vm_name}/virtualbox/private_key" "#{vc[:user_home_path]}/.ssh/id_rsa"
chmod 0400 "#{vc[:user_home_path]}/.ssh/id_rsa"
EOF
# Run Ansible provisioning when its last instance, so its only run once
if vm_num_instances_offset === total_instances_count
# Copy ansible directory to enable provisioning
config.vm.provision :shell,
inline: "mkdir -p -m777 /ansible",
privileged: true
config.vm.provision "file", source: "./", destination: "/ansible"
# File-provisioner needs full permissions to copy files,
# but ansible 2.6+ will not work unless parent dir is write-protected.
config.vm.provision :shell,
inline: "chmod 744 /ansible",
privileged: true
config.vm.provision :shell,
inline: "cd /ansible" \
" && /opt/bin/active_python/bin/ansible-playbook" \
" kubernetes.yml -vv",
privileged: true
end
end
end
end
end
end