annotate src/pkg.pl @ 6:941970bc92e3

create skeleton generating code.
author Robert McIntyre <rlm@mit.edu>
date Tue, 08 Jan 2013 16:03:31 +0000
parents fe56b4698cf8
children ab4011ea1463
rev   line source
rlm@2 1 #!/usr/bin/env perl
rlm@2 2
rlm@3 3
rlm@3 4 #this is the root of the package users' homes
rlm@3 5 $pkg_home_root = "/pkg";
rlm@3 6
rlm@3 7
rlm@2 8 if ($ARGV[0] eq "--help"){
rlm@2 9 print <<"HERE";
rlm@2 10
rlm@2 11 pkg -- create a package user.
rlm@2 12
rlm@2 13 syntax:
rlm@2 14 pkg package-name description
rlm@2 15 pkg --help
rlm@3 16
rlm@2 17 Create a package user for the purpose of compiling
rlm@2 18 a particular package.
rlm@2 19
rlm@2 20 The user will be created with user and group both
rlm@2 21 equal to the name you specify.
rlm@2 22
rlm@2 23 You will then be put into the user's home
rlm@2 24 directory where you may proceed with
rlm@3 25 compiling/installing the package.
rlm@2 26
rlm@2 27 If the user already exists, this is equivalent to
rlm@3 28 su --login package-name
rlm@2 29
rlm@2 30 Written by Robert McIntyre. This software is free
rlm@2 31 software and is released to the public domain.
rlm@2 32
rlm@2 33 HERE
rlm@2 34 exit 0;
rlm@2 35 }
rlm@3 36 $" = " ";
rlm@2 37
rlm@2 38 $pkg_name = $ARGV[0];
rlm@2 39
rlm@2 40 $pkg_desc = $ARGV[1];
rlm@2 41
rlm@2 42 # Validate pkg_user as a user name.
rlm@3 43 if (!($pkg_name =~ m/^[a-z_][a-z0-9_-]*\$?$/)){
rlm@2 44 print "$pkg_name is not a valid user name!\n";
rlm@3 45 exit 1;
rlm@2 46 }
rlm@2 47
rlm@2 48 # Validate description.
rlm@2 49 if ($pkg_desc =~ m/:/){
rlm@2 50 print "Package Description cannot contain \":\"\n";
rlm@3 51 exit 1;
rlm@2 52 }
rlm@2 53
rlm@2 54
rlm@3 55 $pkg_home = "$pkg_home_root/$pkg_name";
rlm@2 56
rlm@3 57 #set default for $pkg_desc.
rlm@3 58 if (!($pkg_desc)){$pkg_desc = $pkg_name;}
rlm@2 59
rlm@3 60 @pkg_switch_cmd = ("su", "--login", $pkg_name);
rlm@3 61
rlm@3 62 @pkg_create_cmd =
rlm@3 63 ("useradd", "-c", $pkg_desc, "-d", $pkg_home,
rlm@3 64 "-g", $pkg_name, "-s", "/bin/bash",
rlm@3 65 $pkg_name);
rlm@3 66
rlm@3 67 @pkg_create_group_cmd =
rlm@3 68 ("groupadd", "--force", $pkg_name);
rlm@3 69
rlm@3 70 @pkg_create_home_cmd =
rlm@3 71 ("install", "-d", "-g", $pkg_name, "-o", $pkg_name, "-m", "700", $pkg_home);
rlm@3 72
rlm@4 73 sub execute{print "\t@_\n"; system(@_) and die $!;}
rlm@3 74
rlm@3 75 #determine if the user already exists:
rlm@3 76 `id $pkg_name 2>/dev/null`;
rlm@4 77 if (!$?){
rlm@4 78 print "Change user.\n";
rlm@4 79 execute(@pkg_switch_cmd); exit 0;}
rlm@3 80
rlm@3 81 print "Creating package group.\n";
rlm@3 82 execute(@pkg_create_group_cmd);
rlm@3 83
rlm@3 84 print "Creating package user.\n";
rlm@3 85 execute(@pkg_create_cmd);
rlm@3 86
rlm@3 87 print "Create home directory for $pkg_name.\n";
rlm@3 88 execute(@pkg_create_home_cmd);
rlm@3 89
rlm@3 90 print "Change user.\n";
rlm@3 91 execute(@pkg_switch_cmd);
rlm@3 92
rlm@3 93
rlm@3 94
rlm@3 95
rlm@3 96
rlm@3 97
rlm@3 98