-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdelete_old_jobs.py
More file actions
138 lines (106 loc) · 4.09 KB
/
delete_old_jobs.py
File metadata and controls
138 lines (106 loc) · 4.09 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
#!/usr/bin/python3
#
# Delete artifacts and traces of ci jobs older than max-days
#
# Copyright 2018 ETH Zurich, ISGINF, Bastian Ballmann
# Email: bastian.ballmann@inf.ethz.ch
# Web: http://www.isg.inf.ethz.ch
#
# This is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# It is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License.
# If not, see <http://www.gnu.org/licenses/>.
#
# Import modules
#
import sys
sys.path.append("../")
import os
import json
import time
import argparse
from signal import signal, SIGINT
from multiprocessing import Queue
from dateutil.parser import parse
from datetime import datetime, timedelta
import gitlab_lib
import gitlab_config
#
# PARAMETERS
#
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--debug", help="Show debug messages", action="store_true")
parser.add_argument("-n", "--number", help="Number of processes", type=int, default="4")
parser.add_argument("-D", "--dryrun", help="Don't delete anything. Just simulate", action="store_true", default=False)
parser.add_argument("-m", "--max-days", help="Delete jobs older than max days", default=90)
parser.add_argument("-P", "--project", help="Delete old jobs for projects found by given id or name")
parser.add_argument("-q", "--quiet", help="No messages execpt errors", action="store_true")
parser.add_argument("-s", "--server", help="Gitlab server name", default=gitlab_config.SERVER)
parser.add_argument("-t", "--token", help="Private token", default=gitlab_config.TOKEN)
args = parser.parse_args()
if not args.server or not args.token:
print("You must at least specify --server and --token")
sys.exit(1)
gitlab_lib.SERVER = args.server
gitlab_lib.TOKEN = args.token
gitlab_lib.core.DEBUG = args.debug
gitlab_lib.core.QUIET = args.quiet
queue = Queue()
processes = []
#
# SIGNAL HANDLERS
#
def clean_shutdown(signal, frame):
for process in processes:
process.terminate()
signal(SIGINT, clean_shutdown)
#
# Subroutines
#
#
# This is the main function run by the parallel processes
# It deletes jobs older than max_days
#
def delete_old_jobs(queue):
max_date = datetime.now() - timedelta(days=int(args.max_days))
while queue.qsize() > 0:
project = queue.get()
gitlab_lib.log("Getting jobs for project [%d] %s" % (project["id"], project["name_with_namespace"]))
for job in gitlab_lib.get_jobs(project):
job_date = parse(job["created_at"])
max_date = max_date.replace(tzinfo=job_date.tzinfo)
gitlab_lib.debug("Checking job %d of project [%d] %s with create date %s" % (job["id"], project["id"], project["name_with_namespace"], job["created_at"]))
if job_date < max_date:
gitlab_lib.log("Deleting job %d of project [%d] %s" % (job["id"], project["id"], project["name_with_namespace"]))
if not args.dryrun:
gitlab_lib.delete_job(project["id"], job["id"])
#
# MAIN PART
#
if args.dryrun:
print("Running in simulation mode\n")
gitlab_lib.debug("Setting up work queue")
if args.project:
queue.put(gitlab_lib.get_project(args.project))
delete_old_jobs(queue)
else:
for project in gitlab_lib.get_projects():
queue.put(project)
gitlab_lib.debug("Processing work queue")
for _ in range(args.number):
processes.append( gitlab_lib.create_process(delete_old_jobs, (queue,)) )
# wait for processes to finish the work
while queue.qsize() > 0:
time.sleep(5)
# check if a process crashed and must be restarted
if len(processes) < int(args.number) and queue.qsize() > len(processes):
gitlab_lib.debug("Starting new process")
processes.append( gitlab_lib.create_process(delete_old_jobs, (queue,)) )