mirror of
https://github.com/Adam-Ant/drone-trigger
synced 2024-12-20 07:24:34 +00:00
Improve error handling
This commit is contained in:
parent
d4be0bf82e
commit
3ca4d2c950
69
main.py
69
main.py
@ -8,7 +8,7 @@ from configobj import ConfigObj
|
|||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from json import loads as jload
|
import json
|
||||||
|
|
||||||
defaultconfig = '''[Connection]
|
defaultconfig = '''[Connection]
|
||||||
# Specify the URL to the drone server, including port and protocol
|
# Specify the URL to the drone server, including port and protocol
|
||||||
@ -44,11 +44,11 @@ auth_key = eyJEXAMPLE.AUTH.KEY
|
|||||||
|
|
||||||
def runbuild(repo: str, branch: str):
|
def runbuild(repo: str, branch: str):
|
||||||
url = drone_host + '/api/repos/' + repo
|
url = drone_host + '/api/repos/' + repo
|
||||||
latest = jload(requests.get(url + '/builds/latest', headers={'Authorization': drone_auth_key}).text)
|
latest = json.loads(requests.get(url + '/builds/latest', headers={'Authorization': drone_auth_key}).text)
|
||||||
build_num = False
|
build_num = False
|
||||||
if (latest['branch'] != branch):
|
if (latest['branch'] != branch):
|
||||||
while not build_num:
|
while not build_num:
|
||||||
latest = jload(requests.get(url + '/builds/' + str(latest['number'] - 1), headers={'Authorization': drone_auth_key}).text)
|
latest = json.loads(requests.get(url + '/builds/' + str(latest['number'] - 1), headers={'Authorization': drone_auth_key}).text)
|
||||||
if (latest['branch'] == branch):
|
if (latest['branch'] == branch):
|
||||||
build_num = str(latest['number'])
|
build_num = str(latest['number'])
|
||||||
else:
|
else:
|
||||||
@ -59,17 +59,31 @@ def runbuild(repo: str, branch: str):
|
|||||||
return (requests.post(buildurl, headers={'Authorization': drone_auth_key}))
|
return (requests.post(buildurl, headers={'Authorization': drone_auth_key}))
|
||||||
|
|
||||||
|
|
||||||
def jsonVal(jraw, struct):
|
def jsonVal(url, struct):
|
||||||
|
try:
|
||||||
|
r = requests.get(url)
|
||||||
|
r.raise_for_status()
|
||||||
|
jsondata = r.text
|
||||||
|
except requests.HTTPError as e:
|
||||||
|
print('[' + time.strftime("%d/%m/%Y %H:%M:%S") + '] ERROR: Got Response code ' + str(e.response.status_code) + ' for URL ' + str(url))
|
||||||
|
raise e
|
||||||
|
|
||||||
|
try:
|
||||||
|
dataDict = json.loads(jsondata)
|
||||||
|
except json.decoder.JSONDecodeError as e:
|
||||||
|
# Probably Not Valid JSON?
|
||||||
|
print('[' + time.strftime("%d/%m/%Y %H:%M:%S") + '] ERROR: Could not decode JSON: ')
|
||||||
|
print(jsondata)
|
||||||
|
raise e
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
dataDict = jload(jraw)
|
|
||||||
for i in struct.split('.'):
|
for i in struct.split('.'):
|
||||||
dataDict = dataDict[i]
|
dataDict = dataDict[i]
|
||||||
return dataDict
|
return dataDict
|
||||||
except KeyError:
|
except KeyError:
|
||||||
print('Error: Invalid structure: ' + struct)
|
print('Error: Invalid structure: ' + struct)
|
||||||
print(dataDict)
|
print(dataDict)
|
||||||
exit(1)
|
|
||||||
except:
|
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
||||||
@ -117,13 +131,12 @@ if __name__ == '__main__':
|
|||||||
if not (config[service].get('url', False)) or not (config[service].get('structure', False)) or not(config[service].get('drone_repo'), False):
|
if not (config[service].get('url', False)) or not (config[service].get('structure', False)) or not(config[service].get('drone_repo'), False):
|
||||||
print('Error: Missing required value in status block ' + service)
|
print('Error: Missing required value in status block ' + service)
|
||||||
exit(78)
|
exit(78)
|
||||||
try:
|
|
||||||
r = requests.get(config[service].get('url'))
|
|
||||||
r.raise_for_status()
|
|
||||||
curr_value = jsonVal(r.text, config[service].get('structure'))
|
|
||||||
except:
|
|
||||||
raise
|
|
||||||
if not (config[service].get('current_value', False)):
|
if not (config[service].get('current_value', False)):
|
||||||
|
try:
|
||||||
|
curr_value = jsonVal(config[service].get('url'), config[service].get('structure'))
|
||||||
|
except:
|
||||||
|
exit(1)
|
||||||
print('Writing Initial value for ' + service + ': ' + curr_value)
|
print('Writing Initial value for ' + service + ': ' + curr_value)
|
||||||
config[service]['current_value'] = curr_value
|
config[service]['current_value'] = curr_value
|
||||||
config.write()
|
config.write()
|
||||||
@ -132,22 +145,18 @@ if __name__ == '__main__':
|
|||||||
for service in config:
|
for service in config:
|
||||||
if service == 'Connection':
|
if service == 'Connection':
|
||||||
continue
|
continue
|
||||||
try:
|
new_value = jsonVal(config[service].get('url'), config[service].get('structure'))
|
||||||
r = requests.get(config[service].get('url'))
|
|
||||||
r.raise_for_status()
|
if (new_value != config[service]['current_value']):
|
||||||
new_value = jsonVal(r.text, config[service].get('structure'))
|
print('[' + time.strftime("%d/%m/%Y %H:%M:%S") + '] Got new build - ' + new_value)
|
||||||
if (new_value != config[service]['current_value']):
|
if not (config[service].get('branch', False)):
|
||||||
print(time.strftime("%d/%m/%Y %H:%M:%S") + ': Got new build - ' + new_value)
|
branch = 'master'
|
||||||
if not (config[service].get('branch', False)):
|
else:
|
||||||
branch = 'master'
|
branch = config[service]['branch']
|
||||||
else:
|
if (runbuild(config[service].get('drone_repo'), branch)):
|
||||||
branch = config[service]['branch']
|
config[service]['current_value'] = new_value
|
||||||
if (runbuild(config[service].get('drone_repo'), branch)):
|
with open(filepath + '/dronetrigger.cfg', 'w') as configfile:
|
||||||
config[service]['current_value'] = new_value
|
config.write()
|
||||||
with open(filepath + '/dronetrigger.cfg', 'w') as configfile:
|
print("Successfully build new version for " + service)
|
||||||
config.write()
|
|
||||||
print("Successfully build new version for " + service)
|
|
||||||
|
|
||||||
except:
|
|
||||||
raise
|
|
||||||
time.sleep(sleep_time)
|
time.sleep(sleep_time)
|
||||||
|
Loading…
Reference in New Issue
Block a user