#!/usr/bin/perl -w

# A perl script to handle
# connecting to a VMware Server VM using their perl API,
# take a snapshot,
# copy visual basic scripts used for setup and testing,
# and run the initial setup scripts.
# Copyright Brad Henry <brad@samba.org> 2006
# Released under the GNU GPL v2 or later.

# Library to provide a simple, unified interface to the Perl and VIX VMWare 
# scripting API's.
use VMHost;

sub check_error {
	my $vm = VMHost;
	my $custom_err_str = "";
	($vm, $custom_err_str) = @_;

	my ($err_code, $err_str) = $vm->error;
	if ($err_code != 0) {
		undef $vm;
		die $custom_err_str . "Returned $err_code: $err_str.\n";
	}
}

# Read in parameters from environment.
my $vm_cfg_path = $ENV{'VM_CFG_PATH'};
my $host_server_name = $ENV{'HOST_SERVER_NAME'};
my $host_server_port = $ENV{'HOST_SERVER_PORT'};
if (!defined($host_server_port)) {
	$host_server_port = 902;
}

my $host_username = $ENV{'HOST_USERNAME'};
my $host_password = $ENV{'HOST_PASSWORD'};

my $guest_admin_username = $ENV{'GUEST_ADMIN_USERNAME'};
my $guest_admin_password = $ENV{'GUEST_ADMIN_PASSWORD'};

my $guest_username = $ENV{'GUEST_USERNAME'};
my $guest_password = $ENV{'GUEST_PASSWORD'};
my $guest_hostname = $ENV{'GUEST_HOSTNAME'};
my $guest_workgroup = $ENV{'GUEST_WORKGROUP'};
my $guest_script_path = $ENV{'GUEST_SCRIPT_PATH'};
my $local_script_path = $ENV{'LOCAL_SCRIPT_PATH'};

my $banner_filename = $ENV{'WINDOWS_BANNER_FILENAME'};

my $vm = VMHost;
$vm->host_connect($host_server_name, $host_server_port, $host_username,
			$host_password, $vm_cfg_path, $guest_admin_username,
			$guest_admin_password);
check_error($vm, "Error in \$vm->host_connect().\n");
print "Connected successfully to VM.\n";

my $guest_ip = $vm->get_guest_ip();
check_error($vm, "Error in \$vm->get_guest_ip().\n");
print "The IP address of $vm_cfg_path is: $guest_ip.\n";

# Create a pristine snapshot to revert to if things go badly.
$vm->create_snapshot();
check_error($vm, "Error in \$vm->create_snapshot().\n");
print "Snapshot creation has started.\n";

# Copy the windows setup scripts to the VM.
$vm->copy_to_guest($local_script_path, $guest_script_path);
check_error($vm, "Error in \$vm->copy_to_guest().\n");
print "Windows setup scripts have been copied to the VM.\n";

# Run win_setup.wsf on the VM.
$cmd = "cscript.exe ";
$cmd_args = $guest_script_path . "win_setup.wsf /username:" .
		$guest_username . " /password:" . $guest_password;

if ($guest_hostname) {
	$cmd_args = $cmd_args . " /hostname:" . $guest_hostname;
}

if ($guest_workgroup) {
	$cmd_args = $cmd_args . " /workgroup:" . $guest_workgroup;
}

$vm->run_on_guest($cmd, $cmd_args);
check_error($vm, "Error in \$vm->run_on_guest() while executing: " .
		$cmd . $cmd_args . ".\n");

# Part of the default banner for Microsoft's Telnet server gets parsed by the
# build farm scripts. This one doesn't.
my $login_cmd_path = "%SYSTEMROOT%\\System32\\";

$cmd = "CMD.EXE ";
$cmd_args = "/C COPY /Y " . $login_cmd_path . "Login.cmd " . 
		$login_cmd_path . "Login.cmd.smbtorture";
$vm->run_on_guest($cmd, $cmd_args);
check_error($vm, "Error in \$vm->run_on_guest() while executing: " .
			$cmd . $cmd_args . ".\n");

$cmd = "CMD.EXE ";
$cmd_args = "/C COPY /Y " . $guest_script_path . "\\" . $banner_filename .
		" " . $login_cmd_path . "Login.cmd";
$vm->run_on_guest($cmd, $cmd_args);
check_error($vm, "Error in \$vm->run_on_guest() while executing: " .
			$cmd . $cmd_args . ".\n");

print "Windows setup scripts have been successfully executed on the VM.\n";

# Create a snapshot of the newly configured guest, to revert to if 
# 'make wintest' fails.
$vm->create_snapshot();
check_error($vm, "Error in \$vm->create_snapshot().\n");
print "Snapshot creation has started.\n";

# Destroy the vm object and disconnect from the vm.
undef $vm;
