Changeset 346


Ignore:
Timestamp:
Apr 2, 2008, 2:04:21 AM (11 years ago)
Author:
bruno
Message:

First attempt to code a setupvm/ve function into pb

File:
1 edited

Legend:

Unmodified
Added
Removed
  • devel/pb/bin/pb

    r344 r346  
    202202
    203203Create a new virtual environment
     204
     205=item B<setupvm>
     206
     207Setup a virtual machine for pb usage
     208
     209=item B<setupve>
     210
     211Setup a virtual environment for pb usage
    204212
    205213=item B<newver>
     
    422430} elsif ($action =~ /^newvm$/) {
    423431    pb_launchv("vm",$ENV{'PBV'},1);
     432} elsif ($action =~ /^setupve$/) {
     433    $pbaccount = "root";
     434    my $pbscript = pb_setup_v("ve");
     435    pb_script2v($pbscript,"ve");
     436} elsif ($action =~ /^setupvm$/) {
     437    $pbaccount = "root";
     438    my $pbscript = pb_setup_v("vm");
     439    pb_script2v($pbscript,"vm");
    424440} elsif ($action =~ /^newproj$/) {
    425441    # Nothing to do - already done in pb_env_init
     
    12161232}
    12171233
    1218 sub pb_setup_v {
    12191234# Function to create a potentialy missing pb account on the VM/VE, and adds it to sudo
    12201235# Needs to use root account to connect to the VM/VE
    1221 
    12221236# pb will take your local public SSH key to access
    1223 # the pb account in the VM later on
     1237# the pb account in the VM later on if needed
     1238sub pb_setup_v {
    12241239
    12251240my $vtype = shift;
    12261241
    1227 my $file = "$ENV{'HOME'}/.ssh/id_dsa.pub";
    1228 die "Unable to find your public ssh key as $file";
    1229 
    1230 open(SCRIPT,"> $ENV{'PBDESTDIR'}/pbscript") || die "Unable to create $ENV{'PBDESTDIR'}/pbscript";
     1242# Script generated
     1243my $pbscript = "$ENV{'PBDESTDIR'}/setupv";
     1244
     1245# Name of the account to deal with for VM/VE
     1246my $pbaccount = pb_conf_get($vtype."login");
     1247
     1248# Prepare the script to be executed on the VM/VE
     1249# in $ENV{'PBDESTDIR'}/setupv
     1250
     1251# Check the SSH environment
     1252my $file = undef;
     1253$file = "$ENV{'HOME'}/.ssh/id_rsa.pub" if (-s "$ENV{'HOME'}/.ssh/id_rsa.pub");
     1254$file = "$ENV{'HOME'}/.ssh/id_dsa.pub" if (-s "$ENV{'HOME'}/.ssh/id_dsa.pub");
     1255die "Unable to find your public ssh key under $file" if (not defined $file);
     1256
     1257open(SCRIPT,"> $pbscript") || die "Unable to create $pbscript";
    12311258print SCRIPT << 'EOF';
    12321259#!/usr/bin/perl -w
    12331260
    1234 $file="/etc/passwd";
     1261use strict;
     1262use File::Copy;
     1263
     1264my $file="/etc/passwd";
    12351265open(PBFILE,$file) || die "Unable to open $file";
    12361266my $found = 0;
    12371267while (<PBFILE>) {
    1238     $found = 1 if (/^pb:/);
     1268EOF
     1269print SCRIPT << "EOF"
     1270    \$found = 1 if (/^$pbaccount:/);
     1271EOF
     1272print SCRIPT << 'EOF';
    12391273}
    12401274close(PBFILE);
     
    12441278        mkdir "/home";
    12451279    }
    1246     system "groupadd pb";
    1247     system "useradd pb -g pb -m -d /home/pb";
     1280EOF
     1281print SCRIPT << "EOF"
     1282system "groupadd $pbaccount";
     1283system "useradd $pbaccount -g $pbaccount -m -d /home/$pbaccount";
     1284
     1285# For pb
     1286chdir "/home/$pbaccount";
     1287mkdir ".ssh",0700;
     1288copy("/tmp/pbkey",".ssh/authorized_keys");
     1289chmod 0600,".ssh/authorized_keys";
     1290system 'chown -R $pbaccount:$pbaccount .ssh';
     1291
     1292EOF
     1293print SCRIPT << 'EOF';
    12481294}
    12491295
    12501296# For root
    12511297mkdir ".ssh",0700;
    1252 system 'cp /tmp/pbkey .ssh/authorized_keys';
     1298copy("/tmp/pbkey",".ssh/authorized_keys");
    12531299chmod 0600,".ssh/authorized_keys";
    1254 
    1255 # For pb
    1256 chdir "/home/pb";
    1257 mkdir ".ssh",0700;
    1258 system 'cp /tmp/pbkey .ssh/authorized_keys';
    1259 chmod 0600,".ssh/authorized_keys";
    1260 system 'chown -R pb:pb .ssh';
    12611300
    12621301# No passwd for pb only keys
     
    12651304open(PBOUT,"> $file.new") || die "Unable to open $file.new";
    12661305while (<PBFILE>) {
    1267     s/^pb:\!\!:/pb:*:/;
    1268     s/^pb:\!:/pb:*:/;   #SLES 9 e.g.
     1306EOF
     1307print SCRIPT << "EOF"
     1308    s/^$pbaccount:\!\!:/$pbaccount:*:/;
     1309    s/^$pbaccount:\!:/$pbaccount:*:/;   #SLES 9 e.g.
     1310EOF
     1311print SCRIPT << 'EOF'
    12691312    print PBOUT $_;
    12701313}
     
    12771320unlink "/tmp/pbkey";
    12781321
     1322# Adapt sudoers
     1323$file="/etc/sudoers";
     1324open(PBFILE,$file) || die "Unable to open $file";
     1325open(PBOUT,"> $file.new") || die "Unable to open $file.new";
     1326while (<PBFILE>) {
     1327EOF
     1328print SCRIPT << "EOF"
     1329    next if (/^$pbaccount   /);
     1330EOF
     1331print SCRIPT << 'EOF'
     1332    s/Defaults[ \t]+requiretty//;
     1333    print PBOUT $_;
     1334}
     1335close(PBFILE);
     1336EOF
     1337print SCRIPT << "EOF"
     1338# This is needed in order to be able to halt the machine from the $pbaccount account at least
     1339print PBOUT "$pbaccount   ALL=(ALL) NOPASSWD:ALL\n";
     1340EOF
     1341print SCRIPT << 'EOF'
     1342close(PBOUT);
     1343rename("$file.new",$file);
     1344chmod 0440,$file;
     1345
     1346EOF
     1347
     1348pb_install_pkg_deps(SCRIPT);
     1349
     1350print SCRIPT << 'EOF';
     1351# Suse wants sudoers as 640
     1352if (($ddir eq "sles") || (($ddir eq "suse")) && ($dver ne "10.3")) {
     1353    chmod 0640,$file;
     1354}
     1355
     1356# Sync date
     1357system "/usr/sbin/ntpdate ntp.pool.org";
     1358
     1359system "rm -rf project-builder-* ; wget --passive-ftp ftp://ftp.mondorescue.org/src/project-builder-latest.tar.gz ; tar xvfz project-builder-latest.tar.gz ; cd project-builder-* ; perl Makefile.PL ; make ; make install ; cd ..";
     1360EOF
     1361close(SCRIPT);
     1362chmod 0755,"$pbscript";
     1363return($pbscript);
     1364}
     1365
     1366pb_install_pkg_deps {
     1367
     1368my \*SCRIPT = shift;
     1369
     1370print SCRIPT << 'EOF';
     1371# We need to have that pb_distro_init function
    12791372my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init();
    12801373print "distro tuple: ".join(',',($ddir, $dver, $dfam, $dtype, $pbsuf))."\n";
     
    12831376if ( $ddir eq "fedora" ) {
    12841377    system "yum clean all";
    1285     system "yum update -y";
     1378    #system "yum update -y";
    12861379    my $arch=`uname -m`;
    12871380    my $opt = "";
     
    13031396} elsif ( $dfam eq "du" ) {
    13041397    if (( $dver eq "3.1" ) && ($ddir eq "debian")) {
    1305         system "apt-get update; apt-get -y install wget patch ssh sudo debian-builder dh-make fakeroot ntpdate libdate-manip-perl";
     1398        #system "apt-get update";
     1399        system "apt-get -y install wget patch ssh sudo debian-builder dh-make fakeroot ntpdate libdate-manip-perl";
    13061400    } else  {
    13071401        system "apt-get update; apt-get -y install wget patch openssh-server dpkg-dev sudo debian-builder dh-make fakeroot ntpdate rses5-dev libdate-manip-perl";
     
    13121406    print "No pkg to install\n";
    13131407}
    1314 
    1315 # Adapt sudoers
    1316 $file="/etc/sudoers";
    1317 open(PBFILE,$file) || die "Unable to open $file";
    1318 open(PBOUT,"> $file.new") || die "Unable to open $file.new";
    1319 while (<PBFILE>) {
    1320     next if (/^pb   /);
    1321     s/Defaults[ \t]+requiretty//;
    1322     print PBOUT $_;
    1323 }
    1324 close(PBFILE);
    1325 print PBOUT "pb   ALL=(ALL) NOPASSWD:ALL\n";
    1326 close(PBOUT);
    1327 rename("$file.new",$file);
    1328 chmod 0440,$file;
    1329 
    1330 # Suse wants sudoers as 640
    1331 if (($ddir eq "sles") || (($ddir eq "suse")) && ($dver ne "10.3")) {
    1332     chmod 0640,$file;
    1333 }
    1334 
    1335 # Sync date
    1336 system "/usr/sbin/ntpdate ntp.pool.org";
    1337 
    1338 system "rm -rf project-builder-* ; wget --passive-ftp ftp://ftp.mondorescue.org/src/project-builder-latest.tar.gz ; tar xvfz project-builder-latest.tar.gz ; cd project-builder-* ; perl Makefile.PL ; make ; make install ; cd ..";
    13391408EOF
    1340 close(SCRIPT);
    13411409}
    13421410
Note: See TracChangeset for help on using the changeset viewer.