]> git.proxmox.com Git - pve-container.git/commitdiff
add helper classes to setup container configuration
authorDietmar Maurer <dietmar@proxmox.com>
Sat, 18 Apr 2015 14:00:06 +0000 (16:00 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Sat, 18 Apr 2015 14:00:06 +0000 (16:00 +0200)
src/Makefile
src/PVE/LXCSetup.pm [new file with mode: 0644]
src/PVE/LXCSetup/Base.pm [new file with mode: 0644]
src/PVE/LXCSetup/Debian.pm [new file with mode: 0644]
src/PVE/LXCSetup/Makefile [new file with mode: 0644]
src/PVE/Makefile
src/lxc-pve

index 1cd7c5a28d851545ad1382aa12a62a55858a68c3..10cd2653df9617a7dc89b29766b6469171d24199 100644 (file)
@@ -17,10 +17,10 @@ all:
        cat $<|pod2man -n $* -s 1 -r ${VERSION} -c "Proxmox Documentation"|gzip -c9 >$@
 
 pct.1.pod: pct
-       perl -I.. -T ./pct printmanpod >$@
+       perl -I. -T ./pct printmanpod >$@
 
 lxc-pve.1.pod: lxc-pve
-       perl -I.. -T ./lxc-pve printmanpod >$@
+       perl -I. -T ./lxc-pve printmanpod >$@
 
 .PHONY: install
 install: pct lxc-pve pct.1.pod pct.1.gz lxc-pve.1.pod lxc-pve.1.gz
diff --git a/src/PVE/LXCSetup.pm b/src/PVE/LXCSetup.pm
new file mode 100644 (file)
index 0000000..561e036
--- /dev/null
@@ -0,0 +1,44 @@
+package PVE::LXCSetup;
+
+use strict;
+use warnings;
+
+use PVE::LXCSetup::Debian;
+
+my $plugins = {
+    debian =>  'PVE::LXCSetup::Debian',
+};
+
+sub new {
+    my ($class, $type, $conf) = @_;
+
+    my $self = bless { conf => $conf };
+
+    $self->{plugin} = $plugins->{$type} ||
+       "no such OS type '$type'\n";
+
+    return $self;
+}
+
+sub setup_network {
+
+
+}
+
+sub set_hostname {
+    my ($self) = @_;
+
+    $self->{plugin}->set_hostname($self->{conf});
+}
+
+sub set_user_passwort {
+
+}
+
+sub post_create {
+    my ($self) = @_;
+
+    $self->{plugin}->post_create($self->{conf});
+}
+
+1;
diff --git a/src/PVE/LXCSetup/Base.pm b/src/PVE/LXCSetup/Base.pm
new file mode 100644 (file)
index 0000000..9530d36
--- /dev/null
@@ -0,0 +1,101 @@
+package PVE::LXCSetup::Base;
+
+use strict;
+use warnings;
+
+sub change_hostname  {
+    my ($etc_hosts_data, $hostip, $oldname, $newname) = @_;
+
+    # fixme: searchdomain ?
+    
+    my $done = 0;
+
+    my @lines;
+    
+    foreach my $line (split(/\n/, $etc_hosts_data)) {
+       if ($line =~ m/^#/ || $line =~ m/^\s*$/) {
+           push @lines, $line;
+           next;
+       }
+
+       my ($ip, @names) = split(/\s+/, $line);
+       if (($ip eq '127.0.0.1') || ($ip eq '::1')) {
+           push @lines, $line;
+           next;
+       }
+       
+       my $found = 0;
+       foreach my $name (@names) {
+           if ($name eq $oldname || $name eq $newname) {
+               $found = 1;
+           } else {
+               # fixme: record extra names?
+           }
+       }
+       $found = 1 if defined($hostip) && ($ip eq $hostip);
+       
+       if ($found) {
+           if (!$done) {
+               if (defined($hostip)) {
+                   push @lines, "$ip $newname";
+               } else {
+                   push @lines, "127.0.1.1 $newname";
+               }
+               $done = 1;
+           }
+           next;
+       } else {
+           push @lines, $line;
+       }
+    }
+
+    if (!$done) {
+       if (defined($hostip)) {
+           push @lines, "$hostip $newname";
+       } else {
+           push @lines, "127.0.1.1 $newname";
+       }       
+    }
+
+    $etc_hosts_data = join("\n", @lines) . "\n";
+    
+    return $etc_hosts_data;
+}
+
+sub set_hostname {
+    my ($class, $conf) = @_;
+    
+    my $hostname = $conf->{'lxc.utsname'} || 'debian';
+
+    $hostname =~ s/\..*$//;
+
+    my $rootfs = $conf->{'lxc.rootfs'};
+
+    my $hostname_fn = "$rootfs/etc/hostname";
+    
+    my $oldname = PVE::Tools::file_read_firstline($hostname_fn) || 'debian';
+
+    my $hosts_fn = "$rootfs/etc/hosts";
+    my $etc_hosts_data = '';
+    
+    if (-f $hosts_fn) {
+       $etc_hosts_data =  PVE::Tools::file_get_contents($hosts_fn);
+    }
+
+    my $hostip = undef; # fixme;
+    
+    $etc_hosts_data = change_hostname($etc_hosts_data, $hostip, $oldname, $hostname);
+  
+    PVE::Tools::file_set_contents($hostname_fn, "$hostname\n");
+    PVE::Tools::file_set_contents($hosts_fn, $etc_hosts_data);
+}
+
+sub post_create {
+    my ($class, $conf) = @_;
+
+    $class->set_hostname($conf);
+
+    # fixme: what else (network, ...)
+}
+
+1;
diff --git a/src/PVE/LXCSetup/Debian.pm b/src/PVE/LXCSetup/Debian.pm
new file mode 100644 (file)
index 0000000..e9f72c4
--- /dev/null
@@ -0,0 +1,12 @@
+package PVE::LXCSetup::Debian;
+
+use strict;
+use warnings;
+use PVE::Tools;
+
+use PVE::LXCSetup::Base;
+
+use base qw(PVE::LXCSetup::Base);
+
+
+1;
diff --git a/src/PVE/LXCSetup/Makefile b/src/PVE/LXCSetup/Makefile
new file mode 100644 (file)
index 0000000..023d6a4
--- /dev/null
@@ -0,0 +1,6 @@
+SOURCES=Base.pm Debian.pm
+
+.PHONY: install
+install:
+       install -d -m 0755 ${PERLDIR}/PVE/LXCSetup
+       for i in ${SOURCES}; do install -D -m 0644 $$i ${PERLDIR}/PVE/LXCSetup/$$i; done
index fac35d85379c53a074478be600be2ef286e9792f..0fc81467776881a2f01ade3f840b4d79c32c8a7c 100644 (file)
@@ -1,8 +1,10 @@
-SOURCES=LXC.pm
+SOURCES=LXC.pm LXCSetup.pm
 
 .PHONY: install
 install:
        install -d -m 0755 ${PERLDIR}/PVE
        for i in ${SOURCES}; do install -D -m 0644 $$i ${PERLDIR}/PVE/$$i; done
        make -C API2 install
+       make -C LXCSetup install
+
 
index 3a4669c4510a21c455f38d06698000c34344a8aa..e2a92dda2ef97d74ebee9c3fefa462d80e428e51 100755 (executable)
@@ -14,6 +14,7 @@ use PVE::JSONSchema qw(get_standard_option);
 use PVE::CLIHandler;
 use PVE::Storage;
 use PVE::LXC;
+use PVE::LXCSetup;
 use Data::Dumper;
 
 use base qw(PVE::CLIHandler);
@@ -129,6 +130,10 @@ __PACKAGE__->register_method ({
 
        PVE::LXC::write_config($param->{name}, $conf);
 
+       # fixme: use correct dist
+       my $lxc_setup = PVE::LXCSetup->new('debian', $conf);
+       $lxc_setup->post_create();
+       
        return undef;
     }});