blib/lib/App/FuguVM/Console.pm untested

filter: Statementstmt 0.0% 0 / 69 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 131 CC 13 SCAR 27.9
CC
13
Cov
0%
attach
42.0
run_script
20.0
_expect
12.0
CRAP 16.3
1 # ex:ts=8 sw=4:
2 # $OpenBSD$
3 #
4 # Copyright (c) 2024 Dick Olsson <hi@senzilla.io>
5 #
6 # Permission to use, copy, modify, and distribute this software for any
7 # purpose with or without fee is hereby granted, provided that the above
8 # copyright notice and this permission notice appear in all copies.
9 #
10 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  
18 use v5.36;
19  
20 package App::FuguVM::Console;
21 our $VERSION = '0.3.1';
22  
23 use Fugu::File;
24 use Fugu::Log;
25 use Fugu::Process;
26  
27 # App::FuguVM::Console - drive the serial console of a guest.
28 #
29 # The console answers no protocol, so an expect(1) script types at it.
30 # The module runs the installer that way, and it runs any other script
31 # the operator names. The scripts ship under share/fuguvm/expect, and
32 # Fugu::File resolves them against the install root.
33  
34 # Where the shipped scripts live, relative to the root of the tree.
35 use constant SCRIPT_DIR => 'share/fuguvm/expect';
36  
37 sub new ( $class, %args )
38 {
39 my $self = bless {
40 host => $args{host} // 'localhost',
41 port => $args{port},
42 }, $class;
43  
44 return $self;
45 }
46  
47 # $class_or_self->script_path($script_name):
48 # Return the path of a shipped expect script, or undef.
49 #
50 # The method also works on the class. App::FuguVM::DiskCache hashes
51 # the installer script into its cache key, so it must resolve the
52 # script the same way run_install does.
53 sub script_path ( $self, $script_name )
54 {
55 return Fugu::File->share_path(
56 SCRIPT_DIR . "/$script_name",
57 from => __FILE__,
58 dist => 'App-FuguVM'
59 );
60 }
61  
62 # $self->run_script($script, @args):
63 # Run one expect script against the console of this VM. The
64 # method takes a path, or the name of a shipped script.
65 sub run_script ( $self, $script, @args )
66 {
67 my $path = -f $script ? $script : $self->script_path($script);
68 unless ( defined $path && -f $path ) {
69 Fugu::Log->default->error( 'Expect script not found: %s',
70 $script );
71 return 0;
72 }
73 unless ( -x $path ) {
74 Fugu::Log->default->error( 'Expect script not executable: %s',
75 $path );
76 return 0;
77 }
78  
79 return $self->_expect( $path, @args );
80 }
81  
82 # $self->run_install($config):
83 # Drive a complete OpenBSD installation. The script reads its
84 # arguments by position: the root password, the proxy URL, the
85 # architecture, and the verify word.
86 sub run_install ( $self, $config )
87 {
88 my $script = $self->script_path('install.exp');
89 unless ( defined $script ) {
90 Fugu::Log->default->error('Install script not found');
91 return 0;
92 }
93  
94 return $self->_expect(
95 $script,
96 $config->{root_password} // 'openbsd',
97 $config->{proxy_url} // 'none',
98 $config->{arch},
99 $config->{verify} // 'yes',
100 );
101 }
102  
103 # $self->run_autoinstall($config):
104 # Start an autoinstall(8) over the serial console. The script
105 # answers the install prompt and the response-file prompt, and
106 # the response file answers everything else. The configuration
107 # gives the response-file URL and the architecture.
108 #
109 # The method calls _expect, like run_install, and it must not use
110 # run_script: run_script needs the execute bit, and an installed
111 # share tree does not keep it.
112 sub run_autoinstall ( $self, $config )
113 {
114 my $script = $self->script_path('autoinstall.exp');
115 unless ( defined $script ) {
116 Fugu::Log->default->error('Autoinstall script not found');
117 return 0;
118 }
119  
120 return $self->_expect( $script, $config->{autoinstall_url},
121 $config->{arch} );
122 }
123  
124 # $self->attach:
125 # Attach the terminal of the caller to this console, with
126 # telnet(1). The method returns the exit code of telnet(1).
127 #
128 # The guest never closes the console, so no end of file ends the
129 # attachment. The operator ends it with the telnet escape key,
130 # Ctrl-], and then 'quit'. telnet(1) leaves the terminal raw
131 # when a signal kills it, so the method saves the terminal
132 # attributes first and restores them on every exit path. The
133 # restore is idempotent. A signal that kills the tool also ends
134 # telnet(1), so no raw orphan keeps the terminal.
135 sub attach ($self)
136 {
137 # POSIX loads lazily, as App::FuguVM::Guest already does it.
138 require POSIX;
139  
140 my $saved;
141 if ( POSIX::isatty(*STDIN) ) {
142 $saved = POSIX::Termios->new;
143 $saved->getattr( fileno(STDIN) );
144 }
145 my $restore = sub {
146 $saved->setattr( fileno(STDIN), POSIX::TCSANOW() )
147 if defined $saved;
148 };
149  
150 # A fork and an exec, not Fugu::Process->run: the child must
151 # keep the terminal of the caller, and the parent must know
152 # the child process ID, so a signal handler can end the child.
153 my $pid = fork;
154 if ( !defined $pid ) {
155 Fugu::Log->default->error( 'Cannot fork: %s', $! );
156 return 1;
157 }
158 if ( $pid == 0 ) {
159 exec( 'telnet', $self->{host}, $self->{port} );
160  
161 # The exec failed. Exit like an absent command.
162 POSIX::_exit(1);
163 }
164  
165 # The handler ends the child, restores the terminal, resets
166 # itself, and raises the same signal again. Thus one kill of
167 # the tool ends the whole session, and the exit status of the
168 # tool stays honest.
169 local @SIG{qw(INT TERM HUP)};
170 for my $name (qw(INT TERM HUP)) {
171 $SIG{$name} = sub ($signal) {
172 kill $signal, $pid;
173 $restore->();
174 $SIG{$signal} = 'DEFAULT';
175 kill $signal, $$;
176 };
177 }
178  
179 my $reaped = waitpid( $pid, 0 );
180 my $status = $reaped == $pid ? $? : -1;
181  
182 $restore->();
183  
184 # Fugu::Process->exit_code maps a failure to 1, and a signal
185 # death to 128 plus the signal number.
186 return Fugu::Process->exit_code($status);
187 }
188  
189 # $self->_expect($script, @args):
190 # Run expect(1) on the script, with the host and the port first.
191 # The scripts read their timeout from FUGUVM_TIMEOUT in the
192 # environment themselves, and each carries its own default.
193 #
194 # The run is a passthrough. An installation writes for tens of
195 # minutes, and an operator who waits needs to see the progress
196 # while it happens, not after.
197 sub _expect ( $self, $script, @args )
198 {
199 my $result = Fugu::Process->run(
200 cmd =>
201 [ 'expect', $script, $self->{host}, $self->{port}, @args ],
202 passthrough => 1,
203 );
204  
205 unless ( $result->{success} ) {
206 Fugu::Log->default->error( 'expect %s failed: %s',
207 $script,
208 $result->{error} // "exit $result->{exit_code}" );
209 }
210  
211 return $result->{success} ? 1 : 0;
212 }
213  
214 1;