blob: 2c44ae23bae195335b7306baa13065b7a5854b87 [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')
30arg_parser.add_argument('--project',default='osm-stage_3')
31arg_parser.add_argument('--url',default='http://osm1.etsi.org:8081/')
32arg_parser.add_argument('--keep',default=5)
33arg_parser.add_argument('--password',default='')
34args = arg_parser.parse_args()
35
36url = args.url + 'artifactory/api/build/' + args.project + " :: " + args.branch
37
38resp = requests.get(url)
39jsonData = json.loads(resp.content)
40if 'buildsNumbers' not in jsonData:
41 print("Cannot find any valid builds")
42 exit(1)
43
44# first entry is the latest build
45buildlist = sorted(jsonData['buildsNumbers'], key=lambda x: int(x['uri'][1:]),reverse=True)
46print("total builds is {}".format(len(buildlist)))
47pprint.pprint(buildlist)
48
49if len(buildlist) < args.keep:
50 print("nothing to cleanup")
51 exit(0)
52
53def 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
61def 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
72delete_url = args.url + 'artifactory/ui/builds/buildsDelete'
73headers = {'Content-Type': 'application/json'}
74
75for 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))