blib/script/gbrowse_sync_aws_slave.pl untested

filter: Statementstmt 0.0% 0 / 81 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 12 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 133 CC 2 SCAR 9.6
CC
2
Cov
0%
END
6.0
tally_sizes
2.0
BEGIN
2.0
CRAP 2.6
1 #!/usr/local/bin/perl
2  
3 =head1 NAME
4  
5 gbrowse_sync_aws_slave.pl Synchronize local file system to GBrowse slave volume.
6  
7 =head1 SYNOPSIS
8  
9 % sudo gbrowse_sync_aws_slave.pl --conf /etc/gbrowse2/aws_balancer.conf \
10 --mysql /var/lib/mysql \
11 --postgres /var/lib/postgresql
12  
13 syncing data....done
14 data stored in snapshot(s) snap-12345
15 updated conf file, previous version in /etc/gbrowse2/aws_balancer.conf.bak
16  
17 =head1 DESCRIPTION
18  
19 This script is run in conjunction with Amazon Web Server-based GBrowse
20 render slave load balancing, which is described in more detail in the
21 manual page for gbrowse_aws_balancer.pl.
22  
23 The gbrowse_sync_aws_script.pl script should be run on the GBrowse
24 master machine each time you add a new database to an existing data
25 source, or if you add a whole new data source. What it does is to
26 prepare a new Amazon EBS snapshot containing a copy of all the data
27 needed for the GBrowse slave to run. This snapshot is then attached to
28 new slave instances.
29  
30 After running, it updates the conf file with the current versions of
31 the slave AMI and the data snapshot(s).
32  
33 % sudo gbrowse_sync_aws_script.pl --conf /etc/gbrowse2/aws_balancer.conf \
34 --mysql /var/lib/mysql \
35 --postgres /var/lib/postgresql
36  
37 The --conf argument is required. The script will create a snapshot of
38 the appropriate size, mount it on a temporary staging instance, and
39 rsync a copy of your gbrowse databases directory
40 (e.g. /var/lib/gbrowse2/databases) to the snapshot. If you have
41 created mysql or postgres databases, you must also give the paths to
42 their database file directories, as shown in the example.
43  
44 Note that ALL your mysql and postgres data files located on the master
45 machine will be copied; not just those used for track display.
46  
47 =head1 ENVIRONMENT VARIABLES
48  
49 The following environment variables are used if the corresponding
50 command line options are not present:
51  
52 EC2_ACCESS_KEY AWS EC2 access key EC2_SECRET_KEY AWS EC2 secret key
53  
54 =head1 SEE ALSO
55  
56 L<VM::EC2>, L<VM::EC2::Staging::Manager>
57  
58 =head1 AUTHOR
59  
60 Lincoln Stein, lincoln.stein@gmail.com
61  
62 Copyright (c) 2013 Ontario Institute for Cancer Research
63
64 This package and its accompanying libraries is free software; you can
65 redistribute it and/or modify it under the terms of the GPL (either
66 version 1, or at your option, any later version) or the Artistic
67 License 2.0. Refer to LICENSE for the full license text. In addition,
68 please see DISCLAIMER.txt for disclaimers of warranty.
69  
70 =cut
71  
72 use strict;
73 use FindBin '$Bin';
74 use lib "$Bin/../lib";
75 use lib '/home/lstein/projects/LibVM-EC2-Perl/lib';
76  
77 use Getopt::Long;
78 use GBrowse::ConfigData;
79 use File::Spec;
80 use Bio::Graphics::Browser2::Render::Slave::AWS_Balancer;
81  
82 use constant GB => 1_073_741_824;
83 use constant DEBUG => 0;
84  
85 my ($balancer,$slave);
86 my $program = $0;
87  
88 # this obscures the AWS secrets from ps; it is not 100% effective
89 ($0 = "$program @ARGV") =~ s/(\s--?[as]\S*?)(=|\s+)\S+/$1$2xxxxxxxxxx/g;
90  
91 $SIG{TERM} = sub {exit 0};
92 $SIG{INT} = sub {exit 0};
93  
94 my($ConfFile,$AccessKey,$SecretKey,$MySqlPath,$PostGresPath,$Verbosity);
95 GetOptions(
96 'access_key=s' => \$AccessKey,
97 'secret_key=s' => \$SecretKey,
98 'conf=s' => \$ConfFile,
99 'mysql=s' => \$MySqlPath,
100 'postgres=s' => \$PostGresPath,
101 'verbosity=i' => \$Verbosity,
102 ) or exec 'perldoc',$program;
103  
104 $ConfFile ||= File::Spec->catfile(GBrowse::ConfigData->config('conf'),'aws_balancer.conf');
105 unless (DEBUG || $< == 0) {
106 my @argv;
107 push @argv,('--access_key'=>$AccessKey) if $AccessKey;
108 push @argv,('--secret_key'=>$SecretKey) if $SecretKey;
109 push @argv,('--conf' =>File::Spec->rel2abs($ConfFile)) if $ConfFile;
110 push @argv,('--mysql' =>File::Spec->rel2abs($MySqlPath)) if $MySqlPath;
111 push @argv,('--postgres' =>File::Spec->rel2abs($PostGresPath)) if $PostGresPath;
112 push @argv,('--verbosity' =>$Verbosity) if defined $Verbosity;
113 $program = File::Spec->rel2abs($program);
114  
115 print STDERR <<END;
116 This script needs root privileges in order to access all directories
117 needed for synchronization. It will now invoke sudo to become the
118 'root' user temporarily. You may be prompted for your login password
119 now.
120 END
121 ;
122  
123 exec 'sudo','-E','-u','root',$program,@argv;
124 }
125  
126 $balancer = Bio::Graphics::Browser2::Render::Slave::AWS_Balancer->new(
127 -conf => $ConfFile,
128 -access_key => $AccessKey||'',
129 -secret_key => $SecretKey||'',
130 );
131  
132 $Verbosity = 3 unless defined $Verbosity;
133 $balancer->verbosity($Verbosity);
134  
135 # run the remote staging server
136 print STDERR "[info] Launching a slave server for staging...\n";
137 $slave = $balancer->launch_staging_server();
138  
139 $slave->shell if DEBUG;
140 $slave->stop_services();
141  
142 # figure out total size needed on destination volume
143 my $DataBasePath = GBrowse::ConfigData->config('databases');
144  
145 my $gig_needed = tally_sizes($DataBasePath,$MySqlPath,$PostGresPath); # keep an extra 10 G free
146 my $gig_have = $slave->volume_size;
147 if ($gig_needed + 5 > $gig_have) {
148 $gig_needed = $gig_have + 10; # grow by 10 G increments
149 $slave->info("Increasing size of slave data volume...\n");
150 $slave->grow_volume($gig_needed);
151 }
152  
153 $slave->info("Syncing files...\n");
154 $slave->put("$DataBasePath/",'/opt/gbrowse/databases');
155 $slave->put("$MySqlPath/", '/opt/gbrowse/lib/mysql') if $MySqlPath;
156 $slave->put("$PostGresPath/",'/opt/gbrowse/lib/postgresql') if $PostGresPath;
157 my @snapshots = $slave->snapshot_data_volumes;
158 $balancer->update_data_snapshots(@snapshots);
159 $slave->info("Updating $ConfFile.\n");
160 $slave->info("Synchronization done. New data is in snapshot(s) @snapshots.\n");
161  
162 exit 0;
163  
164 sub tally_sizes {
165 my @dirs = @_;
166 my $out = `sudo du -scb @dirs`;
167 my ($bytes) = $out=~/^(\d+)\s+total/m;
168 return int($bytes/GB)+1;
169 }
170  
171 END {
172 if ($slave) { $slave->terminate }
173 undef $slave;
174 }
175  
176 __END__