Hand base /export directory as parameter to xfs 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 XFS_SCRIPT = <<SCRIPT
206 set -e
207
208 DEVICE=$1
209 PARTDEV=${DEVICE}1
210 DISKDEV="/dev/${DEVICE}"
211 DISKPARTDEV="/dev/${PARTDEV}"
212 EXPORT_BASEDIR=$2
213 MOUNTP=${EXPORT_BASEDIR}/${PARTDEV}
214 BRICKD=${MOUNTP}/brick
215
216 BACKUP_SUFFIX=".orig.$(date +%Y%m%d-%H%M%S)"
217
218 parted -s ${DISKDEV} print > /dev/null 2>&1 && {
219   echo "Label exists on ${DISKDEV}."
220 } || {
221   echo "Creating label on ${DISKDEV}."
222   parted -s ${DISKDEV} mklabel msdos
223 }
224
225 parted -s ${DISKDEV} print 1 > /dev/null 2>&1 && {
226   echo "Partition ${DISKPARTDEV} exists."
227 } || {
228   echo "Creating partition ${DISKPARTDEV}."
229   parted -s ${DISKDEV} mkpart primary 1 100%
230 }
231
232 blkid -s TYPE ${DISKPARTDEV} | grep -q -s 'TYPE="xfs"' && {
233   echo "Partition ${DISKPARTDEV} contains xfs file system."
234 } || {
235   echo "Creating xfs filesystem on ${DISKPARTDEV}."
236   mkfs.xfs -f ${DISKPARTDEV}
237 }
238
239 mkdir -p ${MOUNTP}
240
241 FILE=/etc/fstab
242
243 grep -q -s ${DISKPARTDEV} ${FILE} && {
244   echo "Mount entry for ${DISKPARTDEV} is present in ${FILE}."
245 } || {
246   echo "Creating mount entry for ${DISKPARTDEV} in ${FILE}."
247   test -f ${FILE} || touch ${FILE}
248   cp -f -a ${FILE} ${FILE}${BACKUP_SUFFIX}
249   cat <<EOF >> ${FILE}
250 ${DISKPARTDEV} ${MOUNTP} xfs defaults 0 0
251 EOF
252 }
253
254 mount | grep ${MOUNTP} && {
255   echo "${MOUNTP} is already mounted."
256 } || {
257   echo "Mounting ${MOUNTP}."
258   mount ${MOUNTP}
259 }
260
261 mkdir -p ${BRICKD}
262 SCRIPT
263
264
265 GLUSTER_PROBE_SCRIPT = <<SCRIPT
266 set -e
267
268 PEER_IPS="$@"
269
270 echo "peer probing for [${PEER_IPS}]"
271
272 for PEER_IP in ${PEER_IPS}
273 do
274   echo "peer probing for '${PEER_IP}'"
275
276   for COUNT in $(seq 1 120)
277   do
278     gluster peer probe ${PEER_IP} 2> /dev/null && {
279       echo "reached node '${PEER_IP}'"
280       break
281     } || {
282       sleep 1
283     }
284   done
285
286   gluster peer probe ${PEER_IP} 2> /dev/null || {
287     echo "did not reach node '${PEER_IP}' - stopping here"
288     break
289   }
290 done
291 exit 0
292 SCRIPT
293
294 GLUSTER_WAIT_PEERS_SCRIPT = <<SCRIPT
295 set -e
296
297 NUM_NODES="$1"
298 TIMEOUT=$2
299
300 echo "Waiting for $NUM_NODES peers."
301
302 for count in $(seq 1 ${TIMEOUT})
303 do
304   PEERS=$(gluster pool list | grep -v ^UUID | wc -l)
305   [ "$PEERS" = "$NUM_NODES" ] && {
306     echo "Done waiting: $NUM_NODES peers connected."
307     exit 0
308   } || {
309     sleep 1
310   }
311 done
312
313 echo "TIMEOUT waiting for $NUM_NODES peers."
314 exit 1
315
316 SCRIPT
317
318 GLUSTER_CREATEVOL_SCRIPT = <<SCRIPT
319 #set -e
320
321 VOLNAME=$1
322 shift
323 REP=$1
324 shift
325
326 while true; do
327   MSG="$(gluster volume status ${VOLNAME} 2>&1 1>/dev/null)"
328   RET=$?
329   [ $RET -eq 0 ] && break
330   [ "${MSG}" != "${MSG#Another transaction is in progress}" ] || break
331   sleep 1
332 done
333
334 [ $RET -eq 0 ] && {
335   echo "gluster volume ${VOLNAME} already exists and is active."
336   exit 0
337 }
338
339 [ "$MSG" = "Volume ${VOLNAME} does not exist" ] && {
340   echo "Creating gluster volume ${VOLNAME}."
341   echo "cmd: gluster volume create $VOLNAME rep $REP transport tcp $@"
342   while true; do
343     MSG=$(gluster volume create $VOLNAME rep $REP transport tcp $@ 2>&1 1>/dev/null)
344     RET=$?
345     [ $RET -eq 0 ] && break
346     [ "$MSG" = "volume create: ${VOLNAME}: failed: Volume ${VOLNAME} already exists" ] && {
347       RET=0
348       break
349     }
350     [ "${MSG}" != "${MSG#Another transaction is in progress}" ] || break
351   done
352
353   [ $RET -eq 0 ] || {
354     echo "gluster volume create $VOLNAME failed ('$MSG')- trying to force."
355
356     while true; do
357       MSG=$(gluster volume create $VOLNAME rep $REP transport tcp $@ force 2>&1 1>/dev/null)
358       RET=$?
359       [ $RET -eq 0 ] && break
360       [ "$MSG" = "volume create: ${VOLNAME}: failed: Volume ${VOLNAME} already exists" ] && {
361         RET=0
362         break
363       }
364       [ "${MSG}" != "${MSG#Another transaction is in progress}" ] || break
365     done
366   }
367
368   [ $RET -eq 0 ] || {
369     echo "gluster volume create $VOLNAME failed with force ('$MSG')- giving up"
370     exit 1
371   }
372
373   while true; do
374     MSG="$(gluster volume status ${VOLNAME} 2>&1 1>/dev/null)"
375     RET=$?
376     [ $RET -eq 0 ] && break
377     [ "${MSG}" != "${MSG#Another transaction is in progress}" ] || break
378     sleep 1
379   done
380
381   [ $RET -eq 0 ] && {
382     echo "gluster volume ${VOLNAME} is already started."
383     exit 0
384   }
385 }
386
387 [ "$MSG" = "Volume ${VOLNAME} is not started" ] && {
388   echo "starting gluster volume ${VOLNAME}."
389   while true; do
390     MSG=$(gluster volume start ${VOLNAME} 2>&1 1> /dev/null)
391     RET=$?
392     [ $RET -eq 0 ] && break
393     [ "$MSG" = "volume start: ${VOLNAME}: failed: Volume ${VOLNAME} already started" ] && {
394       RET=0
395       break
396     }
397     [ "${MSG}" != "${MSG#Another transaction is in progress}" ] || break
398   done
399
400   [ $RET -eq 0 ] || {
401     echo "gluster volume start ${VOLNAME} failed ('$MSG')."
402     exit 1
403   }
404 } || {
405   echo "Error: 'gluster volume status ${VOLNAME}' gave '$MSG' ($RET)"
406   exit 1
407 }
408
409 exit 0
410
411 SCRIPT
412
413 GLUSTER_MOUNT_SCRIPT = <<SCRIPT
414 set -e
415
416 VOLNAME=$1
417 shift
418 MOUNTPT=$1
419 shift
420
421 MOUNTDEV="127.0.0.1:/${VOLNAME}"
422
423 mkdir -p ${MOUNTPT}
424
425 BACKUP_SUFFIX=".orig.$(date +%Y%m%d-%H%M%S)"
426
427 FILE=/etc/fstab
428
429 grep -q -s "${MOUNTPT}" ${FILE} || {
430   test -f ${FILE} || touch ${FILE}
431   cp -f -a ${FILE} ${FILE}${BACKUP_SUFFIX}
432
433   cat <<EOF >> ${FILE}
434 ${MOUNTDEV} ${MOUNTPT} glusterfs defaults,selinux 0 0
435 EOF
436 }
437
438 mount | grep -q -s ${MOUNTPT} && {
439   echo "${MOUNTPT} is already mounted."
440 } || {
441   echo "Mounting ${MOUNTPT}."
442   mount ${MOUNTPT}
443 }
444 SCRIPT
445
446
447 CTDB_CREATE_NODES_SCRIPT = <<SCRIPT
448 set -e
449
450 BACKUP_SUFFIX=".orig.$(date +%Y%m%d-%H%M%S)"
451
452 NODES_IPS="$@"
453
454 FILE=/etc/ctdb/nodes
455 test -f ${FILE} || touch ${FILE}
456 cp -f -a ${FILE} ${FILE}${BACKUP_SUFFIX}
457
458 echo -n > ${FILE}
459 for IP in ${NODES_IPS}
460 do
461   echo "$IP" >> ${FILE}
462 done
463 SCRIPT
464
465 CTDB_CREATE_PUBADDRS_SCRIPT = <<SCRIPT
466 set -e
467
468 BACKUP_SUFFIX=".orig.$(date +%Y%m%d-%H%M%S)"
469
470 PUB_IPS="$@"
471
472 FILE=/etc/ctdb/public_addresses
473 test -f ${FILE} || touch ${FILE}
474 cp -f -a ${FILE} ${FILE}${BACKUP_SUFFIX}
475
476 echo -n > ${FILE}
477 for IP in ${PUB_IPS}
478 do
479   echo ${IP} >> ${FILE}
480 done
481 SCRIPT
482
483 CTDB_CREATE_CONF_SCRIPT = <<SCRIPT
484 set -e
485
486 BACKUP_SUFFIX=".orig.$(date +%Y%m%d-%H%M%S)"
487
488 RECLOCKDIR=/gluster/gv0/ctdb
489 mkdir -p ${RECLOCKDIR}
490 RECLOCKFILE=${RECLOCKDIR}/reclock
491
492 PUBLIC_ADDRESSES_FILE=/etc/ctdb/public_addresses
493 NODES_FILE=/etc/ctdb/nodes
494
495 FILE=/etc/sysconfig/ctdb
496 test -f ${FILE} || touch ${FILE}
497 cp -f -a ${FILE} ${FILE}${BACKUP_SUFFIX}
498
499 echo -n > ${FILE}
500 cat <<EOF >> ${FILE}
501 CTDB_NODES=${NODES_FILE}
502 #CTDB_PUBLIC_ADDRESSES=${PUBLIC_ADDRESSES_FILE}
503 CTDB_RECOVERY_LOCK=${RECLOCKFILE}
504 CTDB_MANAGES_SAMBA="yes"
505 CTDB_SAMBA_SKIP_SHARE_CHECK="yes"
506 #CTDB_MANAGES_WINBIND="yes"
507 EOF
508 SCRIPT
509
510 SAMBA_CREATE_CONF_SCRIPT = <<SCRIPT
511 set -e
512
513 BACKUP_SUFFIX=".orig.$(date +%Y%m%d-%H%M%S)"
514
515 GLUSTER_VOL=$1
516
517 GLUSTER_VOL_MOUNT=$2
518
519 mkdir -p ${GLUSTER_VOL_MOUNT}/share1
520 chmod -R 0777 ${GLUSTER_VOL_MOUNT}/share1
521
522 mkdir -p ${GLUSTER_VOL_MOUNT}/share2
523 chmod -R 0777 ${GLUSTER_VOL_MOUNT}/share2
524
525 FILE=/etc/samba/smb.conf
526 test -f ${FILE} || touch ${FILE}
527 cp -f -a ${FILE} ${FILE}${BACKUP_SUFFIX}
528
529 echo -n > ${FILE}
530 cat <<EOF >> ${FILE}
531 [global]
532     netbios name = sambacluster
533     workgroup = vagrant
534     security = user
535
536     clustering = yes
537     #include = registry
538
539 [share1]
540     path = /share1
541     vfs objects = acl_xattr glusterfs
542     glusterfs:volume = ${GLUSTER_VOL}
543     kernel share modes = no
544     read only = no
545
546 [share2]
547     path = ${GLUSTER_VOL_MOUNT}/share2
548     vfs objects = acl_xattr
549     read only = no
550 EOF
551 SCRIPT
552
553 #
554 # The vagrant machine definitions
555 #
556
557 Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
558
559   config.vm.synced_folder ".", "/vagrant", disabled: true
560
561   #if Vagrant.has_plugin?("vagrant-cachier")
562   #  config.cache.scope = :machine
563   #  #config.cache.scope = :box
564
565   #  config.cache.synced_folder_opts = {
566   #    type: :nfs,
567   #    # The nolock option can be useful for an NFSv3 client that wants to avoid the
568   #    # NLM sideband protocol. Without this option, apt-get might hang if it tries
569   #    # to lock files needed for /var/cache/* operations. All of this can be avoided
570   #    # by using NFSv4 everywhere. Please note that the tcp option is not the default.
571   #    #mount_options: ['rw', 'vers=3', 'tcp', 'nolock']
572   #  }
573   #end
574
575   # just let one node do the probing
576   probing = false
577
578   vms.each do |machine|
579     config.vm.define machine[:hostname] do |node|
580       node.vm.box = machine[:provider][:libvirt][:box]
581       node.vm.hostname = machine[:hostname]
582
583       node.vm.provider :libvirt do |libvirt|
584         libvirt.default_prefix = machine[:provider][:libvirt][:prefix]
585         libvirt.memory = 1024
586         libvirt.storage :file, :size => '64M', :device => 'vdb'
587         libvirt.storage :file, :size => '10G', :device => 'vdc'
588
589         machine[:networks].each do |net|
590           if not net[:ipv4] == ''
591             node.vm.network :private_network, :ip => net[:ipv4]
592           end
593         end
594       end
595
596
597       node.vm.provision "selinux", type: "shell" do |s|
598         s.path = "provision/shell/sys/selinux-off.sh"
599       end
600
601       # There is some problem with the fedora base box:
602       # Upon first boot, ifdown eth1 fails and the dhclient
603       # keep being active. Simply bringing down and up again
604       # the interface is not sufficient. We need to restart
605       # NetworkManager in order to teach it to not feel
606       # responsible for the interface any more.
607       ###node.vm.provision "net_fix_initial", type: "shell" do |s|
608       ###  s.inline = NET_FIX_INITIAL_SCRIPT
609       ###end
610
611       node.vm.provision "install", type: "shell" do |s|
612         s.path = "provision/shell/sys/install-yum.sh"
613         s.args = [ "xfsprogs",
614                    "glusterfs",
615                    "glusterfs-server",
616                    "glusterfs-fuse",
617                    "glusterfs-geo-replication",
618                    "ctdb",
619                    "samba",
620                    "samba-client",
621                    "samba-vfs-glusterfs" ]
622       end
623
624       # There is some problem with the fedora base box:
625       # We need to up the interface on reboots.
626       # It does not come up automatically.
627       ###node.vm.provision "net_fix_always", type: "shell", run: "always" do |s|
628       ###  s.inline = NET_FIX_ALWAYS_SCRIPT
629       ###  s.args = [ '/gluster/gv0', '/gluster/gv1' ]
630       ###end
631
632       # multiple privisioners with same name possible?
633       node.vm.provision "xfs_0", type: "shell" do |s|
634         s.inline = XFS_SCRIPT
635         s.args = [ "vdb", "/export" ]
636       end
637
638       node.vm.provision "xfs_1", type: "shell" do |s|
639         s.inline = XFS_SCRIPT
640         s.args = [ "vdc", "/export" ]
641       end
642
643       node.vm.provision "gluster_start", type: "shell" do |s|
644         s.path = "provision/shell/gluster/gluster-start.sh"
645       end
646
647       if !probing
648         probing = true
649         node.vm.provision "gluster_probe", type: "shell" do |s|
650           s.inline = GLUSTER_PROBE_SCRIPT
651           s.args = cluster_internal_ips
652         end
653         probing = false
654       end
655
656       node.vm.provision "gluster_wait_peers", type: "shell" do |s|
657         s.inline = GLUSTER_WAIT_PEERS_SCRIPT
658         s.args = [ cluster_internal_ips.length, 300]
659       end
660
661       node.vm.provision "gluster_createvol_0", type: "shell" do |s|
662         mount_points = cluster_internal_ips.map do |ip|
663           "#{ip}:/export/vdb1/brick"
664         end
665         s.inline = GLUSTER_CREATEVOL_SCRIPT
666         s.args = [ "gv0", "3" ] + mount_points
667       end
668
669       node.vm.provision "gluster_mount_0", type: "shell" do |s|
670         s.inline = GLUSTER_MOUNT_SCRIPT
671         s.args = [ "gv0", "/gluster/gv0" ]
672       end
673
674       node.vm.provision "gluster_createvol_1", type: "shell" do |s|
675         mount_points = cluster_internal_ips.map do |ip|
676           "#{ip}:/export/vdc1/brick"
677         end
678         s.inline = GLUSTER_CREATEVOL_SCRIPT
679         s.args = [ "gv1", "3" ] + mount_points
680       end
681
682       node.vm.provision "gluster_mount_1", type: "shell" do |s|
683         s.inline = GLUSTER_MOUNT_SCRIPT
684         s.args = [ "gv1", "/gluster/gv1" ]
685       end
686
687       #
688       # ctdb / samba config
689       #
690
691       node.vm.provision "ctdb_stop", type: "shell" do |s|
692         s.path = "provision/shell/ctdb/ctdb-stop.sh"
693       end
694
695       node.vm.provision "ctdb_create_nodes", type: "shell" do |s|
696         s.inline = CTDB_CREATE_NODES_SCRIPT
697         s.args = cluster_internal_ips
698       end
699
700       #node.vm.provision "ctdb_create_pubaddrs", type: "shell" do |s|
701       #  s.inline = CTDB_CREATE_PUBADDRS_SCRIPT
702       #  s.arg =
703       #end
704
705       node.vm.provision "ctdb_create_conf", type: "shell" do |s|
706         s.inline = CTDB_CREATE_CONF_SCRIPT
707       end
708
709       node.vm.provision "samba_create_conf", type: "shell" do |s|
710         s.inline = SAMBA_CREATE_CONF_SCRIPT
711         s.args = [ "gv1", "/gluster/gv1" ]
712       end
713
714       node.vm.provision "ctdb_start", type: "shell" do |s|
715         s.path = "provision/shell/ctdb/ctdb-start.sh"
716       end
717
718     end
719   end
720
721 end