App-FuguVM

 view release on metacpan or  search on metacpan

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

	    : $self->image_cache;

	# Normalize the verification switch the same way
	$vm->{verify} =
	    defined $vm->{verify}
	    ? $self->_bool( $vm->{verify}, 1 )
	    : $self->verify;

	# Resolve the signify_dir directive, from the VM block or the
	# enclosing configuration. This loader is the boundary of the
	# directive: an absent or unreadable directory fails here, not
	# deep inside an install.
	$vm->{signify_dir} =
	    defined $vm->{signify_dir}
	    ? $self->_resolve_path( $vm->{signify_dir} )
	    : $self->signify_dir;
	if ( defined $vm->{signify_dir} && !-d $vm->{signify_dir} ) {
		$self->{error} =
		    sprintf( "VM '%s': signify_dir is not a directory: %s",
			$name, $vm->{signify_dir} );
		return;
	}

	# Include the distfile cap of the project. The distfile tree is
	# one tree under cache_dir, and each guest of a project shares
	# it, so the cap is a project fact and not a per-guest
	# directive. A cap in a VM block would silently not apply, so
	# the operator hears about it, like an unrecognized switch.
	Fugu::Log->default->warning(
		"VM '%s': distfile_cache lives in the project or the"
		    . ' global .fuguvmrc, not in a VM declaration',
		$name
	) if defined $vm->{distfile_cache};
	$vm->{distfile_cache} = $self->distfile_cache;

	return if !$self->_validate_origin( $name, $vm );

	return $vm;
}

# $self->_validate_origin($name, $vm):
#	Validate the three image-lifecycle directives, and derive
#	install_mode: expect, autoinstall or import. This loader is the
#	boundary of each directive, so no module downstream repeats a
#	check. Return 1 when the configuration loads, and 0 with the
#	reason in error() otherwise.
sub _validate_origin ( $self, $name, $vm )
{
	if ( defined $vm->{autoinstall} && defined $vm->{base_disk} ) {
		$self->{error} = sprintf(
			"VM '%s': autoinstall and base_disk contradict each"
			    . " other; one guest has one origin",
			$name
		);
		return 0;
	}

	# Each path directive resolves against the project root, and the
	# file must be readable now. A path that fails at 'up' time, deep
	# inside an install, is a path that failed too late.
	for my $directive (qw(autoinstall base_disk root_password_file)) {
		next if !defined $vm->{$directive};
		my $path = $self->_resolve_path( $vm->{$directive} );
		if ( !-f $path || !-r $path ) {
			$self->{error} =
			    sprintf( "VM '%s': %s file is not readable: %s",
				$name, $directive, $path );
			return 0;
		}
		$vm->{$directive} = $path;
	}

	$vm->{install_mode} =
	      defined $vm->{autoinstall} ? 'autoinstall'
	    : defined $vm->{base_disk}   ? 'import'
	    :                              'expect';

	if ( $vm->{install_mode} eq 'import' && !$vm->{image_cache} ) {
		$self->{error} = sprintf(
			"VM '%s': base_disk needs the image cache;"
			    . " remove 'image_cache no'",
			$name
		);
		return 0;
	}

	# Outside the expect mode the tool does not know the root
	# password of the image, and it cannot install a key without
	# one.
	if (       $vm->{install_mode} ne 'expect'
		&& defined $vm->{ssh_pubkey}
		&& $vm->{ssh_pubkey} ne ''
		&& !defined $vm->{root_password_file} )
	{
		$self->{error} = sprintf(
			"VM '%s': ssh_pubkey needs root_password_file in the"
			    . " %s mode; add the directive, or unset"
			    . " ssh_pubkey and bake the key into the image",
			$name, $vm->{install_mode} );
		return 0;
	}

	return 1;
}

# $self->_resolve_path($value):
#	Expand a leading tilde, then make a relative path absolute
#	against the project root. The result is always absolute: the
#	project root itself can be relative, from a --project option,
#	and a daemonized child can read the path from an other working
#	directory.
sub _resolve_path ( $self, $value )
{
	my $path = Fugu::File->expand_tilde($value);
	$path = "$self->{project_root}/$path" if $path !~ m{^/};

	require File::Spec;
	return File::Spec->rel2abs($path);
}

# $self->error:
#	Return the reason of the last failed load_vm, or undef.
sub error ($self)
{
	return $self->{error};
}

# _valid_port($value):
#	Report if a port directive holds the word AUTO_PORT or a
#	decimal number from 1 to 65535.
sub _valid_port ($value)
{
	return 0 if !defined $value;
	return 1 if $value eq AUTO_PORT;

	# A leading zero is not decimal, and the string-keyed port sets
	# of the probe would not match it.
	return 0 if $value !~ /^[1-9][0-9]*$/;

	return $value <= 65535 ? 1 : 0;
}

# _valid_ipv4($value):
#	Report if a value is one IPv4 address in dotted-decimal form.
#	A host name is not: a name resolves once for QEMU and once for
#	the tool, and the two answers can differ.
sub _valid_ipv4 ($value)
{
	return 0 if !defined $value;

	my @parts = split /\./, $value, -1;
	return 0 if @parts != 4;

	for my $part (@parts) {

		# A leading zero reads as octal in inet_aton, so such a



( run in 2.228 seconds using v1.01-cache-2.11-cpan-f5f21f1076c )