| 1 | package App::Ordo::Command::Cluster::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 { "cluster create" } | ||
| 10 | sub summary { "Create a new cluster (folder/DAG)" } | ||
| 11 | sub usage { "<path> [options]" } | ||
| 12 | |||
| 13 | sub option_spec { | ||
| 14 | return { | ||
| 15 | #'desc|description|d=s' => 'Cluster description', | ||
| 16 | 'desc' => 'Cluster description', | ||
| 17 | 'needs=s@' => 'Clusters this depends on (AND logic)', | ||
| 18 | 'needs_any=s@' => 'Clusters this depends on (OR logic)', | ||
| 19 | 'cal=s' => 'Attach calendar by name', | ||
| 20 | 'loops=i' => 'Number of loops', | ||
| 21 | 'clonable=i' => 'Allow cloning (0/1)', | ||
| 22 | 'on_fail=s' => 'Cluster to run on failure', | ||
| 23 | }; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub execute { | ||
| 27 | my ($self, $opt, $path) = @_; | ||
| 28 | |||
| 29 | unless ($path) { | ||
| 30 | say colored(["bold red"], "Usage: cluster create <path> [options]"); | ||
| 31 | return; | ||
| 32 | } | ||
| 33 | |||
| 34 | if ($opt->{needs}) { | ||
| 35 | my @expanded; | ||
| 36 | for my $item (@{$opt->{needs}}) { | ||
| 37 | push @expanded, split /,/, $item; | ||
| 38 | } | ||
| 39 | $opt->{needs} = \@expanded; | ||
| 40 | } | ||
| 41 | |||
| 42 | if ($opt->{needs_any}) { | ||
| 43 | my @expanded; | ||
| 44 | for my $item (@{$opt->{needs_any}}) { | ||
| 45 | push @expanded, split /,/, $item; | ||
| 46 | } | ||
| 47 | $opt->{needs_any} = \@expanded; | ||
| 48 | } | ||
| 49 | |||
| 50 | my $payload = { | ||
| 51 | name => $path, | ||
| 52 | description => $opt->{desc}, | ||
| 53 | needs => $opt->{needs} || [], | ||
| 54 | needs_any => $opt->{needs_any} || [], | ||
| 55 | cal => $opt->{cal}, | ||
| 56 | loops => $opt->{loops}, | ||
| 57 | clonable => $opt->{clonable}, | ||
| 58 | on_fail => $opt->{on_fail}, | ||
| 59 | }; | ||
| 60 | |||
| 61 | my $res = $self->api->call('create_cluster', $payload); | ||
| 62 | |||
| 63 | if ($res->{success}) { | ||
| 64 | say colored(["bold green"], "Cluster '$path' created"); | ||
| 65 | } else { | ||
| 66 | say colored(["bold red"], "Failed: " . ($res->{message} || 'unknown error')); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | 1; |