CloudDeploy/CommandLine/History.pm untested

filter: Statementstmt 0.0% 0 / 115 Branchbran 0.0% 0 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 160 CC 14 SCAR 33.2
CC
14
Cov
0%
inspect_deployments
72.0
print_inspect_output
20.0
run
12.0
CRAP 27.8
1 package CloudDeploy::CommandLine::History {
2 use MooseX::App;
3 use ARGV::Struct;
4 use CloudDeploy::Utils;
5 use CloudDeploy::DeploymentCollection;
6 use Text::ASCIITable;
7 use Data::Printer;
8 use Term::ANSIColor;
9  
10 option include_common => (
11 is => 'ro',
12 isa => 'Bool',
13 documentation => 'Include common parameters when inspecting',
14 default => 0,
15 );
16  
17 option include_attached => (
18 is => 'ro',
19 isa => 'Bool',
20 documentation => 'Include parameters generated by attachments when inspecting',
21 default => 0,
22 );
23  
24 option limit => (
25 is => 'ro',
26 isa => 'Int',
27 documentation => 'Limit listing length',
28 default => 10,
29 );
30  
31 parameter subcommand => (
32 is => 'ro',
33 isa => 'Str',
34 documentation => 'list / search / inspect',
35 default => 'list',
36 );
37  
38 has find_args => (
39 is => 'ro',
40 isa => 'HashRef',
41 lazy => 1,
42 default => sub {
43 my $self = shift;
44 my $args = ARGV::Struct->new(argv => [ '{', @{ $self->extra_argv }, '}' ])->parse;
45 return { map { lc($_) => $args->{ $_ } } keys(%$args) };
46 }
47 );
48  
49 sub run {
50 my ($self) = @_;
51  
52 my $deployments = CloudDeploy::DeploymentCollection->new(account => $ENV{'CPSD_AWS_ACCOUNT'});
53  
54 if ($self->subcommand eq 'search') {
55 search_deployments($self, $deployments);
56 }
57 elsif ($self->subcommand eq 'inspect') {
58 inspect_deployments($self, $deployments);
59 }
60 else {
61 list_deployments($self, $deployments);
62 }
63 }
64  
65 sub list_deployments {
66 my ($self, $deploys) = @_;
67  
68 my @list = $deploys->last_log_deployments($self->limit);
69  
70 my $table = Text::ASCIITable->new;
71 $table->setCols(qw(Timestamp Status Name));
72  
73 foreach my $dep (uniq(@list)) {
74 $table->addRow(@{ $dep });
75 }
76  
77 print $table;
78 }
79  
80 sub search_deployments {
81 my ($self, $deploys) = @_;
82  
83 # Allow regex in string arguments and MongoDB operators
84 foreach my $arg (keys %{ $self->find_args }) {
85 my $value = $self->find_args->{ $arg };
86 $self->find_args->{ $arg } = qr/$value/i unless (ref $value eq 'HASH');
87 }
88  
89 my @list = $deploys->search_log_deployments($self->find_args, $self->limit);
90  
91 my $table = Text::ASCIITable->new;
92 $table->setCols(qw(Timestamp Status Name));
93  
94 foreach my $dep (uniq(@list)) {
95 $table->addRow(@{ $dep });
96 }
97  
98 print $table;
99 }
100  
101 sub inspect_deployments {
102 my ($self, $deploys) = @_;
103  
104 die "Error: deployment name required!\n" unless (defined $self->find_args->{name});
105  
106 unless (defined $self->find_args->{timestamp}) {
107 my @list = $deploys->search_log_deployments($self->find_args, 1);
108 $self->find_args->{timestamp} = $list[0]->[0];
109 }
110  
111 my $name = $self->find_args->{name};
112 my $timestamp = $self->find_args->{timestamp};
113 my $dep = $deploys->get_old_deployment($name, $timestamp);
114  
115 die "Error: deployment does not exist!\n" unless (defined $dep);
116  
117 # Load class from deployment
118 my $module;
119 eval {
120 $module = load_class($dep->type);
121 };
122 $module = undef if ($@);
123  
124 my $params = $dep->params;
125  
126 if (defined $module) {
127  
128 # Find params generated from attachment
129 unless ($self->include_attached) {
130 my @attached =
131 map { $_->name }
132 grep { $_->does('Attached') }
133 $module->{params_class}->meta->get_all_attributes;
134  
135 # Remove attached params
136 delete $params->{$_} for (@attached);
137 }
138  
139 # Remove common params
140 unless ($self->include_common) {
141 my @common_args = ('account', 'ami', 'os_family', 'template', 'devel', 'onlysnapshot', 'update');
142 delete $params->{$_} for (@common_args);
143 }
144  
145 }
146  
147 # Remove undefined params
148 my @undefined = grep { not defined $params->{$_} } keys(%{ $params });
149 delete $params->{$_} for (@undefined);
150  
151 print_inspect_output($module, $dep, $params);
152 }
153  
154 sub print_inspect_output {
155 my ($mod, $obj, $par) = @_;
156  
157 print "\nTYPE:\n";
158 p $obj->type;
159 print "\nPARAMS:\n";
160  
161 print color('bold yellow'),
162 "WARNING: class " . $obj->type . " not found! All parameters are being displayed.\n\n",
163 color('reset')
164 unless (defined $mod);
165  
166 p $par;
167 print "\nOUTPUTS:\n";
168 p $obj->outputs;
169  
170 print "\nCOMMENTS:\n";
171 p $obj->comments;
172  
173 print "\nCOMMAND LINE (" . $obj->timestamp . "):\n";
174 my $command = "clouddeploy deploy";
175  
176 if (defined $mod) {
177 my @classes = $mod->{class}->meta->superclasses;
178 $command = "imager make_ami" if (grep { /MakeAMIBase/ } @classes);
179 }
180 else {
181 print color('bold yellow'),
182 "WARNING: class " . $obj->type . " not found! Cannot find if this is an AMI or infrastructure.\n",
183 color('reset');
184 $command = color('bold red') . "Class missing" . color('clear');
185 }
186  
187 $command .= " " . $obj->type . ".pm --";
188  
189 foreach my $p (keys %{ $par }) {
190 $command .= " --$p '" . $par->{$p} . "'";
191 }
192  
193 print color('bold white'), "\n$command\n\n", color('clear');
194 }
195  
196 # Temporary fix for duplicates in deployment log collection
197 sub uniq {
198 my %seen;
199 grep !$seen{$_->[0] . $_->[1]}++, @_;
200 }
201 }
202  
203 1;
204