-
Notifications
You must be signed in to change notification settings - Fork 3
/
create_basic_cluster.pl
executable file
·357 lines (271 loc) · 14.7 KB
/
create_basic_cluster.pl
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
#!/usr/bin/perl
use strict;
use Getopt::Long;
use File::Temp qw/ tempfile tempdir /;
use FindBin qw($Bin);
my $Bin_f = $Bin;
$Bin_f =~ s/\//\\\//g;
# default values
my $master_ip = "158.227.106.100";
my $master_port = "3333";
my $boss_ip = "192.168.122.100";
my $boss_name = "bossvm";
my $worker_ip = "192.168.122.101";
my $worker_name = "workervm0";
my $gw_ip = "192.168.122.1";
my $boss_mac = createMac();
my $worker_mac = createMac();
my $run_vms = 0;
my $disable_dbpedia = 0;
my $help;
my $command;
# tmp dir
my $tmpdir = File::Temp->newdir( DIR => "/tmp" );
usage() if (!GetOptions('help|?' => \$help, 'master-ip=s'=> \$master_ip, 'master-port=s'=> \$master_port, 'boss-ip=s' => \$boss_ip, 'boss-name=s' => \$boss_name, 'worker-ip=s' => \$worker_ip, 'worker-name=s' => \$worker_name, 'gw-ip=s' => \$gw_ip, 'run' => \$run_vms, 'disable-dbpedia' => \$disable_dbpedia) or defined $help);
sub usage
{
print "Unknown option: @_\n" if ( @_ );
print "Usage:\n";
print " create_basic_cluster.pl [--options]\n";
print "Options:\n";
print " --help\t\t\tList available options\n";
print " --master-ip MASTER_IP\t\tIP Address for master (default: 158.227.106.100)\n";
print " --master-port MASTER_PORT\tPort for master rsync service (default: 3333)\n";
print " --boss-ip BOSS_IP\t\tIP Address for boss (default: 192.168.122.100)\n";
print " --boss-name BOSS_NAME\t\tName for boss (default: bossvm)\n";
print " --worker-ip WORKER_IP\t\tIP Address for worker (default: 192.168.122.101)\n";
print " --worker-name WORKER_NAME\tName for worker (default: workervm0)\n";
print " --gw-ip GATEWAY_IP\t\tIP Address for gateway (default: 192.168.122.1)\n";
print " --disable-dbpedia\t\tDon't run dbpedia-spotlight on workers.\n";
print " --run\t\t\t\tStart basic cluster with 'virsh create'\n";
exit;
}
if ($boss_ip !~ /([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/ || $worker_ip !~ /([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/ || $gw_ip !~ /([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/) {
finish ("Wrong IP address.");
}
unless (-d $Bin."/img") { runCommand("mkdir ".$Bin."/img");}
unless (-d $Bin."/nodes") { runCommand("mkdir ".$Bin."/nodes");}
unless (-f $Bin."/img/base.img") {
print "Download base.img...\n";
runCommand("wget -P ".$Bin."/img http://ixa2.si.ehu.es/newsreader_storm_resources/base.img");
runCommand("wget -P ".$Bin."/img http://ixa2.si.ehu.es/newsreader_storm_resources/base_img_ssh_rsa_key.txt");
}
checkDeps();
createHostsFile();
createKnownHostsFile();
createPuppetFiles();
createNetCfgFiles();
createScripts();
# create boss VM
print "Creating bossvm-> NAME: ".$boss_name." IPADDR: ".$boss_ip."\n";
createBossVM();
# create worker VMs
print "Creating workervm-> NAME: ".$worker_name." IPADDR: ".$worker_ip."\n";
createWorkerVM();
# run VMs?
if ($run_vms) {
print "Starting VMs...\n\n";
runCommand("virsh create ".$Bin."/nodes/".$boss_name.".xml");
runCommand("virsh create ".$Bin."/nodes/".$worker_name.".xml");
print "Now you can log into ".$boss_ip." (".$boss_name.") as root and run /root/init_system.sh\n\n";
} else {
print "Now you can run the VMs with these commands:\n";
print "virsh create ".$Bin."/nodes/".$boss_name.".xml \n";
print "virsh create ".$Bin."/nodes/".$worker_name.".xml \n";
}
sub createBossVM {
# prepare def
my $uuid = `uuidgen`;
chomp $uuid;
$uuid =~ s/\-/\\\-/g;
runCommand("cp ".$Bin."/templates/vmdef/def.xml ".$Bin."/nodes/".$boss_name.".xml");
runCommand("sed -i 's/_VM_NAME_/".$boss_name."/g' ".$Bin."/nodes/".$boss_name.".xml");
runCommand("sed -i 's/_UUID_/".$uuid."/g' ".$Bin."/nodes/".$boss_name.".xml");
runCommand("sed -i 's/_IMG_PATH_/".$Bin_f."\\/nodes\\/".$boss_name.".img/g' ".$Bin."/nodes/".$boss_name.".xml");
runCommand("sed -i 's/_MACADDR_/".$boss_mac."/g' ".$Bin."/nodes/".$boss_name.".xml");
# prepare img
runCommand("cp ".$Bin."/img/base.img ".$Bin."/nodes/".$boss_name.".img");
runCommand("guestfish -a ".$Bin."/nodes/".$boss_name.".img -i rm /etc/udev/rules.d/70-persistent-net.rules");
runCommand("virt-copy-in -a ".$Bin."/nodes/".$boss_name.".img ".$tmpdir."/ifcfg-eth0.".$boss_name." /etc/sysconfig/network-scripts/");
runCommand("guestfish -a ".$Bin."/nodes/".$boss_name.".img -i mv /etc/sysconfig/network-scripts/ifcfg-eth0.".$boss_name." /etc/sysconfig/network-scripts/ifcfg-eth0");
runCommand("virt-copy-in -a ".$Bin."/nodes/".$boss_name.".img ".$tmpdir."/network.".$boss_name." /etc/sysconfig/");
runCommand("guestfish -a ".$Bin."/nodes/".$boss_name.".img -i mv /etc/sysconfig/network.".$boss_name." /etc/sysconfig/network");
runCommand("virt-copy-in -a ".$Bin."/nodes/".$boss_name.".img ".$tmpdir."/hosts /etc/");
runCommand("virt-copy-in -a ".$Bin."/nodes/".$boss_name.".img ".$tmpdir."/known_hosts /root/.ssh");
runCommand("virt-copy-in -a ".$Bin."/nodes/".$boss_name.".img ".$tmpdir."/known_hosts /home/newsreader/.ssh");
runCommand("guestfish -a ".$Bin."/nodes/".$boss_name.".img -i command '/bin/chown 500:500 /home/newsreader/.ssh/known_hosts'");
runCommand("virt-copy-in -a ".$Bin."/nodes/".$boss_name.".img ".$tmpdir."/network.".$boss_name." /etc/sysconfig/");
runCommand("guestfish -a ".$Bin."/nodes/".$boss_name.".img -i mv /etc/sysconfig/network.".$boss_name." /etc/sysconfig/network");
runCommand("virt-copy-in -a ".$Bin."/nodes/".$boss_name.".img ".$Bin."/templates/various/ntp.conf /etc");
# copy puppet files and chkconfig puppetmaster
runCommand("virt-copy-in -a ".$Bin."/nodes/".$boss_name.".img ".$tmpdir."/puppet.conf ".$tmpdir."/fileserver.conf ".$tmpdir."/conf_files ".$tmpdir."/manifests /etc/puppet/");
runCommand("virt-copy-in -a ".$Bin."/nodes/".$boss_name.".img ".$tmpdir."/hosts /etc/puppet/conf_files/");
runCommand("guestfish -a ".$Bin."/nodes/".$boss_name.".img -i command '/sbin/chkconfig puppetmaster on'");
runCommand("guestfish -a ".$Bin."/nodes/".$boss_name.".img -i command '/sbin/chkconfig mongod on'");
runCommand("guestfish -a ".$Bin."/nodes/".$boss_name.".img -i command '/sbin/chkconfig ntpd on'");
# copy scripts
runCommand("virt-copy-in -a ".$Bin."/nodes/".$boss_name.".img ".$tmpdir."/update_nlp_components_boss.sh /home/newsreader");
runCommand("virt-copy-in -a ".$Bin."/nodes/".$boss_name.".img ".$Bin."/templates/conf_files/master_rsync_secret /etc");
runCommand("virt-copy-in -a ".$Bin."/nodes/".$boss_name.".img ".$tmpdir."/init_system.sh /root/");
runCommand("virt-copy-in -a ".$Bin."/nodes/".$boss_name.".img ".$Bin."/templates/scripts/upload_file_to_queue.php /var/www/html");
}
sub createWorkerVM {
my $nodename = $worker_name;
# prepare def
my $uuid = `uuidgen`;
chomp $uuid;
$uuid =~ s/\-/\\\-/g;
runCommand("cp ".$Bin."/templates/vmdef/def.xml ".$Bin."/nodes/".$nodename.".xml");
runCommand("sed -i 's/_VM_NAME_/".$nodename."/g' ".$Bin."/nodes/".$nodename.".xml");
runCommand("sed -i 's/_UUID_/".$uuid."/g' ".$Bin."/nodes/".$nodename.".xml");
runCommand("sed -i 's/_IMG_PATH_/".$Bin_f."\\/nodes\\/".$nodename.".img/g' ".$Bin."/nodes/".$nodename.".xml");
runCommand("sed -i 's/_MACADDR_/".$worker_mac."/g' ".$Bin."/nodes/".$nodename.".xml");
# prepare img
runCommand("cp ".$Bin."/img/base.img ".$Bin."/nodes/".$nodename.".img");
runCommand("guestfish -a ".$Bin."/nodes/".$nodename.".img -i rm /etc/udev/rules.d/70-persistent-net.rules");
runCommand("virt-copy-in -a ".$Bin."/nodes/".$nodename.".img ".$tmpdir."/ifcfg-eth0.".$nodename." /etc/sysconfig/network-scripts/");
runCommand("guestfish -a ".$Bin."/nodes/".$nodename.".img -i mv /etc/sysconfig/network-scripts/ifcfg-eth0.".$nodename." /etc/sysconfig/network-scripts/ifcfg-eth0");
runCommand("virt-copy-in -a ".$Bin."/nodes/".$nodename.".img ".$tmpdir."/network.".$nodename." /etc/sysconfig/");
runCommand("guestfish -a ".$Bin."/nodes/".$nodename.".img -i mv /etc/sysconfig/network.".$nodename." /etc/sysconfig/network");
runCommand("virt-copy-in -a ".$Bin."/nodes/".$nodename.".img ".$tmpdir."/hosts /etc/");
runCommand("virt-copy-in -a ".$Bin."/nodes/".$nodename.".img ".$tmpdir."/known_hosts /root/.ssh");
runCommand("virt-copy-in -a ".$Bin."/nodes/".$nodename.".img ".$tmpdir."/known_hosts /home/newsreader/.ssh");
runCommand("guestfish -a ".$Bin."/nodes/".$nodename.".img -i command '/bin/chown 500:500 /home/newsreader/.ssh/known_hosts'");
runCommand("virt-copy-in -a ".$Bin."/nodes/".$nodename.".img ".$tmpdir."/ntp.conf /etc");
# copy puppet.conf file
runCommand("virt-copy-in -a ".$Bin."/nodes/".$nodename.".img ".$tmpdir."/puppet.conf /etc/puppet/");
# ntpd / Don't activate we'll sync time in workers using puppet
# runCommand("guestfish -a ".$Bin."/nodes/".$nodename.".img -i command '/sbin/chkconfig ntpd on'");
# copy scripts
runCommand("virt-copy-in -a ".$Bin."/nodes/".$nodename.".img ".$tmpdir."/update_nlp_components_worker.sh /home/newsreader");
}
sub createHostsFile {
open HFILE, ">".$tmpdir."/hosts" or finish("ERROR: Cannot create ".$tmpdir."/hosts");
print HFILE "127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4\n";
print HFILE "$boss_ip $boss_name\n";
print HFILE "$worker_ip $worker_name\n";
close HFILE;
}
sub createKnownHostsFile {
my $rsakey = "";
open RFILE, "<".$Bin."/img/base_img_ssh_rsa_key.txt" or finish("ERROR: Cannot read ".$Bin."/img/base_img_ssh_rsa_key.txt");
$rsakey = do { local $/; <RFILE> };
close RFILE;
chomp $rsakey;
open HFILE, ">".$tmpdir."/known_hosts" or finish("ERROR: Cannot create ".$tmpdir."/known_hosts");
print HFILE "$master_ip $rsakey\n";
print HFILE "$boss_ip,$boss_name $rsakey\n";
print HFILE "$worker_ip,$worker_name $rsakey\n";
close HFILE;
}
sub createPuppetFiles {
# puppet main conf
open PFILE, ">".$tmpdir."/puppet.conf" or finish("ERROR: Cannot create ".$tmpdir."/puppet.conf");
print PFILE "[main]\n";
print PFILE " logdir = /var/log/puppet\n";
print PFILE " rundir = /var/run/puppet\n";
print PFILE " ssldir = \$vardir/ssl\n";
print PFILE " server=$boss_name\n";
print PFILE " runinterval=3600\n";
print PFILE "[master]\n";
print PFILE " autosign = true\n";
print PFILE "[agent]\n";
print PFILE " classfile = \$vardir/classes.txt\n";
print PFILE " localconfig = \$vardir/localconfig\n";
close PFILE;
# file server conf for master
open PFILE, ">".$tmpdir."/fileserver.conf" or finish("ERROR: Cannot create ".$tmpdir."/fileserver.conf");
print PFILE "[conf_files]\n";
print PFILE "path /etc/puppet/conf_files\n";
print PFILE "allow *\n";
close PFILE;
#
# puppet manifests
#
runCommand ("cp -R ".$Bin."/templates/puppet_manifests ".$tmpdir."/manifests");
# main manifest for master (site.pp)
runCommand("sed -i 's/_BOSS_NAME_/".$boss_name."/g' ".$tmpdir."/manifests/site.pp");
#
# diverse conf files
#
runCommand("cp -R ".$Bin."/templates/conf_files ".$tmpdir."/");
if ($disable_dbpedia == 1) {
runCommand("mv ".$tmpdir."/conf_files/worker_supervisord.conf ".$tmpdir."/conf_files/worker_supervisord.conf.dbpedia_enabled");
runCommand("mv ".$tmpdir."/conf_files/worker_supervisord.conf.dbpedia_disabled ".$tmpdir."/conf_files/worker_supervisord.conf");
}
# storm.conf
runCommand("sed -i 's/_BOSS_NAME_/".$boss_name."/g' ".$tmpdir."/conf_files/storm.boss.conf");
runCommand("sed -i 's/_BOSS_NAME_/".$boss_name."/g' ".$tmpdir."/conf_files/storm.worker.conf");
# isrunning_zookeeper.sh
runCommand("sed -i 's/_BOSS_NAME_/".$boss_name."/g' ".$tmpdir."/conf_files/isrunning_zookeeper.sh");
}
sub createNetCfgFiles {
open NFILE, ">".$tmpdir."/ifcfg-eth0.$boss_name" or finish("ERROR: Cannot create ".$tmpdir."/ifcfg-eth0.$boss_name");
print NFILE "DEVICE=eth0\n";
print NFILE "HWADDR=".$boss_mac."\n";
print NFILE "TYPE=Ethernet\n";
print NFILE "ONBOOT=yes\n";
print NFILE "NM_CONTROLLED=no\n";
print NFILE "BOOTPROTO=none\n";
print NFILE "IPADDR=$boss_ip\n";
print NFILE "NETMASK=255.255.252.0\n";
print NFILE "GATEWAY=".$gw_ip."\n";
close NFILE;
open NFILE, ">".$tmpdir."/network.$boss_name" or finish("ERROR: Cannot create ".$tmpdir."/network.$boss_name");
print NFILE "HOSTNAME=$boss_name\n";
print NFILE "NETWORKING=yes\n";
close NFILE;
open NFILE, ">".$tmpdir."/ifcfg-eth0.$worker_name" or finish("ERROR: Cannot create ".$tmpdir."/ifcfg-eth0.$worker_name");
print NFILE "DEVICE=eth0\n";
print NFILE "HWADDR=".$worker_mac."\n";
print NFILE "TYPE=Ethernet\n";
print NFILE "ONBOOT=yes\n";
print NFILE "NM_CONTROLLED=no\n";
print NFILE "BOOTPROTO=none\n";
print NFILE "IPADDR=$worker_ip\n";
print NFILE "NETMASK=255.255.252.0\n";
print NFILE "GATEWAY=".$gw_ip."\n";
close NFILE;
open NFILE, ">".$tmpdir."/network.$worker_name" or finish("ERROR: Cannot create ".$tmpdir."/network.$worker_name");
print NFILE "HOSTNAME=$worker_name\n";
print NFILE "NETWORKING=yes\n";
close NFILE;
# worker ntp config
runCommand("cp ".$Bin."/templates/various/worker_ntp.conf ".$tmpdir."/ntp.conf");
runCommand("sed -i 's/_BOSS_NAME_/".$boss_name."/g' ".$tmpdir."/ntp.conf");
}
sub createScripts() {
# update_nlp_components_boss.sh
runCommand("cp ".$Bin."/templates/scripts/update_nlp_components_boss.sh ".$tmpdir."/update_nlp_components_boss.sh");
runCommand("sed -i 's/_MASTER_IP_/".$master_ip."/g' ".$tmpdir."/update_nlp_components_boss.sh");
runCommand("sed -i 's/_MASTER_PORT_/".$master_port."/g' ".$tmpdir."/update_nlp_components_boss.sh");
# update_nlp_components_worker.sh
runCommand( "cp ".$Bin."/templates/scripts/update_nlp_components_worker.sh ".$tmpdir."/update_nlp_components_worker.sh");
runCommand("sed -i 's/_BOSS_NAME_/".$boss_name."/g' ".$tmpdir."/update_nlp_components_worker.sh");
# init_system.sh
runCommand("cp ".$Bin."/templates/scripts/init_system.sh ".$tmpdir."/init_system.sh");
runCommand( "sed -i 's/_WORKER_NAME_/".$worker_name."/g' ".$tmpdir."/init_system.sh");
}
sub checkDeps {
if (!-f "/usr/bin/wget" || !-x "/usr/bin/wget") { finish("ERROR: We need /usr/bin/wget"); }
if (!-f "/usr/bin/virsh" || !-x "/usr/bin/virsh") { finish("ERROR: We need /usr/bin/virsh"); }
if (!-f "/usr/bin/guestfish" || !-x "/usr/bin/guestfish") { finish("ERROR: We need /usr/bin/guestfish"); }
if (!-f "/usr/bin/virt-copy-in" || !-x "/usr/bin/virt-copy-in") { finish("ERROR: We need /usr/bin/virt-copy-in"); }
}
sub createMac {
my $mac = "52:54:00:";
for (my $i=0;$i<3;$i++) {
$mac.=sprintf("%02X",int(rand(255))).(($i<2)?':':'');
}
return $mac;
}
sub finish {
my ($p) = @_;
print $p."\n";
exit;
}
sub runCommand {
my ($command) = @_;
system ($command) == 0
or finish("FAILED: ".$command);
}