| 1 | package App::Ordo::Command::Cluster::Show; | ||
| 2 | use Moo; | ||
| 3 | use feature qw(say); | ||
| 4 | use POSIX qw(strftime); | ||
| 5 | extends 'App::Ordo::Command::Base'; | ||
| 6 | |||
| 7 | use App::Ordo qw($CURRENT_PATH); | ||
| 8 | use Term::ANSIColor qw(colored); | ||
| 9 | use Text::Table::Tiny 1.02 qw(generate_table); | ||
| 10 | |||
| 11 | sub name { "cluster show" } | ||
| 12 | sub summary { "Show detailed information about a cluster (current if no path)" } | ||
| 13 | sub usage { "[path]" } | ||
| 14 | |||
| 15 | sub option_spec { {} } | ||
| 16 | |||
| 17 | sub execute { | ||
| 18 | my ($self, $opt, $path) = @_; | ||
| 19 | |||
| 20 | my $res = $self->api->call('read_cluster', { name => $path }); | ||
| 21 | |||
| 22 | unless ($res->{success}) { | ||
| 23 | say colored(["bold red"], "Cluster not found: $path"); | ||
| 24 | say $res->{message} if $res->{message}; | ||
| 25 | return; | ||
| 26 | } | ||
| 27 | |||
| 28 | my $c = $res; | ||
| 29 | $path //= $c->{name}; | ||
| 30 | |||
| 31 | say colored(["bold cyan"], "Cluster: ") . colored(["bold white"], $c->{name}); | ||
| 32 | say colored(["cyan"], "Path: ") . $path; | ||
| 33 | say colored(["cyan"], "State: ") . colored(["bold " . ($c->{jobstate} eq 'running' ? 'green' : 'yellow')], $c->{jobstate}); | ||
| 34 | say colored(["cyan"], "Description: ") . ($c->{description} || colored(["bright_black"], "none")); | ||
| 35 | say colored(["cyan"], "Calendar: ") . ($c->{cal_name} ? $c->{cal_name} : colored(["bright_black"], "none")); | ||
| 36 | say colored(["cyan"], "Created: ") . ($c->{creation_time} | ||
| 37 | ? strftime("%a %b %d %H:%M:%S %Y", localtime($c->{creation_time})) | ||
| 38 | : "unknown"); | ||
| 39 | |||
| 40 | if ($c->{needs} && keys %{$c->{needs}}) { | ||
| 41 | my @all = grep { $c->{needs}{$_}{mode} == 0 } keys %{$c->{needs}}; | ||
| 42 | my @any = grep { $c->{needs}{$_}{mode} == 1 } keys %{$c->{needs}}; | ||
| 43 | say colored(["cyan"], "Depends on: ") . join(", ", @all) if scalar @all; | ||
| 44 | say colored(["cyan"], "Depends on any: ") . join(", ", @any) if scalar @any; | ||
| 45 | } else { | ||
| 46 | say colored(["cyan"], "Dependencies: ") . colored(["green"], "(none)"); | ||
| 47 | } | ||
| 48 | |||
| 49 | if ($c->{ended}) { | ||
| 50 | my $ago = int(time - $c->{ended}); | ||
| 51 | my $when = $ago < 3600 ? "$ago seconds ago" | ||
| 52 | : $ago < 86400 ? int($ago/3600) . " hours ago" | ||
| 53 | : int($ago/86400) . " days ago"; | ||
| 54 | say colored(["cyan"], "Last run: ") . strftime("%a %b %d %H:%M:%S %Y", localtime($c->{ended})) | ||
| 55 | . colored(["green"], " ($when)"); | ||
| 56 | } else { | ||
| 57 | say colored(["cyan"], "Last run: ") . colored(["yellow"], "never"); | ||
| 58 | } | ||
| 59 | |||
| 60 | if ($c->{jobs} && @{$c->{jobs}}) { | ||
| 61 | say "\n" . colored(["bold green"], "Jobs in this cluster:"); | ||
| 62 | my $rows = [ [qw(ID NAME SERVER STATE SCRIPT)] ]; | ||
| 63 | for my $j (@{$c->{jobs}}) { | ||
| 64 | my $state_color = $j->{jobstate} eq 'complete' ? 'green' : | ||
| 65 | $j->{jobstate} eq 'running' ? 'magenta' : | ||
| 66 | $j->{jobstate} eq 'failed' ? 'red' : 'yellow'; | ||
| 67 | push @$rows, [ | ||
| 68 | $j->{id}, | ||
| 69 | $j->{name}, | ||
| 70 | $j->{server_name} || '-', | ||
| 71 | colored(["bold $state_color"], $j->{jobstate}), | ||
| 72 | $j->{script} || '(no script)', | ||
| 73 | ]; | ||
| 74 | } | ||
| 75 | say generate_table(rows => $rows, header_row => 1, style => 'boxrule'); | ||
| 76 | } else { | ||
| 77 | say colored(["yellow"], "\nNo jobs in this cluster"); | ||
| 78 | } | ||
| 79 | say ""; | ||
| 80 | } | ||
| 81 | |||
| 82 | 1; |