File Coverage

blib/lib/Metabrik/Remote/Ssh.pm
Criterion Covered Total %
statement 9 87 10.3
branch 0 62 0.0
condition 0 54 0.0
subroutine 3 10 30.0
pod 1 7 14.2
total 13 220 5.9


line stmt bran cond sub pod time code
1             #
2             # $Id$
3             #
4             # remote::ssh Brik
5             #
6             package Metabrik::Remote::Ssh;
7 1     1   621 use strict;
  1         3  
  1         30  
8 1     1   5 use warnings;
  1         2  
  1         28  
9              
10 1     1   8 use base qw(Metabrik::Client::Ssh);
  1         1  
  1         453  
11              
12             sub brik_properties {
13             return {
14 0     0 1   revision => '$Revision$',
15             tags => [ qw(unstable) ],
16             author => 'GomoR ',
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             publickey => [ qw(file) ],
24             privatekey => [ qw(file) ],
25             use_publickey => [ qw(0|1) ],
26             },
27             attributes_default => {
28             username => 'root',
29             port => 22,
30             use_publickey => 1,
31             },
32             commands => {
33             my_os => [ qw(hostname|OPTIONAL port|OPTIONAL username|OPTIONAL) ],
34             list_processes => [ qw(hostname|OPTIONAL port|OPTIONAL username|OPTIONAL) ],
35             is_process_running => [ qw(process hostname|OPTIONAL port|OPTIONAL username|OPTIONAL) ],
36             execute => [ qw(command hostname|OPTIONAL port|OPTIONAL username|OPTIONAL) ],
37             execute_in_background => [ qw(command hostname|OPTIONAL port|OPTIONAL username|OPTIONAL) ],
38             capture => [ qw(command hostname|OPTIONAL port|OPTIONAL username|OPTIONAL) ],
39             },
40             require_modules => {
41             'Metabrik::System::Process' => [ ],
42             },
43             };
44             }
45              
46             sub my_os {
47 0     0 0   my $self = shift;
48 0           my ($hostname, $port, $username) = @_;
49              
50 0   0       $hostname ||= $self->hostname;
51 0   0       $port ||= $self->port;
52 0   0       $username ||= $self->username;
53 0 0         $self->brik_help_run_undef_arg('my_os', $hostname) or return;
54 0 0         $self->brik_help_run_undef_arg('my_os', $port) or return;
55 0 0         $self->brik_help_run_undef_arg('my_os', $username) or return;
56              
57 0 0         my $cs = $self->connect($hostname, $port, $username) or return;
58 0 0         my $lines = $self->capture('uname -s') or return;
59 0           $self->disconnect;
60              
61 0 0         if (@$lines > 0) {
62 0           return $lines->[0];
63             }
64              
65 0           return 'undef';
66             }
67              
68             sub list_processes {
69 0     0 0   my $self = shift;
70 0           my ($hostname, $port, $username) = @_;
71              
72 0   0       $hostname ||= $self->hostname;
73 0   0       $port ||= $self->port;
74 0   0       $username ||= $self->username;
75 0 0         $self->brik_help_run_undef_arg('list_processes', $hostname) or return;
76 0 0         $self->brik_help_run_undef_arg('list_processes', $port) or return;
77 0 0         $self->brik_help_run_undef_arg('list_processes', $username) or return;
78              
79 0 0         my $lines = $self->capture('ps awuxw') or return;
80              
81 0 0         my $sp = Metabrik::System::Process->new_from_brik_init($self) or return;
82 0           return $sp->parse_ps_output($lines);
83             }
84              
85             sub is_process_running {
86 0     0 0   my $self = shift;
87 0           my ($process, $hostname, $port, $username) = @_;
88              
89 0   0       $hostname ||= $self->hostname;
90 0   0       $port ||= $self->port;
91 0   0       $username ||= $self->username;
92              
93 0 0         my $list = $self->list_processes($hostname, $port, $username) or return;
94              
95 0           for my $this (@$list) {
96 0           my $command = $this->{COMMAND};
97 0           my @toks = split(/\s+/, $command);
98 0           $toks[0] =~ s/^.*\/(.*?)$/$1/;
99 0 0         if ($toks[0] eq $process) {
100 0           return 1;
101             }
102             }
103              
104 0           return 0;
105             }
106              
107             sub execute {
108 0     0 0   my $self = shift;
109 0           my ($cmd, $hostname, $port, $username) = @_;
110              
111 0   0       $hostname ||= $self->hostname;
112 0   0       $port ||= $self->port;
113 0   0       $username ||= $self->username;
114 0 0         $self->brik_help_run_undef_arg('execute', $cmd) or return;
115 0 0         $self->brik_help_run_undef_arg('execute', $hostname) or return;
116 0 0         $self->brik_help_run_undef_arg('execute', $port) or return;
117 0 0         $self->brik_help_run_undef_arg('execute', $username) or return;
118              
119 0 0         $self->connect($hostname, $port, $username) or return;
120 0 0         my $ssh = $self->SUPER::execute($cmd) or return;
121 0           $self->disconnect;
122              
123 0           return $ssh;
124             }
125              
126             sub execute_in_background {
127 0     0 0   my $self = shift;
128 0           my ($cmd, $hostname, $port, $username) = @_;
129              
130 0   0       $hostname ||= $self->hostname;
131 0   0       $port ||= $self->port;
132 0   0       $username ||= $self->username;
133 0 0         $self->brik_help_run_undef_arg('execute_in_background', $cmd) or return;
134 0 0         $self->brik_help_run_undef_arg('execute_in_background', $hostname) or return;
135 0 0         $self->brik_help_run_undef_arg('execute_in_background', $port) or return;
136 0 0         $self->brik_help_run_undef_arg('execute_in_background', $username) or return;
137              
138 0 0         $self->connect($hostname, $port, $username) or return;
139 0 0         my $r = $self->SUPER::execute_in_background($cmd) or return;
140 0           $self->disconnect;
141              
142 0           return $r;
143             }
144              
145             sub capture {
146 0     0 0   my $self = shift;
147 0           my ($cmd, $hostname, $port, $username) = @_;
148              
149 0   0       $hostname ||= $self->hostname;
150 0   0       $port ||= $self->port;
151 0   0       $username ||= $self->username;
152 0 0         $self->brik_help_run_undef_arg('capture', $cmd) or return;
153 0 0         $self->brik_help_run_undef_arg('capture', $hostname) or return;
154 0 0         $self->brik_help_run_undef_arg('capture', $port) or return;
155 0 0         $self->brik_help_run_undef_arg('capture', $username) or return;
156              
157 0 0         $self->connect($hostname, $port, $username) or return;
158 0 0         my $lines = $self->SUPER::capture($cmd) or return;
159 0           $self->disconnect;
160              
161 0           return $lines;
162             }
163              
164             1;
165              
166             __END__