remoting works
[scpubgit/Object-Remote.git] / lib / Object / Remote / Connector / SSH.pm
CommitLineData
47c83a13 1package Object::Remote::Connector::SSH;
2
3use Object::Remote::FatNode;
84b04178 4use Object::Remote::ModuleSender;
5use IPC::Open2;
47c83a13 6use Moo;
7
8with 'Object::Remote::Role::Connector';
9
47c83a13 10sub _open2_for {
11 my $self = shift;
84b04178 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);
47c83a13 16}
17
84b04178 18around 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
47c83a13 29sub _ssh_object_for {
30 my ($self, $on) = @_;
31 $self->ssh_masters->{$on} ||= Net::OpenSSH->new($on);
32}
33
84b04178 34push @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
47c83a13 441;