forked from nadeau/lxc-wizard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlxc-conjure
executable file
·448 lines (337 loc) · 10.5 KB
/
lxc-conjure
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#!/usr/bin/perl -w
use strict;
use warnings;
use Storable 'dclone';
use File::Basename;
use File::Temp qw/ :POSIX /;
use File::Slurp;
use Getopt::Long;
use Parallel::Runner;
use Net::Ping;
use Data::Dumper;
############################################################
# #
# Copyright (C) 2013 Mate1 Inc. #
# #
############################################################
=head1 NAME
lxc-conjure - Create complete lxc environments easily.
=head1 DESCRIPTION
B<lxc-conjure> is a wrapper around lxc-wizard that makes creating
complete environments via spell (definition) files quick
and pretty painless.
=head1 DEPENDENCIES
cpan -i Parallel::Runner
cpan -i Number::Spell
apt-get install libtie-ixhash-perl libfile-slurp-perl libfile-slurp-unicode-perl libmojolicious-perl libdata-section-simple-perl
=cut
my ($help, $spell, $create, $start, $stop, $restart, $destroy, $info, $parallel);
GetOptions(
'help' => \$help,
'spell=s' => \$spell,
'create' => \$create,
'start' => \$start,
'stop' => \$stop,
'restart' => \$restart,
'destroy' => \$destroy,
'info' => \$info,
'parallel'=> \$parallel,
);
help() if $help;
sub help {
print <<EOF
Usage: $0 --spell <spell> <action>
--spell Spell (file) to get containers from.
Actions:
--create Create containers.
--start Start containers.
--stop Stop containers.
--restart Restart containers.
--destroy Destroy containers.
--info Get containers' information.
Spell enhancers:
--parallel Create containers in parallel across
different hosts.
EOF
;
exit;
}
# set to 0 to run for real
my $DRYRUN = 0;
# environment specific settings
# required, the domain name that the environment's servers will have
my $DOMAIN = "";
# required, the gateway address that the environment's servers will use
my $GATEWAY = "";
# required, the template that lxc-wizard will use (eg: debian/squeeze),
# can be overridden per container
my $TEMPLATE = "";
# location of the lxc-wizard
my $WIZ = "sudo -- lxc-wizard";
# if set then this user is used to ssh into
# the host machines
my $USER="root@";
# ssh key that will be transfered to containers
# more configuration follows to determine what
# user accounts the key will go into
my $KEY="";
# where the profiles (postcreate) scripts are found.
# for now there is a limitation that the scripts must
# be on every host. we'll copy them over with us later.
my $PROFILE_HOME = "";
# default settings that will be used for every container.
my $DEFAULTS = {};
# containers to create.
# anything passed in here will override
# the default options.
my $CONTAINERS = [];
die ("No spell specified or spell not found.") if (!defined($spell) || ! -f $spell);
my $conf = read_file($spell);
eval $conf;
warn $@ if $@;
if ($create && $parallel) {
# parallel creation
my $hosts = {};
for my $contoptions (@$CONTAINERS) {
my $options = hash_merge($DEFAULTS, $contoptions);
# TODO: this is duplicated further down, clean
# cleanups and sanity checks
$options->{container} = $options->{fqdn} if !$options->{container};
$options->{template} = $TEMPLATE if !$options->{template};
push @{ $hosts->{$options->{containerhost}} }, $options;
}
my $runner = Parallel::Runner->new(scalar keys %$hosts);
for my $host ( keys %$hosts ) {
my $containers = $hosts->{$host};
$runner->run( sub { wizard($_) foreach (@$containers) } );
}
$runner->finish();
} else {
# serial execution
CONT: foreach my $contoptions (@$CONTAINERS) {
my $options = hash_merge($DEFAULTS, $contoptions);
# TODO: this is duplicated above, clean
# cleanups and sanity checks
$options->{container} = $options->{fqdn} if !$options->{container};
$options->{template} = $TEMPLATE if !$options->{template};
next CONT if ($options->{ignore} && $options->{ignore} == 1);
if ($create) { wizard($options); }
elsif ($info) { info($options); }
elsif ($stop) { stop($options); }
elsif ($start) { start($options); }
elsif ($restart) { restart($options); }
elsif ($destroy) { destroy($options); }
}
}
sub sshrun {
my ($ipaddr, $user, $p, $args) = @_;
my $profile = "/tmp/" . $$ . "-" . basename($p);
system("scp $p $user$ipaddr:$profile");
system("ssh -t $user$ipaddr chmod +x $profile");
system("ssh -t $user$ipaddr $profile " . $args);
}
sub hash_merge {
my ($dest, $source) = @_;
my $hash = dclone $dest;
# source values overwrite dest values
foreach my $key ( keys %$source ) { $hash->{$key} = $source->{$key} }
return $hash;
}
sub lxc_command {
my $options = shift;
my $command = shift;
my $conthost = $options->{containerhost};
my $cmd = "ssh -t $USER$conthost -- lxc-$command -n " . $options->{container};
return $cmd;
}
sub start {
my $options = shift;
my $cmd = lxc_command($options, "info");
my $info = `$cmd`;
if ($info =~ /STOPPED/) {
$cmd = lxc_command($options, "start -d");
system($cmd);
} else {
print "Not starting $options->{container}, not in STOPPED state.\n";
}
}
sub stop {
my $cmd = lxc_command(shift, "stop");
system($cmd);
}
sub destroy {
my $options = shift;
# TODO: don't do this if it's not needed
stop($options);
sleep(2);
my $conthost = $options->{containerhost};
if ($options->{LVM} eq "Y") {
# destroy the LVM volume
my @vgs = `ssh -t $USER$conthost -- vgdisplay | grep Name`;
$vgs[0] =~ /Name\s+(\w+)/;
my $vgname = $1;
my $lvname = $options->{container};
my $volume = "/dev/mapper/$vgname-$lvname";
my $mountpoint = "/var/lib/lxc/$lvname";
print "Umounting Logical Volume...\n";
system("ssh -t $USER$conthost -- umount $mountpoint");
print "Removing Logical Volume...\n";
system("ssh -t $USER$conthost -- lvremove -f $volume");
print "Updating /etc/fstab...\n";
# delete the line containing a slash followed by the lvname
# for example: ... /var/lib/lxc/1.sender.tiamail.qa.blackhowler.gene ...
system("ssh -t $USER$conthost -- sed -i '/\\\\\\/$lvname/d' /etc/fstab");
}
system("ssh -t $USER$conthost -- rm -rf --preserve-root /var/lib/lxc/$options->{container}");
}
sub info {
my $options = shift;
my $cmd = lxc_command($options, "info");
print $options->{container} . ":\n";
system($cmd);
print "\n";
}
sub restart {
stop(@_);
sleep 3;
start(@_);
}
sub wizard {
my $options = shift;
my $conthost = $options->{containerhost};
# if we're passed a "user" option, check if it's an array or
# a scalar. if it's an array it should container user:pass entries
# otherwise it's a single user:pass entry.
my $users = "";
if ($options->{user}) {
if (ref($options->{user}) eq "ARRAY") {
for my $userpass (@{$options->{user}}) {
$users .= "--user $userpass " if ($userpass =~ /(.+):(.+)/);
}
} elsif (ref($options->{user}) eq "") {
$users .= "--user " . $options->{user}. " ";
}
}
my $postcreate = "";
my $keys = "";
# handle ssh keys if any
if ($options->{key}) {
if (ref($options->{key}) eq "ARRAY") {
# we're passed multiple keys
for my $userkey (@{$options->{key}}) {
# copy key over and change key file accordingly
my ($user, $keyfile) = split(/:/, $userkey);
my $key = tmpnam() . $$ . ".key";
system("scp $keyfile $USER$conthost:$key");
$keys .= "--key $user:$key ";
}
} elsif (ref($options->{key}) eq "") { # we're passed one key
# copy key over and change key file accordingly
my $userkey = $options->{key};
my ($user, $keyfile) = split(/:/, $userkey);
my $key = tmpnam() . $$ . ".key";
system("scp $keyfile $USER$conthost:$key");
$keys .= "--key $user:$key ";
}
}
my $search = "";
if ($options->{search}) {
if (ref($options->{search}) eq "ARRAY") {
for my $s (@{$options->{search}}) {
$search .= "--search $s ";
}
} elsif (ref($options->{search}) eq "") {
$users .= "--search " . $options->{search}. " ";
}
}
my $extras = "";
$extras = " -- " . $options->{"--"} if ($options->{"--"});
# generate the final command
my $cmd = <<EOF
ssh -t $USER$conthost -- $WIZ \\
--template $options->{template} \\
--fqdn $options->{fqdn} \\
--ipaddr $options->{ipaddr} \\
--gwaddr $options->{gwaddr} \\
--dnsdomain $options->{dnsdomain} \\
$search \\
--ns1 $options->{ns1} \\
--ns2 $options->{ns2} \\
--cpushares=$options->{cpushares} \\
--ioweight=$options->{ioweight} \\
--memory=$options->{memory} \\
--container $options->{container} \\
--SWAP=$options->{SWAP} \\
--LVM=$options->{LVM} \\
--lvsize=$options->{LVSize} \\
--auto \\
--rootpass $options->{rootpass} \\
$users \\
$keys \\
$extras
EOF
;
if ($DRYRUN) { print($cmd . "\n"); }
else {
system($cmd);
if ($options->{profile} || $options->{deployscript}) {
# wait for container to come up
my $pingobj = Net::Ping->new("tcp");
$pingobj->port_number(22);
while ( ! $pingobj->ping($options->{ipaddr}, 1) ) {
print ($options->{ipaddr} . " unreachable, wait more time\n");
sleep(2);
}
# if ($options->{startsleep}) { sleep ($options->{startsleep}) }
# else { sleep 30 };
if ($options->{profile}) {
print("Running profile(s)...\n");
# profiles can be scalars, in which case
# we will take "--" as their args.
# they can also be arrays, and have no args,
# or they can be hashes, like this:
# profile -> profile args
my $r = ref($options->{profile});
if ($r eq "ARRAY") {
for my $p (@{$options->{profile}}) {
my $NOARGS = "";
sshrun($options->{ipaddr}, $USER, "$PROFILE_HOME/$p", $NOARGS);
}
} elsif ($r eq "HASH") {
my $profiles = $options->{profile};
for my $p (keys %$profiles) {
my $args = $profiles->{$p};
sshrun($options->{ipaddr}, $USER, "$PROFILE_HOME/$p", $args);
}
} elsif ($r eq "") {
my $p = $options->{profile};
my $args = $options->{"--"} || "";
sshrun($options->{ipaddr}, $USER, "$PROFILE_HOME/$p", $args);
}
}
if ($options->{deployscript}) {
print("Running deployscript...\n");
my $depscript = $options->{deployscript};
my $r = ref($depscript);
if ($r eq "") {
# scalar, should be a string representing a script
system($depscript);
} elsif ($r eq "CODE") {
# code, run it
&$depscript();
} elsif ($r eq "ARRAY" && scalar($depscript) > 0) {
if (ref(@$depscript[0]) eq "CODE") {
# callback, run it and pass params if any
my $cb = @$depscript[0];
if (scalar($depscript) > 1) {
shift(@$depscript);
&$cb(@$depscript);
} else {
&$cb();
}
}
}
}
}
}
}