From: Mike Marchetti Date: Thu, 16 Nov 2017 21:06:15 +0000 (-0500) Subject: add utilities for artifact cleanup X-Git-Tag: v3.0.2~16^2 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=refs%2Fchanges%2F96%2F5696%2F1;p=osm%2Fdevops.git add utilities for artifact cleanup Change-Id: Ic3741f893c5678e986d8db157dac2103f9f9ea85 Signed-off-by: Mike Marchetti --- diff --git a/tools/cleanupBuilds.py b/tools/cleanupBuilds.py new file mode 100755 index 00000000..2c44ae23 --- /dev/null +++ b/tools/cleanupBuilds.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python +# +# Copyright 2017 Sandvine +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# +import requests +import json +import pprint +import argparse + +from dateutil import parser +from dateutil.tz import tzutc +from datetime import datetime +import time + +arg_parser=argparse.ArgumentParser(description="Tool to retrieve the latest build from the artifactory server") +arg_parser.add_argument('branch') +arg_parser.add_argument('--project',default='osm-stage_3') +arg_parser.add_argument('--url',default='http://osm1.etsi.org:8081/') +arg_parser.add_argument('--keep',default=5) +arg_parser.add_argument('--password',default='') +args = arg_parser.parse_args() + +url = args.url + 'artifactory/api/build/' + args.project + " :: " + args.branch + +resp = requests.get(url) +jsonData = json.loads(resp.content) +if 'buildsNumbers' not in jsonData: + print("Cannot find any valid builds") + exit(1) + +# first entry is the latest build +buildlist = sorted(jsonData['buildsNumbers'], key=lambda x: int(x['uri'][1:]),reverse=True) +print("total builds is {}".format(len(buildlist))) +pprint.pprint(buildlist) + +if len(buildlist) < args.keep: + print("nothing to cleanup") + exit(0) + +def convert_to_ms(datetime): + #get the millisecond from the date/time + ms=datetime.split('.')[1].split('+')[0] + parser_out=parser.parse(datetime) + timeval=parser_out + tuple=int(time.mktime(timeval.timetuple())) + return (tuple*1000+int(ms)-(time.timezone*1000)) + +def buildPost(dateInMS,buildNumber): + build = {} + data = {} + build['buildName'] = args.project + " :: " + args.branch + build['buildNumber'] = buildNumber + build['date'] = str(dateInMS) + + data['buildsCoordinates'] = list() + data['buildsCoordinates'].append(build) + return data + +delete_url = args.url + 'artifactory/ui/builds/buildsDelete' +headers = {'Content-Type': 'application/json'} + +for entry in buildlist[int(args.keep):]: + ms = convert_to_ms(entry['started']) + buildNumber = entry['uri'].split('/')[1] + print("deleting build {} ms {}".format(args.project + " :: " + args.branch + '/' + buildNumber,ms)) + postData = buildPost(ms,entry['uri'].split('/')[1]) + + requests.post(delete_url,data=json.dumps(postData),headers=headers,auth=('admin',args.password)) diff --git a/tools/cleanupRepo.py b/tools/cleanupRepo.py new file mode 100755 index 00000000..d25b5c55 --- /dev/null +++ b/tools/cleanupRepo.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python +# +# Copyright 2017 Sandvine +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# +import requests +import json +import pprint +import argparse + +from dateutil import parser +from dateutil.tz import tzutc +from datetime import datetime +import time + +arg_parser=argparse.ArgumentParser(description="Tool to retrieve the latest build from the artifactory server") +arg_parser.add_argument('--branch',default=None) +arg_parser.add_argument('repo') +arg_parser.add_argument('--url',default='http://osm1.etsi.org:8081/') +arg_parser.add_argument('--keep',default=5) +arg_parser.add_argument('--password',default='') +arg_parser.add_argument('--debug',default=None) + +args = arg_parser.parse_args() + + +if args.branch: + url = args.url + 'artifactory/api/storage/' + args.repo + '/' + args.branch + delete_url = args.url + 'artifactory/' + args.repo + '/' + args.branch +else: + url = args.url + 'artifactory/api/storage/' + args.repo + delete_url = args.url + 'artifactory/' + args.repo + +resp = requests.get(url) +jsonData = json.loads(resp.content) + +# first entry is the latest build +filtered_list = filter(lambda x: x['uri'].split('/')[1].isdigit(), jsonData['children']) +folders_sorted = sorted(filtered_list, key=lambda x: int(x['uri'].split('/')[1]),reverse=True) + +if len(folders_sorted) < int(args.keep): + print("nothing to cleanup") + exit(0) + + +for entry in folders_sorted[int(args.keep):]: + if args.debug: + print("going to delete {}".format(delete_url+entry['uri'])) + else: + requests.delete(delete_url + entry['uri'], auth=('admin',args.password)) + + +# empty the trash can +empty_trash_url=args.url + 'artifactory/ui/artifactactions/emptytrash' +headers = {'Content-Type': 'application/json'} +requests.post(empty_trash_url,headers=headers,auth=('admin',args.password)) + +