diff src/pkg.pl @ 3:9ff0fcf34920

first draft of pkg.pl complete.
author Robert McIntyre <rlm@mit.edu>
date Tue, 08 Jan 2013 14:42:09 +0000
parents a75581c89dde
children fe56b4698cf8
line wrap: on
line diff
     1.1 --- a/src/pkg.pl	Tue Jan 08 12:48:59 2013 +0000
     1.2 +++ b/src/pkg.pl	Tue Jan 08 14:42:09 2013 +0000
     1.3 @@ -1,5 +1,10 @@
     1.4  #!/usr/bin/env perl
     1.5  
     1.6 +
     1.7 +#this is the root of the package users' homes
     1.8 +$pkg_home_root =  "/pkg";
     1.9 +
    1.10 +
    1.11  if ($ARGV[0] eq "--help"){
    1.12    print <<"HERE";
    1.13  
    1.14 @@ -8,7 +13,7 @@
    1.15  syntax:
    1.16    pkg package-name description
    1.17    pkg --help 
    1.18 -
    1.19 +  
    1.20  Create a package user for the purpose of compiling
    1.21  a particular package.
    1.22  
    1.23 @@ -17,10 +22,10 @@
    1.24  
    1.25  You will then be put into the user's home
    1.26  directory where you may proceed with
    1.27 -compiling/installation of the package.
    1.28 +compiling/installing the package.
    1.29  
    1.30  If the user already exists, this is equivalent to 
    1.31 -  su package-name; cd ~; 
    1.32 +  su --login package-name
    1.33  
    1.34  Written by Robert McIntyre. This software is free
    1.35  software and is released to the public domain.
    1.36 @@ -28,25 +33,64 @@
    1.37  HERE
    1.38  exit 0;
    1.39  }
    1.40 -
    1.41 +$" = " ";
    1.42  
    1.43  $pkg_name = $ARGV[0];
    1.44  
    1.45  $pkg_desc = $ARGV[1];
    1.46  
    1.47  # Validate pkg_user as a user name.
    1.48 -if (!($pkg_name =~ m/^[A-Za-z][A-Za-z0-9_-]*$/)){
    1.49 +if (!($pkg_name =~ m/^[a-z_][a-z0-9_-]*\$?$/)){
    1.50    print "$pkg_name is not a valid user name!\n";
    1.51 -  exit 0;
    1.52 +  exit 1;
    1.53  }
    1.54  
    1.55  # Validate description.
    1.56  if ($pkg_desc =~ m/:/){
    1.57    print "Package Description cannot contain \":\"\n";
    1.58 -  exit 0
    1.59 +  exit 1;
    1.60  }
    1.61  
    1.62  
    1.63 -print "name: $pkg_name\ndesc: $pkg_desc\n";
    1.64 +$pkg_home = "$pkg_home_root/$pkg_name";
    1.65  
    1.66 +#set default for $pkg_desc.
    1.67 +if (!($pkg_desc)){$pkg_desc = $pkg_name;}
    1.68  
    1.69 +@pkg_switch_cmd =  ("su", "--login", $pkg_name);
    1.70 +
    1.71 +@pkg_create_cmd =
    1.72 +  ("useradd", "-c", $pkg_desc, "-d", $pkg_home, 
    1.73 +   "-g", $pkg_name, "-s", "/bin/bash",
    1.74 +   $pkg_name);
    1.75 +
    1.76 +@pkg_create_group_cmd =
    1.77 +  ("groupadd", "--force", $pkg_name);
    1.78 +
    1.79 +@pkg_create_home_cmd = 
    1.80 +  ("install", "-d", "-g", $pkg_name, "-o", $pkg_name, "-m", "700", $pkg_home);
    1.81 +
    1.82 +sub execute{print "@_\n"; system(@_) and die $!;}
    1.83 +
    1.84 +#determine if the user already exists:
    1.85 +`id $pkg_name 2>/dev/null`;
    1.86 +if (!$?){execute(@pkg_switch_cmd); exit 0;}
    1.87 +
    1.88 +print "Creating package group.\n";
    1.89 +execute(@pkg_create_group_cmd);
    1.90 +
    1.91 +print "Creating package user.\n";
    1.92 +execute(@pkg_create_cmd);
    1.93 +
    1.94 +print "Create home directory for $pkg_name.\n";
    1.95 +execute(@pkg_create_home_cmd);
    1.96 +
    1.97 +print "Change user.\n";
    1.98 +execute(@pkg_switch_cmd);
    1.99 +
   1.100 +
   1.101 +
   1.102 +
   1.103 +
   1.104 +
   1.105 +