doooooooom
[scpubgit/Tak.git] / lib / Tak.pm
CommitLineData
31a246e4 1package Tak;
2
3use Tak::Loop;
4use strictures 1;
5
e937993b 6our $VERSION = '0.001004'; # 0.1.4
51c8325b 7
fb0f5964 8our ($loop, $did_upgrade);
31a246e4 9
10sub loop { $loop ||= Tak::Loop->new }
11
addc801e 12sub loop_upgrade {
fb0f5964 13 return if $did_upgrade;
addc801e 14 require IO::Async::Loop;
15 my $new_loop = IO::Async::Loop->new;
16 $loop->pass_watches_to($new_loop) if $loop;
17 $loop = $new_loop;
fb0f5964 18 $did_upgrade = 1;
addc801e 19}
20
31a246e4 21sub loop_until {
22 my ($class, $done) = @_;
23 return if $done;
addc801e 24 $class->loop->loop_once until $_[1];
31a246e4 25}
26
934a93db 27sub await_all {
28 my ($class, @requests) = @_;
29 @requests = grep !$_->is_done, @requests;
30 return unless @requests;
31 my %req = map +("$_" => "$_"), @requests;
32 my $done;
934a93db 33 my %on_r = map {
b1aab0cf 34 my $orig = $_->{on_result};
934a93db 35 my $tag = $req{$_};
36 ($_ => sub { delete $req{$tag}; $orig->(@_); $done = 1 unless keys %req; })
37 } @requests;
b1aab0cf 38 my $call = sub { $class->loop_until($done) };
39 foreach (@requests) {
40 my $req = $_;
41 my $inner = $call;
42 $call = sub { local $req->{on_result} = $on_r{$req}; $inner->() };
43 }
44 $call->();
934a93db 45 return;
46}
47
b61f9637 481;
51c8325b 49
50=head1 NAME
51
e937993b 52Tak - Multi host remote control over ssh (then I wrote Object::Remote)
51c8325b 53
54=head1 SYNOPSIS
55
e937993b 56 # Curse at mst for doing it again under a different name
57 # Curse at mst some more
58 $ cpanm Object::Remote
59 # Now go use that
60
61(sorry, I should've done a tombstone release ages back)
62
51c8325b 63 bin/tak -h user1@host1 -h user2@host2 exec cat /etc/hostname
64
65or
66
67 in Takfile:
68
69 package Tak::MyScript;
70
71 use Tak::Takfile;
72 use Tak::ObjectClient;
73
74 sub each_get_homedir {
75 my ($self, $remote) = @_;
76 my $oc = Tak::ObjectClient->new(remote => $remote);
77 my $home = $oc->new_object('Path::Class::Dir')->absolute->stringify;
78 $self->stdout->print(
79 $remote->host.': '.$home."\n"
80 );
81 }
82
83 1;
84
85then
86
87 tak -h something get-homedir
88
89=head1 WHERE'S THE REST?
90
330ac6a2 91A drink leaked in my bag on the way back from LPW. My laptop is finally
92alive again though so I'll try and turn my slides into a vague attempt
93at documentation while I'm traveling to/from christmas things.
51c8325b 94
4a66f528 95=head1 Example
96
97$ cat Takfile
98package Tak::MyScript;
99
100use strict;
101use warnings;
102
103use Tak::Takfile;
104use Tak::ObjectClient;
105use lib "./lib";
106
107sub each_host {
108 my ($self, $remote) = @_;
109
110 my $oc = Tak::ObjectClient->new(remote => $remote);
111 my $name = $oc->new_object('My::Hostname');
112 print "Connected to hostname: " . $name . "\n";
113 }
114
1151;
116
117-----
118
119$cat ./lib/My/Hostname
120package My::Hostname;
121
122use Sys::Hostname;
123
124sub new {
125 my ($self) = @_;
126 my $name = hostname;
127 return $name;
128 }
129
1301;
131
51c8325b 132=head1 AUTHOR
133
134mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
135
136=head1 CONTRIBUTORS
137
138None required yet. Maybe this module is perfect (hahahahaha ...).
139
140=head1 COPYRIGHT
141
330ac6a2 142Copyright (c) 2011 the Tak L</AUTHOR> and L</CONTRIBUTORS>
51c8325b 143as listed above.
144
145=head1 LICENSE
146
147This library is free software and may be distributed under the same terms
148as perl itself.
149
150=cut