-
Notifications
You must be signed in to change notification settings - Fork 3
/
tlinks.py
executable file
·76 lines (67 loc) · 3.2 KB
/
tlinks.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
#!/usr/bin/env python
import argparse
from terminalcloud import terminal
def link_terminals(term, src_term, ports):
try:
ckey = terminal.get_terminal(subdomain=term)['terminal']['container_key']
links = [{"port": ports[port], "source": src_term} for port in range(len(ports))]
output = terminal.add_terminal_links(ckey, links)
return output['success']
except Exception, e:
return e
def unlink_terminals(term, src_term, ports):
try:
ckey = terminal.get_terminal(subdomain=term)['terminal']['container_key']
links = [{"port": ports[port], "source": src_term} for port in range(len(ports))]
output = terminal.remove_terminal_links(ckey,links)
return output['success']
except Exception, e:
return e
def clean_terminal_links(term):
try:
ckey = terminal.get_terminal(subdomain=term)['terminal']['container_key']
links = terminal.list_terminal_access(ckey)['links']
flinks = map(lambda x: {'port':x.split(':')[0],'source':x.split(':')[-1]},links)
terminal.remove_terminal_links(ckey,flinks)
return 'success'
except Exception, e:
print e
def show_terminal_links(term):
try:
ckey = terminal.get_terminal(subdomain=term)['terminal']['container_key']
links = terminal.list_terminal_access(ckey)['links']
flinks = map(lambda x: {'port':x.split(':')[0],'source':x.split(':')[-1]},links)
print '%s\t\t%s' % ('Source','Port')
print '---------------------'
for link in range(len(flinks)):
print '%s\t%s' % (flinks[link]['source'],flinks[link]['port'])
except Exception, e:
return e
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("action", type=str, help="link, unlink, clean, show")
parser.add_argument("terminal", type=str, help="Terminal to be modified")
parser.add_argument("-s", "--source", type=str, default=None, help="Source Terminal. (subdomain)")
parser.add_argument("-p", "--ports", type=str, default='*', help="Ports, separated by comma. By default '*' (all ports)")
parser.add_argument('-u', '--utoken', type=str, help="Your user token (see https://www.terminal.com/settings/api)")
parser.add_argument('-a', '--atoken', type=str, help="Your access token (see https://www.terminal.com/settings/api)")
parser.add_argument('-c', '--creds', type=str, default='creds.json', help="Credentials Json file. By default 'creds.json'")
args = parser.parse_args()
try:
terminal.setup_credentials(args.utoken, args.atoken, args.creds)
except Exception, e:
print "Cannot setup your credentials. Check if they're correct"
exit(e)
if args.action == 'link':
print link_terminals(args.terminal, args.source, args.ports.split(','))
else:
if args.action == 'unlink':
print unlink_terminals(args.terminal, args.source, args.ports.split(','))
else:
if args.action == 'clean':
clean_terminal_links(args.terminal)
else:
if args.action == 'show':
show_terminal_links(args.terminal)
else:
exit('Action not found. Exiting.')