-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnewgpfsdir.py
136 lines (94 loc) · 3.13 KB
/
newgpfsdir.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python3.6
__author__ = '[email protected]'
import argparse
import pwd
import grp
import os
import shutil
import sys
from slackclient import SlackClient
support = '[email protected]'
parser = argparse.ArgumentParser()
parser.add_argument('path', help='location on gpfs filesystem' +
' where new directory will be created')
parser.add_argument('directory', help='directory to create under path')
parser.add_argument('owner', help='user that owns the new directory')
parser.add_argument('group', help='group that owns new directory')
print("\nThis script allows authorized power users to create new" +
" directories under GPFS filesystems managed by the SCU.\n")
print("For assistance contact", __author__, "or , support + ""." + "\n")
args = parser.parse_args()
# global vars
path = sys.argv[1]
newdir = sys.argv[2]
owner = sys.argv[3]
group = sys.argv[4]
# get user who ran script using sudo
runas_user = os.environ['SUDO_USER']
# slack variables
#
# token thus this script must not be readable by normal users
# also do not send this variable into gitlab
# slack_token = os.environ["QUOTA_TOKEN"]
slack_token = "blahtoken"
sc = SlackClient(slack_token)
# slack messages sent
msg = runas_user + ' created new directory ' + path + '/' + newdir
emsg1 = runas_user + ' could NOT create directory ' + path
emsg2 = '/' + newdir + ' due error - please contact ' + support
emsg = emsg1 + emsg2
# list of power users and defined filesets where user quotas can be set
lab = ['poweruser', '/gpfs/location/for/new/dir']
# only allow valid path to be chosen
def sanitize():
if path == lab[1]:
print("Valid path chosen\n")
else:
print("You must choose a valid path. Exiting...")
sys.exit()
sanitize()
# ensure running this script on gpfs node
def gpfs_node():
try:
open('/var/adm/ras/mmfs.log.latest', 'r')
except IOError:
print("You must run this script on a GPFS node!")
sys.exit()
gpfs_node()
# ensure user owner of newdir actually exists
def checkuser():
try:
pwd.getpwnam(owner)
except KeyError:
print("User", owner, "does not exist.")
sys.exit()
checkuser()
# ensure group owner of newdir actually exists
def checkgroup():
try:
grp.getgrnam(group)
except KeyError:
print("Group", group, "does not exist.")
sys.exit()
checkgroup()
def privs():
if runas_user == lab[0]:
print("Creating new directory", newdir, "at location", path + "\n")
elif os.getuid() == 0:
print("Creating new directory", newdir, "at location", path + "\n")
else:
print("You must run with sudo!", "\n \n""Contact", support)
sys.exit()
privs()
def createdir():
ndir = path + '/' + newdir
if not os.path.exists(ndir):
os.makedirs(ndir)
print("New directory created")
sc.api_call("chat.postMessage", channel="#newgpfsdir", text=msg)
else:
print(ndir + " already exists!")
sc.api_call("chat.postMessage", channel="#newgpfsdir", text=emsg)
# set permissions
shutil.chown(ndir, user=owner, group=group)
createdir()