-
Notifications
You must be signed in to change notification settings - Fork 1
/
remove-last-users.py
executable file
·84 lines (74 loc) · 2.72 KB
/
remove-last-users.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
#!/usr/bin/env python
import gitlab
import sys
import maya
from datetime import datetime, timedelta
import pytz
import getopt
import auth
group_gl = {}
users_gl = {}
gitlab_server_object, tokens = auth.get_auth()
root_gl = gitlab.Gitlab(tokens["root"]['url'], private_token=tokens["root"]['api'],api_version=4)
root_gl.auth()
print("version of ",tokens["root"]['url'], "is ", root_gl.version())
def remove_group_gl(since_hours):
hours_since_now = datetime.now().replace(tzinfo=pytz.UTC) - timedelta(hours=since_hours)
groups = root_gl.groups.list(all=True)
for group in groups:
#print("#####################")
group_details = root_gl.groups.get(group.id)
for project in group_details.projects.list():
if project.name == "Notebooks":
#print(project.created_at)
dt = maya.parse(project.created_at).datetime()
if dt > hours_since_now:
print("delete group: ",dt, group.name)
group.delete()
return groups
def remove_project_gl(since_hours):
hours_since_now = datetime.now().replace(tzinfo=pytz.UTC) - timedelta(hours=since_hours)
projects = root_gl.projects.list(all=True)
for project in projects:
#print("#####################")
#group_details = root_gl.projects.get(project.id)
#for project in group_details.projects.list():
if project.name == "Notebooks":
#print(project.created_at)
dt = maya.parse(project.created_at).datetime()
if dt > hours_since_now:
print("delete project: ",dt, project.name_with_namespace)
project.delete()
return projects
def remove_users_gl(since_hours):
hours_since_now = datetime.now().replace(tzinfo=pytz.UTC) - timedelta(hours=since_hours)
users = root_gl.users.list(all=True)
for user in users:
dt = maya.parse(user.created_at).datetime()
print(dt, hours_since_now)
if ( dt > hours_since_now
and not user.name == 'Ghost User'
and not user.name == 'Administrator'
and not user.name == 'Frank Neuber'
and not user.name == 'Dydra Tester'
):
print("delete user: ",dt, user.name)
user.delete()
return users
days = 1
try:
opts, args = getopt.getopt(sys.argv[1:],"hd:",["days="])
except getopt.GetoptError:
print('cleanup-devel.py -d <number of days>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('cleanup-devel.py -d <number of days>')
sys.exit()
elif opt in ("-d", "--days"):
days = int(arg)
print('clean "', days)
remove_users_gl(24*days)
remove_group_gl(4)
remove_project_gl(4)
sys.exit()