-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVagrantfile
183 lines (160 loc) · 5.86 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
require 'fileutils'
Vagrant.require_version ">= 1.9.4"
# set defaults
$boxes = []
$update_channel = "alpha"
$os = "coreos-%s" % $update_channel
$os_version = '>= 1548.0.0'
$os_url = "http://%s.release.core-os.net/amd64-usr/current/coreos_production_vagrant.json" % $update_channel
$rancher_version = 'latest'
$ip_prefix = '10.0.0'
$disable_folder_sync = true
$proxies = {
"http" => nil,
"https" => nil,
"no_proxy" => nil
}
# read and load the config file
CONFIG = File.join(File.dirname(__FILE__), "config.rb")
if File.exist?(CONFIG)
require CONFIG
end
# install the vagrant-rancher provisioner plugin
unless Vagrant.has_plugin?('vagrant-rancher')
puts 'vagrant-rancher plugin not found, installing...'
`vagrant plugin install vagrant-rancher`
abort 'vagrant-rancher plugin installed, but you need to rerun the vagrant command'
end
unless Vagrant.has_plugin?('vagrant-proxyconf')
puts 'vagrant-proxyconf plugin not found, installing...'
`vagrant plugin install vagrant-proxyconf`
abort 'vagrant-proxyconf plugin installed, but you need to rerun the vagrant command'
end
# validate existance of rancher server
def parse_boxes(boxes)
servers = []
agents = []
boxes.each do |box|
abort 'Must specify name for box' if box['name'].nil?
if !box['role'].nil? and box['role'] == 'server'
servers.push(box)
else
agents.push(box)
end
end
abort 'At least one server must be specified in the $boxes config' if servers.empty?
return servers + agents
end
# loop boxes to get ip address of the first server box found
def get_server_ip(boxes, hostname='')
default_server_ip = nil
boxes.each_with_index do |box, i|
if not box['role'].nil? and box['role'] == 'server'
ip = box['ip'] ? box['ip'] : "#{$ip_prefix}.#{i+1}#{i+1}"
default_server_ip = ip if default_server_ip.nil?
if hostname == "#{box['name']}-%02d" % i
return ip
end
end
end
return default_server_ip
end
# sort boxes
$sorted_boxes = parse_boxes $boxes
# determine server ip
$default_server_ip = get_server_ip $sorted_boxes
Vagrant.configure(2) do |config|
# global config
config.ssh.insert_key = false
# plugin conflict
if Vagrant.has_plugin?("vagrant-vbguest") then
config.vbguest.auto_update = false
end
# On VirtualBox, we don't have guest additions or a functional vboxsf in CoreOS, so tell Vagrant that so it can be smarter.
config.vm.provider :virtualbox do |v|
v.check_guest_additions = false
v.functional_vboxsf = false
end
config.vm.box = $os
config.vm.box_url = $os_url unless $os_url.nil?
config.vm.box_version = $os_version unless $os_version.nil?
config.vm.guest = :coreos
if $disable_folder_sync
config.vm.synced_folder '.', '/vagrant', disabled: true
else
config.vm.synced_folder '.', '/vagrant', disabled: false
end
# Set correct proxies if defined, defaults to none
if Vagrant.has_plugin?("vagrant-proxyconf")
unless $proxies['http'].nil?
config.proxy.http = $proxies['http']
end
unless $proxies['https'].nil?
config.proxy.https = $proxies['https']
end
unless $proxies['ftp'].nil?
config.proxy.ftp = $proxies['ftp']
end
unless $proxies['no_proxy'].nil?
config.proxy.no_proxy = $proxies['no_proxy']
# Determine box ips for private networking
$sorted_boxes.each_with_index do |box, box_index|
count = box['count'] || 1
# loop instances of agents
(0..count).each do |i|
ip = box['ip'] ? box['ip'] : "#{$ip_prefix}.#{box_index+1}#{i}"
config.proxy.no_proxy = config.proxy.no_proxy + "," + ip
end
end
end
end
$sorted_boxes.each_with_index do |box, box_index|
count = box['count'] || 1
# loop instances
(1..count).each do |i|
# configure network
hostname = "#{box['name']}-%02d" % i
config.vm.define hostname do |node|
node.vm.hostname = hostname
ip = box['ip'] ? box['ip'] : "#{$ip_prefix}.#{box_index+1}#{i}"
node.vm.network 'private_network', ip: ip
# configure hardware
unless box['memory'].nil?
node.vm.provider 'virtualbox' do |vb|
vb.memory = box['memory']
vb.customize ["modifyvm", :id, "--vram", "2"]
vb.customize ["modifyvm", :id, "--cpuhotplug", "on"]
vb.customize ["modifyvm", :id, "--ioapic", "on"]
vb.customize ["modifyvm", :id, "--hwvirtex", "on"]
vb.customize ["modifyvm", :id, "--boot1", "disk"]
vb.customize ["modifyvm", :id, "--boot2", "none"]
vb.customize ["modifyvm", :id, "--boot3", "none"]
vb.customize ["modifyvm", :id, "--boot4", "none"]
vb.customize ["storageattach", :id, "--storagectl", "IDE Controller", '--port', '0', '--device', '0', '--nonrotational', 'on']
end
end
if !box['role'].nil? and box['role'] == 'server'
node.vm.provision :rancher do |rancher|
rancher.role = 'server'
rancher.hostname = ip
rancher.version = $rancher_version
rancher.deactivate = true
rancher.install_agent = box['install_agent'] || false
rancher.labels = box['labels'] unless box['labels'].nil?
rancher.project = box['project'] unless box['project'].nil?
rancher.project_type = box['project_type'] unless box['project_type'].nil?
end
else
node.vm.provision :rancher do |rancher|
rancher.role = 'agent'
rancher.hostname = box['server'] || $default_server_ip
rancher.install_agent = box['install_agent'] unless box['install_agent'].nil?
rancher.labels = box['labels'] unless box['labels'].nil?
rancher.project = box['project'] unless box['project'].nil?
rancher.project_type = box['project_type'] unless box['project_type'].nil?
end
end
end
end
end
end