| 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') |
| 30 | arg_parser.add_argument('--project',default='osm-stage_3') |
| 31 | arg_parser.add_argument('--url',default='http://osm1.etsi.org:8081/') |
| 32 | arg_parser.add_argument('--keep',default=5) |
| 33 | arg_parser.add_argument('--password',default='') |
| 34 | args = arg_parser.parse_args() |
| 35 | |
| 36 | url = args.url + 'artifactory/api/build/' + args.project + " :: " + args.branch |
| 37 | |
| 38 | resp = requests.get(url) |
| 39 | jsonData = json.loads(resp.content) |
| 40 | if 'buildsNumbers' not in jsonData: |
| 41 | print("Cannot find any valid builds") |
| 42 | exit(1) |
| 43 | |
| 44 | # first entry is the latest build |
| 45 | buildlist = sorted(jsonData['buildsNumbers'], key=lambda x: int(x['uri'][1:]),reverse=True) |
| 46 | print("total builds is {}".format(len(buildlist))) |
| 47 | pprint.pprint(buildlist) |
| 48 | |
| 49 | if len(buildlist) < args.keep: |
| 50 | print("nothing to cleanup") |
| 51 | exit(0) |
| 52 | |
| 53 | def convert_to_ms(datetime): |
| 54 | #get the millisecond from the date/time |
| 55 | ms=datetime.split('.')[1].split('+')[0] |
| 56 | parser_out=parser.parse(datetime) |
| 57 | timeval=parser_out |
| 58 | tuple=int(time.mktime(timeval.timetuple())) |
| 59 | return (tuple*1000+int(ms)-(time.timezone*1000)) |
| 60 | |
| 61 | def buildPost(dateInMS,buildNumber): |
| 62 | build = {} |
| 63 | data = {} |
| 64 | build['buildName'] = args.project + " :: " + args.branch |
| 65 | build['buildNumber'] = buildNumber |
| 66 | build['date'] = str(dateInMS) |
| 67 | |
| 68 | data['buildsCoordinates'] = list() |
| 69 | data['buildsCoordinates'].append(build) |
| 70 | return data |
| 71 | |
| 72 | delete_url = args.url + 'artifactory/ui/builds/buildsDelete' |
| 73 | headers = {'Content-Type': 'application/json'} |
| 74 | |
| 75 | for entry in buildlist[int(args.keep):]: |
| 76 | ms = convert_to_ms(entry['started']) |
| 77 | buildNumber = entry['uri'].split('/')[1] |
| 78 | print("deleting build {} ms {}".format(args.project + " :: " + args.branch + '/' + buildNumber,ms)) |
| 79 | postData = buildPost(ms,entry['uri'].split('/')[1]) |
| 80 | |
| 81 | requests.post(delete_url,data=json.dumps(postData),headers=headers,auth=('admin',args.password)) |