-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgg-server-delete
executable file
·93 lines (71 loc) · 2.37 KB
/
gg-server-delete
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
#!/usr/bin/env python
"""
A tool to delete servers.
Examples:
./gg-server-delete -i id <-- removes server by id
./gg-server-delete -n name <-- removes server by name
./gg-server-delete -rn regex <-- removes all the servers that match
'regex' regular expression
./gg-server-delete -qrn regex <-- same as above, but without asking
for confirmation
"""
import re
import sys
import getopt
from GoGridManager import GoGridManager
from GoGridClient import GoGridException
def delete_server(manager, id, name):
try:
server = manager.delete_server(id=id, name=name)
print "%s %s" % (server.name, server.ip.ip)
except GoGridException, err:
print str(err)
sys.exit(1)
if __name__ == "__main__":
account = "default"
id = name = None
regexp_mode = False
quiet = False
try:
opts, args = getopt.getopt(sys.argv[1:], "p:i:n:rq")
except getopt.GetoptError, err:
print str(err)
sys.exit(2)
for o, a in opts:
if o == "-p":
account = a
elif o == "-i":
id = a
elif o == "-n":
name = a
elif o == '-r':
regexp_mode = True
elif o == '-q':
quiet = True
if name is None and id is None:
print "You should specifiy either id or name!"
sys.exit(1)
if regexp_mode is True and name is None:
print "You can use regex mode only with names"
sys.exit(1)
manager = GoGridManager(account=account)
if not regexp_mode:
# if we're not in regexp mode, just delete stuff
delete_server(manager, id, name)
else:
# otherwise we need to get a list of all servers
# and test our regex againt every name
regex = re.compile(name)
servers = manager.get_servers()
matches = [server.name for server in servers if regex.match(server.name)]
print "List of servers matched your expression and therefore to be removed:"
for server in matches:
print server
if quiet is False:
answer = ''
while not answer in ['y', 'n']:
answer = raw_input("Are you sure you want to remove all these servers? (y/n): ")
if 'n' == answer:
sys.exit(0)
for server in matches:
delete_server(manager, None, server)