def validate_template(template_name): template_file = open("templates/%s.template" % template_name, "r") cf_conn = AWS.get_cf_connection() print "Attempting to validate %s template" % template_name # use a try catch because boto throws an exception. # it's pretty bad on their part but whatever. try: cf_conn.validate_template(template_body=template_file.read()) print "AWS claims no problems!" except boto.exception.BotoServerError, e: # and the exception contains the error message! print e
def main(): # arg parse parser = argparse.ArgumentParser(description="Command Line Tools for HCS Cloud Machines.") group = parser.add_mutually_exclusive_group() parser.add_argument( "--list_resources", "-l", help="list AWS resources. supports 'stacks','servers','dns','rds'", default=None ) # use a group because these are exclusive flags group.add_argument("--create", "-c", help="create a new HCS server from template", default=None) group.add_argument( "--purge", "-p", help="purges all cloudformation stacks that were rolled back", action="store_true" ) group.add_argument("--destroy", "-d", help="destroys specified stack", default=None) # this will cause boto to create a log file in your working directory parser.add_argument("--verbose", help="turn on boto logger for debugging", action="store_true") parser.add_argument("--level", help="verbosity level if verbose mode enabled", type=int, default=10) # option to validate templates parser.add_argument( "--validate", "-v", help="validate an AWS template. must be located in the templates folder", default=None ) # option to reboot things. parser.add_argument("--reboot", "-r", help="reboot a service instance. supports 'rds'", default=None) args = parser.parse_args() if args.verbose: boto.set_file_logger("hcs-cloudrunner", filepath="./boto.log", level=args.level) if args.create is not None: print "Creating a %s server" % args.create create_stack(args.create) elif args.list_resources is not None: if args.list_resources.lower() == "stacks": get_active_stacks(AWS.get_cf_connection()) elif args.list_resources.lower() == "servers": print_servers() elif args.list_resources.lower() == "dns": print_dns() elif args.list_resources.lower() == "rds": RDS.view_databases() else: print "Resource type not supported!" elif args.purge: cf_conn = AWS.get_cf_connection() purge_stacks(cf_conn) elif args.destroy is not None: cf_conn = AWS.get_cf_connection() print "WARNING!" print "THIS WILL PERMANENTLY DESTROY RESOURCES!" print "I'm going to confirm your action. Use ctrl+c to quit!" # generate a random 5 char sequence pw = "".join(random.choice(string.ascii_uppercase + string.digits) for a in range(5)) print "Are you sure you want to destroy the stack: [ " + args.destroy + " ] ?" match = raw_input("Type in the passkey to confirm: " + pw + " ") if match != pw: print "Stopped deletion!" return finalcountdown = raw_input("Are you REALLY sure? There's no going back! [ Type YES ] ") if finalcountdown.upper() == "YES": destroy_stack(cf_conn, args.destroy) else: print "Stopped deletion!" elif args.validate is not None: validate_template(args.validate) elif args.reboot is not None: if args.reboot.lower() == "rds": RDS.restart_database() else: parser.print_help()