summaryrefslogtreecommitdiffstats
path: root/vagrant/gluster/Vagrantfile
blob: 0b1c6417739b1c6c42309c2470d9340e3a6c2b11 (plain)
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile for GlusterFS using Puppet-Gluster
# Copyright (C) 2010-2013+ James Shubin
# Written by James Shubin <james@shubin.ca>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# NOTE: vagrant-libvirt needs to run in series (not in parallel) to avoid trying
# to create the network twice, eg: 'vagrant up --no-parallel'. alternatively you
# can build the first host (puppet server) manually, and then the hosts next eg:
# 'vagrant up puppet && vagrant up' which ensures the puppet server is up first!
# NOTE: https://github.com/pradels/vagrant-libvirt/issues/104 is the open bug...

# README: https://ttboj.wordpress.com/2013/12/09/vagrant-on-fedora-with-libvirt/
# README: https://ttboj.wordpress.com/2013/12/21/vagrant-vsftp-and-other-tricks/
# ALSO: https://ttboj.wordpress.com/2014/01/02/vagrant-clustered-ssh-and-screen/
# README: https://ttboj.wordpress.com/2014/01/08/automatically-deploying-glusterfs-with-puppet-gluster-vagrant

# NOTE: this will not work properly on Fedora 19 or anything that does not have
# the libvirt broadcast patch included. You can check which libvirt version you
# have and see if that version tag is in the libvirt git or in your distro. eg:
# git tag --contains 51e184e9821c3740ac9b52055860d683f27b0ab6 | grep <version#>
# this is because old libvirt broke the vrrp broadcast packets from keepalived!

# TODO: the /etc/hosts DNS setup is less than ideal, but I didn't implement
# anything better yet. Please feel free to suggest something else!

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = '2'

require 'ipaddr'
require 'yaml'

#
#	globals
#
domain = 'example.com'
network = IPAddr.new '192.168.142.0/24'
range = network.to_range.to_a
cidr = (32-(Math.log(range.length)/Math.log(2))).to_i
offset = 100		# start gluster hosts after here
#puts range[0].to_s	# network
#puts range[1].to_s	# router (reserved)
#puts range[2].to_s	# puppetmaster
#puts range[3].to_s	# vip

# mutable by ARGV and settings file
count = 4		# default number of gluster hosts to build
version = ''		# default gluster version (empty string means latest!)
firewall = false	# default firewall enabled (FIXME: default to true when keepalived bug is fixed)
replica = 1		# default replica count
clients = 1		# default number of gluster clients to build
cachier = false		# default cachier usage

#
#	ARGV parsing
#
projectdir = File.expand_path File.dirname(__FILE__)	# vagrant project dir!!
f = File.join(projectdir, 'puppet-gluster.yaml')

# load settings
if File.exist?(f)
	settings = YAML::load_file f
	count = settings[:count]
	version = settings[:version]
	firewall = settings[:firewall]
	replica = settings[:replica]
	clients = settings[:clients]
	cachier = settings[:cachier]
end

# ARGV parser
skip = 0
while skip < ARGV.length
	#puts "#{skip}, #{ARGV[skip]}"	# debug
	if ARGV[skip].start_with?(arg='--gluster-count=')
		v = ARGV.delete_at(skip).dup
		v.slice! arg
		#puts "#{arg}, #{v}"	# debug

		count = v.to_i		# set gluster host count

	elsif ARGV[skip].start_with?(arg='--gluster-version=')
		v = ARGV.delete_at(skip).dup
		v.slice! arg

		version = v.to_s	# set gluster version

	elsif ARGV[skip].start_with?(arg='--gluster-firewall=')
		v = ARGV.delete_at(skip).dup
		v.slice! arg

		firewall = v.to_s	# set firewall flag
		if ['false', 'no'].include?(firewall.downcase)
			firewall = false
		else
			firewall = true
		end

	elsif ARGV[skip].start_with?(arg='--gluster-replica=')
		v = ARGV.delete_at(skip).dup
		v.slice! arg

		replica = v.to_i	# set gluster replica

	elsif ARGV[skip].start_with?(arg='--gluster-clients=')
		v = ARGV.delete_at(skip).dup
		v.slice! arg

		clients = v.to_i	# set gluster client count

	elsif ARGV[skip].start_with?(arg='--gluster-cachier=')
		v = ARGV.delete_at(skip).dup
		v.slice! arg

		cachier = v.to_s	# set cachier flag
		if ['true', 'yes'].include?(cachier.downcase)
			cachier = true
		else
			cachier = false
		end

	else	# skip over "official" vagrant args
		skip = skip + 1
	end
end

# save settings (ARGV overrides)
settings = {
	:count => count,
	:version => version,
	:firewall => firewall,
	:replica => replica,
	:clients => clients,
	:cachier => cachier
}
File.open(f, 'w') do |file|
	file.write settings.to_yaml
end

#puts "ARGV: #{ARGV}"	# debug

# erase host information from puppet so that the user can do partial rebuilds
snoop = ARGV.select { |x| !x.start_with?('-') }
if snoop.length > 1 and snoop[0] == 'destroy'
	snoop.shift	# left over array snoop should be list of hosts
	if snoop.include?('puppet')	# doesn't matter then...
		snoop = []
	end
else
	# important! clear snoop because we're not using 'destroy'
	snoop = []
end

# figure out which hosts are getting destroyed
destroy = ARGV.select { |x| !x.start_with?('-') }
if destroy.length > 0 and destroy[0] == 'destroy'
	destroy.shift	# left over array destroy should be list of hosts or []
	if destroy.length == 0
		destroy = true	# destroy everything
	end
else
	destroy = false		# destroy nothing
end

# figure out which hosts are getting provisioned
provision = ARGV.select { |x| !x.start_with?('-') }
if provision.length > 0 and ['up', 'provision'].include?(provision[0])
	provision.shift	# left over array provision should be list of hosts or []
	if provision.length == 0
		provision = true	# provision everything
	end
else
	provision = false		# provision nothing
end

# XXX: workaround for: https://github.com/mitchellh/vagrant/issues/2447
# only run on 'vagrant init' or if it's the first time running vagrant
if (ARGV.length > 0 and ARGV[0] == 'init') or not(File.exist?(f))
	`sudo systemctl restart nfs-server`
	`firewall-cmd --permanent --zone public --add-service mountd`
	`firewall-cmd --permanent --zone public --add-service rpc-bind`
	`firewall-cmd --permanent --zone public --add-service nfs`
	`firewall-cmd --reload`
end

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

	#config.landrush.enable	# TODO ?

	#
	#	box (pre-built base image)
	#
	config.vm.box = 'centos-6'		# i built it

	# box source url
	# TODO: this box should be GPG signed
	config.vm.box_url = 'https://download.gluster.org/pub/gluster/purpleidea/vagrant/centos-6.box'

	#
	#	cache
	#
	# NOTE: you should probably erase the cache between rebuilds if you are
	# installing older package versions. This is because the newer packages
	# will get cached, and then subsequently might get silently installed!!
	if cachier
		# TODO: this doesn't cache metadata, full offline operation not possible
		config.cache.auto_detect = true
		config.cache.enable :yum
		#config.cache.enable :apt
		if not ARGV.include?('--no-parallel')	# when running in parallel,
			config.cache.scope = :machine	# use the per machine cache
		end
		config.cache.enable_nfs = true	# sets nfs => true on the synced_folder
		# the nolock option is required, otherwise the NFSv3 client will try to
		# access the NLM sideband protocol to lock files needed for /var/cache/
		# all of this can be avoided by using NFSv4 everywhere. die NFSv3, die!
		config.cache.mount_options = ['rw', 'vers=3', 'tcp', 'nolock']
	end

	#
	#	vip
	#
	vip_ip = range[3].to_s
	vip_hostname = 'annex'

	#
	#	puppetmaster
	#
	puppet_ip = range[2].to_s
	puppet_hostname = 'puppet'
	fv = File.join(projectdir, '.vagrant', "#{puppet_hostname}-hosts.done")
	if destroy.is_a?(TrueClass) or (destroy.is_a?(Array) and destroy.include?(puppet_hostname))
		if File.exists?(fv)	# safety
			puts "Unlocking shell provisioning for: #{puppet_hostname}..."
			File.delete(fv)	# delete hosts token
		end
	end

	#puppet_fqdn = "#{puppet_hostname}.#{domain}"
	config.vm.define :puppet, :primary => true do |vm|
		vm.vm.hostname = puppet_hostname
		# red herring network so that management happens here...
		vm.vm.network :private_network,
		:ip => "10.10.1.#{1+10}",	# FIXME: remove the +10, see: https://github.com/mitchellh/vagrant/issues/2868
		:libvirt__netmask => '255.255.0.0',
		#:libvirt__dhcp_enabled => false,	# XXX: not allowed here
		:libvirt__network_name => 'default'

		# this is the real network that we'll use...
		vm.vm.network :private_network,
		:ip => puppet_ip,
		:libvirt__dhcp_enabled => false,
		:libvirt__network_name => 'gluster'

		#vm.landrush.host puppet_hostname, puppet_ip	# TODO ?

		# ensure the gluster module is present for provisioning...
		if provision.is_a?(TrueClass) or (provision.is_a?(Array) and provision.include?(puppet_hostname))
			cwd = `pwd`
			mod = File.join(projectdir, 'puppet', 'modules')
			`cd #{mod} && make gluster &> /dev/null; cd #{cwd}`
		end

		#
		#	shell
		#
		if not File.exists?(fv)	# only modify /etc/hosts once
			if provision.is_a?(TrueClass) or (provision.is_a?(Array) and provision.include?(puppet_hostname))
				File.open(fv, 'w') {}	# touch
			end
			vm.vm.provision 'shell', inline: 'puppet resource host localhost.localdomain ip=127.0.0.1 host_aliases=localhost'
			vm.vm.provision 'shell', inline: "puppet resource host #{puppet_hostname} ensure=absent"	# so that fqdn works

			vm.vm.provision 'shell', inline: "puppet resource host #{vip_hostname}.#{domain} ip=#{vip_ip} host_aliases=#{vip_hostname} ensure=present"
			vm.vm.provision 'shell', inline: "puppet resource host #{puppet_hostname}.#{domain} ip=#{puppet_ip} host_aliases=#{puppet_hostname} ensure=present"
			(1..count).each do |i|
				h = "annex#{i}"
				ip = range[offset+i].to_s
				vm.vm.provision 'shell', inline: "puppet resource host #{h}.#{domain} ip=#{ip} host_aliases=#{h} ensure=present"
			end

			# hosts entries for all clients
			(1..clients).each do |i|
				h = "client#{i}"
				ip = range[offset+count+i].to_s
				vm.vm.provision 'shell', inline: "puppet resource host #{h}.#{domain} ip=#{ip} host_aliases=#{h} ensure=present"
			end
		end
		#
		#	puppet (apply)
		#
		vm.vm.provision :puppet do |puppet|
			puppet.module_path = 'puppet/modules'
			puppet.manifests_path = 'puppet/manifests'
			puppet.manifest_file = 'site.pp'
			# custom fact
			puppet.facter = {
				'vagrant' => '1',
				'vagrant_gluster_firewall' => firewall ? 'true' : 'false',
				'vagrant_gluster_allow' => (1..count).map{|z| range[offset+z].to_s}.join(','),
			}
		end

		vm.vm.provider :libvirt do |libvirt|
			libvirt.cpus = 2
			libvirt.memory = 1024
		end
	end

	#
	#	annex
	#
	(1..count).each do |i|
		h = "annex#{i}"
		ip = range[offset+i].to_s	# eg: "192.168.142.#{100+i}"
		#fqdn = "annex#{i}.#{domain}"
		fvx = File.join(projectdir, '.vagrant', "#{h}-hosts.done")
		if destroy.is_a?(TrueClass) or (destroy.is_a?(Array) and destroy.include?(h))
			if File.exists?(fvx)	# safety
				puts "Unlocking shell provisioning for: #{h}..."
				File.delete(fvx)	# delete hosts token
			end
		end

		if snoop.include?(h)		# should we clean this machine?
			cmd = "puppet cert clean #{h}.#{domain}"
			puts "Running 'puppet cert clean' for: #{h}..."
			`vagrant ssh #{puppet_hostname} -c 'sudo #{cmd}'`
			cmd = "puppet node deactivate #{h}.#{domain}"
			puts "Running 'puppet node deactivate' for: #{h}..."
			`vagrant ssh #{puppet_hostname} -c 'sudo #{cmd}'`
		end

		config.vm.define h.to_sym do |vm|
			vm.vm.hostname = h
			# red herring network so that management happens here...
			vm.vm.network :private_network,
			:ip => "10.10.2.#{i+10}",	# FIXME: remove the +10, see: https://github.com/mitchellh/vagrant/issues/2868
			:libvirt__netmask => '255.255.0.0',
			:libvirt__network_name => 'default'

			# this is the real network that we'll use...
			vm.vm.network :private_network,
			:ip => ip,
			:libvirt__dhcp_enabled => false,
			:libvirt__network_name => 'gluster'

			#vm.landrush.host h, ip	# TODO ?

			#
			#	shell
			#
			if not File.exists?(fvx)	# only modify /etc/hosts once
				if provision.is_a?(TrueClass) or (provision.is_a?(Array) and provision.include?(h))
					File.open(fvx, 'w') {}	# touch
				end
				vm.vm.provision 'shell', inline: 'puppet resource host localhost.localdomain ip=127.0.0.1 host_aliases=localhost'
				vm.vm.provision 'shell', inline: "puppet resource host #{h} ensure=absent"	# so that fqdn works

				vm.vm.provision 'shell', inline: "puppet resource host #{vip_hostname}.#{domain} ip=#{vip_ip} host_aliases=#{vip_hostname} ensure=present"
				vm.vm.provision 'shell', inline: "puppet resource host #{puppet_hostname}.#{domain} ip=#{puppet_ip} host_aliases=#{puppet_hostname} ensure=present"
				#vm.vm.provision 'shell', inline: "[ ! -e /root/puppet-cert-is-clean ] && ssh -o 'StrictHostKeyChecking=no' #{puppet_hostname} puppet cert clean #{h}.#{domain} ; touch /root/puppet-cert-is-clean"
				# hosts entries for all hosts
				(1..count).each do |j|
					oh = "annex#{j}"
					oip = range[offset+j].to_s	# eg: "192.168.142.#{100+i}"
					vm.vm.provision 'shell', inline: "puppet resource host #{oh}.#{domain} ip=#{oip} host_aliases=#{oh} ensure=present"
				end

				# hosts entries for all clients
				(1..clients).each do |j|
					oh = "client#{j}"
					oip = range[offset+count+j].to_s
					vm.vm.provision 'shell', inline: "puppet resource host #{oh}.#{domain} ip=#{oip} host_aliases=#{oh} ensure=present"
				end
			end
			#
			#	puppet (agent)
			#
			vm.vm.provision :puppet_server do |puppet|
				#puppet.puppet_node = "#{h}"	# redundant
				#puppet.puppet_server = "#{puppet_hostname}.#{domain}"
				puppet.puppet_server = puppet_hostname
				#puppet.options = '--verbose --debug'
				puppet.options = '--test'	# see the output
				puppet.facter = {
					'vagrant' => '1',
					'vagrant_gluster_replica' => replica.to_s,
					'vagrant_gluster_vip' => vip_ip,
					'vagrant_gluster_vip_fqdn' => "#{vip_hostname}.#{domain}",
					'vagrant_gluster_firewall' => firewall ? 'true' : 'false',
					'vagrant_gluster_version' => version,
				}
			end
		end
	end

	#
	#	client
	#
	(1..clients).each do |i|
		h = "client#{i}"
		ip = range[offset+count+i].to_s
		#fqdn = "annex#{i}.#{domain}"
		fvy = File.join(projectdir, '.vagrant', "#{h}-hosts.done")
		if destroy.is_a?(TrueClass) or (destroy.is_a?(Array) and destroy.include?(h))
			if File.exists?(fvy)	# safety
				puts "Unlocking shell provisioning for: #{h}..."
				File.delete(fvy)	# delete hosts token
			end
		end

		if snoop.include?(h)		# should we clean this machine?
			cmd = "puppet cert clean #{h}.#{domain}"
			puts "Running 'puppet cert clean' for: #{h}..."
			`vagrant ssh #{puppet_hostname} -c 'sudo #{cmd}'`
			cmd = "puppet node deactivate #{h}.#{domain}"
			puts "Running 'puppet node deactivate' for: #{h}..."
			`vagrant ssh #{puppet_hostname} -c 'sudo #{cmd}'`
		end

		config.vm.define h.to_sym do |vm|
			vm.vm.hostname = h
			# red herring network so that management happens here...
			vm.vm.network :private_network,
			:ip => "10.10.3.#{i+10}",	# FIXME: remove the +10, see: https://github.com/mitchellh/vagrant/issues/2868
			:libvirt__netmask => '255.255.0.0',
			:libvirt__network_name => 'default'

			# this is the real network that we'll use...
			vm.vm.network :private_network,
			:ip => ip,
			:libvirt__dhcp_enabled => false,
			:libvirt__network_name => 'gluster'

			#vm.landrush.host h, ip	# TODO ?

			#
			#	shell
			#
			if not File.exists?(fvy)	# only modify /etc/hosts once
				if provision.is_a?(TrueClass) or (provision.is_a?(Array) and provision.include?(h))
					File.open(fvy, 'w') {}	# touch
				end
				vm.vm.provision 'shell', inline: 'puppet resource host localhost.localdomain ip=127.0.0.1 host_aliases=localhost'
				vm.vm.provision 'shell', inline: "puppet resource host #{h} ensure=absent"	# so that fqdn works

				vm.vm.provision 'shell', inline: "puppet resource host #{vip_hostname}.#{domain} ip=#{vip_ip} host_aliases=#{vip_hostname} ensure=present"
				vm.vm.provision 'shell', inline: "puppet resource host #{puppet_hostname}.#{domain} ip=#{puppet_ip} host_aliases=#{puppet_hostname} ensure=present"
				# hosts entries for all hosts
				(1..count).each do |j|
					oh = "annex#{j}"
					oip = range[offset+j].to_s	# eg: "192.168.142.#{100+i}"
					vm.vm.provision 'shell', inline: "puppet resource host #{oh}.#{domain} ip=#{oip} host_aliases=#{oh} ensure=present"
				end

				# hosts entries for all clients
				(1..clients).each do |j|
					oh = "client#{j}"
					oip = range[offset+count+j].to_s
					vm.vm.provision 'shell', inline: "puppet resource host #{oh}.#{domain} ip=#{oip} host_aliases=#{oh} ensure=present"
				end
			end
			#
			#	puppet (agent)
			#
			vm.vm.provision :puppet_server do |puppet|
				#puppet.puppet_node = "#{h}"	# redundant
				#puppet.puppet_server = "#{puppet_hostname}.#{domain}"
				puppet.puppet_server = puppet_hostname
				#puppet.options = '--verbose --debug'
				puppet.options = '--test'	# see the output
				puppet.facter = {
					'vagrant' => '1',
					'vagrant_gluster_vip' => vip_ip,
					'vagrant_gluster_vip_fqdn' => "#{vip_hostname}.#{domain}",
					'vagrant_gluster_firewall' => firewall ? 'true' : 'false',
					'vagrant_gluster_version' => version,
				}
			end
		end
	end

	#
	#	misc
	#
	#config.vm.synced_folder "hacking/", "/vagrant_hacking", :nfs => true, :mount_options => ['rw', 'vers=3', 'tcp']

	#
	#	libvirt
	#
	config.vm.provider :libvirt do |libvirt|
		libvirt.driver = 'qemu'
		# leave out to connect directly with qemu:///system
		#libvirt.host = 'localhost'
		libvirt.connect_via_ssh = false
		libvirt.username = 'root'
		libvirt.storage_pool_name = 'default'
		#libvirt.default_network = 'default'	# XXX: this does nothing
		#libvirt.default_prefix = ''	# set a prefix for your vm's...
	end

end