Client/Openssh.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Client/Mysql.pm ↑ Summary Client/Redis.pm »
1 #
2 # $Id$
3 #
4 # client::openssh Brik
5 #
6 package Metabrik::Client::Openssh;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use base qw(Metabrik);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub brik_properties {
13 return {
14 revision => '$Revision$',
15 tags => [ qw(unstable ssh) ],
16 author => 'GomoR <GomoR[at]metabrik.org>',
17 license => 'http://opensource.org/licenses/BSD-3-Clause',
18 attributes => {
19 hostname => [ qw(hostname) ],
20 port => [ qw(integer) ],
21 username => [ qw(username) ],
22 password => [ qw(password) ],
23 pid => [ qw(master_pid) ],
24 slave_pids => [ qw(slave_pids) ],
25 forward_agent => [ qw(0|1) ],
26 ssh => [ qw(INTERNAL) ],
27 },
28 attributes_default => {
29 hostname => 'localhost',
30 port => 22,
31 forward_agent => 1,
32 slave_pids => {},
33 },
34 commands => {
35 connect => [ qw(hostname|OPTIONAL port|OPTIONAL username|OPTIONAL password|OPTIONAL) ],
36 is_connected => [ ],
37 disconnect => [ ],
38 open_tunnel => [ qw(hostname port) ],
39 close_tunnel => [ qw(hostname port) ],
40 },
41 require_modules => {
42 'Net::OpenSSH' => [ ],
43 'Metabrik::System::Process' => [ ],
44 },
45 };
46 }
47  
48 sub is_connected {
49 my $self = shift;
50  
51 if (defined($self->ssh)) {
52 return 1;
53 }
54  
55 return 0;
56 }
57  
58 sub connect {
59 my $self = shift;
60 my ($hostname, $port, $username, $password) = @_;
61  
62 if ($self->is_connected) {
63 return $self->log->verbose("connect: already connected");
64 }
65  
66 $hostname ||= $self->hostname;
67 $port ||= $self->port;
68 $username ||= $self->username;
69 $password ||= $self->password;
70  
71 my %opts = (
72 timeout => 5,
73 );
74 if (length($username)) {
75 $opts{user} = $username;
76 }
77 if (length($password)) {
78 $opts{password} = $password;
79 }
80 if ($port) {
81 $opts{port} = $port;
82 }
83 if ($self->forward_agent) {
84 $opts{forward_agent} = 1;
85 }
86  
87 my $ssh = Net::OpenSSH->new($hostname, %opts);
88 if ($ssh->error) {
89 return $self->log->error("connect: cannot connect to [$hostname]:$port: ".$ssh->error);
90 }
91  
92 $self->log->verbose("connect: connected to [$hostname]:$port");
93  
94 $self->pid($ssh->get_master_pid);
95  
96 return $self->ssh($ssh);
97 }
98  
99 sub disconnect {
100 my $self = shift;
101  
102 if (! $self->is_connected) {
103 return $self->log->verbose("disconnect: not connected");
104 }
105  
106 my $ssh = $self->ssh;
107  
108 my $sp = Metabrik::System::Process->new_from_brik_init($self) or return;
109  
110 my @pids = ( $self->pid, keys %{$self->slave_pids} );
111 for (@pids) {
112 $sp->kill($_);
113 }
114 $self->pid(undef);
115 $self->slave_pids({});
116 $self->ssh(undef);
117  
118 return 1;
119 }
120  
121 sub open_tunnel {
122 my $self = shift;
123 my ($hostname, $port) = @_;
124  
125 if (! $self->is_connected) {
126 return $self->log->verbose("open_tunnel: not connected");
127 }
128 $self->brik_help_run_undef_arg('open_tunnel', $hostname) or return;
129 $self->brik_help_run_undef_arg('open_tunnel', $port) or return;
130  
131 my $ssh = $self->ssh;
132 my $slave_pids = $self->slave_pids;
133  
134 my ($tunnel, $pid) = $ssh->open_tunnel({}, $hostname, $port);
135 if ($ssh->error) {
136 return $self->log->error("open_tunnel: failed for [$hostname]:$port: ".$ssh->error);
137 }
138  
139 $tunnel->blocking(0);
140 $tunnel->autoflush(1);
141  
142 $slave_pids->{$pid} = { uid => "$hostname:$port" };
143  
144 return $tunnel;
145 }
146  
147 sub close_tunnel {
148 my $self = shift;
149 my ($hostname, $port) = @_;
150  
151 if (! $self->is_connected) {
152 return $self->log->verbose("close_tunnel: not connected");
153 }
154 $self->brik_help_run_undef_arg('close_tunnel', $hostname) or return;
155 $self->brik_help_run_undef_arg('close_tunnel', $port) or return;
156  
157 my $ssh = $self->ssh;
158 my $slave_pids = $self->slave_pids;
159  
160 my $sp = Metabrik::System::Process->new_from_brik_init($self) or return;
161  
162 my $found = 0;
163 for my $k (keys %$slave_pids) {
164 if ($slave_pids->{$k}{uid} eq "$hostname:$port") {
165 $sp->kill($k);
166 delete $slave_pids->{$k};
167 $found++;
168 last;
169 }
170 }
171  
172 if (! $found) {
173 $self->log->verbose("close_tunnel: tunnel [$hostname]:$port not connected");
174 }
175  
176 return 1;
177 }
178  
179 sub brik_fini {
180 my $self = shift;
181  
182 my $ssh = $self->ssh;
183 if (defined($ssh)) {
184 $ssh->disconnect;
185 }
186  
187 return 1;
188 }
189  
190 1;
191  
192 __END__
« Client/Mysql.pm ↑ Summary Client/Redis.pm »