annotate src/pkg.pl @ 14:ac1ca422bd54

saving progress.
author Robert McIntyre <rlm@mit.edu>
date Wed, 09 Jan 2013 10:59:43 +0000
parents e056798af879
children cd47c42f1d0a
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@7 71 ("install", "-d", "-g", $pkg_name, "-o", $pkg_name,
rlm@9 72 "-m", "755", $pkg_home);
rlm@7 73
rlm@7 74 @pkg_add_install_cmd =
rlm@7 75 ("gpasswd", "--add", $pkg_name, "install");
rlm@3 76
rlm@7 77 @pkg_link_bashrc_cmd =
rlm@7 78 ("install", "-g", $pkg_name, "-o", $pkg_name,
rlm@8 79 "/pkg/defaults/.profile", "$pkg_home/.profile");
rlm@7 80
rlm@7 81 @pkg_copy_hg_wrapper_cmd =
rlm@12 82 (install, "-g", "hg-wheel", "-o", "hg-committer",
rlm@12 83 "-m", "6111", "/pkg/defaults/.hg-wrapper", $pkg_home);
rlm@12 84
rlm@12 85 sub execute{print "\t@_\n"; system(@_) and die $!;}
rlm@12 86
rlm@3 87 #determine if the user already exists:
rlm@3 88 `id $pkg_name 2>/dev/null`;
rlm@4 89 if (!$?){
rlm@4 90 print "Change user.\n";
rlm@4 91 execute(@pkg_switch_cmd); exit 0;}
rlm@3 92
rlm@3 93 print "Creating package group.\n";
rlm@3 94 execute(@pkg_create_group_cmd);
rlm@3 95
rlm@3 96 print "Creating package user.\n";
rlm@3 97 execute(@pkg_create_cmd);
rlm@3 98
rlm@3 99 print "Create home directory for $pkg_name.\n";
rlm@3 100 execute(@pkg_create_home_cmd);
rlm@3 101
rlm@7 102 print "Add $pkg_name to install group.\n";
rlm@7 103 execute(@pkg_add_install_cmd);
rlm@7 104
rlm@7 105 print "Copy Default Files.\n";
rlm@7 106 execute(@pkg_link_bashrc_cmd);
rlm@7 107 execute(@pkg_copy_hg_wrapper_cmd);
rlm@7 108
rlm@3 109 print "Change user.\n";
rlm@3 110 execute(@pkg_switch_cmd);