-
Notifications
You must be signed in to change notification settings - Fork 1
/
app-address
executable file
·112 lines (84 loc) · 2.44 KB
/
app-address
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
110
111
112
#!/usr/bin/env python
# Copyright 2016 Pablo Santiago Blum de Aguiar <[email protected]>. All rights
# reserved. Use of this source code is governed by the BSD 2-Clause License that
# can be found in the LICENSE file.
import argparse
import json
import os
import sys
try:
from urllib import request
except ImportError:
import urllib2 as request
def fatal(msg):
sys.stderr.write(msg + '\n')
sys.exit(5)
def get_env(name):
env = os.environ.get(name)
if not env:
fatal('ERROR: missing {}'.format(name))
return env
class Request(request.Request):
def __init__(self, method, *args, **kwargs):
self._method = method
request.Request.__init__(self, *args, **kwargs)
def get_method(self):
return self._method
def app_request(app):
target = get_env('TSURU_TARGET').rstrip('/')
token = get_env('TSURU_TOKEN')
if not target.startswith('http://') and not target.startswith('https://'):
target = 'http://{}'.format(target)
url = '{}/apps/{}'.format(target, app)
req = Request('GET', url)
req.add_header('Authorization', 'bearer ' + token)
headers = {
'Content-Type': 'application/json',
'Accept': 'text/plain'
}
if headers:
for header, value in headers.items():
req.add_header(header, value)
try:
return request.urlopen(req, timeout=30)
except Exception as e:
fatal('{}'.format(e))
def get_app(app):
resp = app_request(app)
return json.loads(resp.read().decode('utf-8'))
def parse_args():
parser = argparse.ArgumentParser(description="Show an App's address(es)")
parser.add_argument(
'-a', '--app',
required=True,
help='The name of the App',
)
parser.add_argument(
'-o', '--one',
action='store_true',
help='Show only one URL',
)
parser.add_argument(
'-i', '--ip',
action='store_true',
help='Show the "IP" URL',
)
return parser.parse_args()
def show_address(address, one):
if type(address) is not list:
address = [address]
if one:
print(address[-1])
else:
print('\n'.join(address))
def main():
args = parse_args()
app = get_app(args.app)
address = app.get('cname', [])
if not address or args.ip:
address = app.get('ip')
else:
address.insert(0, app.get('ip'))
show_address(address, args.one)
if __name__ == '__main__':
main()