-
Notifications
You must be signed in to change notification settings - Fork 8
/
snap_bahmni_batch_fetcher.py
52 lines (40 loc) · 1.98 KB
/
snap_bahmni_batch_fetcher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from subprocess import Popen, PIPE, STDOUT
import json
import os
import urllib
import yum
if os.geteuid() != 0:
print "You need to have root privileges to run this script. Exiting."
exit(-1)
if yum.YumBase().rpmdb.searchNevra(name='bahmni-endtb-batch'):
print "Removing previous install..."
Popen("yum remove -y bahmni-endtb-batch", shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
try:
SNAP_API_KEY = os.environ["SNAP_API_KEY"]
except KeyError:
print "Please set SNAP_API_KEY env variable with the API key"
exit(-1)
print "Fetching pipeline info..."
cmd = "curl -s -u rahul080327:%s -X GET -H 'Accept: application/vnd.snap-ci.com.v1+json' https://api.snap-ci.com/project/Bahmni/bahmni-endtb-batch/branch/master/pipelines" % SNAP_API_KEY
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
output = p.stdout.read()
info = json.loads(output)
print "Parsing pipeline info..."
for pipeline in info["_embedded"]["pipelines"]:
if pipeline["result"] == "passed" and pipeline["stages"][-1]["result"] == "passed":
stage = pipeline["stages"][-1]
if stage["workers"][-1]["result"] == "passed":
url = stage["workers"][-1]["artifacts"][0]["download_url"]
artifact_name = url.split("/")[-1]
print "Fetching download info..."
cmd = "curl -s -u rahul080327:%s -X GET -H 'Accept: application/vnd.snap-ci.com.v1+json' %s" % (SNAP_API_KEY, url)
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
output = p.stdout.read()
info = json.loads(output)
print "Downloading %s..." % artifact_name
file = urllib.URLopener()
file.retrieve(info["_links"]["redirect"]["href"], artifact_name)
print "Complete."
print "Installing..."
Popen("yum localinstall -y %s" % artifact_name, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
break