remoting works
[scpubgit/Object-Remote.git] / lib / Object / Remote / Connector / SSH.pm
1 package Object::Remote::Connector::SSH;
2
3 use Object::Remote::FatNode;
4 use Object::Remote::ModuleSender;
5 use IPC::Open2;
6 use Moo;
7
8 with 'Object::Remote::Role::Connector';
9
10 sub _open2_for {
11   my $self = shift;
12   my $pid = open2(my $ssh_stdout, my $ssh_stdin, 'ssh', $_[0], 'perl', '-')
13     or die "Failed to start ssh connection: $!";;
14   print $ssh_stdin $Object::Remote::FatNode::DATA, "__END__\n";
15   return ($ssh_stdin, $ssh_stdout, $pid);
16 }
17
18 around connect => sub {
19   my ($orig, $self) = (shift, shift);
20   my $conn = $self->$orig(@_);
21   Object::Remote->new(
22     connection => $conn,
23     class => 'Object::Remote::ModuleLoader',
24     args => { module_sender => Object::Remote::ModuleSender->new }
25   )->disarm_free;
26   return $conn;
27 };
28
29 sub _ssh_object_for {
30   my ($self, $on) = @_;
31   $self->ssh_masters->{$on} ||= Net::OpenSSH->new($on);
32 }
33
34 push @Object::Remote::Connection::Guess, sub { 
35   for ($_[0]) {
36     # 0-9 a-z _ - first char, those or . subsequent - hostnamish
37     if (defined and !ref and /^(?:.*?\@)?[\w\-][\w\-\.]*/) {
38       return __PACKAGE__->new->connect($_[0]);
39     }
40   }
41   return;
42 };
43
44 1;