-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.py
executable file
·109 lines (93 loc) · 2.74 KB
/
fetch.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python
import json
import logging
import os
import sys
import urllib
import urllib2
from StringIO import StringIO
target_url = 'https://api.host.com/api-v1/repositories/'
auth_url = 'https://api.host.com/auth'
post_url = 'https://api.host.com/do'
_auth = None
def authenticate():
global _auth
user = os.getenv('API_USER')
pwd = os.getenv('API_PWD')
errors = ''
if user is None:
errors = errors + 'API_USER not set. '
if pwd is None:
errors = errors + 'API_PWD not set. '
if errors != '':
logging.error(errors)
sys.exit(1)
try:
token_req = urllib2.Request(
auth_url,
urllib.urlencode({'username': user, 'password': pwd})
)
token_response = urllib2.urlopen(token_req).read()
token = json.load(StringIO(token_response))['token']
logging.info('Authentication token: ' + token)
_auth = {'Authorization': 'Token ' + token}
return token
except urllib2.HTTPError as e:
logging.error('Could not authenticate: ' + e)
raise
except Exception as e:
logging.error('Could not authenticate:' + e)
raise
def get_data_for(url):
global _auth
if _auth is None:
authenticate()
try:
req = urllib2.Request(url, None, _auth)
data = urllib2.urlopen(req).read()
except urllib2.HTTPError as e:
logging.error('Cannot get {url}. Code {code}: {reason}'.format(
url=url,
code=e.code,
reason=e.reason)
)
sys.exit(1)
except:
logging.error('Cannot get ' + url)
raise
return json.load(StringIO(data))
def post_data_for(url, key, val):
"""
Post the data dict
"""
global _auth
if _auth is None:
authenticate()
data = {"key": "%s", "val": "%s", "env": "staging"} % (key, val)
try:
req = urllib2.Request(url, None, _auth)
req.add_header('Content-Type', 'application/json')
urllib2.urlopen(req, json.dumps(data))
logging.info("Setting value {val} for {key}".format(val=val, key=key))
except urllib2.HTTPError as e:
logging.error('Cannot get {url}. Code {code}: {reason}'.format(
url=url,
code=e.code,
reason=e.reason)
)
sys.exit(1)
except:
logging.error('Cannot get ' + url)
raise
repos = ['repo1', 'repo2']
for repo in repos:
# get API data
target = get_data_for(target_url + repo)
logging.info("working on {repo} with {tag}".format(
repo=target['repo'],
tag=target['tag'])
)
# silly example for a POST
if target['tag'] == "1.0.0":
prefix = "prefix"
post_data_for(post_url, target['repo'], prefix)