App-FuguVM

 view release on metacpan or  search on metacpan

lib/App/FuguVM/Console.pm  view on Meta::CPAN

use Fugu::Log;
use Fugu::Process;

# App::FuguVM::Console - drive the serial console of a guest.
#
# The console answers no protocol, so an expect(1) script types at it.
# The module runs the installer that way, and it runs any other script
# the operator names. The scripts ship under share/fuguvm/expect, and
# Fugu::File resolves them against the install root.

# Where the shipped scripts live, relative to the root of the tree.
use constant SCRIPT_DIR => 'share/fuguvm/expect';

sub new ( $class, %args )
{
	my $self = bless {
		host => $args{host} // 'localhost',
		port => $args{port},
	}, $class;

	return $self;
}

# $class_or_self->script_path($script_name):
#	Return the path of a shipped expect script, or undef.
#
#	The method also works on the class. App::FuguVM::DiskCache hashes
#	the installer script into its cache key, so it must resolve the
#	script the same way run_install does.
sub script_path ( $self, $script_name )
{
	return Fugu::File->share_path(
		SCRIPT_DIR . "/$script_name",
		from => __FILE__,
		dist => 'App-FuguVM'
	);
}

# $self->run_script($script, @args):
#	Run one expect script against the console of this VM. The
#	method takes a path, or the name of a shipped script.
sub run_script ( $self, $script, @args )
{
	my $path = -f $script ? $script : $self->script_path($script);
	unless ( defined $path && -f $path ) {
		Fugu::Log->default->error( 'Expect script not found: %s',
			$script );
		return 0;
	}
	unless ( -x $path ) {
		Fugu::Log->default->error( 'Expect script not executable: %s',
			$path );
		return 0;
	}

	return $self->_expect( $path, @args );
}

# $self->run_install($config):
#	Drive a complete OpenBSD installation. The script reads its
#	arguments by position: the root password, the proxy URL, the
#	architecture, and the verify word.
sub run_install ( $self, $config )
{
	my $script = $self->script_path('install.exp');
	unless ( defined $script ) {
		Fugu::Log->default->error('Install script not found');
		return 0;
	}

	return $self->_expect(
		$script,
		$config->{root_password} // 'openbsd',
		$config->{proxy_url}     // 'none',
		$config->{arch},
		$config->{verify} // 'yes',
	);
}

# $self->run_autoinstall($config):
#	Start an autoinstall(8) over the serial console. The script
#	answers the install prompt and the response-file prompt, and
#	the response file answers everything else. The configuration
#	gives the response-file URL and the architecture.
#
#	The method calls _expect, like run_install, and it must not use
#	run_script: run_script needs the execute bit, and an installed
#	share tree does not keep it.
sub run_autoinstall ( $self, $config )
{
	my $script = $self->script_path('autoinstall.exp');
	unless ( defined $script ) {
		Fugu::Log->default->error('Autoinstall script not found');
		return 0;
	}

	return $self->_expect( $script, $config->{autoinstall_url},
		$config->{arch} );
}

# $self->attach:
#	Attach the terminal of the caller to this console, with
#	telnet(1). The method returns the exit code of telnet(1).
#
#	The guest never closes the console, so no end of file ends the
#	attachment. The operator ends it with the telnet escape key,
#	Ctrl-], and then 'quit'. telnet(1) leaves the terminal raw
#	when a signal kills it, so the method saves the terminal
#	attributes first and restores them on every exit path. The
#	restore is idempotent. A signal that kills the tool also ends
#	telnet(1), so no raw orphan keeps the terminal.
sub attach ($self)
{
	# POSIX loads lazily, as App::FuguVM::Guest already does it.
	require POSIX;

	my $saved;
	if ( POSIX::isatty(*STDIN) ) {
		$saved = POSIX::Termios->new;
		$saved->getattr( fileno(STDIN) );
	}
	my $restore = sub {
		$saved->setattr( fileno(STDIN), POSIX::TCSANOW() )
		    if defined $saved;
	};

	# A fork and an exec, not Fugu::Process->run: the child must
	# keep the terminal of the caller, and the parent must know
	# the child process ID, so a signal handler can end the child.
	my $pid = fork;
	if ( !defined $pid ) {
		Fugu::Log->default->error( 'Cannot fork: %s', $! );
		return 1;



( run in 1.052 second using v1.01-cache-2.11-cpan-f5f21f1076c )