Skip to content
Browse files

added custom formatting support to hub-list-* commands

Example usage:

    hub-list-servers "instanceid=%instanceid status=%status ipaddress=%ipaddress"
  • Loading branch information...
1 parent 37bacbc commit 4948a28bc5e94b21d75e7ad68c94042b6865a39e @alonswartz alonswartz committed
Showing with 131 additions and 19 deletions.
  1. +3 −0 cmd_launch.py
  2. +32 −6 cmd_list_appliances.py
  3. +39 −7 cmd_list_backups.py
  4. +37 −6 cmd_list_servers.py
  5. +2 −0 cmd_terminate.py
  6. +18 −0 hub/formatter.py
View
3 cmd_launch.py
@@ -13,11 +13,13 @@
Launch a new cloud server
Arguments:
+
appliance Appliance name to launch (e.g. core)
OR
backup_id Restore backup to a new cloud server (e.g. 2)
Options:
+
--region Region for instance launch (default: us-east-1)
--type Instance size (default: m1.small)
--root-pass Root password to set (default: random)
@@ -29,6 +31,7 @@
--app-domain Domain for application
Environment variables:
+
HUB_APIKEY Displayed in your Hub account's user profile
"""
import re
View
38 cmd_list_appliances.py
@@ -12,15 +12,29 @@
"""
List appliances
+By default uses a built-in format, unless a user-specified format is specified.
+Format variables:
+
+ %name Appliance code name
+ %version Appliance version code
+ %description Appliance descriptive label
+ %preseeds Appliance supported/required preseeding arguments
+
+Examples:
+
+ hub-list-appliances
+ hub-list-appliances "name=%name preseeds=%preseeds"
+
Environment variables:
- HUB_APIKEY Displayed in your Hub account's user profile
+
+ HUB_APIKEY Displayed in your Hub account's user profile
"""
import os
import sys
import getopt
from hub import Hub
-from hub.formatter import fmt_appliance_header, fmt_appliance
+from hub.formatter import Formatter, fmt_appliance_header, fmt_appliance
def fatal(e):
print >> sys.stderr, "error: " + str(e)
@@ -50,14 +64,26 @@ def main():
if not apikey:
fatal("HUB_APIKEY not specified in environment")
- hub = Hub(apikey)
+ if args:
+ if len(args) != 1:
+ usage("incorrect number of arguments")
+ format = args[0]
+ else:
+ format = None
+
+ hub = Hub(apikey)
appliances = hub.appliances.get()
appliances = sorted(appliances, key=lambda appliance: appliance.name)
- print fmt_appliance_header()
- for appliance in appliances:
- print fmt_appliance(appliance)
+ if format:
+ format = Formatter(format)
+ for appliance in appliances:
+ print format(appliance)
+ else:
+ print fmt_appliance_header()
+ for appliance in appliances:
+ print fmt_appliance(appliance)
if __name__ == "__main__":
main()
View
46 cmd_list_backups.py
@@ -12,15 +12,34 @@
"""
List backup records
+By default uses a built-in format, unless a user-specified format is specified.
+Format variables:
+
+ %backup_id Backup ID
+ %label Descriptive label
+ %turnkey_version Appliance version code
+ %server_id Associated server id
+ %created Date the backup record was created
+ %updated Date the backup record was last updated
+ %size Aggregate size of backup, in bytes
+ %address Backup target address
+ %skpp Secret Key Passphrase Protection (bool)
+
+Examples:
+
+ hub-list-backups
+ hub-list-backups "backup_id=%backup_id label=%label size=%{size}bytes"
+
Environment variables:
- HUB_APIKEY Displayed in your Hub account's user profile
+
+ HUB_APIKEY Displayed in your Hub account's user profile
"""
import os
import sys
import getopt
from hub import Hub
-from hub.formatter import fmt_backup_header, fmt_backup
+from hub.formatter import Formatter, fmt_backup_header, fmt_backup
def fatal(e):
print >> sys.stderr, "error: " + str(e)
@@ -30,7 +49,7 @@ def usage(e=None):
if e:
print >> sys.stderr, "error: " + str(e)
- print >> sys.stderr, "Syntax: %s" % (sys.argv[0])
+ print >> sys.stderr, "Syntax: %s [ <format> ]" % (sys.argv[0])
print >> sys.stderr, __doc__
sys.exit(1)
@@ -45,17 +64,30 @@ def main():
if opt in ('-h', '--help'):
usage()
+ 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)
-
backups = hub.backups.get()
+
if backups:
- print fmt_backup_header()
- for backup in backups:
- print fmt_backup(backup)
+ if format:
+ format = Formatter(format)
+ for backup in backups:
+ print format(backup)
+ else:
+ print fmt_backup_header()
+ for backup in backups:
+ print fmt_backup(backup)
if __name__ == "__main__":
main()
View
43 cmd_list_servers.py
@@ -13,17 +13,36 @@
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
+ %type Instance size
+ %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
+
+ HUB_APIKEY Displayed in your Hub account's user profile
"""
import os
import sys
import getopt
from hub import Hub
-from hub.formatter import fmt_server_header, fmt_server
+from hub.formatter import Formatter, fmt_server_header, fmt_server
def fatal(e):
print >> sys.stderr, "error: " + str(e)
@@ -51,18 +70,30 @@ def main():
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:
- print fmt_server_header()
servers = sorted(servers, key=lambda server: server.status)
- for server in servers:
- print fmt_server(server)
+ 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()
View
2 cmd_terminate.py
@@ -13,9 +13,11 @@
Terminate a cloud server
Arguments:
+
instance_id Instance ID of server to terminate
Environment variables:
+
HUB_APIKEY Displayed in your Hub account's user profile
"""
import os
View
18 hub/formatter.py
@@ -8,6 +8,24 @@
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#
+import string
+
+# custom formatting
+class Formatter:
+ def __init__(self, format):
+ tpl = format.replace('$', '$$')
+ tpl = tpl.replace('\\n', '\n')
+ tpl = tpl.replace('\\t', '\t')
+ tpl = tpl.replace('%%', '__PERCENT__')
+ tpl = tpl.replace('%', '$')
+ tpl = tpl.replace('__PERCENT__', '%')
+
+ self.tpl = string.Template(tpl)
+
+ def __call__(self, obj):
+ return self.tpl.substitute(**obj)
+
+# helper formatting
def _fmt_bool(bool):
return "Yes" if bool else "No"

0 comments on commit 4948a28

Please sign in to comment.
Something went wrong with that request. Please try again.