| 1 | package App::Ordo::Command::Job::Create; | ||
| 2 | use Moo; | ||
| 3 | use feature qw(say); | ||
| 4 | extends 'App::Ordo::Command::Base'; | ||
| 5 | |||
| 6 | use App::Ordo qw($CURRENT_PATH); | ||
| 7 | use Term::ANSIColor qw(colored); | ||
| 8 | |||
| 9 | sub name { "job create" } | ||
| 10 | sub summary { "Create a new job" } | ||
| 11 | sub usage { "<name> --server <name> --script 'command' [options]" } | ||
| 12 | |||
| 13 | sub option_spec { | ||
| 14 | return { | ||
| 15 | 'server=s' => 'Server to run on (required)', | ||
| 16 | 'script=s' => 'Shell command/script (required)', | ||
| 17 | 'desc=s' => 'Job description', | ||
| 18 | 'needs=s@' => 'Jobs this depends on (AND logic)', | ||
| 19 | 'needs_any=s@' => 'Jobs this depends on (OR logic)', | ||
| 20 | 'retrys=i' => 'Number of retries', | ||
| 21 | 'loops=i' => 'Number of loops', | ||
| 22 | 'delay=i' => 'Delay between loops', | ||
| 23 | 'clonable=i' => 'Allow cloning', | ||
| 24 | 'on_fail=i' => 'Cluster to run on failure', | ||
| 25 | 'json=s' => 'Extra JSON data', | ||
| 26 | }; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub execute { | ||
| 30 | my ($self, $opt, $name) = @_; | ||
| 31 | |||
| 32 | unless ($name && $opt->{server} && $opt->{script}) { | ||
| 33 | say colored(["bold red"], "Missing required: name, --server, --script"); | ||
| 34 | $self->show_help; | ||
| 35 | return; | ||
| 36 | } | ||
| 37 | |||
| 38 | if ($opt->{needs}) { | ||
| 39 | my @expanded; | ||
| 40 | for my $item (@{$opt->{needs}}) { | ||
| 41 | push @expanded, split /,/, $item; | ||
| 42 | } | ||
| 43 | $opt->{needs} = \@expanded; | ||
| 44 | } | ||
| 45 | |||
| 46 | if ($opt->{needs_any}) { | ||
| 47 | my @expanded; | ||
| 48 | for my $item (@{$opt->{needs_any}}) { | ||
| 49 | push @expanded, split /,/, $item; | ||
| 50 | } | ||
| 51 | $opt->{needs_any} = \@expanded; | ||
| 52 | } | ||
| 53 | |||
| 54 | my $payload = { | ||
| 55 | name => $name, | ||
| 56 | server => $opt->{server}, | ||
| 57 | script => $opt->{script}, | ||
| 58 | description => $opt->{desc}, | ||
| 59 | needs => $opt->{needs} || [], | ||
| 60 | needs_any => $opt->{needs_any} || [], | ||
| 61 | retrys => $opt->{retrys}, | ||
| 62 | loops => $opt->{loops}, | ||
| 63 | delay => $opt->{delay}, | ||
| 64 | clonable => $opt->{clonable}, | ||
| 65 | on_fail => $opt->{on_fail}, | ||
| 66 | json => $opt->{json}, | ||
| 67 | }; | ||
| 68 | |||
| 69 | my $res = $self->api->call('create_job', $payload); | ||
| 70 | |||
| 71 | if ($res->{success}) { | ||
| 72 | say colored(["bold green"], "Job '$name' created on server '$opt->{server}'"); | ||
| 73 | } else { | ||
| 74 | say colored(["bold red"], "Failed: " . ($res->{message} || 'unknown error')); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | 1; |