Describer.pm 5.61 KB
Newer Older
1 2
=head1 LICENSE

Magali Ruffier's avatar
Magali Ruffier committed
3
Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Tiago Grego's avatar
Tiago Grego committed
4
Copyright [2016-2019] EMBL-European Bioinformatics Institute
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut

Stephen Keenan's avatar
Stephen Keenan committed
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
package EnsCloud::Describer;

use Moose::Role;
requires 'region_alias';

use Net::Amazon::EC2;
use Text::SimpleTable;
our $col_widths_headings = {
    tags          => [ 22, 'Tag' ],
    instance_id   => [ 10, 'Instance' ],
    ip_address    => [ 14, 'Ip Address' ],
    dns_name      => [ 57, 'DNS Name' ],
    instance_type => [ 9,  'Type' ],
    launch_time   => [ 16, 'Launch' ],
    image_id      => [ 12, 'Ami id' ],
    name          => [ 45, 'Name' ],
    description   => [ 45, 'Description' ],
    image_state   => [ 7,  'State' ],

    # snap_creation =>    [ 23, 'Snap Creation' ],
    #     [ 13, 'Vol Creation' ]
    size        => [ 5,  'Size' ],
    create_time => [ 20, 'Create Time' ],

    #     [ 20, 'Attach Time' ],
    #     [ 13, 'Volume Id' ],
    #     [ 10, 'Device' ],
    #     [ 10, 'Vol. status' ],
    #     [ 8,  'Attach status' ],
    # zone =>    [ 10, 'Zone' ]
    #     [ 13, 'Snapshot Id' ],
    #     [ 19, 'Start Time' ],
    #     [ 8,  'Progress' ],
    # status =>    [ 10, 'Status' ],
    #     [ 12, 'owner' ],
    #     [ 11, 'owner alias' ],
};

has ec2_wanted_version => (
    is      => 'rw',
    default => '2011-02-28'

);

#has 'ec2' => (
#    is         => 'ro',
#    isa        => 'Net::Amazon::EC2',
#lazy_
#);

# has 'region_alias' => (
#     is  => 'rw',
#     isa => 'Str',
#     required =>1
# );

sub ec2 {
    my $self          = shift;
    my $region_lookup = {
        'eu'     => 'eu-west-1',
        'useast' => 'us-east-1',
        'uswest' => 'us-west-1',
        'asia'   => 'ap-southeast-1',
    };
    return Net::Amazon::EC2->new(
        AWSAccessKeyId  => $ENV{AWS_ACCESS_KEY_ID},
        SecretAccessKey => $ENV{AWS_ACCESS_KEY_SECRET},
        debug           => 0,
        region          => $region_lookup->{ $self->region_alias },
        version         => $self->ec2_wanted_version
    );

}

sub list_volumes {

    my $self           = shift;
    my $volumes        = $self->ec2->describe_volumes;
    my @sorted_volumes = sort { $a->{size} <=> $b->{size} } @$volumes;

    my $instances = $self->ec2->describe_instances;

    my $instance2ip;

    foreach my $instance (@$instances) {

        #         print dump $instance->instances_set;

        my $instance_set = $instance->instances_set->[0];
        $instance2ip->{ $instance_set->{instance_id} } =
          $instance_set->{ip_address};

    }

    #     die dump $volumes;
    my $table = Text::SimpleTable->new(
        [ 5,  'Size' ],
        [ 20, 'Create Time' ],
        [ 20, 'Attach Time' ],
        [ 13, 'Volume Id' ],
        [ 13, 'Instance Id' ],
        [ 15, 'Ip Address' ],
        [ 10, 'Device' ],
        [ 10, 'Vol. status' ],
        [ 8,  'Attach status' ],
        [ 13, 'Snapshot Id' ],
        [ 10, 'Zone' ]
    );

    foreach my $volume (@sorted_volumes) {

        #         print dump $instance->instances_set;

        my $attachments = $volume->attachments;

        print
"Volume [$volume->{volume_id}] is attached to multiple instances: this not handled by this script\n"
          and exit
          if @$attachments > 1;
        $volume->{create_time} =~ s/.000Z$//;
        $attachments->[0]->{attach_time} =~ s/.000Z$//;

        #         die dump $instance_set;
        $table->row(
            $volume->{size},
            $volume->{create_time},
            $attachments->[0]->{attach_time} || '',
            $volume->{volume_id},
            $attachments->[0]->{instance_id}                   || '',
            $instance2ip->{ $attachments->[0]->{instance_id} } || '',
            $attachments->[0]->{device}                        || '',
            $volume->{status},
            $attachments->[0]->{status} || '',

            $volume->{snapshot_id} || 'no snapshot',
            $volume->{zone}
        );
    }

    print $table->draw;

}

sub list_instances {
    my ($self) = @_;

    my $instances = $self->ec2->describe_instances;
    my $table     = Text::SimpleTable->new(
        $col_widths_headings->{tags},
        $col_widths_headings->{instance_id},
        $col_widths_headings->{ip_address},
        $col_widths_headings->{dns_name},
        $col_widths_headings->{instance_type},
        $col_widths_headings->{launch_time},
        $col_widths_headings->{image_state},
        $col_widths_headings->{image_id}
    );
    foreach my $instance (@$instances) {

        #    print dump $instance->instances_set;

        my $instance_set = $instance->instances_set->[0];
        $instance_set->{launch_time} =~ s/:\d+\.000Z$//;

        #die dump $instance_set;
        $table->row(
            $instance_set->{tags} || '',
            $instance_set->{instance_id},
            $instance_set->{ip_address} || '',
            $instance_set->{dns_name}   || '',
            $instance_set->{instance_type},
            $instance_set->{launch_time},
            $instance_set->{instance_state}->name,
            $instance_set->{image_id},
        );
    }
    return $table->draw;
}

# __PACKAGE__->meta->make_immutable;

1;

__END__