annotate src/pkg.pl @ 17:cb1a44d080db

consolidating init scripts.
author Robert McIntyre <rlm@mit.edu>
date Wed, 09 Jan 2013 11:41:55 +0000
parents cd47c42f1d0a
children 4fbe69d24a9c
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@3 54 $pkg_home = "$pkg_home_root/$pkg_name";
rlm@2 55
rlm@3 56 #set default for $pkg_desc.
rlm@3 57 if (!($pkg_desc)){$pkg_desc = $pkg_name;}
rlm@2 58
rlm@3 59 @pkg_switch_cmd = ("su", "--login", $pkg_name);
rlm@3 60
rlm@3 61 @pkg_create_cmd =
rlm@3 62 ("useradd", "-c", $pkg_desc, "-d", $pkg_home,
rlm@3 63 "-g", $pkg_name, "-s", "/bin/bash",
rlm@3 64 $pkg_name);
rlm@3 65
rlm@3 66 @pkg_create_group_cmd =
rlm@3 67 ("groupadd", "--force", $pkg_name);
rlm@3 68
rlm@3 69 @pkg_create_home_cmd =
rlm@7 70 ("install", "-d", "-g", $pkg_name, "-o", $pkg_name,
rlm@9 71 "-m", "755", $pkg_home);
rlm@7 72
rlm@7 73 @pkg_add_install_cmd =
rlm@7 74 ("gpasswd", "--add", $pkg_name, "install");
rlm@3 75
rlm@7 76 @pkg_link_bashrc_cmd =
rlm@7 77 ("install", "-g", $pkg_name, "-o", $pkg_name,
rlm@8 78 "/pkg/defaults/.profile", "$pkg_home/.profile");
rlm@7 79
rlm@7 80 @pkg_copy_hg_wrapper_cmd =
rlm@12 81 (install, "-g", "hg-wheel", "-o", "hg-committer",
rlm@12 82 "-m", "6111", "/pkg/defaults/.hg-wrapper", $pkg_home);
rlm@12 83
rlm@12 84 sub execute{print "\t@_\n"; system(@_) and die $!;}
rlm@12 85
rlm@3 86 #determine if the user already exists:
rlm@3 87 `id $pkg_name 2>/dev/null`;
rlm@4 88 if (!$?){
rlm@4 89 print "Change user.\n";
rlm@4 90 execute(@pkg_switch_cmd); exit 0;}
rlm@3 91
rlm@3 92 print "Creating package group.\n";
rlm@3 93 execute(@pkg_create_group_cmd);
rlm@3 94
rlm@3 95 print "Creating package user.\n";
rlm@3 96 execute(@pkg_create_cmd);
rlm@3 97
rlm@3 98 print "Create home directory for $pkg_name.\n";
rlm@3 99 execute(@pkg_create_home_cmd);
rlm@3 100
rlm@7 101 print "Add $pkg_name to install group.\n";
rlm@7 102 execute(@pkg_add_install_cmd);
rlm@7 103
rlm@7 104 print "Copy Default Files.\n";
rlm@7 105 execute(@pkg_link_bashrc_cmd);
rlm@7 106 execute(@pkg_copy_hg_wrapper_cmd);
rlm@7 107
rlm@3 108 print "Change user.\n";
rlm@3 109 execute(@pkg_switch_cmd);