Skip to content

Instantly share code, notes, and snippets.

@tobert
Created February 7, 2011 23:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobert/815553 to your computer and use it in GitHub Desktop.
Save tobert/815553 to your computer and use it in GitHub Desktop.
cfmteset CLI module first pass
import os
import sys
import optparse
import cfmtest.build
#import cfmtest.test
#import cfmtest.destroy
CFMTEST_COMMANDS = [ "build" ] #, "test", "destroy" ]
class CLI:
"""
Implement CLI parsing for cfmtest.
"""
def __init__(self):
self.parser = optparse.OptionParser(
description = "a configuration management LXC test harness"
)
def get_command(self, args):
"""
Get the subcommand before parsing all options.
"""
if len(args) < 2:
print >> sys.stderr, "At least one argument is required - (build|test|destroy)."
sys.exit(1)
elif args[1] in CFMTEST_COMMANDS:
return args.pop(1)
print >> sys.stderr, "Invalid command, '%s'. Use one of (build|test|destroy)." % args[1]
sys.exit(1)
def check(self):
"""
Make sure privileges & environment are sufficient to run.
"""
if os.geteuid() != 0:
print >> sys.stderr, "These tools must run as root for now."
sys.exit(1)
def run(self, args):
"""
Process the command line.
"""
# pull the command from the arguments before calling into optparse
cmd = self.get_command(args)
# optparse
self.parser.add_option("--build-id", dest="build_id",
help="CI Tool build ID, e.g. Hudson/Jenkin's BUILD_ID environment variable")
self.parser.add_option("--root-subvol", dest="root_subvol",
help="a BTRFS subvolume containing an OS filesystem root to use for testing")
if cmd is "build":
self.parser.add_option("--workspace", dest="workspace",
help="Path to a checked out copy of the CFM policy.")
opts, leftover = self.parser.parse_args(args)
options = vars(opts)
# for some weird reason, optparse doesn't have require switch
if options["build_id"] is None:
self.parser.error("--build-id is a required option")
if options["root_subvol"] is None:
self.parser.error("--root-subvol is a required option")
# each command has its own class and they're already loaded, so
# instantiate the object and call its run()
#
# convert the command string to a module object
cmd_module = sys.modules["cfmtest.%s" % cmd]
# now get the object for the class
cmd_class = getattr(cmd_module, cmd.capitalize())
# instantiate an object using CLI args
cmd_object = cmd_class(**options)
# call the run method
return cmd_object.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment