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
='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()
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")
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
)
58 tuple=int(time
.mktime(timeval
.timetuple()))
59 return (tuple*1000+int(ms
)-(time
.timezone
*1000))
61 def buildPost(dateInMS
,buildNumber
):
64 build
['buildName'] = args
.project
+ " :: " + args
.branch
65 build
['buildNumber'] = buildNumber
66 build
['date'] = str(dateInMS
)
68 data
['buildsCoordinates'] = list()
69 data
['buildsCoordinates'].append(build
)
72 delete_url
= args
.url
+ 'artifactory/ui/builds/buildsDelete'
73 headers
= {'Content-Type': 'application/json'}
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])
81 requests
.post(delete_url
,data
=json
.dumps(postData
),headers
=headers
,auth
=('admin',args
.password
))