« blib/lib/Bio/Graphics/Browser2/Action.pm
↑ Summary
blib/lib/Bio/Graphics/Browser2/DataLoader/wig2bigwig.pm »
| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | gbrowse_grow_cloud-vol.pl Grow the GBrowse volume by the requested amount | ||
| 6 | |||
| 7 | =head1 SYNOPSYS | ||
| 8 | |||
| 9 | Grow /opt/gbrowse by another 100 gigabytes | ||
| 10 | |||
| 11 | % gbrowse_grow_cloud_vol.pl 100 | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | This script grows /opt/gbrowse by the requested number of | ||
| 16 | gigabytes. The single argument must be a number between 1 and 1000, | ||
| 17 | which indicates the number of GB to grow the volume by (not the new | ||
| 18 | size of the volume). | ||
| 19 | |||
| 20 | It works by creating a new EBS volume and adding it to the logical | ||
| 21 | volume manager (LVM2) for this machine. The filesystem is then | ||
| 22 | extended to the desired size. | ||
| 23 | |||
| 24 | =head1 COMMAND-LINE OPTIONS | ||
| 25 | |||
| 26 | Options can be abbreviated. For example, you can use -a for | ||
| 27 | --access_key: | ||
| 28 | |||
| 29 | --access_key EC2 access key | ||
| 30 | --secret_key EC2 secret key | ||
| 31 | |||
| 32 | =head1 ENVIRONMENT VARIABLES | ||
| 33 | |||
| 34 | The following environment variables are used if the corresponding | ||
| 35 | options are not present: | ||
| 36 | |||
| 37 | EC2_ACCESS_KEY your access key | ||
| 38 | EC2_SECRET_KEY your secret key | ||
| 39 | |||
| 40 | =head1 SEE ALSO | ||
| 41 | |||
| 42 | L<VM::EC2>, L<VM::EC2::Staging::Manager> | ||
| 43 | |||
| 44 | =head1 AUTHOR | ||
| 45 | |||
| 46 | Lincoln Stein, lincoln.stein@gmail.com | ||
| 47 | |||
| 48 | Copyright (c) 2012 Ontario Institute for Cancer Research | ||
| 49 | |||
| 50 | This package and its accompanying libraries is free software; you can | ||
| 51 | redistribute it and/or modify it under the terms of the GPL (either | ||
| 52 | version 1, or at your option, any later version) or the Artistic | ||
| 53 | License 2.0. Refer to LICENSE for the full license text. In addition, | ||
| 54 | please see DISCLAIMER.txt for disclaimers of warranty. | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | use strict; | ||
| 59 | use lib '../lib'; | ||
| 60 | |||
| 61 | use VM::EC2; | ||
| 62 | use File::Basename 'basename'; | ||
| 63 | use Getopt::Long; | ||
| 64 | |||
| 65 | my($Access_key,$Secret_key,$Endpoint); | ||
| 66 | my $Program_name = basename($0); | ||
| 67 | |||
| 68 | GetOptions( | ||
| 69 | 'access_key=s' => \$Access_key, | ||
| 70 | 'secret_key=s' => \$Secret_key, | ||
| 71 | ) or exec 'perldoc',$0; | ||
| 72 | |||
| 73 | my $extra_size = shift or die "Please provide size to grow /opt/gbrowse by. Use --help for details."; | ||
| 74 | |||
| 75 | #setup defaults | ||
| 76 | $ENV{EC2_ACCESS_KEY} = $Access_key if defined $Access_key; | ||
| 77 | $ENV{EC2_SECRET_KEY} = $Secret_key if defined $Secret_key; | ||
| 78 | |||
| 79 | my $meta = VM::EC2->instance_metadata; | ||
| 80 | my $zone = $meta->availabilityZone; | ||
| 81 | (my $region = $zone) =~ s/[a-z]$//; # hack | ||
| 82 | my $instance = $meta->instanceId; | ||
| 83 | my ($ebs_device,$local_device) = unused_block_device() or die "Couldn't find a suitable device to attach to"; | ||
| 84 | |||
| 85 | my $ec2 = VM::EC2->new(-region=>$region) or die VM::EC2->error_str; | ||
| 86 | |||
| 87 | print STDERR "Creating $extra_size EBS volume.\n"; | ||
| 88 | my $vol = $ec2->create_volume(-availability_zone => $zone, | ||
| 89 | -size => $extra_size) or die "Couldn't create EBS volume: ",$ec2->error_str; | ||
| 90 | $vol->add_tag(Name=>"GBrowse lvm disk $local_device"); | ||
| 91 | |||
| 92 | print STDERR "Attaching volume.\n"; | ||
| 93 | my $a = $vol->attach($instance=>$ebs_device) or die "Couldn't attach EBS volume to $ebs_device: ",$ec2->error_str; | ||
| 94 | $ec2->wait_for_attachments($a); | ||
| 95 | $a->deleteOnTermination(1); | ||
| 96 | -e $local_device or die "EBS volume did not appear at $local_device as expected"; | ||
| 97 | |||
| 98 | print STDERR "Creating lvm physical device.\n"; | ||
| 99 | system("sudo pvcreate $local_device") == 0 or die "pvcreate failed"; | ||
| 100 | |||
| 101 | print STDERR "Extending 'volumes' volume group.\n"; | ||
| 102 | system("sudo vgextend volumes $local_device") == 0 or die "vgextend failed"; | ||
| 103 | |||
| 104 | my $result = `sudo vgdisplay -c volumes` or die "vgdisplay filed"; | ||
| 105 | my @result = split /:/,$result; | ||
| 106 | my $free = $result[15] or die "volume group has no free extents"; | ||
| 107 | |||
| 108 | print STDERR "Extending 'gbrowse' logical volume.\n"; | ||
| 109 | system("sudo lvextend -l +$free /dev/volumes/gbrowse") == 0 or die "lvextend failed"; | ||
| 110 | |||
| 111 | print STDERR "Extending /opt/gbrowse filesystem.\n"; | ||
| 112 | system("sudo resize2fs /dev/volumes/gbrowse") == 0 or die "resize2fs failed"; | ||
| 113 | |||
| 114 | print STDERR "Volume resized successfully\n"; | ||
| 115 | |||
| 116 | exit 0; | ||
| 117 | |||
| 118 | sub unused_block_device { | ||
| 119 | my $major_start = shift || 'g'; | ||
| 120 | |||
| 121 | my @devices = `ls -1 /dev/sd?* /dev/xvd?* 2>/dev/null`; | ||
| 122 | chomp(@devices); | ||
| 123 | return unless @devices; | ||
| 124 | my %used = map {$_ => 1} @devices; | ||
| 125 | |||
| 126 | my $base = $used{'/dev/sda1'} ? "/dev/sd" | ||
| 127 | : $used{'/dev/xvda1'} ? "/dev/xvd" | ||
| 128 | : ''; | ||
| 129 | die "Device list contains neither /dev/sda1 nor /dev/xvda1; don't know how blocks are named on this system" | ||
| 130 | unless $base; | ||
| 131 | |||
| 132 | my $ebs = '/dev/sd'; | ||
| 133 | for my $major ($major_start..'p') { | ||
| 134 | for my $minor (1..15) { | ||
| 135 | my $local_device = "${base}${major}${minor}"; | ||
| 136 | next if $used{$local_device}++; | ||
| 137 | my $ebs_device = "/dev/sd${major}${minor}"; | ||
| 138 | return ($ebs_device,$local_device); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | return; | ||
| 142 | } |