Revision 47 (by dpavlin, 2008/03/30 12:48:39) cleanup hard-coded dist and arh
#!/usr/bin/perl -w
#
# Dobrica Pavlinusic <dpavlin@rot13.org> 2007-01-07
# 
use strict;
use Shell qw/mkdir vzsplit rm/;
use IO::Prompt;
use Regexp::Common qw/net/;
use lib 'lib';
use VZ;
use Getopt::Long;

# default debian distribution
my $dist = 'etch';
# debian mirror to use
my $debian_mirror_uri = 'http://www.debian.org/debian';
my $arh = 'i386';
# split physicial machine in how meny virtual ones?
my $split = 4;
# swap size (Mb)
my $swap_size = 512;
# diskspace
my $diskspace = '2G:4G';

GetOptions(
	'dist=s'	=> \$dist,
	'arh=s'		=> \$arh,
	'mirror=s'	=> \$debian_mirror_uri,
	'split=i'	=> \$split,
);

check_root;

my $config_file = $0;
$config_file =~ s!-create.pl!-tools.conf!;
warn "## $config_file\n";
if (-e $config_file) {
	open(my $fh, '<', $config_file) || die "can't open $config_file: $!";
	eval join("\n", <$fh>);
	close($fh);
	die "Error in $config_file: $@" if ($@);
}

print "Creating new OpenVZ instance...\n";

my ($hostname, $ip) = ('localhost','');

foreach my $arg ( @ARGV ) {

	if ($arg =~ m/$RE{net}{IPv4}/) {
		$ip = $arg;
		if ( my $h = ip2hostname($ip) ) {
			$hostname = $h;
		}
	} elsif ($arg) {
		if ( my $addr = hostname2ip($arg) ) {
			( $hostname, $ip ) = ( $arg, $addr );
		} else {
			$hostname = $arg;
		}
	}

}

# nuke arguments so that prompt doesn't get confused
@ARGV = ();

$ip ||= prompt('Enter IP: ', -require => {
	'Must be IP (e.g. 192.168.0.1): ' => qr/$RE{net}{IPv4}/,
}) unless ($ip =~ /$RE{net}{IPv4}/);

$hostname ||= prompt('Enter hostname: ') unless ($hostname);

my @ip_split = split(/\./,$ip);

my $ve_id = sprintf('%d%03d', $ip_split[2], $ip_split[3]);

print "VEID: $ve_id hostname: $hostname ip: $ip\n";

warn ">> creating directories\n";

mkdir('-p', "$vz_root/root/$ve_id", "$vz_root/private/$ve_id");

warn ">> installing debian $dist $arh from $debian_mirror_uri\n";

if (! -e "$vz_root/private/$ve_id/etc/debian_version") {

	my $debootstrap = "debootstrap --arch $arh $dist $vz_root/private/$ve_id $debian_mirror_uri";
	warn "# $debootstrap\n";
	system($debootstrap);

} else {
	warn "Debian allready installed in $vz_root/private/$ve_id\n";
}

my $conf_path = "$vz_conf/${ve_id}.conf";
warn ">> creating configuration file $conf_path\n";

if (-e $conf_path) {
	warn "$conf_path allready exists, not touching it\n";
} else {
	vzsplit('-n', $split, '-s', $swap_size * 1024, '>', $conf_path);

	die "configuration file not created" unless -e $conf_path;

	open(my $tmp, '>>', $conf_path) || die "can't open $conf_path: $!";
	print $tmp "OSTEMPLATE=debian-3.1\n";
	close($tmp);

#	vzctl('set', $ve_id, '--applyconfig', 'vps.basic', '--save');
	vzctl('set', $ve_id, '--ipadd', $ip, '--save');
	vzctl('set', $ve_id, '--hostname', $hostname, '--save');
	vzctl('set', $ve_id, '--diskspace', $diskspace, '--save');
}

sub create_file {
	my ($path, $code) = @_;
	if (! -e $path) {
		warn ">> creating $path\n";
		open(my $tmp, '>', $path) || die "can't create $path: $!";
		print $tmp $code->($path);
		close($tmp);
	}
}

create_file(
	"$vz_root/private/$ve_id/etc/apt/sources.list",
	"deb $debian_mirror_uri $dist main contrib non-free\n"
);

vzctl('start', $ve_id);

runscript( $ve_id, 'custom/00-all.sh' );

#vzctl('stop', $ve_id);

my $passwd = prompt( -prompt => 'root passwd: ', -echo=>'*' );
vzctl('set', $ve_id, '--userpasswd', 'root:' . $passwd ) if $passwd;

my $login = prompt('create login: ');
if ($login) {
	$passwd = prompt( -prompt => "$login passwd: ", -echo=>'*');
	vzctl('exec', $ve_id, "useradd --create-home $login");
	vzctl('set', $ve_id, '--userpasswd', "$login:$passwd" );
}

runscript( $ve_id, 'custom/50-hypertable.sh' );
print "OK: $ve_id created\n";