| 1 | package App::Ordo::Command::Server::Add; | ||
| 2 | use Moo; | ||
| 3 | use feature qw(say); | ||
| 4 | extends 'App::Ordo::Command::Base'; | ||
| 5 | |||
| 6 | use Term::ANSIColor qw(colored); | ||
| 7 | use Term::ReadKey; | ||
| 8 | |||
| 9 | sub name { "server add" } | ||
| 10 | sub summary { "Add a new worker server" } | ||
| 11 | sub usage { "<name> --host <hostname> --user <username> [--password] [--port 22]" } | ||
| 12 | |||
| 13 | sub option_spec { | ||
| 14 | return { | ||
| 15 | 'host=s' => 'Hostname or IP address (required)', | ||
| 16 | 'user=s' => 'SSH username (required)', | ||
| 17 | 'port=i' => 'SSH port (default: 22)', | ||
| 18 | 'password' => 'Prompt for password (optional — key auth preferred)', | ||
| 19 | }; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub execute { | ||
| 23 | my ($self, $opt, $name) = @_; | ||
| 24 | |||
| 25 | unless ($name && $opt->{host} && $opt->{user}) { | ||
| 26 | say colored(["bold red"], "Missing required: name, --host, --user"); | ||
| 27 | $self->show_help; | ||
| 28 | return; | ||
| 29 | } | ||
| 30 | |||
| 31 | my $password; | ||
| 32 | if ($opt->{password}) { | ||
| 33 | print "Password for $opt->{user}\@$opt->{host}: "; | ||
| 34 | ReadMode('noecho'); | ||
| 35 | chomp($password = <STDIN>); | ||
| 36 | ReadMode('normal'); | ||
| 37 | print "\n"; | ||
| 38 | } | ||
| 39 | |||
| 40 | my $payload = { | ||
| 41 | name => $name, | ||
| 42 | host => $opt->{host}, | ||
| 43 | user => $opt->{user}, | ||
| 44 | port => $opt->{port} // 22, | ||
| 45 | password => $password, | ||
| 46 | }; | ||
| 47 | |||
| 48 | my $res = $self->api->call('create_monitor', $payload); | ||
| 49 | |||
| 50 | if ($res->{success}) { | ||
| 51 | say colored(["bold green"], "Server '$name' ($opt->{host}) added successfully"); | ||
| 52 | } else { | ||
| 53 | say colored(["bold red"], "Failed to add server: " . ($res->{message} || 'unknown error')); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | 1; |