blib/lib/RackMan/Device/Server/HP_ProLiant.pm

filter: Statementstmt 23.4% 31 / 132 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 50.0% 8 / 16 Podpod 100% 3 / 3 totaltotal 16.8% 42 / 250 CC 47 SCAR 54.9
CC
47
Cov
14%
push_config
506.0
diff_config
240.0
__ANON__
30.0
CRAP 242.5
« blib/lib/RackMan/Utils.pm ↑ Summary bin/cisco-status »
1 package RackMan::Device::Server::HP_ProLiant;
2  
3 2 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Net::ILO;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use RackMan;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Term::ANSIColor qw< GREEN RED >;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 CONFIG_SECTION => "device:server:hp_proliant",
13 2 };
14  
15 2 ▶use constant SERIAL_SPEED => qw< 0 9600 19200 38400 57600 115200 >;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 #
19 # additional object attributes
20 #
21 2 has ilo_mac_addr => (
22 is => "ro",
23 isa => "HashRef",
24 lazy => 1,
25 builder => "_get_ilo_mac_addr",
26 );
27  
28 2 has ilo_ipv4addr => (
29 is => "ro",
30 isa => "HashRef",
31 lazy => 1,
32 builder => "_get_ilo_ipv4addr",
33 );
34  
35 2 has default_ilo_ipv4_gateway => (
36 is => "ro",
37 isa => "HashRef",
38 lazy => 1,
39 builder => "_get_default_ilo_ipv4_gateway",
40 );
41  
42 2 has ilo_fqdn => (
43 is => "ro",
44 isa => "Str",
45 lazy => 1,
46 builder => "_get_ilo_fqdn",
47 );
48  
49 2 has has_ilo => (
50 is => "ro",
51 isa => "Int",
52 default => 1,
53 );
54  
55  
56 #
57 # _get_ilo_mac_addr()
58 # -----------------
59 0 ▶sub _get_ilo_mac_addr {
Subroutine _get_ilo_mac_addr: not called CC=3 SCAR=24.8
60 0 for my $port (@{ $_[0]->ports }) {
61 0 ▶ return $port if $port->{name} eq "ilo"
Branch: 1 branch
BranchTrueFalseTotal
if $port->{"name"} eq "ilo" 0 0 0
62 }
63  
64 return
65 0 }
66  
67  
68 #
69 # _get_ilo_ipv4addr()
70 # -----------------
71 0 ▶sub _get_ilo_ipv4addr {
Subroutine _get_ilo_ipv4addr: not called CC=4 SCAR=30.0
72 0 for my $addr (@{ $_[0]->ipv4addrs}) {
73 0 ▶ if ($addr->{type} eq "regular" and $addr->{iface} eq "ilo") {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr->{"type"} eq "regular" and $addr->{"iface"} eq "ilo") 0 0 0
Condition0%0/3
$addr->{"type"} eq "regular" and $addr->{"iface"} eq "ilo"
!ll&&!rl&&r
000
MC/DC0%0/2
$addr->{"type"} eq "regular" and $addr->{"iface"} eq "ilo"
$addr->{"type"} eq "regular" $addr->{"iface"} eq "ilo"
Truth table0%0/3
A and B
A= $addr->{"type"} eq "regular"B= $addr->{"iface"} eq "ilo"
ABresult
0X0
100
111
74 0 return $addr
75 }
76 }
77  
78 return
79 0 }
80  
81  
82 #
83 # _get_ilo_fqdn()
84 # -------------
85 0 ▶sub _get_ilo_fqdn {
Subroutine _get_ilo_fqdn: not called CC=2 SCAR=17.9
86 0 my ($self) = @_;
87  
88 0 my $ilo_addr = $self->ilo_ipv4addr->{addr};
89 0 my $ilo_fqdn = gethostbyaddr(inet_aton($ilo_addr), AF_INET);
90  
91 0 ▶ RackMan->error("the iLO subsystem of ", $self->object_name,
Branch: 1 branch
BranchTrueFalseTotal
unless $ilo_fqdn 0 0 0
92 " has an IP address ($ilo_addr) but no associated FQDN; please",
93 " check the reverse record") unless $ilo_fqdn;
94  
95 0 return $ilo_fqdn
96 }
97  
98  
99 #
100 # _get_default_ilo_ipv4_gateway()
101 # -------------------------
102 0 ▶sub _get_default_ilo_ipv4_gateway {
Subroutine _get_default_ilo_ipv4_gateway: not called CC=1 SCAR=6.9
103 0 my ($self) = @_;
104 0 my @addrs = $self->ilo_ipv4addr;
105 return $self->_get_default_ipv4_gateway($addrs[0]{addr})
106 0 }
107  
108  
109 #
110 # write_config()
111 # ------------
112 0 ▶sub write_config {
Subroutine write_config: not called CC=2 SCAR=17.9
Pod documented
113 0 my ($self, $args) = @_;
114  
115 print " ! This action is not implemented. ",
116 "Please use the 'push' action instead.\n"
117 0 ▶ if $args->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"verbose"} 0 0 0
118 }
119  
120  
121 #
122 # diff_config()
123 # -----------
124 0 ▶sub diff_config {
Subroutine diff_config: not called CC=15 SCAR=54.8
Pod documented
125 0 my ($self, $args) = @_;
126  
127 0 my $rackman = $args->{rackman};
128 0 my $config = $rackman->config;
129  
130 # fetch the credentials
131 my $ilo_password = $rackman->options->{device_password}
132 0 ▶ || $config->val(CONFIG_SECTION, "ilo_password")
Branch: 1 branch
BranchTrueFalseTotal
my $ilo_password = $rackman->options->{"device_password"} || $config->val("device:server:hp_proliant", "ilo_password") or "RackMan"->error("missing password for iLO account 'Administrator'") 0 0 0
Condition0%0/3
$rackman->options->{'device_password'} || $config->val('device:server:hp_proliant', 'ilo_password')
l!l&&r!l&&!r
000
MC/DC0%0/2
$rackman->options->{'device_password'} || $config->val('device:server:hp_proliant', 'ilo_password')
$rackman->options->{'device_password'} $config->val('device:server:hp_proliant', 'ilo_password')
Truth table0%0/3
A || B
A= $rackman->options->{'device_password'}B= $config->val('device:server:hp_proliant', 'ilo_password')
ABresult
1X1
011
000
133 or RackMan->error("missing password for iLO account 'Administrator'");
134  
135 # fetch the IP address and FQDN of the iLO subsystem
136 0 my $ilo_addr = $self->ilo_ipv4addr->{addr};
137 0 my $ilo_fqdn = $self->ilo_fqdn;
138 0 my ($ilo_name, $ilo_domain) = split /\./, $ilo_fqdn;
139  
140 # connect to the iLO API
141 0 my $ilo = Net::ILO->new({
142 address => $ilo_addr,
143 username => "Administrator",
144 password => $ilo_password,
145 });
146  
147 0 ▶ my $ilo_type = $ilo->fw_type or RackMan->error($ilo->error);
Branch: 1 branch
BranchTrueFalseTotal
my $ilo_type = $ilo->fw_type or "RackMan"->error($ilo->error) 0 0 0
148  
149 print " $ilo_type v", $ilo->fw_version, " / ", $ilo->model, "\n"
150 0 ▶ if $args->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"verbose"} 0 0 0
151  
152 # construct the expected values
153 0 my $serial_speed = $config->val(CONFIG_SECTION, "serial_cli_speed", 5);
154 0 my %expected = (
155 hostname => $ilo_name,
156 domain_name => $ilo_domain,
157 server_name => $self->object_name,
158 serial_cli_speed => (SERIAL_SPEED)[$serial_speed],
159 );
160  
161 0 my @dns_servers = split / +/, $config->val(general => "dns_servers");
162 0 my @dns_fields = qw< prim_dns_server sec_dns_server ter_dns_server >;
163 0 $expected{$dns_fields[$_]} = $dns_servers[$_] for 0 .. $#dns_servers;
164  
165 0 ▶ if ($ilo->_version >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ilo->_version >= 3) 0 0 0
166 0 my @ntp_servers = split / +/, $config->val(general => "ntp_servers");
167 0 my @ntp_fields = qw< sntp_server1 sntp_server2 >;
168 0 $expected{$ntp_fields[$_]} = $ntp_servers[$_] for 0 .. $#ntp_servers;
169 }
170  
171 # fetch the corresponding current values
172 0 ▶ my %current = map { $_ => $ilo->$_ || "(undef)" } keys %expected;
Condition0%0/2
$ilo->$_ || '(undef)'
l!l
00
MC/DC0%0/1
$ilo->$_ || '(undef)'
$ilo->$_
Truth table0%0/2
A
A= $ilo->$_
Aresult
11
00
173  
174 0 my $diff = 0;
175  
176 # compare the two hashes and print the differences
177 0 for my $field (sort keys %expected) {
178 0 ▶ next if $current{$field} eq $expected{$field};
Branch: 1 branch
BranchTrueFalseTotal
if $current{$field} eq $expected{$field} 0 0 0
179 0 my $sp = " " x length $field;
180 0 print " - $field: ", RED("current value: $current{$field}"), "\n",
181 " $sp ", GREEN("expected value: $expected{$field}"), "\n\n";
182 0 $diff = 1;
183 }
184  
185 # check if the user "admin" is present
186 0 my $user_info = $ilo->get_user("admin");
187  
188 0 ▶ if ($user_info) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user_info) { } 0 0 0
189 0 ▶ if (not $user_info->{admin} =~ /^y(?:es)?/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($user_info->{"admin"} =~ /^y(?:es)?/i) 0 0 0
190 0 print " - account 'admin' exists but lack admin privilege\n";
191 0 $diff = 1;
192 }
193 }
194 else {
195 0 print " - account 'admin' does not exist\n";
196 0 $diff = 1;
197 }
198  
199 0 ▶ print " - no differences\n" if !$diff and $args->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if not $diff and $args->{"verbose"} 0 0 0
Condition0%0/3
not $diff and $args->{"verbose"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $diff and $args->{"verbose"}
not $diff $args->{"verbose"}
Truth table0%0/3
not(A) and B
A= not $diffB= $args->{"verbose"}
ABresult
0X0
100
111
200  
201 0 RackMan->set_status($diff);
202 }
203  
204  
205 #
206 # push_config()
207 # -----------
208 0 ▶sub push_config {
Subroutine push_config: not called CC=22 SCAR=62.3
Pod documented
209 0 my ($self, $args) = @_;
210  
211 0 my $rackman = $args->{rackman};
212 0 my $config = $rackman->config;
213  
214 # fetch the credentials
215 my $ilo_password = $rackman->options->{device_password}
216 0 ▶ || $config->val(CONFIG_SECTION, "ilo_password")
Branch: 1 branch
BranchTrueFalseTotal
my $ilo_password = $rackman->options->{"device_password"} || $config->val("device:server:hp_proliant", "ilo_password") or "RackMan"->error("missing password for iLO account 'Administrator'") 0 0 0
Condition0%0/3
$rackman->options->{'device_password'} || $config->val('device:server:hp_proliant', 'ilo_password')
l!l&&r!l&&!r
000
MC/DC0%0/2
$rackman->options->{'device_password'} || $config->val('device:server:hp_proliant', 'ilo_password')
$rackman->options->{'device_password'} $config->val('device:server:hp_proliant', 'ilo_password')
Truth table0%0/3
A || B
A= $rackman->options->{'device_password'}B= $config->val('device:server:hp_proliant', 'ilo_password')
ABresult
1X1
011
000
217 or RackMan->error("missing password for iLO account 'Administrator'");
218 0 ▶ my $admin_password = $config->val(CONFIG_SECTION, "admin_password")
Branch: 1 branch
BranchTrueFalseTotal
my $admin_password = $config->val("device:server:hp_proliant", "admin_password") or "RackMan"->error("missing password for iLO account 'admin'") 0 0 0
219 or RackMan->error("missing password for iLO account 'admin'");
220  
221 # fetch the IP address and FQDN of the iLO subsystem
222 0 my $ilo_addr = $self->ilo_ipv4addr->{addr};
223 0 my $ilo_fqdn = $self->ilo_fqdn;
224 0 my ($ilo_name, $ilo_domain) = split /\./, $ilo_fqdn;
225  
226 # connect to the iLO API
227 0 ▶ print " > configuring iLO subsystem" if $args->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"verbose"} 0 0 0
228 0 my $ilo = Net::ILO->new({
229 address => $ilo_addr,
230 username => "Administrator",
231 password => $ilo_password,
232 });
233  
234 0 ▶ my $ilo_type = $ilo->fw_type or RackMan->error_n($ilo->error);
Branch: 1 branch
BranchTrueFalseTotal
my $ilo_type = $ilo->fw_type or "RackMan"->error_n($ilo->error) 0 0 0
235 0 ▶ print " (" if $args->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"verbose"} 0 0 0
236  
237 # configure serial port
238 0 ▶ print "serial CLI, " if $args->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"verbose"} 0 0 0
239 0 my $serial_speed = $config->val(CONFIG_SECTION, "serial_cli_speed", 5);
240 0 ▶ $ilo->serial_cli_speed($serial_speed)
Branch: 1 branch
BranchTrueFalseTotal
$ilo->serial_cli_speed($serial_speed) or "RackMan"->error_n($ilo->error) 0 0 0
241 or RackMan->error_n($ilo->error);
242  
243 # configure the server name
244 0 ▶ print "server name, " if $args->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"verbose"} 0 0 0
245 0 ▶ $ilo->server_name($self->object_name)
Branch: 1 branch
BranchTrueFalseTotal
$ilo->server_name($self->object_name) or "RackMan"->error_n($ilo->error) 0 0 0
246 or RackMan->error_n($ilo->error);
247  
248 # check if the user "admin" is present
249 0 ▶ print "user 'admin', " if $args->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"verbose"} 0 0 0
250 0 my $user_info = $ilo->get_user("admin");
251  
252 0 ▶ if (not $user_info) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $user_info) { } 0 0 0
253 # if not, create it
254 0 ▶ $ilo->add_user({
Branch: 1 branch
BranchTrueFalseTotal
$ilo->add_user({"name", "admin", "username", "admin", "password", $admin_password, "admin", "Yes", "remote_console_privilege", "Yes", "reset_privilege", "Yes", "virtual_media_privilege", "Yes", "config_ilo_privilege", "Yes"}) or "RackMan"->error_n($ilo->error) 0 0 0
255 name => "admin",
256 username => "admin",
257 password => $admin_password,
258 admin => "Yes",
259 remote_console_privilege => "Yes",
260 reset_privilege => "Yes",
261 virtual_media_privilege => "Yes",
262 config_ilo_privilege => "Yes",
263 }) or RackMan->error_n($ilo->error);
264 }
265 else {
266 # if yes, change its password
267 0 ▶ $ilo->mod_user({
Branch: 1 branch
BranchTrueFalseTotal
$ilo->mod_user({"username", "admin", "password", $admin_password}) or "RackMan"->error_n($ilo->error) 0 0 0
268 username => "admin",
269 password => $admin_password,
270 }) or RackMan->error_n($ilo->error);
271 }
272  
273 # activate the license
274 0 ▶ print "license, " if $args->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"verbose"} 0 0 0
275 0 my $license_key = $config->val(CONFIG_SECTION, "license_key");
276  
277 0 ▶ if ($license_key and not $ilo->license($license_key)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($license_key and not $ilo->license($license_key)) 0 0 0
Condition0%0/3
$license_key and not $ilo->license($license_key)
!ll&&!rl&&r
000
MC/DC0%0/2
$license_key and not $ilo->license($license_key)
$license_key not $ilo->license($license_key)
Truth table0%0/3
A and not(B)
A= $license_keyB= not $ilo->license($license_key)
ABresult
0X0
100
111
278 0 ▶ RackMan->error_n($ilo->error)
Branch: 1 branch
BranchTrueFalseTotal
unless $ilo->error =~ /already active/ 0 0 0
279 unless $ilo->error =~ /already active/;
280 }
281  
282 # complete network settings
283 0 ▶ print "network" if $args->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"verbose"} 0 0 0
284 0 my @ntp_servers = split / +/, $config->val(general => "ntp_servers");
285 0 my @dns_servers = split / +/, $config->val(general => "dns_servers");
286  
287 0 ▶ $ilo->network({
Branch: 1 branch
BranchTrueFalseTotal
$ilo->network({"hostname", $ilo_name, "dhcp_enabled", "no", "domain_name", $ilo_domain, "prim_dns_server", $dns_servers[0], "sec_dns_server", $dns_servers[1], "ter_dns_server", $dns_servers[2], "sntp_server1", $ntp_servers[0], "sntp_server2", $ntp_servers[1], "sntp_server3", $ntp_servers[2]}) or "RackMan"->error_n($ilo->error) 0 0 0
288 hostname => $ilo_name,
289 dhcp_enabled => 'no',
290 domain_name => $ilo_domain,
291 prim_dns_server => $dns_servers[0],
292 sec_dns_server => $dns_servers[1],
293 ter_dns_server => $dns_servers[2],
294 sntp_server1 => $ntp_servers[0],
295 sntp_server2 => $ntp_servers[1],
296 sntp_server3 => $ntp_servers[2],
297 }) or RackMan->error_n($ilo->error);
298  
299 0 ▶ print ") done\n" if $args->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"verbose"} 0 0 0
300 }
301  
302  
303 #
304 # tmpl_params()
305 # -----------
306 0 ▶around tmpl_params => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
307 0 my ($orig, $self) = @_;
308  
309 0 my $name = $self->object_name;
310  
311 # fetch the IP address and FQDN of the iLO subsystem
312 0 my $ilo_addr = $self->ilo_ipv4addr->{addr};
313 0 my $ilo_fqdn = $self->ilo_fqdn;
314 0 my ($ilo_name, $ilo_domain) = split /\./, $ilo_fqdn;
315  
316 # find iLO MAC address
317 0 my @ilo_mac_addr = $self->ilo_mac_addr;
318 0 ▶ RackMan->error("RackObject '$name' lacks an iLO interface")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ilo_mac_addr[0] 0 0 0
319 unless defined $ilo_mac_addr[0];
320 RackMan->error("RackObject '$name' lacks an iLO MAC address")
321 0 ▶ unless defined $ilo_mac_addr[0]{l2address};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ilo_mac_addr[0]{"l2address"} 0 0 0
322  
323 # fetch iLO IPv4 address
324 0 my @ilo_ipv4addr = $self->ilo_ipv4addr;
325 RackMan->error("RackObject '$name' lacks an iLO IPv4 address")
326 0 ▶ unless defined $ilo_ipv4addr[0]{addr};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ilo_ipv4addr[0]{"addr"} 0 0 0
327  
328 # iLO IPv4 network parameters
329 0 my $ilo_gateway = $self->default_ilo_ipv4_gateway;
330 0 my $iaddr = NetAddr::IP->new($ilo_ipv4addr[0]{addr}, $ilo_gateway->{masklen});
331 0 my $ilo_netmask = $iaddr->mask;
332  
333 my %params = (
334 $self->$orig,
335 ilo_fqdn => $ilo_fqdn,
336 ilo_name => $ilo_name,
337 ilo_ip => $ilo_ipv4addr[0]{addr},
338 ilo_mac => $ilo_mac_addr[0]{l2address_text},
339 ilo_netmask => $ilo_netmask,
340 ilo_network => $ilo_gateway->{network},
341 0 ▶ ilo_gateway => $ilo_gateway->{addr} || "0.0.0.0",
Condition0%0/2
$ilo_gateway->{'addr'} || '0.0.0.0'
l!l
00
MC/DC0%0/1
$ilo_gateway->{'addr'} || '0.0.0.0'
$ilo_gateway->{'addr'}
Truth table0%0/2
A
A= $ilo_gateway->{'addr'}
Aresult
11
00
342 );
343  
344 0 return %params
345 2 };
346  
347  
348  
349 2 __PACKAGE__
350  
351 __END__
« blib/lib/RackMan/Utils.pm ↑ Summary bin/cisco-status »