Remove commented out unused stuff
[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
154 NET_FIX_ALWAYS_SCRIPT = <<SCRIPT
155 set -e
156
157 # eth1 is not brought up automatically
158 # by 'vagrant up' of the existing vm.
159 # because eth1 is not up, glusterd can
160 # not be started and gluster volumes can
161 # not be mounted. fix it all up here until
162 # we have a correctly working environment.
163 ifdown eth1
164 ifup eth1
165
166 MOUNTPTS="$@"
167
168 for MOUNTPT in $MOUNTPTS
169 do
170   grep -q -s "${MOUNTPT}" /etc/fstab && {
171     # already provisioned...
172     systemctl start glusterd
173     # sleep to give glusterd some time to start up
174     sleep 2
175
176     mount | grep -q -s "${MOUNTPT}" && {
177       echo "${MOUNTPT} is already mounted."
178     } || {
179       echo "Mounting ${MOUNTPT}."
180       mount ${MOUNTPT}
181     }
182
183     systemctl start ctdb
184   } || {
185     # not provisioned yet
186     echo "${MOUNTPT} not set up yet. Not mounting."
187   }
188 done
189
190 SCRIPT
191
192 NET_FIX_INITIAL_SCRIPT = <<SCRIPT
193 set -e
194 # Fix dhclient running on private network IF
195 ifdown eth1
196 systemctl restart NetworkManager
197 ifdown eth1
198 ifup eth1
199 SCRIPT
200
201
202 SAMBA_CREATE_CONF_SCRIPT = <<SCRIPT
203 set -e
204
205 BACKUP_SUFFIX=".orig.$(date +%Y%m%d-%H%M%S)"
206
207 GLUSTER_VOL=$1
208
209 GLUSTER_VOL_MOUNT=$2
210
211 mkdir -p ${GLUSTER_VOL_MOUNT}/share1
212 chmod -R 0777 ${GLUSTER_VOL_MOUNT}/share1
213
214 mkdir -p ${GLUSTER_VOL_MOUNT}/share2
215 chmod -R 0777 ${GLUSTER_VOL_MOUNT}/share2
216
217 FILE=/etc/samba/smb.conf
218 test -f ${FILE} || touch ${FILE}
219 cp -f -a ${FILE} ${FILE}${BACKUP_SUFFIX}
220
221 echo -n > ${FILE}
222 cat <<EOF >> ${FILE}
223 [global]
224     netbios name = sambacluster
225     workgroup = vagrant
226     security = user
227
228     clustering = yes
229     #include = registry
230
231 [share1]
232     path = /share1
233     vfs objects = acl_xattr glusterfs
234     glusterfs:volume = ${GLUSTER_VOL}
235     kernel share modes = no
236     read only = no
237
238 [share2]
239     path = ${GLUSTER_VOL_MOUNT}/share2
240     vfs objects = acl_xattr
241     read only = no
242 EOF
243 SCRIPT
244
245 #
246 # The vagrant machine definitions
247 #
248
249 Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
250
251   config.vm.synced_folder ".", "/vagrant", disabled: true
252
253   #if Vagrant.has_plugin?("vagrant-cachier")
254   #  config.cache.scope = :machine
255   #  #config.cache.scope = :box
256
257   #  config.cache.synced_folder_opts = {
258   #    type: :nfs,
259   #    # The nolock option can be useful for an NFSv3 client that wants to avoid the
260   #    # NLM sideband protocol. Without this option, apt-get might hang if it tries
261   #    # to lock files needed for /var/cache/* operations. All of this can be avoided
262   #    # by using NFSv4 everywhere. Please note that the tcp option is not the default.
263   #    #mount_options: ['rw', 'vers=3', 'tcp', 'nolock']
264   #  }
265   #end
266
267   # just let one node do the probing
268   probing = false
269
270   vms.each do |machine|
271     config.vm.define machine[:hostname] do |node|
272       node.vm.box = machine[:provider][:libvirt][:box]
273       node.vm.hostname = machine[:hostname]
274
275       node.vm.provider :libvirt do |libvirt|
276         libvirt.default_prefix = machine[:provider][:libvirt][:prefix]
277         libvirt.memory = 1024
278         libvirt.storage :file, :size => '64M', :device => 'vdb'
279         libvirt.storage :file, :size => '10G', :device => 'vdc'
280
281         machine[:networks].each do |net|
282           if not net[:ipv4] == ''
283             node.vm.network :private_network, :ip => net[:ipv4]
284           end
285         end
286       end
287
288
289       node.vm.provision "selinux", type: "shell" do |s|
290         s.path = "provision/shell/sys/selinux-off.sh"
291       end
292
293       # There is some problem with the fedora base box:
294       # Upon first boot, ifdown eth1 fails and the dhclient
295       # keep being active. Simply bringing down and up again
296       # the interface is not sufficient. We need to restart
297       # NetworkManager in order to teach it to not feel
298       # responsible for the interface any more.
299       ###node.vm.provision "net_fix_initial", type: "shell" do |s|
300       ###  s.inline = NET_FIX_INITIAL_SCRIPT
301       ###end
302
303       node.vm.provision "install", type: "shell" do |s|
304         s.path = "provision/shell/sys/install-yum.sh"
305         s.args = [ "xfsprogs",
306                    "glusterfs",
307                    "glusterfs-server",
308                    "glusterfs-fuse",
309                    "glusterfs-geo-replication",
310                    "ctdb",
311                    "samba",
312                    "samba-client",
313                    "samba-vfs-glusterfs" ]
314       end
315
316       # There is some problem with the fedora base box:
317       # We need to up the interface on reboots.
318       # It does not come up automatically.
319       ###node.vm.provision "net_fix_always", type: "shell", run: "always" do |s|
320       ###  s.inline = NET_FIX_ALWAYS_SCRIPT
321       ###  s.args = [ '/gluster/gv0', '/gluster/gv1' ]
322       ###end
323
324       # multiple privisioners with same name possible?
325       node.vm.provision "xfs_0", type: "shell" do |s|
326         s.path = "provision/shell/gluster/create-brick.sh"
327         s.args = [ "vdb", "/export" ]
328       end
329
330       node.vm.provision "xfs_1", type: "shell" do |s|
331         s.path = "provision/shell/gluster/create-brick.sh"
332         s.args = [ "vdc", "/export" ]
333       end
334
335       node.vm.provision "gluster_start", type: "shell" do |s|
336         s.path = "provision/shell/gluster/gluster-start.sh"
337       end
338
339       if !probing
340         probing = true
341         node.vm.provision "gluster_probe", type: "shell" do |s|
342           s.path = "provision/shell/gluster/gluster-probe.sh"
343           s.args = cluster_internal_ips
344         end
345         probing = false
346       end
347
348       node.vm.provision "gluster_wait_peers", type: "shell" do |s|
349         s.path = "provision/shell/gluster/gluster-wait-peers.sh"
350         s.args = [ cluster_internal_ips.length, 300]
351       end
352
353       node.vm.provision "gluster_createvol_0", type: "shell" do |s|
354         mount_points = cluster_internal_ips.map do |ip|
355           "#{ip}:/export/vdb1/brick"
356         end
357         s.path = "provision/shell/gluster/gluster-create-volume.sh"
358         s.args = [ "gv0", "3" ] + mount_points
359       end
360
361       node.vm.provision "gluster_mount_0", type: "shell" do |s|
362         s.path = "provision/shell/gluster/gluster-mount-volume.sh"
363         s.args = [ "gv0", "/gluster/gv0" ]
364       end
365
366       node.vm.provision "gluster_createvol_1", type: "shell" do |s|
367         mount_points = cluster_internal_ips.map do |ip|
368           "#{ip}:/export/vdc1/brick"
369         end
370         s.path = "provision/shell/gluster/gluster-create-volume.sh"
371         s.args = [ "gv1", "3" ] + mount_points
372       end
373
374       node.vm.provision "gluster_mount_1", type: "shell" do |s|
375         s.path = "provision/shell/gluster/gluster-mount-volume.sh"
376         s.args = [ "gv1", "/gluster/gv1" ]
377       end
378
379       #
380       # ctdb / samba config
381       #
382
383       node.vm.provision "ctdb_stop", type: "shell" do |s|
384         s.path = "provision/shell/ctdb/ctdb-stop.sh"
385       end
386
387       node.vm.provision "ctdb_create_nodes", type: "shell" do |s|
388         s.path = "provision/shell/ctdb/ctdb-create-nodes.sh"
389         s.args = cluster_internal_ips
390       end
391
392       #node.vm.provision "ctdb_create_pubaddrs", type: "shell" do |s|
393       #  s.path = "provision/shell/ctdb/ctdb-create-pubaddrs.sh"
394       #  s.arg =
395       #end
396
397       node.vm.provision "ctdb_create_conf", type: "shell" do |s|
398         s.path = "provision/shell/ctdb/ctdb-create-conf.sh"
399         s.args = [ "/gluster/gv0/ctdb" ]
400       end
401
402       node.vm.provision "samba_create_conf", type: "shell" do |s|
403         s.inline = SAMBA_CREATE_CONF_SCRIPT
404         s.args = [ "gv1", "/gluster/gv1" ]
405       end
406
407       node.vm.provision "ctdb_start", type: "shell" do |s|
408         s.path = "provision/shell/ctdb/ctdb-start.sh"
409       end
410
411     end
412   end
413
414 end