| 1 | package WWW::Hetzner::CLI::Cmd::Server::Cmd::Rescue; | ||
| 2 | # ABSTRACT: Enable or disable rescue mode | ||
| 3 | |||
| 4 | our $VERSION = '0.100'; | ||
| 5 | |||
| 6 | use Moo; | ||
| 7 | use MooX::Cmd; | ||
| 8 | use MooX::Options protect_argv => 0, usage_string => 'USAGE: hcloud.pl server rescue <id> [--disable] [--type linux64|linux32]'; | ||
| 9 | use JSON::MaybeXS qw(encode_json); | ||
| 10 | |||
| 11 | option disable => ( | ||
| 12 | is => 'ro', | ||
| 13 | doc => 'Disable rescue mode instead of enabling', | ||
| 14 | default => 0, | ||
| 15 | ); | ||
| 16 | |||
| 17 | option type => ( | ||
| 18 | is => 'ro', | ||
| 19 | format => 's', | ||
| 20 | doc => 'Rescue system type: linux64, linux32 (default: linux64)', | ||
| 21 | default => 'linux64', | ||
| 22 | ); | ||
| 23 | |||
| 24 | option ssh_key => ( | ||
| 25 | is => 'ro', | ||
| 26 | format => 's@', | ||
| 27 | doc => 'SSH key name or ID (repeatable)', | ||
| 28 | autosplit => ',', | ||
| 29 | ); | ||
| 30 | |||
| 31 | sub execute { | ||
| 32 | my ($self, $args, $chain) = @_; | ||
| 33 | my $id = $args->[0] or die "Usage: hcloud.pl server rescue <id> [--disable]\n"; | ||
| 34 | |||
| 35 | my $main = $chain->[0]; | ||
| 36 | my $cloud = $main->cloud; | ||
| 37 | |||
| 38 | if ($self->disable) { | ||
| 39 | print "Disabling rescue mode for server $id...\n"; | ||
| 40 | $cloud->servers->disable_rescue($id); | ||
| 41 | print "Rescue mode disabled.\n"; | ||
| 42 | } else { | ||
| 43 | print "Enabling rescue mode for server $id...\n"; | ||
| 44 | my $result = $cloud->servers->enable_rescue($id, | ||
| 45 | type => $self->type, | ||
| 46 | ssh_keys => $self->ssh_key, | ||
| 47 | ); | ||
| 48 | |||
| 49 | if ($main->output eq 'json') { | ||
| 50 | print encode_json($result), "\n"; | ||
| 51 | } else { | ||
| 52 | print "Rescue mode enabled.\n"; | ||
| 53 | if ($result->{root_password}) { | ||
| 54 | print "Root password: $result->{root_password}\n"; | ||
| 55 | } | ||
| 56 | print "Reboot the server to enter rescue mode.\n"; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | 1; | ||
| 62 | |||
| 63 | __END__ |