/[vz-tools]/trunk/vz-create.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/vz-create.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 44 - (show annotations)
Sat Feb 9 00:28:10 2008 UTC (16 years, 2 months ago) by dpavlin
File MIME type: text/plain
File size: 3498 byte(s)
- moved runscript into VZ.pm
- hide passwords in vzctl
1 #!/usr/bin/perl -w
2 #
3 # Dobrica Pavlinusic <dpavlin@rot13.org> 2007-01-07
4 #
5 use strict;
6 use Shell qw/mkdir vzsplit rm/;
7 use IO::Prompt;
8 use Regexp::Common qw/net/;
9 use lib 'lib';
10 use VZ;
11
12 # default debian distribution
13 my $dist = 'etch';
14 # debian mirror to use
15 my $debian_mirror_uri = 'http://www.debian.org/debian';
16 my $arh = 'i386';
17 # split physicial machine in how meny virtual ones?
18 my $ve_total = 4;
19 # swap size (Mb)
20 my $swap_size = 512;
21 # diskspace
22 my $diskspace = '2G:4G';
23
24 $dist = 'testing';
25 $arh = 'amd64';
26
27 check_root;
28
29 my $config_file = $0;
30 $config_file =~ s!-create.pl!-tools.conf!;
31 warn "## $config_file\n";
32 if (-e $config_file) {
33 open(my $fh, '<', $config_file) || die "can't open $config_file: $!";
34 eval join("\n", <$fh>);
35 close($fh);
36 die "Error in $config_file: $@" if ($@);
37 }
38
39 print "Creating new OpenVZ instance...\n";
40
41 my ($hostname, $ip) = ('localhost','');
42
43 foreach my $arg ( @ARGV ) {
44
45 if ($arg =~ m/$RE{net}{IPv4}/) {
46 $ip = $arg;
47 if ( my $h = ip2hostname($ip) ) {
48 $hostname = $h;
49 }
50 } elsif ($arg) {
51 if ( my $addr = hostname2ip($arg) ) {
52 ( $hostname, $ip ) = ( $arg, $addr );
53 } else {
54 $hostname = $arg;
55 }
56 }
57
58 }
59
60 # nuke arguments so that prompt doesn't get confused
61 @ARGV = ();
62
63 $ip ||= prompt('Enter IP: ', -require => {
64 'Must be IP (e.g. 192.168.0.1): ' => qr/$RE{net}{IPv4}/,
65 }) unless ($ip =~ /$RE{net}{IPv4}/);
66
67 $hostname ||= prompt('Enter hostname: ') unless ($hostname);
68
69 my @ip_split = split(/\./,$ip);
70
71 my $ve_id = sprintf('%d%03d', $ip_split[2], $ip_split[3]);
72
73 print "VEID: $ve_id hostname: $hostname ip: $ip\n";
74
75 warn ">> creating directories\n";
76
77 mkdir('-p', "$vz_root/root/$ve_id", "$vz_root/private/$ve_id");
78
79 warn ">> installing debian $dist $arh from $debian_mirror_uri\n";
80
81 if (! -e "$vz_root/private/$ve_id/etc/debian_version") {
82
83 my $debootstrap = "debootstrap --arch $arh $dist $vz_root/private/$ve_id $debian_mirror_uri";
84 warn "# $debootstrap\n";
85 system($debootstrap);
86
87 } else {
88 warn "Debian allready installed in $vz_root/private/$ve_id\n";
89 }
90
91 my $conf_path = "$vz_conf/${ve_id}.conf";
92 warn ">> creating configuration file $conf_path\n";
93
94 if (-e $conf_path) {
95 warn "$conf_path allready exists, not touching it\n";
96 } else {
97 vzsplit('-n', $ve_total, '-s', $swap_size * 1024, '>', $conf_path);
98
99 die "configuration file not created" unless -e $conf_path;
100
101 open(my $tmp, '>>', $conf_path) || die "can't open $conf_path: $!";
102 print $tmp "OSTEMPLATE=debian-3.1\n";
103 close($tmp);
104
105 # vzctl('set', $ve_id, '--applyconfig', 'vps.basic', '--save');
106 vzctl('set', $ve_id, '--ipadd', $ip, '--save');
107 vzctl('set', $ve_id, '--hostname', $hostname, '--save');
108 vzctl('set', $ve_id, '--diskspace', $diskspace, '--save');
109 }
110
111 sub create_file {
112 my ($path, $code) = @_;
113 if (! -e $path) {
114 warn ">> creating $path\n";
115 open(my $tmp, '>', $path) || die "can't create $path: $!";
116 print $tmp $code->($path);
117 close($tmp);
118 }
119 }
120
121 create_file(
122 "$vz_root/private/$ve_id/etc/apt/sources.list",
123 "deb $debian_mirror_uri $dist main contrib non-free\n"
124 );
125
126 vzctl('start', $ve_id);
127
128 runscript( $ve_id, 'custom/00-all.sh' );
129
130 #vzctl('stop', $ve_id);
131
132 my $passwd = prompt( -prompt => 'root passwd: ', -echo=>'*' );
133 vzctl('set', $ve_id, '--userpasswd', 'root:' . $passwd ) if $passwd;
134
135 my $login = prompt('create login: ');
136 if ($login) {
137 $passwd = prompt( -prompt => "$login passwd: ", -echo=>'*');
138 vzctl('exec', $ve_id, "useradd --create-home $login");
139 vzctl('set', $ve_id, '--userpasswd', "$login:$passwd" );
140 }
141
142 print "OK: $ve_id created\n";

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26