Automation/Unix.pm

filter: Statementstmt 18.3% 13 / 71 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 42.8% 3 / 7 Podpod 100% 4 / 4 totaltotal 16.9% 20 / 118 CC 12 SCAR 28.3
CC
12
Cov
13%
validate_process
30.0
validate_mountpoint
20.0
connect
12.0
CRAP 16.9
« Automation/Web.pm ↑ Summary Automation/Logging.pm »
1 package App::Validation::Automation::Unix;
2
3 7 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 =head1 NAME
8
9 App::Validation::Automation::Unix - Base Classs App::Validation::Automation
10
11 Stores utilities that perform Unix based validation
12
13 =head1 SYNOPSIS
14
15 App::Validation::Automation::Unix connects to remote server using SSH.The Public/Private Key pair should be generated and stored else connection establishment will fail.The location of Private key also needs to be specifed in the configuration file.Logs in and validates processes and filesystems post connection establishment.
16
17 =head1 ATTRIBUTES
18
19 unix_msg stores the message generated by Unix Class Methods,
20 connection stores the ssh connection object.
21
22 =cut
23
24 7 has 'unix_msg' => (
25 is => 'rw',
26 isa => 'Str',
27 clearer => 'clear_unix_msg',
28 );
29
30 7 has 'connection' => (
31 is => 'rw',
32 isa => 'Net::SSH::Perl',
33 );
34
35 =head1 METHODS
36
37 =head2 connect
38
39 Connect and login to remote server. Host to connect to and user is passed as arguments.Returns true on success.
40
41 =cut
42
43 0 ▶sub connect {
Subroutine connect: not called CC=3 SCAR=24.8
Pod documented
44 0 my $self = shift;
45 0 my $hostname = shift;
46 0 my $user = shift;
47 0 my ($ssh);
48
49 #Prepare Connetion
50 $ssh = Net::SSH::Perl->new(
51 $hostname,
52 protocol => $self->config->{'COMMON.SSH_PROTO'},
53 debug => $self->config->{'COMMON.DEBUG_SSH'},
54 0 ▶ identity_files => $self->config->{'COMMON.ID_RSA'},
Condition0%0/2
'Net::SSH::Perl'->new($hostname, 'protocol', $self->config->{'COMMON.SSH_PROTO'}, 'debug', $self->config->{'COMMON.DEBUG_SSH'}, 'identity_files', $self->config->{'COMMON.ID_RSA'}) || confess("Couldn't ssh to $hostname : $App::Validation::Automation::Unix::OS_ERROR")
l!l
00
MC/DC0%0/1
'Net::SSH::Perl'->new($hostname, 'protocol', $self->config->{'COMMON.SSH_PROTO'}, 'debug', $self->config->{'COMMON.DEBUG_SSH'}, 'identity_files', $self->config->{'COMMON.ID_RSA'}) || confess("Couldn't ssh to $hostname : $App::Validation::Automation::Unix::OS_ERROR")
'Net::SSH::Perl'->new($hostname, 'protocol', $self->config->{'COMMON.SSH_PROTO'}, 'debug', $self->config->{'COMMON.DEBUG_SSH'}, 'identity_files', $self->config->{'COMMON.ID_RSA'})
Truth table0%0/2
A
A= 'Net::SSH::Perl'->new($hostname, 'protocol', $self->config->{'COMMON.SSH_PROTO'}, 'debug', $self->config->{'COMMON.DEBUG_SSH'}, 'identity_files', $self->config->{'COMMON.ID_RSA'})
Aresult
11
00
55 ) || confess "Couldn't ssh to $hostname : $OS_ERROR";
56 0 ▶ $ssh->login($user)
Branch: 1 branch
BranchTrueFalseTotal
$ssh->login($user) or confess("Couldn't login $user\@$hostname : $App::Validation::Automation::Unix::OS_ERROR") 0 0 0
57 || confess "Couldn't login $user\@$hostname : $OS_ERROR";
58
59 #set connection state in connection attribute
60 0 $self->connection($ssh);
61
62 0 return 1;
63 }
64
65 =head2 validate_process
66
67 Validates if correct no of process is running on remote server.The command fired to test process can either be a part of config or passed as argument.Returns true on success else returns false.
68
69 =cut
70
71 0 ▶sub validate_process {
Subroutine validate_process: not called CC=5 SCAR=34.0
Pod documented
72 0 my $self = shift;
73 0 my $process_tmpl = shift;
74 0 my $ssh = $self->connection;
75 0 ▶ my $remote_cmd_tmpl = shift || $self->config->{'COMMON.PROCESS_TMPL'};
Condition0%0/3
shift() || $self->config->{'COMMON.PROCESS_TMPL'}
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->config->{'COMMON.PROCESS_TMPL'}
shift() $self->config->{'COMMON.PROCESS_TMPL'}
Truth table0%0/3
A || B
A= shift()B= $self->config->{'COMMON.PROCESS_TMPL'}
ABresult
1X1
011
000
76 0 my ($process_name, $min, $msg, @processes, $process_count,
77 $remote_cmd, $error, $status);
78
79 0 ($process_name, $min)
80 = ($process_tmpl =~ /^(.+?)\:(.+?)$/);
81 0 $remote_cmd = sprintf("$remote_cmd_tmpl",$process_name);
82 0 ($process_count, $error, $status) = $ssh->cmd($remote_cmd);
83 0 chomp($process_count);
84 0 ▶ if($error || ($status != 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error or $status != 0) 0 0 0
Condition0%0/3
$error or $status != 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$error or $status != 0
$error $status != 0
Truth table0%0/3
A or B
A= $errorB= $status != 0
ABresult
1X1
011
000
85 0 $msg .= "Remote Cmd Fired : $remote_cmd : Failed\n";
86 0 $msg .= "Error : $error";
87 0 $self->unix_msg($msg);
88 0 return 0;
89 }
90 0 ▶ if( $process_count < $min ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($process_count < $min) 0 0 0
91 0 $msg .= "Remote Cmd Fired : $remote_cmd \n";
92 0 $msg .= "Expected $min Found $process_count running";
93 0 $self->unix_msg($msg);
94 0 return 0;
95 }
96
97 0 return 1;
98
99 }
100
101 =head2 validate_mountpoint
102
103 Validates if a mountpoint is accessible on remote server.The command fired to test mountpoint can either be a part of config or passed as argument.Returns true on success else returns false.
104
105 =cut
106
107 0 ▶sub validate_mountpoint {
Subroutine validate_mountpoint: not called CC=4 SCAR=30.0
Pod documented
108 0 my $self = shift;
109 0 my $mountpoint = shift;
110 0 my $ssh = $self->connection;
111 0 ▶ my $remote_cmd_tmpl = shift || $self->config->{'COMMON.FILESYS_TMPL'};
Condition0%0/3
shift() || $self->config->{'COMMON.FILESYS_TMPL'}
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->config->{'COMMON.FILESYS_TMPL'}
shift() $self->config->{'COMMON.FILESYS_TMPL'}
Truth table0%0/3
A || B
A= shift()B= $self->config->{'COMMON.FILESYS_TMPL'}
ABresult
1X1
011
000
112 0 my ($output, $remote_cmd, $error, $status, $msg);
113
114 0 $remote_cmd = sprintf("$remote_cmd_tmpl",$mountpoint);
115 0 ($output , $error, $status) = $ssh->cmd($remote_cmd);
116 0 ▶ if($error || ($status != 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error or $status != 0) 0 0 0
Condition0%0/3
$error or $status != 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$error or $status != 0
$error $status != 0
Truth table0%0/3
A or B
A= $errorB= $status != 0
ABresult
1X1
011
000
117 0 $msg .= "\nRemote Cmd Fired : $remote_cmd : Failed\n";
118 0 $msg .= "Error : $error";
119 0 $self->unix_msg($msg);
120 0 return 0;
121 }
122
123 0 return 1;
124 }
125
126 =head2 change_unix_pwd
127
128 Change password at unix level after password expiration at Website level.Uses Secret passphrase to decrypt the newly created password.Stores new encrypted password in password file
129
130 =cut
131
132 0 ▶sub change_unix_pwd {
Subroutine change_unix_pwd: not called CC=3 SCAR=24.8
Pod documented
133 0 my $self = shift;
134 0 my ($home, $old_pwd, $new_pwd, $enc_pwd_file, $secret_pphrase, $crypt,
135 $enc_new_pwd);
136 0 $old_pwd = $self->config->{'COMMON.OLD_PASSWORD'};
137 0 $new_pwd = $self->config->{'COMMON.PASSWORD'};
138 0 $home = $self->config->{'COMMON.DEFAULT_HOME'};
139 0 $enc_pwd_file = $self->config->{'COMMON.ENC_PASS_FILE'};
140 0 $secret_pphrase = $self->secret_pphrase;
141 0 $crypt = Crypt::Lite->new( debug => 0 );
142
143 #if old and new password match return in error
144 0 ▶ if( $old_pwd eq $new_pwd ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_pwd eq $new_pwd) 0 0 0
145 0 $self->unix_msg("Password change failed : old pwd = new pwd");
146 0 return 0;
147 }
148
149 #Encrypt new password
150 0 $enc_new_pwd = $crypt->encrypt($new_pwd, $secret_pphrase);
151
152 0 ▶ open my $pwd_handle,">","$home/$enc_pwd_file"
Condition0%0/2
"$home/$enc_pwd_file" || confess("$home/$enc_pwd_file : $App::Validation::Automation::Unix::OS_ERROR")
l!l
00
MC/DC0%0/1
"$home/$enc_pwd_file" || confess("$home/$enc_pwd_file : $App::Validation::Automation::Unix::OS_ERROR")
"$home/$enc_pwd_file"
Truth table0%0/2
A
A= "$home/$enc_pwd_file"
Aresult
11
00
153 || confess "$home/$enc_pwd_file : $OS_ERROR";
154 0 print {$pwd_handle} $enc_new_pwd;
155 0 close $pwd_handle;
156
157 0 return 1;
158
159 }
160
161 7 __PACKAGE__->meta->make_immutable;
162
163
164 7 1;
« Automation/Web.pm ↑ Summary Automation/Logging.pm »