-
Notifications
You must be signed in to change notification settings - Fork 16
/
cleanup-orgs
44 lines (36 loc) · 1.27 KB
/
cleanup-orgs
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
#!/usr/bin/env python3
import argparse
import mongoengine as me
from mist.api.users.methods import purge_org
from mist.api.models import User, Organization
def main():
argparser = argparse.ArgumentParser(
description='Cleanup broken references from orgs'
)
argparser.add_argument('-o', '--organization',
help='ID of org to cleanup.',
required=False)
args = argparser.parse_args()
def cleanup_org(org):
for member in org.members:
if member.__class__ != User:
if len(org.members) > 1:
org.members.remove(member)
try:
org.save()
except me.ValidationError:
print('Purging org %s' % org.name)
purge_org(org)
print('Done purging org')
else:
print('Purging org %s' % org.name)
purge_org(org)
print('Done purging org')
if not args.organization:
for org in Organization.objects:
cleanup_org(org)
else:
org = Organization.objects.get(id=args.organization)
cleanup_org(org)
if __name__ == '__main__':
main()