ensembl-hive  2.8.1
Describer.pm
Go to the documentation of this file.
1 =head1 LICENSE
2 
3 See the NOTICE file distributed with this work for additional information
4 regarding copyright ownership.
5 
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9 
10  http://www.apache.org/licenses/LICENSE-2.0
11 
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 
18 =cut
19 
20 package EnsCloud::Describer;
21 
22 use Moose::Role;
23 requires 'region_alias';
24 
25 use Net::Amazon::EC2;
26 use Text::SimpleTable;
27 our $col_widths_headings = {
28  tags => [ 22, 'Tag' ],
29  instance_id => [ 10, 'Instance' ],
30  ip_address => [ 14, 'Ip Address' ],
31  dns_name => [ 57, 'DNS Name' ],
32  instance_type => [ 9, 'Type' ],
33  launch_time => [ 16, 'Launch' ],
34  image_id => [ 12, 'Ami id' ],
35  name => [ 45, 'Name' ],
36  description => [ 45, 'Description' ],
37  image_state => [ 7, 'State' ],
38 
39  # snap_creation => [ 23, 'Snap Creation' ],
40  # [ 13, 'Vol Creation' ]
41  size => [ 5, 'Size' ],
42  create_time => [ 20, 'Create Time' ],
43 
44  # [ 20, 'Attach Time' ],
45  # [ 13, 'Volume Id' ],
46  # [ 10, 'Device' ],
47  # [ 10, 'Vol. status' ],
48  # [ 8, 'Attach status' ],
49  # zone => [ 10, 'Zone' ]
50  # [ 13, 'Snapshot Id' ],
51  # [ 19, 'Start Time' ],
52  # [ 8, 'Progress' ],
53  # status => [ 10, 'Status' ],
54  # [ 12, 'owner' ],
55  # [ 11, 'owner alias' ],
56 };
57 
58 has ec2_wanted_version => (
59  is => 'rw',
60  default => '2011-02-28'
61 
62 );
63 
64 #has 'ec2' => (
65 # is => 'ro',
66 # isa => 'Net::Amazon::EC2',
67 #lazy_
68 #);
69 
70 # has 'region_alias' => (
71 # is => 'rw',
72 # isa => 'Str',
73 # required =>1
74 # );
75 
76 sub ec2 {
77  my $self = shift;
78  my $region_lookup = {
79  'eu' => 'eu-west-1',
80  'useast' => 'us-east-1',
81  'uswest' => 'us-west-1',
82  'asia' => 'ap-southeast-1',
83  };
84  return Net::Amazon::EC2->new(
85  AWSAccessKeyId => $ENV{AWS_ACCESS_KEY_ID},
86  SecretAccessKey => $ENV{AWS_ACCESS_KEY_SECRET},
87  debug => 0,
88  region => $region_lookup->{ $self->region_alias },
89  version => $self->ec2_wanted_version
90  );
91 
92 }
93 
94 sub list_volumes {
95 
96  my $self = shift;
97  my $volumes = $self->ec2->describe_volumes;
98  my @sorted_volumes = sort { $a->{size} <=> $b->{size} } @$volumes;
99 
100  my $instances = $self->ec2->describe_instances;
101 
102  my $instance2ip;
103 
104  foreach my $instance (@$instances) {
105 
106  # print dump $instance->instances_set;
107 
108  my $instance_set = $instance->instances_set->[0];
109  $instance2ip->{ $instance_set->{instance_id} } =
110  $instance_set->{ip_address};
111 
112  }
113 
114  # die dump $volumes;
115  my $table = Text::SimpleTable->new(
116  [ 5, 'Size' ],
117  [ 20, 'Create Time' ],
118  [ 20, 'Attach Time' ],
119  [ 13, 'Volume Id' ],
120  [ 13, 'Instance Id' ],
121  [ 15, 'Ip Address' ],
122  [ 10, 'Device' ],
123  [ 10, 'Vol. status' ],
124  [ 8, 'Attach status' ],
125  [ 13, 'Snapshot Id' ],
126  [ 10, 'Zone' ]
127  );
128 
129  foreach my $volume (@sorted_volumes) {
130 
131  # print dump $instance->instances_set;
132 
133  my $attachments = $volume->attachments;
134 
135  print
136 "Volume [$volume->{volume_id}] is attached to multiple instances: this not handled by this script\n"
137  and exit
138  if @$attachments > 1;
139  $volume->{create_time} =~ s/.000Z$//;
140  $attachments->[0]->{attach_time} =~ s/.000Z$//;
141 
142  # die dump $instance_set;
143  $table->row(
144  $volume->{size},
145  $volume->{create_time},
146  $attachments->[0]->{attach_time} || '',
147  $volume->{volume_id},
148  $attachments->[0]->{instance_id} || '',
149  $instance2ip->{ $attachments->[0]->{instance_id} } || '',
150  $attachments->[0]->{device} || '',
151  $volume->{status},
152  $attachments->[0]->{status} || '',
153 
154  $volume->{snapshot_id} || 'no snapshot',
155  $volume->{zone}
156  );
157  }
158 
159  print $table->draw;
160 
161 }
162 
163 sub list_instances {
164  my ($self) = @_;
165 
166  my $instances = $self->ec2->describe_instances;
167  my $table = Text::SimpleTable->new(
168  $col_widths_headings->{tags},
169  $col_widths_headings->{instance_id},
170  $col_widths_headings->{ip_address},
171  $col_widths_headings->{dns_name},
172  $col_widths_headings->{instance_type},
173  $col_widths_headings->{launch_time},
174  $col_widths_headings->{image_state},
175  $col_widths_headings->{image_id}
176  );
177  foreach my $instance (@$instances) {
178 
179  # print dump $instance->instances_set;
180 
181  my $instance_set = $instance->instances_set->[0];
182  $instance_set->{launch_time} =~ s/:\d+\.000Z$//;
183 
184  #die dump $instance_set;
185  $table->row(
186  $instance_set->{tags} || '',
187  $instance_set->{instance_id},
188  $instance_set->{ip_address} || '',
189  $instance_set->{dns_name} || '',
190  $instance_set->{instance_type},
191  $instance_set->{launch_time},
192  $instance_set->{instance_state}->name,
193  $instance_set->{image_id},
194  );
195  }
196  return $table->draw;
197 }
198 
199 # __PACKAGE__->meta->make_immutable;
200 
201 1;
202 
203 __END__
debug
public debug()