-
Notifications
You must be signed in to change notification settings - Fork 4
/
hub_list_servers.py
executable file
·102 lines (80 loc) · 2.62 KB
/
hub_list_servers.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
#!/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.
#
"""
List servers
Options:
-r --refresh Force refresh of Hubs Amazon EC2 cache
By default uses a built-in format, unless a user-specified format is specified.
Format variables:
%instanceid Instance ID
%size Instance size
%type Instance type
%region Instance region
%label Descriptive label
%name Appliance code name
%ipaddress Associated IP address
%status Amazon EC2 reported status
%boot_status Hub status (sec-updates, tklbam-restore, etc.)
Examples:
hub-list-servers
hub-list-servers "instanceid=%instanceid status=%status
ipaddress=%ipaddress"
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 Formatter, 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" % (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:], "hr", ["help", "refresh"])
except getopt.GetoptError as e:
usage(e)
refresh = False
for opt, val in opts:
if opt in ('-h', '--help'):
usage()
if opt in ('-r', '--refresh'):
refresh = True
if args:
if len(args) != 1:
usage("incorrect number of arguments")
format = args[0]
else:
format = None
apikey = os.getenv('HUB_APIKEY', None)
if not apikey:
fatal("HUB_APIKEY not specified in environment")
hub = Hub(apikey)
servers = hub.servers.get(refresh_cache=refresh)
if servers:
servers = sorted(servers, key=lambda server: server.status)
if format:
format = Formatter(format)
for server in servers:
print(format(server))
else:
print(fmt_server_header())
for server in servers:
print(fmt_server(server))
if __name__ == "__main__":
main()