blib/lib/Rex/Rancher/Node.pm

filter: Statementstmt 88.9% 129 / 145 Branchbran 83.9% 47 / 56 Conditioncond 76.9% 20 / 26 MC/DCmcdc 56.2% 9 / 16 Subroutinesub 83.3% 20 / 24 Podpod 100% 1 / 1 totaltotal 84.3% 226 / 268 CC 40 SCAR 17.0
CC
40
Cov
86%
prepare_node
12.1
_static_hostname
6.0
_setup_ntp
6.0
CRAP 5.5
« lib/Rex/Rancher/Server.pm ↑ Summary lib/Rex/Rancher/Options.pm »
1 # ABSTRACT: Linux node preparation for Rancher Kubernetes distributions (RKE2/K3s)
2  
3 package Rex::Rancher::Node;
4 40 our $VERSION = '0.004';
5 40 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 40 ▶use Rex::Commands::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use Rex::Commands::Gather;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use Rex::Commands::Host;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 40 ▶use Rex::Commands::Pkg;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 40 ▶use Rex::Commands::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 40 ▶use Rex::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 40 require Rex::Exporter;
16 40 ▶use base qw(Rex::Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 40 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 40 @EXPORT = qw(
21 prepare_node
22 );
23  
24  
25 17 ▶sub prepare_node {
Subroutine prepare_node: called CC=12 SCAR=24.9
Pod documented
26 17 my (%opts) = @_;
27  
28 17 my $hostname = $opts{hostname};
29 17 my $domain = $opts{domain};
30 17 ▶ my $timezone = $opts{timezone} // 'UTC';
Condition100%2/2
$opts{"timezone"} // "UTC"
l!l
107
MC/DC100%1/1
$opts{"timezone"} // "UTC"
$opts{"timezone"}
Truth table100%2/2
A
A= $opts{"timezone"}
Aresult
11
00
31 17 ▶ my $locale = $opts{locale} // 'en_US.UTF-8';
Condition100%2/2
$opts{"locale"} // "en_US.UTF-8"
l!l
512
MC/DC100%1/1
$opts{"locale"} // "en_US.UTF-8"
$opts{"locale"}
Truth table100%2/2
A
A= $opts{"locale"}
Aresult
11
00
32 17 ▶ my $ntp = exists $opts{ntp} ? $opts{ntp} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'ntp'} ? : 16 1 17
33  
34 17 ▶ my $fqdn = ($hostname && $domain) ? "$hostname.$domain" : undef;
Branch: 1 branch
BranchTrueFalseTotal
$hostname && $domain ? : 0 17 17
Condition66%2/3
$hostname and $domain
!ll&&!rl&&r
1610
MC/DC0%0/2
$hostname and $domain
$hostname $domain
Truth table66%2/3
A and B
A= $hostnameB= $domain
ABresult
0X0
100
111
35  
36 # Before anything runs: the locale and the timezone end up in shell
37 # commands (and the timezone in a path under /usr/share/zoneinfo).
38 17 ▶ die "locale must look like en_US.UTF-8 (language_TERRITORY.charset\@modifier: "
Branch: 1 branch
BranchTrueFalseTotal
unless $locale =~ /\A[A-Za-z0-9_]+(?:\.[A-Za-z0-9-]+)?(?:\@[A-Za-z0-9]+)?\z/u 5 12 17
39 . "letters, digits, _ and -), got '$locale'\n"
40 unless $locale =~ /\A[A-Za-z0-9_]+(?:\.[A-Za-z0-9-]+)?(?:\@[A-Za-z0-9]+)?\z/;
41 12 ▶ die "timezone must look like Europe/Berlin, UTC or Etc/GMT+5 (a zoneinfo "
Branch: 1 branch
BranchTrueFalseTotal
unless $timezone =~ m[\A[A-Za-z][A-Za-z0-9_+-]*(?:/[A-Za-z][A-Za-z0-9_+-]*)*\z]u 9 3 12
42 . "name: parts of letters, digits, _, + and -, each starting with a "
43 . "letter, separated by /), got '$timezone'\n"
44 unless $timezone =~ m{\A[A-Za-z][A-Za-z0-9_+-]*(?:/[A-Za-z][A-Za-z0-9_+-]*)*\z};
45  
46 3 ▶ Rex::Logger::info("Preparing node " . ($fqdn // "(unnamed)") . " for Kubernetes");
Condition50%1/2
$fqdn // "(unnamed)"
l!l
03
MC/DC0%0/1
$fqdn // "(unnamed)"
$fqdn
Truth table50%1/2
A
A= $fqdn
Aresult
11
00
47  
48 3 _install_base_packages();
49 3 ▶ if ($hostname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hostname) 1 2 3
50 1 _set_hostname($hostname, $fqdn);
51 1 _set_hosts_entry($hostname, $fqdn);
52 }
53 3 _set_timezone($timezone);
54 3 _set_locale($locale);
55 3 ▶ _setup_ntp() if $ntp;
Branch: 1 branch
BranchTrueFalseTotal
if $ntp 1 2 3
56 3 _disable_swap();
57 3 _load_kernel_modules();
58 3 _configure_sysctl();
59  
60 3 ▶ Rex::Logger::info("Node preparation complete" . ($fqdn ? " for $fqdn" : ""));
Branch: 1 branch
BranchTrueFalseTotal
$fqdn ? : 0 3 3
61 }
62  
63 0 ▶sub _install_base_packages {
Subroutine _install_base_packages: not called CC=2 SCAR=17.9
64 0 Rex::Logger::info("Installing base packages");
65 0 ▶ if (is_debian()) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_debian()) 0 0 0
66 # Stop automatic apt services first — on a fresh Hetzner boot,
67 # unattended-upgrades holds /var/lib/dpkg/lock-frontend and apt-get
68 # fails immediately (DPkg::Lock::Timeout only covers the dpkg lock,
69 # not the apt frontend lock).
70 0 run "systemctl stop unattended-upgrades apt-daily.service apt-daily-upgrade.service 2>/dev/null || true",
71 auto_die => 0;
72 0 run "apt-get -o DPkg::Lock::Timeout=120 update -q", auto_die => 0;
73 }
74 0 pkg ["curl", "ca-certificates"], ensure => "present";
75 }
76  
77 # A re-run must not rename a running node: without node-name in config.yaml
78 # rke2/k3s register the kernel hostname, and setting the short name over the
79 # FQDN a provider or installer wrote changes it at the next start (k86).
80 # Hostnames are case-insensitive, and the kubelet lowercases the node name.
81 29 ▶sub _set_hostname {
Subroutine _set_hostname: called CC=5 SCAR=16.1
82 29 my ($hostname, $fqdn) = @_;
83 29 my $static = _static_hostname();
84 29 ▶ if (defined $static && lc((split /\./, $static, 2)[0]) eq lc $hostname) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $static and lc((split(/\./u, $static, 2))[0]) eq lc $hostname) 9 20 29
Condition100%3/3
defined $static and lc((split(/\./u, $static, 2))[0]) eq lc $hostname
!ll&&!rl&&r
8129
MC/DC100%2/2
defined $static and lc((split(/\./u, $static, 2))[0]) eq lc $hostname
defined $static lc((split(/\./u, $static, 2))[0]) eq lc $hostname
Truth table100%3/3
A and B
A= defined $staticB= lc((split(/\./u, $static, 2))[0]) eq lc $hostname
ABresult
0X0
100
111
85 9 ▶ Rex::Logger::info("Static hostname is already $static, leaving it"
Branch: 1 branch
BranchTrueFalseTotal
lc $static eq lc $hostname ? : 3 6 9
86 . (lc $static eq lc $hostname ? "" : " (first label matches $hostname)"));
87 9 return;
88 }
89 20 Rex::Logger::info("Setting hostname to $hostname");
90 20 ▶ if (can_run("hostnamectl")) {
Branch: 1 branch
BranchTrueFalseTotal
if (can_run('hostnamectl')) { } 10 10 20
91 10 run "hostnamectl set-hostname $hostname", auto_die => 0;
92 }
93 else {
94 10 file "/etc/hostname", content => "$hostname\n";
95 10 run "hostname $hostname", auto_die => 0;
96 }
97 }
98  
99 # The static hostname from /etc/hostname: the file hostnamectl persists it in
100 # and the fallback above writes, readable over an exec channel without SFTP,
101 # D-Bus or a systemd-version-specific hostnamectl flag. The first line that
102 # is neither blank nor a # comment, as systemd reads it; undef when the file
103 # is missing, unreadable or holds no name.
104 29 ▶sub _static_hostname {
Subroutine _static_hostname: called CC=6 SCAR=17.9
105 29 my $out = run "cat /etc/hostname 2>/dev/null", auto_die => 0;
106 29 ▶ return if $? != 0 || !defined $out;
Branch: 1 branch
BranchTrueFalseTotal
if $? != 0 or not defined $out 2 27 29
Condition66%2/3
$? != 0 or not defined $out
l!l&&r!l&&!r
2027
MC/DC50%1/2
$? != 0 or not defined $out
$? != 0 not defined $out
Truth table66%2/3
A or not(B)
A= $? != 0B= not defined $out
ABresult
1X1
011
000
107 27 for my $line (split /\n/, $out) {
108 29 $line =~ s/\A\s+|\s+\z//g;
109 29 ▶ return $line if length $line && $line !~ /\A#/;
Branch: 1 branch
BranchTrueFalseTotal
if length $line and not $line =~ /\A#/u 21 8 29
Condition100%3/3
length $line and not $line =~ /\A#/u
!ll&&!rl&&r
5321
MC/DC100%2/2
length $line and not $line =~ /\A#/u
length $line not $line =~ /\A#/u
Truth table100%3/3
A and not(B)
A= length $lineB= not $line =~ /\A#/u
ABresult
0X0
100
111
110 }
111 6 return;
112 }
113  
114 3 ▶sub _set_hosts_entry {
Subroutine _set_hosts_entry: called CC=3 SCAR=11.0
115 3 my ($hostname, $fqdn) = @_;
116 3 ▶ if ($fqdn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fqdn) 1 2 3
117 1 Rex::Logger::info("Configuring /etc/hosts for $fqdn");
118 1 host_entry $fqdn,
119 ensure => "present",
120 ip => "127.0.1.1",
121 aliases => [$hostname];
122 1 return;
123 }
124 # Without a domain only add a missing name: host_entry replaces every line
125 # naming the host -- the provider's public-IP line, or a
126 # "127.0.0.1 localhost <hostname>" line and localhost with it.
127 2 ▶ if (get_host($hostname)) {
Branch: 1 branch
BranchTrueFalseTotal
if (get_host($hostname)) 1 1 2
128 1 Rex::Logger::info("/etc/hosts already names $hostname, leaving it");
129 1 return;
130 }
131 1 Rex::Logger::info("Configuring /etc/hosts for $hostname");
132 1 host_entry $hostname,
133 ensure => "present",
134 ip => "127.0.1.1";
135 }
136  
137 # The timezone is validated in prepare_node: no quote can end the single
138 # quotes, and no part can be . or .. to leave /usr/share/zoneinfo.
139 14 ▶sub _set_timezone {
Subroutine _set_timezone: called CC=2 SCAR=6.9
140 14 my ($timezone) = @_;
141 14 Rex::Logger::info("Setting timezone to $timezone");
142 14 ▶ if (can_run("timedatectl")) {
Branch: 1 branch
BranchTrueFalseTotal
if (can_run('timedatectl')) { } 7 7 14
143 7 run "timedatectl set-timezone '".$timezone."'", auto_die => 0;
144 }
145 else {
146 7 run "ln -sf '/usr/share/zoneinfo/".$timezone."' /etc/localtime", auto_die => 0;
147 7 file "/etc/timezone", content => "$timezone\n";
148 }
149 }
150  
151 9 ▶sub _set_locale {
Subroutine _set_locale: called CC=3 SCAR=11.2
152 9 my ($locale) = @_;
153 9 Rex::Logger::info("Setting locale to $locale");
154 # Generate first: localectl refuses a locale that is not installed.
155 9 ▶ _generate_locale($locale) if is_debian();
Branch: 1 branch
BranchTrueFalseTotal
if is_debian() 8 1 9
156 9 ▶ if (can_run("localectl")) {
Branch: 1 branch
BranchTrueFalseTotal
if (can_run('localectl')) { } 9 0 9
157 9 run "localectl set-locale LANG=$locale", auto_die => 0;
158 }
159 else {
160 0 file "/etc/default/locale", content => "LANG=$locale\n";
161 }
162 }
163  
164 # Debian/Ubuntu only. Debian's locale-gen ignores its arguments and builds
165 # what /etc/locale.gen enables, Ubuntu's generates the locale it is given;
166 # enabling the line and naming the locale covers both.
167 8 ▶sub _generate_locale {
Subroutine _generate_locale: called CC=5 SCAR=16.2
168 8 my ($locale) = @_;
169 8 ▶ return if $locale =~ /^(?:C|POSIX)(?:\.|$)/;
Branch: 1 branch
BranchTrueFalseTotal
if $locale =~ /^(?:C|POSIX)(?:\.|$)/u 1 7 8
170 7 ▶ unless (can_run("locale-gen")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (can_run("locale-gen")) 1 6 7
171 1 Rex::Logger::info("locale-gen not installed, $locale is not generated", 'warn');
172 1 return;
173 }
174 6 my ($name, $charset) = _locale_gen_name($locale);
175 6 ▶ run _enable_locale_cmd($name.' '.$charset, '/etc/locale.gen'), auto_die => 0
Branch: 1 branch
BranchTrueFalseTotal
if $charset 6 0 6
176 if $charset;
177 6 run "locale-gen $name", auto_die => 0;
178 6 ▶ Rex::Logger::info("locale-gen $name failed", 'warn') if $? != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $? != 0 0 6 6
179 }
180  
181 # The locale as locale.gen spells it, and its charset there (undef without
182 # one): glibc accepts de_DE.utf8 for de_DE.UTF-8, but locale.gen lists only
183 # "de_DE.UTF-8 UTF-8", and enabling "de_DE.utf8 utf8" builds nothing.
184 6 ▶sub _locale_gen_name {
Subroutine _locale_gen_name: called CC=5 SCAR=16.6
185 6 my ($locale) = @_;
186 6 my ($base, $charset, $modifier) = $locale =~ /\A([^.@]+)(?:\.([^@]+))?(\@.+)?\z/;
187 6 ▶ return ($locale) unless defined $charset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $charset 0 6 6
188 6 $charset = uc $charset;
189 6 ▶ $charset = $charset =~ /\AUTF-?8\z/ ? 'UTF-8'
Branch: 2 branches
BranchTrueFalseTotal
$charset =~ /\AUTF-?8\z/u ? : 4 2 6
$charset =~ /\AISO-?8859-?(\d+)\z/u ? : 2 0 2
190 : $charset =~ /\AISO-?8859-?(\d+)\z/ ? "ISO-8859-$1"
191 : $charset;
192 6 ▶ return ($base.'.'.$charset.($modifier // ''), $charset);
Condition50%1/2
$modifier // ""
l!l
06
MC/DC0%0/1
$modifier // ""
$modifier
Truth table50%1/2
A
A= $modifier
Aresult
11
00
193 }
194  
195 # Uncomment "<locale> <charset>" in locale.gen, append it when absent. The
196 # line is a validated locale (see prepare_node): no quote can end the
197 # shell's single quotes, and every ERE metacharacter is escaped.
198 6 ▶sub _enable_locale_cmd {
Subroutine _enable_locale_cmd: called CC=1 SCAR=0.0
199 6 my ($line, $file) = @_;
200 6 (my $re = $line) =~ s/([.\[\]()*+?{}|^\$\\])/\\$1/g;
201 6 return 'if [ -f '.$file.' ]; then '
202 .q{sed -i -E 's/^#\s*(}.$re.q{)\s*$/\1/' }.$file.'; '
203 .q{grep -qE '^}.$re.q{\s*$' }.$file.q{ || echo '}.$line.q{' >> }.$file.'; fi';
204 }
205  
206 5 ▶sub _setup_ntp {
Subroutine _setup_ntp: called CC=6 SCAR=17.9
207 5 my $synced = run "timedatectl show --property=NTPSynchronized --value 2>/dev/null", auto_die => 0;
208 5 ▶ if (defined $synced && $synced =~ /^\s*yes\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $synced and $synced =~ /^\s*yes\s*$/u) 1 4 5
Condition66%2/3
defined $synced and $synced =~ /^\s*yes\s*$/u
!ll&&!rl&&r
041
MC/DC50%1/2
defined $synced and $synced =~ /^\s*yes\s*$/u
defined $synced $synced =~ /^\s*yes\s*$/u
Truth table66%2/3
A and B
A= defined $syncedB= $synced =~ /^\s*yes\s*$/u
ABresult
0X0
100
111
209 1 Rex::Logger::info("Clock already NTP-synchronized, not installing chrony");
210 1 return;
211 }
212  
213 4 Rex::Logger::info("Installing and enabling chrony for NTP");
214 4 ▶ if (eval { pkg ["chrony"], ensure => "present"; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { pkg(["chrony"], "ensure", "present"); 1 } }) 2 2 4
215 2 run "systemctl enable chronyd 2>/dev/null || systemctl enable chrony 2>/dev/null", auto_die => 0;
216 2 run "systemctl start chronyd 2>/dev/null || systemctl start chrony 2>/dev/null", auto_die => 0;
217 2 return;
218 }
219 2 my $err = $@;
220 2 chomp $err;
221 2 Rex::Logger::info("chrony install failed ($err), falling back to systemd-timesyncd", 'warn');
222  
223 2 run "systemctl enable --now systemd-timesyncd 2>/dev/null", auto_die => 0;
224 2 my $active = run "systemctl is-active systemd-timesyncd 2>/dev/null", auto_die => 0;
225 2 ▶ if (defined $active && $active =~ /^\s*active\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $active and $active =~ /^\s*active\s*$/u) 1 1 2
Condition66%2/3
defined $active and $active =~ /^\s*active\s*$/u
!ll&&!rl&&r
011
MC/DC50%1/2
defined $active and $active =~ /^\s*active\s*$/u
defined $active $active =~ /^\s*active\s*$/u
Truth table66%2/3
A and B
A= defined $activeB= $active =~ /^\s*active\s*$/u
ABresult
0X0
100
111
226 1 Rex::Logger::info("Using systemd-timesyncd for NTP");
227 1 return;
228 }
229 # Not fatal (k42): the node works, only its clock may drift. Loud, because
230 # the skew shows up later as TLS and etcd errors far from this step.
231 1 Rex::Logger::info("NO TIME SYNCHRONIZATION IS ACTIVE on this node: chrony install "
232 . "failed ($err) and systemd-timesyncd is not active (the RHEL family does not "
233 . "ship it). Clock skew between nodes breaks certificate validation and etcd; "
234 . "set up NTP on this host yourself", 'warn');
235 }
236  
237 0 ▶sub _disable_swap {
Subroutine _disable_swap: not called CC=1 SCAR=6.9
238 0 Rex::Logger::info("Disabling swap");
239 0 run "swapoff -a", auto_die => 0;
240 0 delete_lines_matching "/etc/fstab", matching => qr/\sswap\s/;
241 }
242  
243 0 ▶sub _load_kernel_modules {
Subroutine _load_kernel_modules: not called CC=1 SCAR=6.9
244 0 Rex::Logger::info("Loading required kernel modules");
245 0 run "modprobe br_netfilter", auto_die => 0;
246 0 run "modprobe overlay", auto_die => 0;
247 0 file "/etc/modules-load.d/kubernetes.conf", content => "br_netfilter\noverlay\n";
248 }
249  
250 0 ▶sub _configure_sysctl {
Subroutine _configure_sysctl: not called CC=1 SCAR=6.9
251 0 Rex::Logger::info("Configuring kernel parameters for Kubernetes");
252 0 file "/etc/sysctl.d/99-kubernetes.conf",
253 content => join("\n",
254 "net.bridge.bridge-nf-call-iptables = 1",
255 "net.bridge.bridge-nf-call-ip6tables = 1",
256 "net.ipv4.ip_forward = 1",
257 ) . "\n";
258 0 run "sysctl --system", auto_die => 0;
259 }
260  
261 40 1;
262  
263 __END__
« lib/Rex/Rancher/Server.pm ↑ Summary lib/Rex/Rancher/Options.pm »