-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-drone-agent.py
59 lines (48 loc) · 1.44 KB
/
get-drone-agent.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
#!/usr/bin/env python
'''
This script is used to find the Drone agent that executed a past build/deploy.
Example:
python get-drone-agent.py https://drone.example.net/SERVICE/frontend-service/173
'''
import argparse
import yaml
import os
import requests
import json
args = ''
drone_build = ''
headers = ''
DRONE_API_URL = 'https://drone.example.net/api/repos/{}/{}/builds/{}'
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('drone_build',
help='URL of the Drone build',
type=str)
args = parser.parse_args()
return args
def get_token():
home = os.path.expanduser('~')
credentials_file = home + '/.credentials'
if os.path.isfile(credentials_file):
credentials = yaml.load(open(credentials_file))
token = credentials['drone']['token']
return token
else:
token = raw_input('Enter Drone Token: ')
return token
def create_api_url(url):
project = url.split('/')[3]
repo = url.split('/')[4]
build = url.split('/')[5]
api_url = DRONE_API_URL.format(project, repo, build)
return api_url
def main():
args = get_args()
token = get_token()
headers = {'Authorization': 'Bearer ' + token}
api_url = create_api_url(args.drone_build)
r = requests.get(api_url, headers=headers)
data = r.json()
print('Build agent: ' + data['procs'][0]['machine'])
if __name__ == '__main__':
main()