-
Notifications
You must be signed in to change notification settings - Fork 1
/
env-list
executable file
·133 lines (102 loc) · 3.12 KB
/
env-list
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 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 logging
import os
import sys
try:
from urllib import request
except ImportError:
import urllib2 as request
def set_logging_level(level):
if level > 3:
level = 3
handler = request.HTTPHandler(debuglevel=level)
request.install_opener(request.build_opener(handler))
log_level = ['ERROR', 'WARNING', 'INFO', 'DEBUG'][level]
logging.basicConfig(
level=getattr(logging, log_level), format='%(levelname)-8s %(message)s',
)
def fatal(msg):
logging.error(msg)
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 env_request(app, env=None):
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)
if env:
env = '?' + '&'.join(['env=' + e for e in env])
url = '{}/apps/{}/env{}'.format(target, app, env or '')
req = Request('GET', url)
req.add_header('Authorization', 'bearer ' + token)
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
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_vars(app, env):
resp = env_request(app, env)
return json.loads(resp.read().decode('utf-8'))
def parse_args():
parser = argparse.ArgumentParser(
description="List an App's environment variables"
)
parser.add_argument(
'-a', '--app',
required=True,
help='the name of the App',
)
parser.add_argument(
'-p', '--private',
action='store_true',
help='list private variables',
)
parser.add_argument(
'-v', '--verbose',
action='count',
default=0,
help='Logging level: v=warning, vv=info, vvv=debug',
)
parser.add_argument(
'env',
nargs='*',
help='environment variables to list',
)
return parser.parse_args()
def show_var(env, private=False):
if not env:
return
for var in sorted(env, key=lambda e: e['name']):
if private or var['public']:
print('{name}={value}'.format(**var))
else:
print('{name}=*** (private variable)'.format(**var))
def main():
args = parse_args()
set_logging_level(args.verbose)
env = get_app_vars(args.app, args.env)
show_var(env, args.private)
if __name__ == '__main__':
main()