Debian Codegraph @

extras/devel-tools/devel-vagrant/Vagrantfile

raw
1 # -*- mode: ruby -*-
2 # vi: set ft=ruby :
3 #
4 # Author: Rajesh Joseph ([email protected])
5 # Author: Christopher Blum ([email protected])
6 #
7
8
9 #Variables
10 box_name = "gluster-dev-fedora"
11 box_url = "http://download.gluster.org/pub/gluster/glusterfs/vagrant/gluster-dev-fedora/boxes/gluster-dev-fedora.json"
12 node_count = 0
13 disk_count = -1
14 node_name = "Node"
15 ipbase="192.168.99."
16 source_path = "/source/glusterfs"
17 target_path = "/mnt/src"
18
19 if ARGV[0] == "up"
20 environment = open('vagrant_env.conf', 'w')
21
22 print "\n\e[1;37mEnter Node (or VM) Count? Default: 1 \e[32m"
23 while node_count < 1 or node_count > 99
24 node_count = $stdin.gets.strip.to_i
25 if node_count == 0 # The user pressed enter without input or we cannot parse the input to a number
26 node_count = 1
27 elsif node_count < 1
28 print "\e[31mWe need at least 1 VM ;) Try again \e[32m"
29 elsif node_count > 99
30 print "\e[31mWe don't support more than 99 VMs - Try again \e[32m"
31 end
32 end
33
34 print "\e[1;37mEnter per Node Disc (Brick) Count? Default: 2 \e[32m"
35
36 while disk_count < 1
37 disk_count = $stdin.gets.strip.to_i
38 if disk_count == 0 # The user pressed enter without input or we cannot parse the input to a number
39 disk_count = 2
40 elsif disk_count < 1
41 print "\e[31mWe need at least 1 disk ;) Try again \e[32m"
42 end
43 end
44
45 print "\e[1;37mEnter GlusterFS source location? Default: \"#{source_path}\" : \e[32m"
46 tmploc = $stdin.gets.strip.to_s
47 if tmploc != ""
48 source_path = "#{tmploc}"
49 end
50
51 environment.puts("# BEWARE: Do NOT modify ANY settings in here or your vagrant environment will be messed up")
52 environment.puts(node_count.to_s)
53 environment.puts(disk_count.to_s)
54 environment.puts(source_path)
55
56 print "\e[32m\nOK I will provision #{node_count} VMs for you and each one will have #{disk_count} disks for bricks\e[37m\n\n"
57 system "sleep 1"
58 else # So that we destroy and can connect to all VMs...
59 environment = open('vagrant_env.conf', 'r')
60
61 environment.readline # Skip the comment on top
62 node_count = environment.readline.to_i
63 disk_count = environment.readline.to_i
64 source_path = environment.readline.gsub(/\s+/, "")
65
66 if ARGV[0] != "ssh-config"
67 puts "Detected settings from previous vagrant up:"
68 puts " We deployed #{node_count} VMs with each #{disk_count} disks"
69 puts ""
70 end
71 end
72
73 environment.close
74
75 $ansivar = Hash.new{ |hash,key| hash[key] = [] }
76 $devnamecreated = false
77
78 #
79 # Function to create and attach disks
80 #
81 def attachDisks(numDisk, provider)
82 suffix = "bcdefghijklmn".split("")
83 for i in 1..numDisk.to_i
84 devname = "vd" + (suffix[i-1]).to_s
85 if $devnamecreated == false
86 $ansivar["device"].push "#{devname}"
87 end
88 provider.storage :file,
89 :size => '1G',
90 :device => "vd" + (suffix[i-1]).to_s,
91 :type => "qcow2",
92 :bus => "virtio",
93 :cache => "default"
94 end
95 $devnamecreated = true
96 end
97
98
99 $ansivar["src_path"].push "#{source_path}"
100 $ansivar["trg_path"].push "#{target_path}"
101
102 groups = Hash.new{ |hash,key| hash[key] = [] }
103
104 groups["origin"].push "#{node_name}1"
105 groups["all"].push "#{node_name}1"
106
107 (2..node_count).each do |num|
108 $ansivar["peer_nodes"].push "#{node_name}#{num}"
109 groups["all"].push "#{node_name}#{num}"
110 end
111
112 hostsFile = "\n"
113 (1..node_count).each do |num|
114 hostsFile += "#{ipbase}#{( 100 + num).to_s} #{node_name}#{num.to_s}\n"
115 end
116
117 Vagrant.configure("2") do |config|
118 (1..node_count).each do |num|
119 config.vm.define "#{node_name}#{num}" do |node|
120 ip_addr = "#{ipbase}#{(100 + num).to_s}"
121 node.vm.network "private_network", ip: "#{ip_addr}"
122 node.vm.box = box_name
123 node.vm.box_url = box_url
124 node.vm.hostname = "#{node_name}#{num}"
125 node.ssh.insert_key = false
126 node.vm.synced_folder "#{source_path}", "#{target_path}", type: "nfs"
127
128 # Define basic config for VM, memory, cpu, storage pool
129 node.vm.provider "libvirt" do |virt|
130 virt.storage_pool_name = "default"
131 virt.memory = 1024
132 virt.cpus = 1
133
134 attachDisks( disk_count, virt )
135 end
136
137 node.vm.post_up_message = "\e[37mBuilding of this VM is finished \n"
138 "You can access it now with: \n"
139 "vagrant ssh #{node_name}#{num.to_s}\n\n"
140 "#{target_path} directory in VM #{node_name}#{num.to_s}"
141 "is synced with Host machine. \nSo any changes done in this"
142 "directory will be reflected in the host machine as well\n"
143 "Beware of this when you delete content from this directory\e[32m"
144
145 node.vm.provision :shell, path: "bootstrap.sh"
146
147 node.vm.provision "shell", inline: <<-SHELL
148 echo '#{hostsFile}' | sudo tee -a /etc/hosts
149 SHELL
150
151 if num == node_count
152 # Let's provision
153 node.vm.provision "ansible" do |setup|
154 setup.verbose = "v"
155 setup.playbook = "ansible/setup.yml"
156 setup.limit = "all"
157 setup.sudo = "true"
158 setup.groups = groups
159 setup.extra_vars = $ansivar
160 end
161 end
162
163 end
164 end
165 end