Add integration between pol and mysql charms
[osm/devops.git] / tools / cleanupBuilds.py
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='https://artifactory-osm.etsi.org/')
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
54 def convert_to_ms(datetime):
55 # get the millisecond from the date/time
56 ms = datetime.split('.')[1].split('+')[0]
57 parser_out = parser.parse(datetime)
58 timeval = parser_out
59 tuple = int(time.mktime(timeval.timetuple()))
60 return (tuple * 1000 + int(ms) - (time.timezone * 1000))
61
62
63 def buildPost(dateInMS, buildNumber):
64 build = {}
65 data = {}
66 build['buildName'] = args.project + " :: " + args.branch
67 build['buildNumber'] = buildNumber
68 build['date'] = str(dateInMS)
69
70 data['buildsCoordinates'] = list()
71 data['buildsCoordinates'].append(build)
72 return data
73
74
75 delete_url = args.url + 'artifactory/ui/builds/buildsDelete'
76 headers = {'Content-Type': 'application/json'}
77
78 for entry in buildlist[int(args.keep):]:
79 ms = convert_to_ms(entry['started'])
80 buildNumber = entry['uri'].split('/')[1]
81 print("deleting build {} ms {}".format(args.project + " :: " + args.branch + '/' + buildNumber, ms))
82 postData = buildPost(ms, entry['uri'].split('/')[1])
83
84 requests.post(delete_url, data=json.dumps(postData), headers=headers, auth=('admin', args.password))