-
Notifications
You must be signed in to change notification settings - Fork 21
/
Vagrantfile
253 lines (224 loc) · 8.53 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
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Need to run some install tasks first
#
# vagrant plugin install vagrant-hostsupdater
# vagrant plugin install vagrant-vbguest # if you want to ensure guest addition parity
#
# This all needs to be run from the parent directory of kalastack
Vagrant.configure("2") do |config|
# All Vagrant configuration is done here. The most common configuration
# options are documented and commented below. For a complete reference,
# please see the online documentation at vagrantup.com.
# Wooden ships on the water. Very free. And Easy.
# Load correct config JSON
default_conf_path = File.expand_path(File.dirname(__FILE__)) + "/default.config.json"
conf = JSON.parse(File.read(default_conf_path))
# Merge in any custom settings
if File.exist?(File.expand_path(File.dirname(__FILE__)) + "/config.json")
conf.merge!(JSON.parse(File.read(File.expand_path(File.dirname(__FILE__)) + "/config.json")))
end
# Generate a UUID to identify this box to the puppet master
unless File.directory?(".kalabox")
FileUtils.mkdir_p(".kalabox")
end
unless File.exist?(".kalabox/uuid")
require 'securerandom';
kalauuid = "kala." + SecureRandom.hex + ".box"
File.new(".kalabox/uuid", 'w')
File.open(".kalabox/uuid", "wb") do |file|
file.write(kalauuid)
end
end
# Some basic vm config
config.vm.box = conf["boxname"]
config.vm.hostname = File.read(".kalabox/uuid")
# Gather some more data about the host machine - this is only tested on mac
hostarch = %x[ uname -m ].strip
hostker = %x[ uname ].strip
if hostker == "Darwin" then #looking for way to strip newline
hostmem = Integer(%x[ sysctl hw.memsize ].scan(/\d+/).shift) / 1049000
elsif hostker == "Linux" then
hostmem = Integer(%x[ grep MemTotal /proc/meminfo ].scan(/\d+/).shift) / 1024
else
hostmem = Integer(conf["default_memory"])
end
# Set a hard maximum on memsize
# This should translate to a 3GB MAXBOX
if hostmem > 12288 then
hostmem = 12288
elsif hostmem < 4096 then
puts "WARNING: Kalabox is designed to work best with at least 4096MB of RAM! You have #{ hostmem }MB."
end
# The url from where the 'config.vm.box' box will be fetched if it
# doesn't already exist on the user's system.
if hostarch.include? "64" then
config.vm.box_url = conf["box_location"]["64"]
else
config.vm.box_url = conf["box_location"]["32"]
end
# Create a private network, which allows host-only access to the machine
# using a specific IP.
config.vm.network :private_network, ip: conf["ip"]
config.hostsupdater.aliases = conf["host_entries"]
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
config.vm.synced_folder ENV["HOME"] + conf["synced_www_folder"]["host_path"], conf["synced_www_folder"]["guest_path"], :create => conf["synced_www_folder"]["create"], :nfs => conf["synced_www_folder"]["nfs"], :nfs_version => conf["synced_www_folder"]["nfs_version"], :mount_options => ['nolock']
# Use for drush debugging. Be careful with this!!! It could bork your box.
# config.vm.synced_folder "~/kalabox/drush/", "/usr/share/drush/", :create => true, :nfs => true
# Set some SSH config
config.ssh.forward_agent = conf["ssh_forwarding"]
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
config.vm.provider :virtualbox do |vb|
# # Don't boot with headless mode
# vb.gui = true
#
# Use VBoxManage to customize the VM. For example to change memory:
vb.customize ["modifyvm", :id, "--memory", (hostmem / conf["memory_divisor"].to_i)]
vb.name = File.read(".kalabox/uuid")
end
#
# View the documentation for the provider you're using for more
# information on available options.
# Use the correct provisioner based on some environmental settings
if ENV['KALABOX_LEGACY']=='TRUE' then
config.vm.provision :puppet_server do |ps|
ps.puppet_node = File.read(".kalabox/uuid")
ps.puppet_server = conf["puppet_master"]["server"]
ps.options = "--verbose --debug --test --environment " + conf["puppet_environment"]
ps.facter = {
"vagrant" => "1",
"kalauser" => conf["boxuser"],
"kalahost" => conf["host_ip"],
"kalaversion" => conf["kalastack_version"],
"kalamem" => (hostmem / conf["memory_divisor"].to_i),
"terminatur_version" => conf["terminatur_version"],
}
end
elsif ENV['KALABOX_REMOTE']=='TRUE' then
config.vm.provision :puppet_server do |ps|
ps.puppet_node = File.read(".kalabox/uuid")
ps.puppet_server = conf["puppet_master"]["server"]
ps.options = "--verbose --debug --test --environment development"
ps.facter = {
"vagrant" => "1",
"kalauser" => conf["boxuser"],
"kalahost" => conf["host_ip"],
"kalaversion" => conf["kalastack_version"],
"kalamem" => (hostmem / conf["memory_divisor"].to_i),
"terminatur_version" => conf["terminatur_version"],
}
end
elsif ENV['KALABOX_SOLR']=='TRUE' then
config.vm.provision :puppet do |p|
p.manifests_path = "manifests"
p.manifest_file = "solr.pp"
p.module_path = "modules"
p.options = "--verbose --debug"
p.facter = {
"vagrant" => "1",
"kalauser" => conf["boxuser"],
"kalahost" => conf["host_ip"],
"kalamem" => (hostmem / conf["memory_divisor"].to_i),
"terminatur_version" => conf["terminatur_version"],
}
end
elsif ENV['KALABOX_FRONTEND']=='TRUE' then
config.vm.provision :puppet do |p|
p.manifests_path = "manifests"
p.manifest_file = "frontend.pp"
p.module_path = "modules"
p.options = "--verbose --debug"
p.facter = {
"vagrant" => "1",
"kalauser" => conf["boxuser"],
"kalahost" => conf["host_ip"],
"kalamem" => (hostmem / conf["memory_divisor"].to_i),
"terminatur_version" => conf["terminatur_version"],
}
end
else
config.vm.provision :puppet do |p|
p.manifests_path = "manifests"
p.manifest_file = "site.pp"
p.module_path = "modules"
p.options = "--verbose --debug"
p.facter = {
"vagrant" => "1",
"kalauser" => conf["boxuser"],
"kalahost" => conf["host_ip"],
"kalamem" => (hostmem / conf["memory_divisor"].to_i),
"terminatur_version" => conf["terminatur_version"],
}
end
end
# Here are some faux plugins that we are using to perform various operations
# like making sure an SSH key exists and is forwarded, deleting the UUID on destroy
# and making sure the puppet master is awake
# Delete UUID when box is destroyed
module VagrantPlugins
module KBOX
module Action
class RemoveKUUID
def initialize(app, env)
@app = app
@machine = env[:machine]
@ui = env[:ui]
end
def call(env)
machine_action = env[:machine_action]
if machine_action == :destroy
if File.exist?(".kalabox/uuid")
File.delete(".kalabox/uuid")
@ui.info "Removing UUID"
end
end
end
end
class WakeMaster
def initialize(app, env)
@app = app
@machine = env[:machine]
@ui = env[:ui]
end
def call(env)
machine_action = env[:machine_action]
if machine_action == :up
require 'open-uri'
@ui.info "Making sure puppet master is awake"
begin
contents = open('http://foreman.kalamuna.com') {|io| io.read}
rescue Exception => e
@ui.info "Error connecting to puppet master"
delete('KALABOX_REMOTE')
end
end
@app.call(env)
end
end
end
end
end
module VagrantPlugins
module KBOX
class Plugin < Vagrant.plugin('2')
name 'KBOX'
description <<-DESC
This plugin does some kalabox stuff
DESC
action_hook("RemoveUUID", :machine_action_destroy) do |hook|
hook.append(Action::RemoveKUUID)
end
if ENV['KALABOX_REMOTE']=='TRUE' then
action_hook("WakeMaster", :machine_action_up) do |hook|
hook.prepend(Action::WakeMaster)
end
end
end
end
end
end