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