App-FuguVM

 view release on metacpan or  search on metacpan

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

		$key, $name,
		$state->disk_path,
		{
			installed            => 1,
			installed_ssh_pubkey =>
			    $state->get_installed_ssh_pubkey,
		} );
	if ( !defined $path ) {
		$self->{log}->error("Failed to save snapshot '$name'");
		return EXIT_ERROR;
	}

	$self->{log}->info("Saved snapshot '$name': $path");
	return EXIT_SUCCESS;
}

# $self->_snapshot_restore($cache, $name):
#	Replace the working disk with a fresh overlay on a snapshot.
#	Reseed the state that the disk embodies. The command works from
#	nothing: no disk, no state. Thus a fresh checkout can restore
#	before its first 'up'.
sub _snapshot_restore ( $self, $cache, $name )
{
	my $vm    = $self->_load_vm or return $self->{load_exit};
	my $state = $self->{state};

	if ( $vm->is_running ) {
		$self->{log}->error("Stop the VM before restoring a snapshot");
		return EXIT_VM_RUNNING;
	}

	my $key = $self->_current_cache_key($cache)
	    or return EXIT_ERROR;

	my $found = $self->_snapshot_found( $cache, $key, $name )
	    or return EXIT_SNAPSHOT_NOT_FOUND;

	# Disk::create returns early on an existing path. Without this
	# removal, a restore would report success and change nothing.
	my $disk_path = $state->disk_path;
	if ( -f $disk_path ) {
		unlink $disk_path or do {
			$self->{log}->error("Cannot remove $disk_path: $!");
			return EXIT_ERROR;
		};
	}

	my $vm_config = $self->{config}->load_vm( $self->{vm_name} );
	my $disk      = App::FuguVM::Disk->new( $self->{config}->state_dir );
	my $created =
	    $disk->create( $vm_config->{name}, undef, $found->{path} );
	if ( !defined $created ) {
		$self->{log}->error("Failed to overlay snapshot '$name'");
		return EXIT_ERROR;
	}

	# Reseed what the disk embodies. The next 'fuguvm up' reconciles
	# a checkout whose SSH key differs from the saved one.
	my $meta = $found->{meta};
	$state->mark_installed( $vm_config->{arch} );
	$state->set_root_password( $meta->{root_password} )
	    if defined $meta->{root_password};
	$state->mark_ssh_key_installed( $meta->{installed_ssh_pubkey} )
	    if defined $meta->{installed_ssh_pubkey};
	$state->data->{cached_from} = "$key/$name";
	$state->save;

	$self->{log}->info("Restored snapshot '$name' of $key");
	return EXIT_SUCCESS;
}

sub _snapshot_list ( $self, $cli, $cache, @args )
{
	my $names = $cli->option('names') // 0;

	my $key = $self->_current_cache_key($cache)
	    or return EXIT_ERROR;

	my $snapshots = $cache->snapshot_list($key);

	# --names writes bare names to stdout, where a shell can read
	# them. The human listing goes through the logger, which writes
	# to stderr and prefixes every line.
	if ($names) {
		say $_->{name} for @$snapshots;
		return EXIT_SUCCESS;
	}

	if ( !@$snapshots ) {
		$self->{log}->info("No snapshots for $key");
		return EXIT_SUCCESS;
	}

	for my $snapshot (@$snapshots) {
		my $created =
		    defined $snapshot->{created_at}
		    ? scalar localtime $snapshot->{created_at}
		    : 'unknown';
		$self->{log}->info(
			sprintf( '  - %s  %s  %s',
				$snapshot->{name},
				_format_size( $snapshot->{size} ),
				$created ) );
	}

	return EXIT_SUCCESS;
}

sub _snapshot_remove ( $self, $cache, $name )
{
	my $key = $self->_current_cache_key($cache)
	    or return EXIT_ERROR;

	$self->_snapshot_found( $cache, $key, $name )
	    or return EXIT_SNAPSHOT_NOT_FOUND;

	if ( !$cache->snapshot_remove( $key, $name ) ) {
		return EXIT_ERROR;
	}

	$self->{log}->info("Removed snapshot '$name'");
	return EXIT_SUCCESS;



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