-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcreate_project.py
More file actions
executable file
·154 lines (114 loc) · 3.9 KB
/
create_project.py
File metadata and controls
executable file
·154 lines (114 loc) · 3.9 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python3
import os
import re
import sys
import json
import getopt
import urllib.error
import urllib.request
import comanage_utils as utils
SCRIPT = os.path.basename(__file__)
ENDPOINT = "https://registry.cilogon.org/registry/"
USER = "co_7.group_fixup"
OSG_CO_ID = 7
_usage = f"""\
usage: [PASS=...] {SCRIPT} [OPTIONS] COGroupNameOrId ProjectName
OPTIONS:
-u USER[:PASS] specify USER and optionally PASS on command line
-c OSG_CO_ID specify OSG CO ID (default = {OSG_CO_ID})
-d passfd specify open fd to read PASS
-f passfile specify path to file to open and read PASS
-e ENDPOINT specify REST endpoint
(default = {ENDPOINT})
-h display this help text
Adds an identifier of type ospoolproject named Yes-ProjectName to
a COGroup based on its Name or CO Group Id.
PASS for USER is taken from the first of:
1. -u USER:PASS
2. -d passfd (read from fd)
3. -f passfile (read from file)
4. read from $PASS env var
"""
def usage(msg=None):
if msg:
print(msg + "\n", file=sys.stderr)
print(_usage, file=sys.stderr)
sys.exit()
class Options:
endpoint = ENDPOINT
osg_co_id = OSG_CO_ID
user = USER
authstr = None
gid = None
gname = None
project = None
options = Options()
# script-specific functions
def add_project_identifier_to_group(gid, project_name):
identifier_name = "Yes-%s" % project_name
type_ = "ospoolproject"
return utils.add_identifier_to_group(gid, type_, identifier_name, options.endpoint, options.authstr)
def gname_to_gid(gname):
resp_data = utils.get_osg_co_groups(options.osg_co_id, options.endpoint, options.authstr)
groups = utils.get_datalist(resp_data, "CoGroups")
matching = [ g for g in groups if g["Name"] == gname ]
if len(matching) > 1:
raise RuntimeError("Multiple groups found with Name '%s'" % gname)
elif not matching:
raise RuntimeError("No group found with Name '%s'" % gname)
group = matching[0]
return group["Id"]
# CLI
def parse_options(args):
try:
ops, args = getopt.getopt(args, 'u:c:d:f:e:h')
except getopt.GetoptError:
usage()
if len(args) != 2:
usage()
cogroup, project = args
if re.fullmatch(r'\d+', cogroup):
options.gid = int(cogroup)
else:
options.gname = cogroup
options.project = project
passfd = None
passfile = None
for op, arg in ops:
if op == '-h': usage()
if op == '-u': options.user = arg
if op == '-c': options.osg_co_id = int(arg)
if op == '-d': passfd = int(arg)
if op == '-f': passfile = arg
if op == '-e': options.endpoint = arg
try:
user, passwd = utils.getpw(options.user, passfd, passfile)
options.authstr = utils.mkauthstr(user, passwd)
except PermissionError:
usage("PASS required")
def main(args):
parse_options(args)
if options.gname:
options.gid = gname_to_gid(options.gname)
else:
options.gname = utils.get_co_group(options.gid, options.endpoint, options.authstr)["Name"]
print('Creating new Identifier for project "%s"\n'
'for CO Group "%s" (%s)'
% (options.project, options.gname, options.gid))
print("")
resp = add_project_identifier_to_group(options.gid, options.project)
print("Server Response:")
print(json.dumps(resp, indent=2, sort_keys=True))
new_identifier = utils.get_identifier(resp["Id"], options.endpoint, options.authstr)
print("")
print("New Identifier Object:")
print(json.dumps(new_identifier, indent=2, sort_keys=True))
# no exceptions, must have worked
print("")
print(":thumbsup:")
if __name__ == "__main__":
try:
main(sys.argv[1:])
except (RuntimeError, urllib.error.HTTPError) as e:
print(e, file=sys.stderr)
sys.exit(1)