1 |
#!/home/jsoc/bin/linux_x86_64/perl -w |
2 |
|
3 |
# Creates a new DRMS user: |
4 |
# 1. creates a new db user with name specified by $dbuser |
5 |
# 2. creates a new db namespace with name specific by $dbns (should be su_$dbuser in most cases). |
6 |
# 3. sets default namespace for db user $dbuser (should be su_$dbuser in most cases) |
7 |
|
8 |
# template cmd-line: |
9 |
# newdrmsuser.pl jsoc hmidb 5432 arta changeme su_arta user 0 |
10 |
|
11 |
use DBI; |
12 |
use DBD::Pg; |
13 |
|
14 |
my($dbname); # name of the db instance to connect to |
15 |
my($dbhost); # name of the db host on which the db instance resides |
16 |
my($dbport); # port on $dbhost through which connections are made |
17 |
my($dbuser); # database user name (to create) |
18 |
my($dbpass); # database password (to create) |
19 |
my($dbns); # database namespace (to create with the masterlists call) |
20 |
my($dbnsgroup); # totally unused, but it must be provided in the cmd-line |
21 |
my($doit); |
22 |
|
23 |
my($dbh); # perl db handle |
24 |
my($dsn); # string thingy that identifies the db to connect to |
25 |
my($stmnt); |
26 |
my($res); |
27 |
my($cmd); |
28 |
|
29 |
$#ARGV == 7 || die "Improper argument list.\n"; |
30 |
|
31 |
$dbname = $ARGV[0]; |
32 |
$dbhost = $ARGV[1]; |
33 |
$dbport = $ARGV[2]; |
34 |
$dbuser = $ARGV[3]; |
35 |
$dbpass = $ARGV[4]; |
36 |
$dbns = $ARGV[5]; |
37 |
$dbnsgroup = $ARGV[6]; |
38 |
$doit = $ARGV[7]; |
39 |
|
40 |
# connect to the database |
41 |
$dsn = "dbi:Pg:dbname=$dbname;host=$dbhost;port=$dbport"; |
42 |
print "Connection to database with '$dsn' as user '$dbuser' ... "; |
43 |
|
44 |
# Despite ALL documentation saying otherwise, it looks like the error codes/string |
45 |
# provided by DBI are all UNDEFINED, unless there is some kind of failure. So, |
46 |
# never try to look at $dbh->err or $dbh->errstr if the call succeeded. |
47 |
$dbh = DBI->connect($dsn, 'postgres', ''); # will need to put pass in .pg_pass |
48 |
|
49 |
if (defined($dbh)) |
50 |
{ |
51 |
print "success!\n"; |
52 |
|
53 |
# create user $dbuser |
54 |
$stmnt = "CREATE USER $dbuser"; |
55 |
ExecStatment(\$dbh, $stmnt, $doit, "Unable to create db user $dbuser.\n"); |
56 |
|
57 |
$stmnt = "ALTER USER $dbuser WITH password '$dbpass'"; |
58 |
ExecStatment(\$dbh, $stmnt, $doit, "Unable to assign default password to $dbuser.\n"); |
59 |
|
60 |
$stmnt = "GRANT jsoc to $dbuser"; |
61 |
ExecStatment(\$dbh, $stmnt, $doit, "Unable to add $dbuser to db group jsoc.\n"); |
62 |
|
63 |
# run masterlists |
64 |
$cmd = "masterlists dbuser=$dbuser namespace=$dbns nsgrp=$dbnsgroup"; |
65 |
print "running cmd-line ==> $cmd\n"; |
66 |
|
67 |
if ($doit) |
68 |
{ |
69 |
system($cmd); |
70 |
|
71 |
if ($? == -1) |
72 |
{ |
73 |
die "Failed to execute '$cmd'.\n"; |
74 |
} |
75 |
elsif ($? & 127) |
76 |
{ |
77 |
die "masterlists crashed.\n"; |
78 |
} |
79 |
elsif ($? >> 8 != 0) |
80 |
{ |
81 |
die "masterlists ran unsuccessfully.\n"; |
82 |
} |
83 |
} |
84 |
|
85 |
# assign default namespace |
86 |
$stmnt = "INSERT INTO admin.sessionns VALUES ('$dbuser', '$dbns')"; |
87 |
ExecStatment(\$dbh, $stmnt, $doit, "Unable to assign default session namespace.\n"); |
88 |
|
89 |
$dbh->disconnect(); |
90 |
} |
91 |
else |
92 |
{ |
93 |
print "failure!!!!\n"; |
94 |
} |
95 |
|
96 |
# AL FINAL |
97 |
|
98 |
sub NoErr |
99 |
{ |
100 |
my($rv) = $_[0]; |
101 |
my($dbh) = $_[2]; |
102 |
my($stmnt) = $_[2]; |
103 |
my($ok) = 1; |
104 |
|
105 |
if (!defined($rv) || !$rv) |
106 |
{ |
107 |
if (defined($$dbh) && defined($$dbh->err)) |
108 |
{ |
109 |
print STDERR "Error " . $$dbh->errstr . ": Statement '$stmnt' failed.\n"; |
110 |
} |
111 |
|
112 |
$ok = 0; |
113 |
} |
114 |
|
115 |
return $ok; |
116 |
} |
117 |
|
118 |
sub ExecStatment |
119 |
{ |
120 |
my($dbh, $stmnt, $doit, $msg) = @_; |
121 |
my($res); |
122 |
|
123 |
print "executing db statment ==> $stmnt\n"; |
124 |
|
125 |
if ($doit) |
126 |
{ |
127 |
$res = $$dbh->do($stmnt); |
128 |
NoErr($res, $dbh, $stmnt) || die $msg; |
129 |
} |
130 |
} |