blob: 997d6bf1f0b26884181ea7bf9ec7af7fb39b8af9 [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')
beierlmff4016f2020-06-30 13:36:26 -040031arg_parser.add_argument('--url',default='https://artifactory-osm.etsi.org/)
Mike Marchetti5f6fade2017-11-16 16:06:15 -050032arg_parser.add_argument('--keep',default=5)
33arg_parser.add_argument('--password',default='')
34arg_parser.add_argument('--debug',default=None)
35
36args = arg_parser.parse_args()
37
38
39if args.branch:
40 url = args.url + 'artifactory/api/storage/' + args.repo + '/' + args.branch
41 delete_url = args.url + 'artifactory/' + args.repo + '/' + args.branch
42else:
43 url = args.url + 'artifactory/api/storage/' + args.repo
44 delete_url = args.url + 'artifactory/' + args.repo
45
46resp = requests.get(url)
47jsonData = json.loads(resp.content)
48
49# first entry is the latest build
50filtered_list = filter(lambda x: x['uri'].split('/')[1].isdigit(), jsonData['children'])
51folders_sorted = sorted(filtered_list, key=lambda x: int(x['uri'].split('/')[1]),reverse=True)
52
53if len(folders_sorted) < int(args.keep):
54 print("nothing to cleanup")
55 exit(0)
56
57
58for entry in folders_sorted[int(args.keep):]:
59 if args.debug:
60 print("going to delete {}".format(delete_url+entry['uri']))
61 else:
62 requests.delete(delete_url + entry['uri'], auth=('admin',args.password))
63
64
65# empty the trash can
66empty_trash_url=args.url + 'artifactory/ui/artifactactions/emptytrash'
67headers = {'Content-Type': 'application/json'}
68requests.post(empty_trash_url,headers=headers,auth=('admin',args.password))
69
70