3 # Copyright 2017 Sandvine
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
23 from dateutil
import parser
24 from dateutil
.tz
import tzutc
25 from datetime
import datetime
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()
36 url
= args
.url
+ 'artifactory/api/build/' + args
.project
+ " :: " + args
.branch
38 resp
= requests
.get(url
)
39 jsonData
= json
.loads(resp
.content
)
40 if 'buildsNumbers' not in jsonData
:
41 print("Cannot find any valid builds")
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
)
49 if len(buildlist
) < args
.keep
:
50 print("nothing to cleanup")
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
)
59 tuple = int(time
.mktime(timeval
.timetuple()))
60 return (tuple * 1000 + int(ms
) - (time
.timezone
* 1000))
63 def buildPost(dateInMS
, buildNumber
):
66 build
['buildName'] = args
.project
+ " :: " + args
.branch
67 build
['buildNumber'] = buildNumber
68 build
['date'] = str(dateInMS
)
70 data
['buildsCoordinates'] = list()
71 data
['buildsCoordinates'].append(build
)
75 delete_url
= args
.url
+ 'artifactory/ui/builds/buildsDelete'
76 headers
= {'Content-Type': 'application/json'}
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])
84 requests
.post(delete_url
, data
=json
.dumps(postData
), headers
=headers
, auth
=('admin', args
.password
))