blob: 44ab6a196ae6ea22645333a71c8771f2b8a2bd18 [file] [log] [blame]
Mike Marchetti5f6fade2017-11-16 16:06:15 -05001#!/usr/bin/env python
2#
3# Copyright 2017 Sandvine
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17#
18import requests
19import json
20import pprint
21import argparse
22
23from dateutil import parser
24from dateutil.tz import tzutc
25from datetime import datetime
26import time
27
28arg_parser=argparse.ArgumentParser(description="Tool to retrieve the latest build from the artifactory server")
29arg_parser.add_argument('--branch',default=None)
30arg_parser.add_argument('repo')
garciadeblasdab1ac62025-01-23 15:55:00 +010031arg_parser.add_argument('--url',default='https://artifactory-osm.etsi.org/')
Mike Marchetti5f6fade2017-11-16 16:06:15 -050032arg_parser.add_argument('--keep',default=5)
garciadeblasdab1ac62025-01-23 15:55:00 +010033arg_parser.add_argument('--user', default='admin')
Mike Marchetti5f6fade2017-11-16 16:06:15 -050034arg_parser.add_argument('--password',default='')
35arg_parser.add_argument('--debug',default=None)
36
37args = arg_parser.parse_args()
38
39
40if args.branch:
41 url = args.url + 'artifactory/api/storage/' + args.repo + '/' + args.branch
42 delete_url = args.url + 'artifactory/' + args.repo + '/' + args.branch
43else:
44 url = args.url + 'artifactory/api/storage/' + args.repo
45 delete_url = args.url + 'artifactory/' + args.repo
46
47resp = requests.get(url)
48jsonData = json.loads(resp.content)
49
50# first entry is the latest build
51filtered_list = filter(lambda x: x['uri'].split('/')[1].isdigit(), jsonData['children'])
52folders_sorted = sorted(filtered_list, key=lambda x: int(x['uri'].split('/')[1]),reverse=True)
53
54if len(folders_sorted) < int(args.keep):
55 print("nothing to cleanup")
56 exit(0)
57
58
59for entry in folders_sorted[int(args.keep):]:
60 if args.debug:
61 print("going to delete {}".format(delete_url+entry['uri']))
62 else:
garciadeblasdab1ac62025-01-23 15:55:00 +010063 requests.delete(delete_url + entry['uri'], auth=(args.user, args.password))
Mike Marchetti5f6fade2017-11-16 16:06:15 -050064
65
66# empty the trash can
67empty_trash_url=args.url + 'artifactory/ui/artifactactions/emptytrash'
68headers = {'Content-Type': 'application/json'}
69requests.post(empty_trash_url,headers=headers,auth=('admin',args.password))
70
71