/
gbrowse_attach_slaves.pl
164 lines (143 loc) · 4.77 KB
/
gbrowse_attach_slaves.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
#!/usr/bin/perl
use strict;
use constant RENDERFARM_CONF => '/srv/gbrowse/etc/renderfarm.conf';
use constant SNAPSHOT_MAP => '/srv/gbrowse/etc/species_volume_map.txt';
use constant IMAGE_MAP => '/srv/gbrowse/etc/ami_map.txt';
use constant SLAVE_SECURITY_GROUP => 'GBrowseSlave';
use constant MASTER_SECURITY_GROUP => 'GBrowseMaster';
use constant PORT_RANGE => '8100-8105';
# This is called on the master to launch instances.
# It discovers which species are mounted
# on the current instance, snapshots them, and
# attaches them to the slave(s).
# EC2_ACCESS_KEY and EC2_SECRET_KEY must be defined
$ENV{PYTHONPATH}='/usr/local/lib/python2.6/dist-packages';
my $SLAVE_COUNT = shift || 1;
my @mounts = get_species_mounts();
my $snapshot_map = get_snapshot_map(\@mounts);
my $ami_map = get_ami_map();
# set up the block devices that need to be attached
my @devices = map {"/dev/sd$_"} ('h'..'z');
my $i = 0;
my @block_args = ('-b','/dev/sdg=:0:true',map {
my $device = $devices[$i++];
my $species = $_->[1];
my $snapshot = $snapshot_map->{$species};
$snapshot ? ('-b',"${device}=${snapshot}:0:true") : (); # all these volumes are terminate-on-delete
} @mounts);
my $ami = $ami_map->{GBROWSE_SLAVE} or die "Couldn't look up current AMI for the slave";
my $key = get_keypair();
my $security = get_security_group();
my @command = ('euca-run-instances','-k',$key,'-g',$security,@block_args,'-t','t1.micro','-n',$SLAVE_COUNT,$ami);
# now get the IP addresses of these instances
my @instances;
open OUTPUT,'-|' or exec @command;
while (<OUTPUT>) {
chomp;
next unless /^INSTANCE\s+(\S+)/;
push @instances,$1;
}
close OUTPUT;
my %ips;
while (keys %ips < $SLAVE_COUNT) {
sleep 5;
chomp (my $output = `euca-describe-instances @instances`);
for my $line (split "\n",$output) {
next unless $line =~ /^INSTANCE/;
my @fields = split "\t",$line;
$ips{$fields[4]}++ if $fields[4];
}
}
open F,'>',RENDERFARM_CONF or die "Can't write ",RENDERFARM_CONF,": $!";
print F "renderfarm = 1\n";
print F "remote renderer =\n";
my ($low,$hi) = split /-/,PORT_RANGE;
foreach my $ip ('localhost',keys %ips) {
my $slaves = join ' ',map {"http://${ip}:$_"} ($low..$hi);
print F " $slaves\n";
}
close F;
system "sudo /etc/init.d/apache2 restart";
exit 0;
# sub get_snapshot_map {
# my %map;
# open F,SNAPSHOT_MAP or die "Can't open ",SNAPSHOT_MAP,": $!";
# while (<F>) {
# chomp;
# next if /^#/;
# my ($mount,$snap_id,$vol_label) = split /\s+/;
# $map{$mount} = [$snap_id,$vol_label];
# }
# close F;
# return \%map;
# }
sub get_snapshot_map {
my $mounts = shift;
my (%vol2snap,%mount2vol,%map);
chomp (my $instance = `curl -s http://169.254.169.254/latest/meta-data/instance-id`);
chomp (my $volumes = `euca-describe-volumes`);
# get volume and snapshot id for each mounted volume
for my $line (split "\n",$volumes) {
if ($line =~ /^VOLUME/) {
my ($vol,$snap) = (split /\s+/,$line)[1,3];
$vol2snap{$vol} = $snap;
} elsif ($line =~ /^ATTACHMENT/) {
next unless $line =~ /\s$instance\s/;
my ($vol,$mount) = (split /\s+/,$line)[1,3];
$mount2vol{$mount} = $vol;
}
}
for my $m (@$mounts) {
my ($device,$species) = @$m;
$device =~ s/\d+$//;
my $vol = $mount2vol{$device} or next;
my $snap = $vol2snap{$vol};
$map{$species} = $snap;
}
return \%map;
}
sub get_species_mounts {
my @mounts;
open F,'/proc/mounts' or die "Can't open /proc/mounts: $!";
while (<F>) {
chomp;
my ($dev,$mount_point,@etc) = split /\s+/;
next unless $mount_point =~ m!/srv/gbrowse/species/([^/]+)!;
push @mounts,[$dev,$1];
}
close F;
return @mounts;
}
sub get_ami_map {
my %map;
open F,IMAGE_MAP or die "Can't open ",IMAGE_MAP,": $!";
while (<F>) {
chomp;
next if /^#/;
my ($role,$ami) = split /\s+/;
$map{$role} = $ami;
}
close F;
return \%map;
}
sub get_keypair {
my $out = `curl -s http://169.254.169.254/latest/meta-data/public-keys/`;
my ($keyname) = $out =~ /0=(.+)/;
return $keyname;
}
sub get_security_group {
my $ssg = SLAVE_SECURITY_GROUP;
my $msg = MASTER_SECURITY_GROUP;
my $range = PORT_RANGE;
chomp (my $result = `euca-describe-group $ssg`);
unless ($result) {
$result = `euca-add-group -d'security group for gbrowse render slaves allows ssh and ports $range' $ssg`;
die "Couldn't create security group for $ssg" unless $result =~ /^GROUP/;
}
chomp (my $ip = `curl -s http://169.254.169.254/latest/meta-data/local-ipv4/`);
$result = `euca-authorize -P tcp -p 22 -s $ip/32 $ssg`;
die $result unless $result =~ /PERMISSION/;
$result = `euca-authorize -P tcp -p $range -o $msg $ssg`;
die $result unless $result =~ /PERMISSION/;
return $ssg;
}