-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathVagrantfile
162 lines (143 loc) · 5.16 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
# -*- mode: ruby -*-
# vi: set ft=ruby :
# set vagrant defaults
$boxes = []
$box = 'rancherio/rancheros'
$box_url = nil
$box_version = nil
$rancher_version = 'latest'
$ip_prefix = '192.168.33'
$disable_folder_sync = true
# install the vagrant-rancher provisioner plugin if
# it is not already installed
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
# validate that at least one box is setup as a rancher server
def parse_boxes(boxes)
servers = []
agents = []
boxes.each do |box|
abort 'Must specify name for box' if box['name'].nil?
if $box == 'rancherio/rancheros'
if !box['memory'].nil? and box['memory'].to_i < 512
puts 'WARNING: Running RancherOS on less than 512MB of RAM has been known to cause issues.'
end
end
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 through boxes and return the 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
# if there is a user-supplied config.rb use that otherwise
# default to the config_sample.rb
if File.exist?(File.join(File.dirname(__FILE__), 'config.rb'))
CONFIG = File.join(File.dirname(__FILE__), 'config.rb')
else
CONFIG = File.join(File.dirname(__FILE__), 'config_sample.rb')
end
# load the set config file
if File.exist?(CONFIG)
require CONFIG
end
# require some capability overrides if the box is rancheros
if $box == 'rancherio/rancheros'
require_relative 'lib/vagrant_rancheros_guest_plugin.rb'
end
# get a list of sorted boxes (starting with server)
$sorted_boxes = parse_boxes $boxes
# determine the default server ip
$default_server_ip = get_server_ip $sorted_boxes
Vagrant.configure(2) do |config|
# Try to use a custom CoreOS box
if $update_channel != nil
config.vm.box = "coreos-%s" % $update_channel
config.vm.box_url = $box_url unless $box_url.nil?
config.vm.box_version = $box_version unless $box_version.nil?
else
# Default to RancherOS if nothing gets overriden
config.vm.box = $box
config.vm.box_url = $box_url unless $box_url.nil?
config.vm.box_version = $box_version unless $box_version.nil?
end
if $disable_folder_sync
config.vm.synced_folder '.', '/vagrant', disabled: true
else
# use rsync when box is rancheros
# otherwise stick with the vagrant defaults
if $box == 'rancherio/rancheros'
config.vm.synced_folder ".", "/vagrant", type: "rsync",
rsync__exclude: ".git/",
rsync__args: ["--verbose", "--archive", "--delete", "--copy-links"],
disabled: false
else
config.vm.synced_folder '.', '/vagrant', disabled: false
end
end
$sorted_boxes.each_with_index do |box, box_index|
# default to only one of each type of box
count = box['count'] || 1
# loop through the desired number of instances
# for a given box
(1..count).each do |i|
# configure the hostname, ex. rancher-server-01
hostname = "#{box['name']}-%02d" % i
# configure node settings
config.vm.define hostname do |node|
# set the hostname
node.vm.hostname = hostname
# set the node ip address
ip = box['ip'] ? box['ip'] : "#{$ip_prefix}.#{box_index+1}#{i}"
node.vm.network 'private_network', ip: ip
# override default memory allocation if set in config
unless box['memory'].nil?
node.vm.provider 'virtualbox' do |vb|
vb.memory = box['memory']
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