1
1
#!/usr/bin/env python3
2
2
3
3
import os
4
+ import re
4
5
import sys
5
6
import json
6
7
import getopt
@@ -110,12 +111,13 @@ def call_api3(method, target, data, **kw):
110
111
return json .loads (payload ) if payload else None
111
112
112
113
114
+ # primary api calls
115
+
116
+
113
117
def get_osg_co_groups ():
114
118
return call_api ("co_groups.json" , coid = options .osg_co_id )
115
119
116
120
117
- # primary api calls
118
-
119
121
def get_co_group_identifiers (gid ):
120
122
return call_api ("identifiers.json" , cogroupid = gid )
121
123
@@ -128,6 +130,13 @@ def get_co_person_identifiers(pid):
128
130
return call_api ("identifiers.json" , copersonid = pid )
129
131
130
132
133
+ def get_co_group (gid ):
134
+ grouplist = call_api ("co_groups/%d.json" % gid ) | get_datalist ("CoGroups" )
135
+ if not grouplist :
136
+ raise RuntimeError ("No such CO Group Id: %s" % gid )
137
+ return grouplist [0 ]
138
+
139
+
131
140
# @rorable
132
141
# def foo(x): ...
133
142
# x | foo -> foo(x)
@@ -146,11 +155,127 @@ def get(data):
146
155
# api call results massagers
147
156
148
157
158
+ def get_unixcluser_autogroups ():
159
+ groups = get_osg_co_groups ()
160
+ return [ g for g in groups ["CoGroups" ]
161
+ if "automatically by UnixCluster" in g ["Description" ] ]
162
+
163
+
164
+ def get_misnamed_unixcluster_groups ():
165
+ groups = get_osg_co_groups ()
166
+ return [ g for g in groups ["CoGroups" ]
167
+ if "UnixCluster Group" in g ["Name" ] ]
168
+
169
+
170
+ def _osgid_sortkey (i ):
171
+ return int (i ["Identifier" ])
172
+
173
+ def get_identifiers_to_delete (identifiers ):
174
+ by_type = collections .defaultdict (list )
175
+ ids_to_delete = []
176
+
177
+ for i in identifiers :
178
+ by_type [i ["Type" ]].append (i )
179
+
180
+ if len (by_type ["osggid" ]) == 2 :
181
+ min_identifier = min (by_type ["osggid" ], key = _osgid_sortkey )
182
+ ids_to_delete .append (min_identifier ["Id" ])
183
+
184
+ for i in by_type ["osggroup" ]:
185
+ if i ["Identifier" ].endswith ("unixclustergroup" ):
186
+ ids_to_delete .append (i ["Id" ])
187
+
188
+ return ids_to_delete
189
+
190
+
191
+ def get_fixed_unixcluster_group_name (name ):
192
+ m = re .search (r'^(.*) UnixCluster Group' , name )
193
+ return m .group (1 ) if m else name
194
+
195
+
196
+ # display functions
197
+
198
+
199
+ def show_misnamed_unixcluster_group (group ):
200
+ print ('CO {CoId} Group {Id}: "{Name}"' .format (** group ))
201
+ oldname = group ["Name" ]
202
+ newname = get_fixed_unixcluster_group_name (oldname )
203
+ if oldname != newname :
204
+ print (' ** Rename group to: "%s"' % newname )
205
+ show_group_identifiers (group ["Id" ])
206
+ print ("" )
207
+
208
+
209
+ def show_all_unixcluster_groups ():
210
+ groups = get_unixcluser_autogroups ()
211
+ for group in groups :
212
+ show_misnamed_unixcluster_group (group )
213
+
214
+
215
+ def show_one_unixcluster_group (gid ):
216
+ group = get_co_group (gid )
217
+ show_misnamed_unixcluster_group (group )
218
+
219
+
220
+ def show_misnamed_unixcluster_groups ():
221
+ groups = get_misnamed_unixcluster_groups ()
222
+ for group in groups :
223
+ show_misnamed_unixcluster_group (group )
224
+
225
+
226
+ def show_group_identifiers (gid ):
227
+ identifiers = get_co_group_identifiers (gid ) | get_datalist ("Identifiers" )
228
+ for i in identifiers :
229
+ print (' - Identifier {Id}: ({Type}) "{Identifier}"' .format (** i ))
230
+
231
+ ids_to_delete = get_identifiers_to_delete (identifiers )
232
+ if ids_to_delete :
233
+ print (' ** Identifier Ids to delete: %s' % ', ' .join (ids_to_delete ))
234
+
235
+
236
+
237
+ # fixup functions
238
+
239
+
240
+ def delete_identifier (id_ ):
241
+ return call_api2 (DELETE , "identifiers/%d.json" % id_ )
242
+
243
+
244
+ def rename_co_group (gid , group , newname ):
245
+ # minimal edit CoGroup Request includes Name+CoId+Status+Version
246
+ new_group_info = {
247
+ "Name" : newname ,
248
+ "CoId" : group ["CoId" ],
249
+ "Status" : group ["Status" ],
250
+ "Version" : group ["Version" ]
251
+ }
252
+ data = {
253
+ "CoGroups" : [new_group_info ],
254
+ "RequestType" : "CoGroups" ,
255
+ "Version" : "1.0"
256
+ }
257
+ return call_api3 (PUT , "co_groups/%d.json" % gid , data )
258
+
149
259
260
+ def fixup_unixcluster_group (gid ):
261
+ group = get_co_group (gid )
262
+ oldname = group ["Name" ]
263
+ newname = get_fixed_unixcluster_group_name (oldname )
264
+ identifiers = get_co_group_identifiers (gid ) | get_datalist ("Identifiers" )
265
+ ids_to_delete = get_identifiers_to_delete (identifiers )
150
266
267
+ show_misnamed_unixcluster_group (group )
268
+ if oldname != newname :
269
+ rename_co_group (gid , group , newname )
270
+ for id_ in ids_to_delete :
271
+ delete_identifier (id_ )
151
272
273
+ # http errors raise exceptions, so at this point we apparently succeeded
274
+ print (":thumbsup:" )
275
+ return 0
152
276
153
277
278
+ # CLI
154
279
155
280
156
281
def parse_options (args ):
0 commit comments