-
Notifications
You must be signed in to change notification settings - Fork 3
/
terminal.py
339 lines (284 loc) · 10.6 KB
/
terminal.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/env python
import json
import urllib
import urllib2
# Authentication
def setup_credentials(utoken, atoken, credsfile):
if utoken is None and atoken is None:
try:
creds = json.load(open(credsfile, 'r'))
utoken = creds['user_token']
atoken = creds['access_token']
except:
print "Can't open credentials file. \n ", \
"You must provide a user token and a access token at least one time, or a valid credentials file"
exit(127)
elif (utoken is not None and atoken is None) or (utoken is None and atoken is not None):
print "--utoken AND --atoken parameters must be passed together"
exit(1)
else:
with open(credsfile, 'w') as cfile:
json.dump({'user_token': utoken, 'access_token': atoken}, cfile)
global user_token
global access_token
user_token, access_token = str(utoken), str(atoken)
return user_token, access_token
# Manage Request
def make_request(call, params=None, url=None, headers=None, raw=False):
try:
if url is None:
url = 'https://api.terminal.com/v0.2/%s' % call
if headers is None:
headers={'user-token': user_token,'access-token':access_token, 'Content-Type':'application/json'}
if params is None:
data = json.dumps({})
else:
parsed_params={}
for key in params.keys():
if params[key] is not None:
parsed_params.update({key:params[key]})
if raw:
data = urllib.urlencode(parsed_params)
headers.pop('Content-Type')
else:
data = json.dumps(parsed_params)
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
results = json.loads(response.read())
results.update({u'success':True})
map(str,results)
return results
except urllib2.HTTPError as e:
return json.loads(e.read())
# Browse Snapshots and Users
def get_snapshot(snapshot_id):
call = 'get_snapshot'
params = {"snapshot_id":snapshot_id}
response = make_request(call, params)
return response
def get_profile(username):
call = 'get_profile'
params = {"username":username}
response = make_request(call, params)
return response
def list_public_snapshots(username, tag=None, featured=None, title=None, page=0, perPage=1000 ,sortby='popularity'):
call = 'list_public_snapshots'
params = {'username':username, 'tag':tag, 'featured':featured, 'title':title,\
'page':page, 'perPage':perPage, 'sortby':sortby}
response = make_request(call, params)
return response
def count_public_snapshots(username=None, tag=None, featured=None, title=None):
call = 'count_public_snapshots'
params = {'username':username, 'tag':tag, 'featured':featured, 'title':title}
response = make_request(call, params)
return response
# Create and Manage Terminals
def list_terminals():
call = 'list_terminals'
params = None
response = make_request(call, params)
return response
def get_terminal(container_key=None, subdomain=None):
if (container_key is None and subdomain is None):
return {'error':'container_key OR subdomain must be passed'}
call = 'get_terminal'
params = {'container_key':container_key, 'subdomain':subdomain}
response = make_request(call, params)
return response
def start_snapshot(snapshot_id, instance_type=None, temporary=None, name=None, autopause=None, startup_script=None):
call = 'start_snapshot'
params = {'snapshot_id': snapshot_id, 'instance_type': instance_type, 'temporary': temporary, 'name': name,
'autopause': autopause, 'startup_script': startup_script}
response = make_request(call, params)
return response
def delete_terminal(container_key):
call = 'delete_terminal'
params = {'container_key':container_key}
response = make_request(call, params)
return response
def restart_terminal(container_key):
call = 'restart_terminal'
params = {'container_key':container_key}
response = make_request(call, params)
return response
def pause_terminal(container_key):
call = 'pause_terminal'
params = {'container_key':container_key}
response = make_request(call, params)
return response
def resume_terminal(container_key):
call = 'resume_terminal'
params = {'container_key':container_key}
response = make_request(call, params)
return response
def edit_terminal(container_key, instance_type=None, diskspace=None, name=None, custom_data=None):
call = 'edit_terminal'
params = {'container_key':container_key, 'instance_type':instance_type, 'diskspace':diskspace, \
'name':name, 'custom_data':custom_data}
response = make_request(call, params)
return response
# Create and Manage Snapshots
def list_snapshots(tag=None, featured=None, title=None, page=0, perPage=1000 ,sortby='popularity'):
call = 'list_snapshots'
params = {'tag':tag, 'featured':featured, 'title':title,'page':page, 'perPage':perPage, 'sortby':sortby}
response = make_request(call, params)
return response
def count_snapshots(tag=None, featured=None, title=None):
call = 'count_snapshots'
params = {'tag':tag, 'featured':featured, 'title':title}
response = make_request(call, params)
return response
def delete_snapshot(snapshot_id):
call = 'delete_snapshot'
params = {'snapshot_id':snapshot_id}
response = make_request(call, params)
return response
def edit_snapshot(snapshot_id, body=None, title=None, readme=None, tags=None):
call = 'edit_snapshot'
params = {'snapshot_id':snapshot_id, 'body':body, 'title':title, 'readme':readme, 'tags':tags}
response = make_request(call, params)
return response
def snapshot_terminal(container_key, body=None, title=None, readme=None, tags=None, public=None):
call = 'snapshot_terminal'
params = {'container_key':container_key, 'body':body, 'title':title, 'readme':readme, \
'tags':tags, 'public':public}
response = make_request(call, params)
return response
# Manage Terminal Access
def add_terminal_links(container_key, links):
call = 'add_terminal_links'
params= {'container_key':container_key, 'links':links}
response = make_request(call, params)
return response
def remove_terminal_links(container_key, links):
call = 'remove_terminal_links'
params= {'container_key':container_key, 'links':links}
response = make_request(call, params)
return response
def list_terminal_access(container_key):
call = 'list_terminal_access'
params = {'container_key':container_key}
response = make_request(call, params)
return response
def edit_terminal_access(container_key, is_public_list, access_rules):
call = 'edit_terminal_access'
params = {'container_key':container_key, 'is_public_list':is_public_list, 'access_rules':access_rules}
response = make_request(call, params)
return response
# Manage Terminal DNS & Domains
def get_cname_records():
call = 'get_cname_records'
params = {}
response = make_request(call, params)
return response
def add_domain_to_pool(domain):
call = 'add_domain_to_pool'
params = {'domain':domain}
response = make_request(call, params)
return response
def remove_domain_from_pool(domain):
call = 'remove_domain_from_pool'
params = {'domain':domain}
response = make_request(call, params)
return response
def add_cname_record(domain, subdomain, port):
call = add_cname_record
params = {'domain':domain, 'subdomain':subdomain, 'port':port}
response = make_request(call, params)
return response
def remove_cname_record(domain):
call = 'remove_cname_record'
params = {'domain':domain}
response = make_request(call, params)
return response
# Manage Terminal Idle Settings
def set_terminal_idle_settings(container_key, triggers=None, action=None):
call = 'set_terminal_idle_settings'
params = {'container_key':container_key}
response = make_request(call, params)
return response
def get_terminal_idle_setting(container_key):
call = 'get_terminal_idle_setting'
params = {'container_key':container_key}
response = make_request(call, params)
return response
# Manage Usage and Credits
def instance_types():
call = 'instance_types'
params = None
response = make_request(call, params)
return response
def instance_price(instance_type):
call = 'instance_price'
params = {'instance_type':instance_type}
response = make_request(call, params)
return response
def balance():
call = 'balance'
params = None
response = make_request(call, params)
return response
def balance_added():
call = 'balance_added'
params = None
response = make_request(call, params)
return response
def gift(email, cents):
call = 'gift'
params = {'email':email, 'cents':cents}
response = make_request(call, params)
return response
def burn_history():
call = 'burn_history'
params = None
response = make_request(call, params)
return response
def terminal_usage_history():
call = 'terminal_usage_history'
params = None
response = make_request(call, params)
return response
def burn_state():
call = 'burn_state'
params = None
response = make_request(call, params)
return response
def burn_estimates():
call = 'burn_estimates'
params = None
response = make_request(call, params)
return response
# Manage SSH Public Keys
def add_authorized_key_to_terminal(container_key, publicKey):
call = 'add_authorized_key_to_terminal'
params = {'container_key':container_key, 'publicKey':publicKey}
response = make_request(call, params)
return response
def add_authorized_key_to_ssh_proxy(name, publicKey):
call = 'add_authorized_key_to_ssh_proxy'
params = {'name':name, 'publicKey':publicKey}
try:
response = make_request(call, params)
except Exception, e:
return {'status':e}
return response
def del_authorized_key_from_ssh_proxy(name, fingerprint):
call = 'del_authorized_key_from_ssh_proxy'
params = {'name':name, 'fingerprint':fingerprint}
response = make_request(call, params)
return response
def get_authorized_keys_from_ssh_proxy():
call = 'get_authorized_keys_from_ssh_proxy'
params = None
response = make_request(call, params)
return response
# Other
def request_progress(request_id):
call = 'request_progress'
params = {'request_id':request_id}
response = make_request(call, params)
return response
def who_am_i():
call = 'who_am_i'
response = make_request(call)
return response