File Coverage

blib/lib/Tapper/Remote/Config.pm
Criterion Covered Total %
statement 54 68 79.4
branch 13 28 46.4
condition 2 6 33.3
subroutine 12 12 100.0
pod 3 3 100.0
total 84 117 71.7


line stmt bran cond sub pod time code
1             package Tapper::Remote::Config;
2             BEGIN {
3 3     3   75337 $Tapper::Remote::Config::AUTHORITY = 'cpan:TAPPER';
4             }
5             {
6             $Tapper::Remote::Config::VERSION = '4.1.1';
7             }
8              
9 3     3   132 use strict;
  3         7  
  3         261  
10 3     3   19 use warnings;
  3         6  
  3         354  
11              
12 3     3   4124 use Getopt::Long;
  3         51262  
  3         21  
13 3     3   2731 use Moose;
  3         1103527  
  3         32  
14 3     3   28968 use Net::TFTP;
  3         143347  
  3         104  
15 3     3   39 use Socket; # for inet_aton and gethostbyname
  3         7  
  3         1992  
16 3     3   2938 use Sys::Hostname;
  3         4101  
  3         170  
17 3     3   9138 use YAML::Syck;
  3         11053  
  3         2642  
18              
19              
20              
21              
22              
23             sub get_tapper_host
24             {
25 1     1 1 1 my ($host, $port);
26             # try kernel command line
27 1         71 open my $FH,'<','/proc/cmdline';
28 1         32 my $cmd_line = <$FH>;
29 1         14 close $FH;
30 1         4 ($host,undef,$port) = $cmd_line =~ m/tapper_host\s*=\s*(\w+)(:(\d+))?/;
31 1 50       4 return($host,$port) if $host;
32              
33 1 50       5 if (my ($ip) = $cmd_line =~ m/tapper_ip\s*=\s*(\S+)/) {
34 0         0 my $iaddr = inet_aton($ip);
35 0         0 $host = gethostbyaddr($iaddr, AF_INET);
36              
37 0         0 ($port) = $cmd_line =~ m/tapper_port\s*=\s*(\d+)/;
38 0 0       0 return($host,$port) if $host;
39             }
40              
41              
42             # try %ENV
43 1 50       7 if ($ENV{TAPPER_MCP_SERVER}) {
44 0         0 $host = $ENV{TAPPER_MCP_SERVER};
45 0         0 $port = $ENV{TAPPER_MCP_PORT};
46 0         0 return ($host, $port);
47             }
48              
49             # try multicast
50             }
51              
52              
53              
54             sub gethostname
55             {
56 2     2 1 4 my ($self) = @_;
57 2         13 my $hostname = Sys::Hostname::hostname();
58 2 50       42 if ($hostname =~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
    50          
59 0 0 0     0 ($hostname) = gethostbyaddr(inet_aton($hostname), AF_INET) or ( print("Can't get hostname: $!") and exit 1);
60 0         0 $hostname =~ s/^(\w+?)\..+$/$1/;
61 0         0 system("hostname", "$hostname");
62             } elsif ($hostname =~ m/^([^\.]+)\./) {
63 0         0 $hostname = $1;
64             }
65 2         6 return $hostname;
66             }
67              
68              
69              
70              
71             sub get_local_data
72             {
73 2     2 1 1055 my ($self, $state) = @_;
74             # logger will usually be initialised by caller
75 2         6 my $tmpcfg={};
76              
77 2         5 my $config_file_name = '/etc/tapper';
78 2 50       11 $config_file_name = $ENV{TAPPER_CONFIG} if $ENV{TAPPER_CONFIG};
79              
80 2         10 my $hostname = $self->gethostname();
81              
82              
83 2         4 my ($server, $port, $help);
84 2         14 Getopt::Long::GetOptions("host=s" => \$server,
85             "port=s" => \$port,
86             "config=s" => \$config_file_name,
87             "help|h" => \$help,);
88 2 50       948 die "Usage: $0 [--host=host --port=port --config=file]\n" if $help;
89              
90 2 100 66     38 if ($state eq 'install' or (not -e $config_file_name)) {
91 1 50       7 ($server, $port) = $self->get_tapper_host() if not $server;
92 1         11 my $tftp = Net::TFTP->new($server);
93 1 50       23 $tftp->get("$hostname-$state", $config_file_name) or return("Can't get local data.",$tftp->error);
94 0         0 $tmpcfg->{server} = $server;
95 0         0 $tmpcfg->{port} = $port;
96 0         0 $tmpcfg->{hostname} = $hostname;
97             }
98              
99 1 50       6 my $config = YAML::Syck::LoadFile($config_file_name) or return ("Can't parse config received from server");
100 1         1078 $config->{filename} = $config_file_name;
101 1 50       7 $config->{hostname} = $hostname unless $config->{hostname};
102 1         9 %$config=(%$config, %$tmpcfg);
103              
104 1         9 return $config;
105             }
106              
107             1;
108              
109             __END__
110             =pod
111              
112             =encoding utf-8
113              
114             =head1 NAME
115              
116             Tapper::Remote::Config
117              
118             =head1 SYNOPSIS
119              
120             use Tapper::Remote::Config;
121              
122             =head1 NAME
123              
124             Tapper::Remote::Config - Get configuration from Tapper host
125              
126             =head1 FUNCTIONS
127              
128             =head2 get_tapper_host
129              
130             Get hostname of tapper MCP host from kernel boot parameters.
131              
132             @returnlist ($host,port) - string, int - hostname and port of MCP server
133              
134             =head2 gethostname
135              
136             This function returns the host name of the machine. When NFS root is
137             used together with DHCP the hostname set in the kernel usually equals
138             the IP address received from DHCP as a string. In this case the kernel
139             hostname is set to the DNS hostname associated to this IP address.
140              
141             @return hostname of the machine as set in the kernel
142              
143             =head2 get_local_data
144              
145             Get local data needed for all tools running locally on NFS. The function tries
146             to get the MCP host and fetches the config from there. This reduces any need
147             for configuration outside MCP host and thus allows to use unchanged NFS root
148             file systems for both testing and production, with different MCP servers and
149             so on.
150              
151             @param string - state
152              
153             @return success - hash reference containing the config
154             @return error - error string
155              
156             =head1 AUTHOR
157              
158             AMD OSRC Tapper Team <tapper@amd64.org>
159              
160             =head1 COPYRIGHT AND LICENSE
161              
162             This software is Copyright (c) 2012 by Advanced Micro Devices, Inc..
163              
164             This is free software, licensed under:
165              
166             The (two-clause) FreeBSD License
167              
168             =cut
169