add utilities for artifact cleanup 96/5696/1
authorMike Marchetti <mmarchetti@sandvine.com>
Thu, 16 Nov 2017 21:06:15 +0000 (16:06 -0500)
committerMike Marchetti <mmarchetti@sandvine.com>
Thu, 16 Nov 2017 21:13:59 +0000 (16:13 -0500)
Change-Id: Ic3741f893c5678e986d8db157dac2103f9f9ea85
Signed-off-by: Mike Marchetti <mmarchetti@sandvine.com>
tools/cleanupBuilds.py [new file with mode: 0755]
tools/cleanupRepo.py [new file with mode: 0755]

diff --git a/tools/cleanupBuilds.py b/tools/cleanupBuilds.py
new file mode 100755 (executable)
index 0000000..2c44ae2
--- /dev/null
@@ -0,0 +1,81 @@
+#!/usr/bin/env python
+#
+#   Copyright 2017 Sandvine
+#
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+#
+#
+import requests
+import json
+import pprint
+import argparse
+
+from dateutil import parser
+from dateutil.tz import tzutc
+from datetime import datetime
+import time
+
+arg_parser=argparse.ArgumentParser(description="Tool to retrieve the latest build from the artifactory server")
+arg_parser.add_argument('branch')
+arg_parser.add_argument('--project',default='osm-stage_3')
+arg_parser.add_argument('--url',default='http://osm1.etsi.org:8081/')
+arg_parser.add_argument('--keep',default=5)
+arg_parser.add_argument('--password',default='')
+args = arg_parser.parse_args()
+
+url = args.url + 'artifactory/api/build/' + args.project + " :: " + args.branch
+
+resp = requests.get(url)
+jsonData = json.loads(resp.content)
+if 'buildsNumbers' not in jsonData:
+    print("Cannot find any valid builds")
+    exit(1)
+
+# first entry is the latest build
+buildlist = sorted(jsonData['buildsNumbers'], key=lambda x: int(x['uri'][1:]),reverse=True)
+print("total builds is {}".format(len(buildlist)))
+pprint.pprint(buildlist)
+
+if len(buildlist) < args.keep:
+    print("nothing to cleanup")
+    exit(0)
+
+def convert_to_ms(datetime):
+    #get the millisecond from the date/time
+    ms=datetime.split('.')[1].split('+')[0]
+    parser_out=parser.parse(datetime)
+    timeval=parser_out
+    tuple=int(time.mktime(timeval.timetuple()))
+    return (tuple*1000+int(ms)-(time.timezone*1000))
+
+def buildPost(dateInMS,buildNumber):
+    build = {}
+    data = {}
+    build['buildName'] = args.project + " :: " + args.branch
+    build['buildNumber'] = buildNumber
+    build['date'] = str(dateInMS)
+
+    data['buildsCoordinates'] = list()
+    data['buildsCoordinates'].append(build)
+    return data
+    
+delete_url = args.url + 'artifactory/ui/builds/buildsDelete'
+headers = {'Content-Type': 'application/json'}
+
+for entry in buildlist[int(args.keep):]:
+   ms = convert_to_ms(entry['started'])
+   buildNumber = entry['uri'].split('/')[1]
+   print("deleting build {} ms {}".format(args.project + " :: " + args.branch + '/' + buildNumber,ms))
+   postData = buildPost(ms,entry['uri'].split('/')[1])
+
+   requests.post(delete_url,data=json.dumps(postData),headers=headers,auth=('admin',args.password))
diff --git a/tools/cleanupRepo.py b/tools/cleanupRepo.py
new file mode 100755 (executable)
index 0000000..d25b5c5
--- /dev/null
@@ -0,0 +1,70 @@
+#!/usr/bin/env python
+#
+#   Copyright 2017 Sandvine
+#
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+#
+#
+import requests
+import json
+import pprint
+import argparse
+
+from dateutil import parser
+from dateutil.tz import tzutc
+from datetime import datetime
+import time
+
+arg_parser=argparse.ArgumentParser(description="Tool to retrieve the latest build from the artifactory server")
+arg_parser.add_argument('--branch',default=None)
+arg_parser.add_argument('repo')
+arg_parser.add_argument('--url',default='http://osm1.etsi.org:8081/')
+arg_parser.add_argument('--keep',default=5)
+arg_parser.add_argument('--password',default='')
+arg_parser.add_argument('--debug',default=None)
+
+args = arg_parser.parse_args()
+
+
+if args.branch:
+    url = args.url + 'artifactory/api/storage/' + args.repo + '/' + args.branch
+    delete_url = args.url + 'artifactory/' + args.repo + '/' + args.branch
+else:
+    url = args.url + 'artifactory/api/storage/' + args.repo
+    delete_url = args.url + 'artifactory/' + args.repo
+
+resp = requests.get(url)
+jsonData = json.loads(resp.content)
+
+# first entry is the latest build
+filtered_list = filter(lambda x: x['uri'].split('/')[1].isdigit(), jsonData['children'])
+folders_sorted = sorted(filtered_list, key=lambda x: int(x['uri'].split('/')[1]),reverse=True)
+
+if len(folders_sorted) < int(args.keep):
+    print("nothing to cleanup")
+    exit(0)
+
+
+for entry in folders_sorted[int(args.keep):]:
+    if args.debug:
+        print("going to delete {}".format(delete_url+entry['uri']))
+    else:
+        requests.delete(delete_url + entry['uri'], auth=('admin',args.password))
+
+
+# empty the trash can
+empty_trash_url=args.url + 'artifactory/ui/artifactactions/emptytrash'
+headers = {'Content-Type': 'application/json'}
+requests.post(empty_trash_url,headers=headers,auth=('admin',args.password))
+
+