| 1 | package Cinnamon::Context; | ||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | |||||||||||||
| 5 | 3 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 3 | ▶ | use YAML (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 3 | ▶ | use Class::Load (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 3 | ▶ | use Hash::MultiValue; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 3 | ▶ | use Term::ReadKey; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | 3 | ▶ | use Cinnamon; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 3 | ▶ | use Cinnamon::Runner; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 3 | ▶ | use Cinnamon::Logger; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | 3 | ▶ | use Cinnamon::Role; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | 3 | ▶ | use Cinnamon::Task; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | 3 | ▶ | use Cinnamon::Config::Loader; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | |||||||||||||
| 19 | 3 | our $CTX; | |||||||||||
| 20 | |||||||||||||
| 21 | has roles => ( | ||||||||||||
| 22 | is => 'ro', | ||||||||||||
| 23 | 18 | ▶ | default => sub { Hash::MultiValue->new() } | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 24 | 3 | ); | |||||||||||
| 25 | |||||||||||||
| 26 | has tasks => ( | ||||||||||||
| 27 | is => 'ro', | ||||||||||||
| 28 | 18 | ▶ | default => sub { Hash::MultiValue->new() } | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 29 | 3 | ); | |||||||||||
| 30 | |||||||||||||
| 31 | has params => ( | ||||||||||||
| 32 | is => 'ro', | ||||||||||||
| 33 | 18 | ▶ | default => sub { Hash::MultiValue->new() } | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 34 | 3 | ); | |||||||||||
| 35 | |||||||||||||
| 36 | 14 | ▶ | sub run { | ||||||||||
|
Subroutine
run: called CC=11 SCAR=24.1
Pod
undocumented
| |||||||||||||
| 37 | 14 | my ($self, $role_name, $task_name, %opts) = @_; | |||||||||||
| 38 | |||||||||||||
| 39 | 14 | Cinnamon::Config::Loader->load(config => $opts{config}); | |||||||||||
| 40 | |||||||||||||
| 41 | 14 | ▶ | if ($opts{info}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 42 | 1 | $self->dump_info; | |||||||||||
| 43 | 1 | return; | |||||||||||
| 44 | } | ||||||||||||
| 45 | |||||||||||||
| 46 | # set role name and task name | ||||||||||||
| 47 | 13 | CTX->set_param(role => $role_name); | |||||||||||
| 48 | 13 | CTX->set_param(task => $task_name); | |||||||||||
| 49 | |||||||||||||
| 50 | # override setting | ||||||||||||
| 51 | 13 | for my $key (keys %{ $opts{override_settings} }) { | |||||||||||
| 52 | 2 | CTX->set_param($key => $opts{override_settings}->{$key}); | |||||||||||
| 53 | } | ||||||||||||
| 54 | |||||||||||||
| 55 | 13 | my $hosts = $self->get_role_hosts($role_name); | |||||||||||
| 56 | 13 | my $task = $self->get_task($task_name); | |||||||||||
| 57 | 13 | ▶ | my $runner = $self->get_param('runner_class') || 'Cinnamon::Runner'; | ||||||||||
|
Condition50%1/2
$self->get_param('runner_class') || 'Cinnamon::Runner'
MC/DC0%0/1
$self->get_param('runner_class') || 'Cinnamon::Runner'
$self->get_param('runner_class')
Truth table50%1/2
A
A= $self->get_param('runner_class')
| |||||||||||||
| 58 | |||||||||||||
| 59 | 13 | ▶ | unless (defined $hosts) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 60 | 0 | log 'error', "undefined role : '$role_name'"; | |||||||||||
| 61 | 0 | return; | |||||||||||
| 62 | } | ||||||||||||
| 63 | 13 | ▶ | unless (defined $task) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 64 | 2 | log 'error', "undefined task : '$task_name'"; | |||||||||||
| 65 | 2 | return; | |||||||||||
| 66 | } | ||||||||||||
| 67 | |||||||||||||
| 68 | 11 | Class::Load::load_class $runner; | |||||||||||
| 69 | |||||||||||||
| 70 | 11 | my $result = $runner->start($hosts, $task); | |||||||||||
| 71 | 11 | my (@success, @error); | |||||||||||
| 72 | |||||||||||||
| 73 | 11 | ▶ | for my $key (keys %{$result || {}}) { | ||||||||||
|
Condition50%1/2
$result || {}
MC/DC0%0/1
$result || {}
$result
Truth table50%1/2
A
A= $result
| |||||||||||||
| 74 | 11 | ▶ | if ($result->{$key}->{error}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 75 | 3 | push @error, $key; | |||||||||||
| 76 | } | ||||||||||||
| 77 | else { | ||||||||||||
| 78 | 8 | push @success, $key; | |||||||||||
| 79 | } | ||||||||||||
| 80 | } | ||||||||||||
| 81 | |||||||||||||
| 82 | 11 | ▶ | log success => sprintf( | ||||||||||
|
Condition100%2/2
join(', ', @success) || ''
MC/DC100%1/1
join(', ', @success) || ''
join(', ', @success)
Truth table100%2/2
A
A= join(', ', @success)
| |||||||||||||
| 83 | "\n========================\n[success]: %s", | ||||||||||||
| 84 | (join(', ', @success) || ''), | ||||||||||||
| 85 | ); | ||||||||||||
| 86 | |||||||||||||
| 87 | 11 | ▶ | log error => sprintf( | ||||||||||
|
Condition100%2/2
join(', ', @error) || ''
MC/DC100%1/1
join(', ', @error) || ''
join(', ', @error)
Truth table100%2/2
A
A= join(', ', @error)
| |||||||||||||
| 88 | "[error]: %s", | ||||||||||||
| 89 | (join(', ', @error) || ''), | ||||||||||||
| 90 | ); | ||||||||||||
| 91 | |||||||||||||
| 92 | 11 | return (\@success, \@error); | |||||||||||
| 93 | } | ||||||||||||
| 94 | |||||||||||||
| 95 | 17 | ▶ | sub add_role { | ||||||||||
|
Subroutine
add_role: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 96 | 17 | my ($self, $name, $hosts, $params) = @_; | |||||||||||
| 97 | 17 | ▶ | $params ||= {}; | ||||||||||
|
Condition100%2/2
$params ||= {}
MC/DC100%1/1
$params ||= {}
$params
Truth table100%2/2
A
A= $params
| |||||||||||||
| 98 | 17 | my $role = Cinnamon::Role->new( | |||||||||||
| 99 | name => $name, | ||||||||||||
| 100 | hosts => $hosts, | ||||||||||||
| 101 | params => Hash::MultiValue->new(%$params), | ||||||||||||
| 102 | ); | ||||||||||||
| 103 | 17 | $self->roles->set($name => $role); | |||||||||||
| 104 | } | ||||||||||||
| 105 | |||||||||||||
| 106 | 20 | ▶ | sub get_role { | ||||||||||
|
Subroutine
get_role: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 107 | 20 | my ($self, $name) = @_; | |||||||||||
| 108 | 20 | return $self->roles->get($name); | |||||||||||
| 109 | } | ||||||||||||
| 110 | |||||||||||||
| 111 | 16 | ▶ | sub get_role_hosts { | ||||||||||
|
Subroutine
get_role_hosts: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 112 | 16 | my ($self, $name) = @_; | |||||||||||
| 113 | 16 | ▶ | my $role = $self->get_role($name) or return undef; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 114 | 16 | my $hosts = $role->get_hosts; | |||||||||||
| 115 | |||||||||||||
| 116 | # set role params | ||||||||||||
| 117 | # TODO: move from here | ||||||||||||
| 118 | 16 | my $params = $role->params; | |||||||||||
| 119 | 16 | for my $key (keys %$params) { | |||||||||||
| 120 | 1 | $self->set_param($key => $params->{$key}); | |||||||||||
| 121 | } | ||||||||||||
| 122 | |||||||||||||
| 123 | 16 | return $hosts; | |||||||||||
| 124 | } | ||||||||||||
| 125 | |||||||||||||
| 126 | 34 | ▶ | sub add_task { | ||||||||||
|
Subroutine
add_task: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 127 | 34 | my ($self, $name, $code) = @_; | |||||||||||
| 128 | 34 | ▶ | unless (ref $code eq 'HASH') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 129 | 31 | my $task = Cinnamon::Task->new( | |||||||||||
| 130 | name => $name, | ||||||||||||
| 131 | code => $code, | ||||||||||||
| 132 | ); | ||||||||||||
| 133 | 31 | return $self->tasks->set($name => $task); | |||||||||||
| 134 | } | ||||||||||||
| 135 | |||||||||||||
| 136 | # a nest task is named as joined by colon | ||||||||||||
| 137 | 3 | for my $child (keys %$code) { | |||||||||||
| 138 | 4 | my $child_name = join ":", $name, $child; | |||||||||||
| 139 | 4 | $self->add_task($child_name => $code->{$child}); | |||||||||||
| 140 | } | ||||||||||||
| 141 | } | ||||||||||||
| 142 | |||||||||||||
| 143 | 21 | ▶ | sub get_task { | ||||||||||
|
Subroutine
get_task: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 144 | 21 | my ($self, $name) = @_; | |||||||||||
| 145 | 21 | return $self->tasks->get($name); | |||||||||||
| 146 | } | ||||||||||||
| 147 | |||||||||||||
| 148 | 55 | ▶ | sub set_param { | ||||||||||
|
Subroutine
set_param: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 149 | 55 | my ($self, $key, $value) = @_; | |||||||||||
| 150 | 55 | $self->params->set($key => $value); | |||||||||||
| 151 | } | ||||||||||||
| 152 | |||||||||||||
| 153 | 51 | ▶ | sub get_param { | ||||||||||
|
Subroutine
get_param: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 154 | 51 | my ($self, $key, @args) = @_; | |||||||||||
| 155 | |||||||||||||
| 156 | 51 | my $value = $self->params->get($key); | |||||||||||
| 157 | 51 | ▶ | $value = $value->(@args) if ref $value eq 'CODE'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 158 | |||||||||||||
| 159 | 51 | return $value; | |||||||||||
| 160 | } | ||||||||||||
| 161 | |||||||||||||
| 162 | # Thread-specific stash | ||||||||||||
| 163 | 13 | ▶ | sub stash { | ||||||||||
|
Subroutine
stash: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 164 | 13 | ▶ | my $stash = $Coro::current->{Cinnamon} ||= {}; | ||||||||||
|
Condition100%2/2
$Coro::current->{"Cinnamon"} ||= {}
MC/DC100%1/1
$Coro::current->{"Cinnamon"} ||= {}
$Coro::current->{"Cinnamon"}
Truth table100%2/2
A
A= $Coro::current->{"Cinnamon"}
| |||||||||||||
| 165 | } | ||||||||||||
| 166 | |||||||||||||
| 167 | 3 | ▶ | sub call_task { | ||||||||||
|
Subroutine
call_task: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 168 | 3 | my ($self, $task_name, $host) = @_; | |||||||||||
| 169 | 3 | ▶ | my $task = $self->get_task($task_name) or die "undefined task : '$task_name'"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 170 | |||||||||||||
| 171 | 2 | $task->execute($host); | |||||||||||
| 172 | } | ||||||||||||
| 173 | |||||||||||||
| 174 | 6 | ▶ | sub run_cmd { | ||||||||||
|
Subroutine
run_cmd: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||
| 175 | 6 | my ($self, $commands, $opts) = @_; | |||||||||||
| 176 | 6 | ▶ | $opts ||= {}; | ||||||||||
|
Condition100%2/2
$opts ||= {}
MC/DC100%1/1
$opts ||= {}
$opts
Truth table100%2/2
A
A= $opts
| |||||||||||||
| 177 | |||||||||||||
| 178 | 6 | ▶ | my $current_host = $self->stash->{current_host} || 'localhost'; | ||||||||||
|
Condition100%2/2
$self->stash->{'current_host'} || 'localhost'
MC/DC100%1/1
$self->stash->{'current_host'} || 'localhost'
$self->stash->{'current_host'}
Truth table100%2/2
A
A= $self->stash->{'current_host'}
| |||||||||||||
| 179 | 6 | log info => sprintf "[%s :: executing] %s", $current_host, join(' ', @$commands); | |||||||||||
| 180 | |||||||||||||
| 181 | 6 | ▶ | if ($opts->{sudo}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 182 | 3 | $opts->{password} = $self->_get_sudo_password(); | |||||||||||
| 183 | } | ||||||||||||
| 184 | |||||||||||||
| 185 | 6 | $opts->{tty} = !! $self->get_param('tty'); | |||||||||||
| 186 | |||||||||||||
| 187 | 6 | my $executor = $self->build_command_executor; | |||||||||||
| 188 | 6 | my $result = $executor->execute($commands, $opts); | |||||||||||
| 189 | |||||||||||||
| 190 | 6 | ▶ | if ($result->{has_error}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 191 | 0 | die sprintf "error status: %d", $result->{error}; | |||||||||||
| 192 | } | ||||||||||||
| 193 | |||||||||||||
| 194 | 6 | return ($result->{stdout}, $result->{stderr}); | |||||||||||
| 195 | } | ||||||||||||
| 196 | |||||||||||||
| 197 | 6 | ▶ | sub build_command_executor { | ||||||||||
|
Subroutine
build_command_executor: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 198 | 6 | my ($self) = @_; | |||||||||||
| 199 | |||||||||||||
| 200 | 6 | ▶ | if (my $remote = $self->stash->{current_remote}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 201 | 2 | return $remote; | |||||||||||
| 202 | } | ||||||||||||
| 203 | else { | ||||||||||||
| 204 | 4 | return Cinnamon::Local->new; | |||||||||||
| 205 | } | ||||||||||||
| 206 | } | ||||||||||||
| 207 | |||||||||||||
| 208 | 1 | ▶ | sub dump_info { | ||||||||||
|
Subroutine
dump_info: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 209 | 1 | my ($self) = @_; | |||||||||||
| 210 | 1 | my $info = {}; | |||||||||||
| 211 | |||||||||||||
| 212 | 1 | my $roles = $self->roles; | |||||||||||
| 213 | my $role_info = +{ | ||||||||||||
| 214 | 1 | map { $_->name => $_->info } $roles->values, | |||||||||||
| 215 | }; | ||||||||||||
| 216 | |||||||||||||
| 217 | 1 | my $tasks = $self->tasks; | |||||||||||
| 218 | my $task_info = +{ | ||||||||||||
| 219 | 1 | map { %{ $_->info } } $tasks->values, | |||||||||||
| 220 | }; | ||||||||||||
| 221 | |||||||||||||
| 222 | 1 | log 'info', YAML::Dump({ | |||||||||||
| 223 | roles => $role_info, | ||||||||||||
| 224 | tasks => $task_info, | ||||||||||||
| 225 | }); | ||||||||||||
| 226 | } | ||||||||||||
| 227 | |||||||||||||
| 228 | 3 | ▶ | sub _get_sudo_password { | ||||||||||
|
Subroutine
_get_sudo_password: called CC=2 SCAR=11.6
| |||||||||||||
| 229 | 3 | my ($self) = @_; | |||||||||||
| 230 | 3 | my $password = $self->get_param('password'); | |||||||||||
| 231 | 3 | ▶ | return $password if defined $password; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 232 | |||||||||||||
| 233 | 0 | print "Enter sudo password: "; | |||||||||||
| 234 | 0 | ReadMode "noecho"; | |||||||||||
| 235 | 0 | chomp($password = ReadLine 0); | |||||||||||
| 236 | 0 | ReadMode 0; | |||||||||||
| 237 | 0 | print "\n"; | |||||||||||
| 238 | |||||||||||||
| 239 | 0 | $self->set_param(password => $password); | |||||||||||
| 240 | 0 | return $password; | |||||||||||
| 241 | } | ||||||||||||
| 242 | |||||||||||||
| 243 | 3 | !!1; | |||||||||||