-
Notifications
You must be signed in to change notification settings - Fork 4
/
hub_destroy.py
executable file
·84 lines (62 loc) · 1.96 KB
/
hub_destroy.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
#!/usr/bin/python3
#
# Copyright (c) 2011 Alon Swartz <[email protected]>
# Copyright (c) 2022 TUrnKey GNU/Linux <[email protected]>
#
# This file is part of HubTools.
#
# HubTools is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#
"""
Destroy a cloud server
Arguments:
instance_id Instance ID of server to destroy
Options:
--disable-unregister Keep server configuration to re-launched later via
Hub
Environment variables:
HUB_APIKEY Displayed in your Hub account's user profile
"""
import os
import sys
import getopt
from hublib import Hub
from hublib.formatter import fmt_server_header, fmt_server
from hublib.utils import fatal
def usage(e=None):
if e:
print("error: " + str(e), file=sys.stderr)
print("Syntax: %s <instance_id> [opts]" % (sys.argv[0]), file=sys.stderr)
print(__doc__, file=sys.stderr)
sys.exit(1)
def main():
try:
opts, args = getopt.gnu_getopt(sys.argv[1:], "h",
["help", "disable-unregister"])
except getopt.GetoptError as e:
usage(e)
auto_unregister = True
for opt, val in opts:
if opt in ('-h', '--help'):
usage()
if opt == "--disable-unregister":
auto_unregister = False
if len(args) != 1:
usage("incorrect number of arguments")
instance_id = args[0]
apikey = os.getenv('HUB_APIKEY', None)
if not apikey:
fatal("HUB_APIKEY not specified in environment")
hub = Hub(apikey)
try:
server = hub.servers.get(instance_id)[0]
server.destroy(auto_unregister=auto_unregister)
except hub.Error as e:
fatal(e.description)
print(fmt_server_header())
print(fmt_server(server))
if __name__ == "__main__":
main()