| Mike Marchetti | 5f6fade | 2017-11-16 16:06:15 -0500 | [diff] [blame] | 1 | #!/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 | # |
| 18 | import requests |
| 19 | import json |
| 20 | import pprint |
| 21 | import argparse |
| 22 | |
| 23 | from dateutil import parser |
| 24 | from dateutil.tz import tzutc |
| 25 | from datetime import datetime |
| 26 | import time |
| 27 | |
| 28 | arg_parser=argparse.ArgumentParser(description="Tool to retrieve the latest build from the artifactory server") |
| 29 | arg_parser.add_argument('--branch',default=None) |
| 30 | arg_parser.add_argument('repo') |
| beierlm | ff4016f | 2020-06-30 13:36:26 -0400 | [diff] [blame^] | 31 | arg_parser.add_argument('--url',default='https://artifactory-osm.etsi.org/) |
| Mike Marchetti | 5f6fade | 2017-11-16 16:06:15 -0500 | [diff] [blame] | 32 | arg_parser.add_argument('--keep',default=5) |
| 33 | arg_parser.add_argument('--password',default='') |
| 34 | arg_parser.add_argument('--debug',default=None) |
| 35 | |
| 36 | args = arg_parser.parse_args() |
| 37 | |
| 38 | |
| 39 | if args.branch: |
| 40 | url = args.url + 'artifactory/api/storage/' + args.repo + '/' + args.branch |
| 41 | delete_url = args.url + 'artifactory/' + args.repo + '/' + args.branch |
| 42 | else: |
| 43 | url = args.url + 'artifactory/api/storage/' + args.repo |
| 44 | delete_url = args.url + 'artifactory/' + args.repo |
| 45 | |
| 46 | resp = requests.get(url) |
| 47 | jsonData = json.loads(resp.content) |
| 48 | |
| 49 | # first entry is the latest build |
| 50 | filtered_list = filter(lambda x: x['uri'].split('/')[1].isdigit(), jsonData['children']) |
| 51 | folders_sorted = sorted(filtered_list, key=lambda x: int(x['uri'].split('/')[1]),reverse=True) |
| 52 | |
| 53 | if len(folders_sorted) < int(args.keep): |
| 54 | print("nothing to cleanup") |
| 55 | exit(0) |
| 56 | |
| 57 | |
| 58 | for 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 |
| 66 | empty_trash_url=args.url + 'artifactory/ui/artifactactions/emptytrash' |
| 67 | headers = {'Content-Type': 'application/json'} |
| 68 | requests.post(empty_trash_url,headers=headers,auth=('admin',args.password)) |
| 69 | |
| 70 | |