Externalize (currently unused) ctdb-create-pubaddrs script
[obnox/vagrant/vagrant-gluster-samba-cluster.git] / Vagrantfile
1 # -*- mode: ruby -*-
2 # vi: ft=ruby:et:ts=2:sts=2:sw=2
3
4 VAGRANTFILE_API_VERSION = 2
5
6
7 require 'yaml'
8
9 #
10 # Defaults for Configuration data.
11 # Will be overridden from the settings file
12 # and (possibly later) from commandline parameters.
13 #
14
15 net_default = {
16   :type   => 'veth',
17   :flags  => 'up',
18   :hwaddr => '',
19   :name   => '',
20   :ipv4   => '',
21   :ipv6   => '',
22 }
23
24 network_opts = [ :type, :link, :flags, :hwaddr, :name, :ipv4, :ipv6 ]
25
26 libvirt_network_parms = {
27   :hwaddr => :mac,
28   :ipv4   => :ip,
29   :ipv6   => '',
30   :link   => '',
31   :flags  => '',
32   :type   => '',
33 }
34
35 defaults = {
36   :provider => {
37     :libvirt => {
38       :prefix => 'vagrant',
39     },
40   },
41 }
42
43
44 vms = [
45   {
46     #:hostname => 'gluno1',
47     :hostname => 'node1',
48     #:box => 'local-fedora-rawhide-64',
49     #:box => 'purpleidea-fedora-21',
50     #:box => 'local-fedora-21.2',
51     :provider => {
52       :lxc => {
53         :container_name => 'gluno1',
54         #:container_name => 'node1',
55       },
56       :libvirt => {
57         :box => 'local-fedora-21.2',
58         :prefix => 'gluster',
59       }, 
60     },
61     :internal_if => 'virbr1',
62     :networks => [
63       {
64         :link => 'virbr1',
65         :ipv4 => '172.20.10.30',
66       },
67       #{
68       #  :link => 'virbr2',
69       #  #:ipv4 => '10.111.222.201',
70       #},
71     ],
72   },
73 ]
74
75 #
76 # Load the config, if it exists,
77 # possibly override with commandline args,
78 # (currently none supported yet)
79 # and then store the config.
80 #
81
82 projectdir = File.expand_path File.dirname(__FILE__)
83 f = File.join(projectdir, 'vagrant.yaml')
84 if File.exists?(f)
85   settings = YAML::load_file f
86
87   if settings[:vms].is_a?(Array)
88     vms = settings[:vms]
89   end
90   puts "Loaded settings from #{f}."
91 end
92
93 # TODO(?): ARGV-processing
94
95 settings = {
96   :vms  => vms,
97 }
98
99 File.open(f, 'w') do |file|
100   file.write settings.to_yaml
101 end
102 puts "Wrote settings to #{f}."
103
104
105 # apply defaults:
106
107 vms.each do |vm|
108   defaults.keys.each do |cat|
109     next if not vm.has_key?(cat)
110     defaults[cat].keys.each do |subcat|
111       next if not vm[cat].has_key?(subcat)
112       defaults[cat][subcat].keys.each do |key|
113         if not vm[cat][subcat].has_key?(key)
114           vm[cat][subcat][key] = defaults[cat][subcat][key]
115         end
116       end
117     end
118   end
119
120   #if not vm[:provider][:libvirt].has_key?(:prefix)
121   #  vm[:provider][:libvirt][:prefix] = default_libvirt_prefix
122   #end
123
124   vm[:networks].each do |net|
125     net_default.keys.each do |key|
126       if not net.has_key?(key)
127         net[key] = net_default[key]
128       end
129     end
130   end
131 end
132
133
134 # compose the list of cluster internal ips
135 #
136 cluster_internal_ips = vms.map do |vm|
137   net = nil
138   vm[:networks].each do |n|
139     if n[:link] == vm[:internal_if]
140       net = n
141       break
142     end
143   end
144   if net != nil
145     net[:ipv4]
146   end
147 end
148
149 #print "internal ips: "
150 #print cluster_internal_ips
151 #print "\n"
152
153 #PROVISION_SCRIPT = <<SCRIPT
154 #yum -y install make samba
155 #SCRIPT
156
157
158 NET_FIX_ALWAYS_SCRIPT = <<SCRIPT
159 set -e
160
161 # eth1 is not brought up automatically
162 # by 'vagrant up' of the existing vm.
163 # because eth1 is not up, glusterd can
164 # not be started and gluster volumes can
165 # not be mounted. fix it all up here until
166 # we have a correctly working environment.
167 ifdown eth1
168 ifup eth1
169
170 MOUNTPTS="$@"
171
172 for MOUNTPT in $MOUNTPTS
173 do
174   grep -q -s "${MOUNTPT}" /etc/fstab && {
175     # already provisioned...
176     systemctl start glusterd
177     # sleep to give glusterd some time to start up
178     sleep 2
179
180     mount | grep -q -s "${MOUNTPT}" && {
181       echo "${MOUNTPT} is already mounted."
182     } || {
183       echo "Mounting ${MOUNTPT}."
184       mount ${MOUNTPT}
185     }
186
187     systemctl start ctdb
188   } || {
189     # not provisioned yet
190     echo "${MOUNTPT} not set up yet. Not mounting."
191   }
192 done
193
194 SCRIPT
195
196 NET_FIX_INITIAL_SCRIPT = <<SCRIPT
197 set -e
198 # Fix dhclient running on private network IF
199 ifdown eth1
200 systemctl restart NetworkManager
201 ifdown eth1
202 ifup eth1
203 SCRIPT
204
205
206 CTDB_CREATE_CONF_SCRIPT = <<SCRIPT
207 set -e
208
209 BACKUP_SUFFIX=".orig.$(date +%Y%m%d-%H%M%S)"
210
211 RECLOCKDIR=/gluster/gv0/ctdb
212 mkdir -p ${RECLOCKDIR}
213 RECLOCKFILE=${RECLOCKDIR}/reclock
214
215 PUBLIC_ADDRESSES_FILE=/etc/ctdb/public_addresses
216 NODES_FILE=/etc/ctdb/nodes
217
218 FILE=/etc/sysconfig/ctdb
219 test -f ${FILE} || touch ${FILE}
220 cp -f -a ${FILE} ${FILE}${BACKUP_SUFFIX}
221
222 echo -n > ${FILE}
223 cat <<EOF >> ${FILE}
224 CTDB_NODES=${NODES_FILE}
225 #CTDB_PUBLIC_ADDRESSES=${PUBLIC_ADDRESSES_FILE}
226 CTDB_RECOVERY_LOCK=${RECLOCKFILE}
227 CTDB_MANAGES_SAMBA="yes"
228 CTDB_SAMBA_SKIP_SHARE_CHECK="yes"
229 #CTDB_MANAGES_WINBIND="yes"
230 EOF
231 SCRIPT
232
233 SAMBA_CREATE_CONF_SCRIPT = <<SCRIPT
234 set -e
235
236 BACKUP_SUFFIX=".orig.$(date +%Y%m%d-%H%M%S)"
237
238 GLUSTER_VOL=$1
239
240 GLUSTER_VOL_MOUNT=$2
241
242 mkdir -p ${GLUSTER_VOL_MOUNT}/share1
243 chmod -R 0777 ${GLUSTER_VOL_MOUNT}/share1
244
245 mkdir -p ${GLUSTER_VOL_MOUNT}/share2
246 chmod -R 0777 ${GLUSTER_VOL_MOUNT}/share2
247
248 FILE=/etc/samba/smb.conf
249 test -f ${FILE} || touch ${FILE}
250 cp -f -a ${FILE} ${FILE}${BACKUP_SUFFIX}
251
252 echo -n > ${FILE}
253 cat <<EOF >> ${FILE}
254 [global]
255     netbios name = sambacluster
256     workgroup = vagrant
257     security = user
258
259     clustering = yes
260     #include = registry
261
262 [share1]
263     path = /share1
264     vfs objects = acl_xattr glusterfs
265     glusterfs:volume = ${GLUSTER_VOL}
266     kernel share modes = no
267     read only = no
268
269 [share2]
270     path = ${GLUSTER_VOL_MOUNT}/share2
271     vfs objects = acl_xattr
272     read only = no
273 EOF
274 SCRIPT
275
276 #
277 # The vagrant machine definitions
278 #
279
280 Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
281
282   config.vm.synced_folder ".", "/vagrant", disabled: true
283
284   #if Vagrant.has_plugin?("vagrant-cachier")
285   #  config.cache.scope = :machine
286   #  #config.cache.scope = :box
287
288   #  config.cache.synced_folder_opts = {
289   #    type: :nfs,
290   #    # The nolock option can be useful for an NFSv3 client that wants to avoid the
291   #    # NLM sideband protocol. Without this option, apt-get might hang if it tries
292   #    # to lock files needed for /var/cache/* operations. All of this can be avoided
293   #    # by using NFSv4 everywhere. Please note that the tcp option is not the default.
294   #    #mount_options: ['rw', 'vers=3', 'tcp', 'nolock']
295   #  }
296   #end
297
298   # just let one node do the probing
299   probing = false
300
301   vms.each do |machine|
302     config.vm.define machine[:hostname] do |node|
303       node.vm.box = machine[:provider][:libvirt][:box]
304       node.vm.hostname = machine[:hostname]
305
306       node.vm.provider :libvirt do |libvirt|
307         libvirt.default_prefix = machine[:provider][:libvirt][:prefix]
308         libvirt.memory = 1024
309         libvirt.storage :file, :size => '64M', :device => 'vdb'
310         libvirt.storage :file, :size => '10G', :device => 'vdc'
311
312         machine[:networks].each do |net|
313           if not net[:ipv4] == ''
314             node.vm.network :private_network, :ip => net[:ipv4]
315           end
316         end
317       end
318
319
320       node.vm.provision "selinux", type: "shell" do |s|
321         s.path = "provision/shell/sys/selinux-off.sh"
322       end
323
324       # There is some problem with the fedora base box:
325       # Upon first boot, ifdown eth1 fails and the dhclient
326       # keep being active. Simply bringing down and up again
327       # the interface is not sufficient. We need to restart
328       # NetworkManager in order to teach it to not feel
329       # responsible for the interface any more.
330       ###node.vm.provision "net_fix_initial", type: "shell" do |s|
331       ###  s.inline = NET_FIX_INITIAL_SCRIPT
332       ###end
333
334       node.vm.provision "install", type: "shell" do |s|
335         s.path = "provision/shell/sys/install-yum.sh"
336         s.args = [ "xfsprogs",
337                    "glusterfs",
338                    "glusterfs-server",
339                    "glusterfs-fuse",
340                    "glusterfs-geo-replication",
341                    "ctdb",
342                    "samba",
343                    "samba-client",
344                    "samba-vfs-glusterfs" ]
345       end
346
347       # There is some problem with the fedora base box:
348       # We need to up the interface on reboots.
349       # It does not come up automatically.
350       ###node.vm.provision "net_fix_always", type: "shell", run: "always" do |s|
351       ###  s.inline = NET_FIX_ALWAYS_SCRIPT
352       ###  s.args = [ '/gluster/gv0', '/gluster/gv1' ]
353       ###end
354
355       # multiple privisioners with same name possible?
356       node.vm.provision "xfs_0", type: "shell" do |s|
357         s.path = "provision/shell/gluster/create-brick.sh"
358         s.args = [ "vdb", "/export" ]
359       end
360
361       node.vm.provision "xfs_1", type: "shell" do |s|
362         s.path = "provision/shell/gluster/create-brick.sh"
363         s.args = [ "vdc", "/export" ]
364       end
365
366       node.vm.provision "gluster_start", type: "shell" do |s|
367         s.path = "provision/shell/gluster/gluster-start.sh"
368       end
369
370       if !probing
371         probing = true
372         node.vm.provision "gluster_probe", type: "shell" do |s|
373           s.path = "provision/shell/gluster/gluster-probe.sh"
374           s.args = cluster_internal_ips
375         end
376         probing = false
377       end
378
379       node.vm.provision "gluster_wait_peers", type: "shell" do |s|
380         s.path = "provision/shell/gluster/gluster-wait-peers.sh"
381         s.args = [ cluster_internal_ips.length, 300]
382       end
383
384       node.vm.provision "gluster_createvol_0", type: "shell" do |s|
385         mount_points = cluster_internal_ips.map do |ip|
386           "#{ip}:/export/vdb1/brick"
387         end
388         s.path = "provision/shell/gluster/gluster-create-volume.sh"
389         s.args = [ "gv0", "3" ] + mount_points
390       end
391
392       node.vm.provision "gluster_mount_0", type: "shell" do |s|
393         s.path = "provision/shell/gluster/gluster-mount-volume.sh"
394         s.args = [ "gv0", "/gluster/gv0" ]
395       end
396
397       node.vm.provision "gluster_createvol_1", type: "shell" do |s|
398         mount_points = cluster_internal_ips.map do |ip|
399           "#{ip}:/export/vdc1/brick"
400         end
401         s.path = "provision/shell/gluster/gluster-create-volume.sh"
402         s.args = [ "gv1", "3" ] + mount_points
403       end
404
405       node.vm.provision "gluster_mount_1", type: "shell" do |s|
406         s.path = "provision/shell/gluster/gluster-mount-volume.sh"
407         s.args = [ "gv1", "/gluster/gv1" ]
408       end
409
410       #
411       # ctdb / samba config
412       #
413
414       node.vm.provision "ctdb_stop", type: "shell" do |s|
415         s.path = "provision/shell/ctdb/ctdb-stop.sh"
416       end
417
418       node.vm.provision "ctdb_create_nodes", type: "shell" do |s|
419         s.path = "provision/shell/ctdb/ctdb-create-nodes.sh"
420         s.args = cluster_internal_ips
421       end
422
423       #node.vm.provision "ctdb_create_pubaddrs", type: "shell" do |s|
424       #  s.path = "provision/shell/ctdb/ctdb-create-pubaddrs.sh"
425       #  s.arg =
426       #end
427
428       node.vm.provision "ctdb_create_conf", type: "shell" do |s|
429         s.inline = CTDB_CREATE_CONF_SCRIPT
430       end
431
432       node.vm.provision "samba_create_conf", type: "shell" do |s|
433         s.inline = SAMBA_CREATE_CONF_SCRIPT
434         s.args = [ "gv1", "/gluster/gv1" ]
435       end
436
437       node.vm.provision "ctdb_start", type: "shell" do |s|
438         s.path = "provision/shell/ctdb/ctdb-start.sh"
439       end
440
441     end
442   end
443
444 end