sample Takfile
[scpubgit/Tak.git] / Takfile
CommitLineData
241e3dd6 1package Tak::MyScript;
2
3use Tak::Takfile;
4
5sub every_exec (stream|s) {
6 my ($self, $remotes, $options, @command) = @_;
7
8 my @requests;
9
10 $_->ensure(command_service => 'Tak::CommandService') for @$remotes;
11
12 foreach my $remote (@$remotes) {
13 if ($options->{stream}) {
14 my $stdout = $self->stdout;
15 my $host = $remote->host;
16 push @requests, $remote->start(
17 {
18 on_result => sub { $self->print_exec_result($remote, @_) },
19 on_progress => sub {
20 $stdout->print($host.' '.$_[0].': '.$_[1]);
21 $stdout->print("\n") unless $_[1] =~ /\n\Z/;
22 }
23 },
24 command_service => stream_exec => \@command
25 );
26 } else {
27 push @requests, $remote->start(
28 { on_result => sub { $self->print_exec_result($remote, @_) } },
29 command_service => exec => \@command
30 );
31 }
32 }
33 Tak->await_all(@requests);
34}
35
36sub print_exec_result {
37 my ($self, $remote, $result) = @_;
38
39 my $res = eval { $result->get }
40 or do {
41 $self->stderr->print("Host ${\$remote->host}: Error: $@\n");
42 return;
43 };
44
45 my $code = $res->{exit_code};
46 $self->stdout->print(
47 "Host ${\$remote->host}: ".($code ? "NOT OK ${code}" : "OK")."\n"
48 );
49 if ($res->{stderr}) {
50 $self->stdout->print("Stderr:\n${\$res->{stderr}}\n");
51 }
52 if ($res->{stdout}) {
53 $self->stdout->print("Stdout:\n${\$res->{stdout}}\n");
54 }
55}
56
571;