+ required_options=['verbose', 'debug', 'registry', 'sm', 'auth', 'user']
+
+ @staticmethod
+ def default_sfi_dir ():
+ if os.path.isfile("./sfi_config"):
+ return os.getcwd()
+ else:
+ return os.path.expanduser("~/.sfi/")
+
+ # dummy to meet Sfi's expectations for its 'options' field
+ # i.e. s/t we can do setattr on
+ class DummyOptions:
+ pass
+
+ def __init__ (self,options=None):
+ if options is None: options=Sfi.DummyOptions()
+ for opt in Sfi.required_options:
+ if not hasattr(options,opt): setattr(options,opt,None)
+ if not hasattr(options,'sfi_dir'): options.sfi_dir=Sfi.default_sfi_dir()
+ self.options = options
+ self.user = None
+ self.authority = None
+ self.logger = sfi_logger
+ self.logger.enable_console()
+ self.available_names = [ tuple[0] for tuple in Sfi.available ]
+ self.available_dict = dict (Sfi.available)
+
+ # tuples command-name expected-args in the order in which they should appear in the help
+ available = [
+ ("version", ""),
+ ("list", "authority"),
+ ("show", "name"),
+ ("add", "record"),
+ ("update", "record"),
+ ("remove", "name"),
+ ("slices", ""),
+ ("resources", "[slice_hrn]"),
+ ("create", "slice_hrn rspec"),
+ ("delete", "slice_hrn"),
+ ("status", "slice_hrn"),
+ ("start", "slice_hrn"),
+ ("stop", "slice_hrn"),
+ ("reset", "slice_hrn"),
+ ("renew", "slice_hrn time"),
+ ("shutdown", "slice_hrn"),
+ ("get_ticket", "slice_hrn rspec"),
+ ("redeem_ticket", "ticket"),
+ ("delegate", "name"),
+ ("create_gid", "[name]"),
+ ("get_trusted_certs", "cred"),
+ ]
+
+ def print_command_help (self, options):
+ verbose=getattr(options,'verbose')
+ format3="%18s %-15s %s"
+ line=80*'-'
+ if not verbose:
+ print format3%("command","cmd_args","description")
+ print line
+ else:
+ print line
+ self.create_parser().print_help()
+ for command in self.available_names:
+ args=self.available_dict[command]
+ method=getattr(self,command,None)
+ doc=""
+ if method: doc=getattr(method,'__doc__',"")
+ if not doc: doc="*** no doc found ***"
+ doc=doc.strip(" \t\n")
+ doc=doc.replace("\n","\n"+35*' ')
+ if verbose:
+ print line
+ print format3%(command,args,doc)
+ if verbose:
+ self.create_command_parser(command).print_help()
+
+ def create_command_parser(self, command):
+ if command not in self.available_dict:
+ msg="Invalid command\n"
+ msg+="Commands: "
+ msg += ','.join(self.available_names)
+ self.logger.critical(msg)
+ sys.exit(2)
+
+ parser = OptionParser(usage="sfi [sfi_options] %s [cmd_options] %s" \
+ % (command, self.available_dict[command]))
+
+ # user specifies remote aggregate/sm/component
+ if command in ("resources", "slices", "create", "delete", "start", "stop",
+ "restart", "shutdown", "get_ticket", "renew", "status"):
+ parser.add_option("-c", "--component", dest="component", default=None,
+ help="component hrn")
+ parser.add_option("-d", "--delegate", dest="delegate", default=None,
+ action="store_true",
+ help="Include a credential delegated to the user's root"+\
+ "authority in set of credentials for this call")
+
+ # registy filter option
+ if command in ("list", "show", "remove"):
+ parser.add_option("-t", "--type", dest="type", type="choice",
+ help="type filter ([all]|user|slice|authority|node|aggregate)",
+ choices=("all", "user", "slice", "authority", "node", "aggregate"),
+ default="all")
+ # display formats
+ if command in ("resources"):
+ parser.add_option("-r", "--rspec-version", dest="rspec_version", default="SFA 1",
+ help="schema type and version of resulting RSpec")
+ parser.add_option("-f", "--format", dest="format", type="choice",
+ help="display format ([xml]|dns|ip)", default="xml",
+ choices=("xml", "dns", "ip"))
+ #panos: a new option to define the type of information about resources a user is interested in
+ parser.add_option("-i", "--info", dest="info",
+ help="optional component information", default=None)
+
+
+ # 'create' does return the new rspec, makes sense to save that too
+ if command in ("resources", "show", "list", "create_gid", 'create'):
+ parser.add_option("-o", "--output", dest="file",
+ help="output XML to file", metavar="FILE", default=None)
+
+ if command in ("show", "list"):
+ parser.add_option("-f", "--format", dest="format", type="choice",
+ help="display format ([text]|xml)", default="text",
+ choices=("text", "xml"))
+
+ parser.add_option("-F", "--fileformat", dest="fileformat", type="choice",
+ help="output file format ([xml]|xmllist|hrnlist)", default="xml",
+ choices=("xml", "xmllist", "hrnlist"))
+
+ if command in ("status", "version"):
+ parser.add_option("-o", "--output", dest="file",
+ help="output dictionary to file", metavar="FILE", default=None)
+ parser.add_option("-F", "--fileformat", dest="fileformat", type="choice",
+ help="output file format ([text]|pickled)", default="text",
+ choices=("text","pickled"))
+
+ if command in ("delegate"):
+ parser.add_option("-u", "--user",
+ action="store_true", dest="delegate_user", default=False,
+ help="delegate user credential")
+ parser.add_option("-s", "--slice", dest="delegate_slice",
+ help="delegate slice credential", metavar="HRN", default=None)
+
+ if command in ("version"):
+ parser.add_option("-R","--registry-version",
+ action="store_true", dest="version_registry", default=False,
+ help="probe registry version instead of sliceapi")
+ parser.add_option("-l","--local",
+ action="store_true", dest="version_local", default=False,
+ help="display version of the local client")
+
+ return parser
+
+
+ def create_parser(self):
+
+ # Generate command line parser
+ parser = OptionParser(usage="sfi [sfi_options] command [cmd_options] [cmd_args]",
+ description="Commands: %s"%(" ".join(self.available_names)))
+ parser.add_option("-r", "--registry", dest="registry",
+ help="root registry", metavar="URL", default=None)
+ parser.add_option("-s", "--sliceapi", dest="sm", default=None, metavar="URL",
+ help="slice API - in general a SM URL, but can be used to talk to an aggregate")
+ parser.add_option("-d", "--dir", dest="sfi_dir",
+ help="config & working directory - default is %default",
+ metavar="PATH", default=Sfi.default_sfi_dir())
+ parser.add_option("-u", "--user", dest="user",
+ help="user name", metavar="HRN", default=None)
+ parser.add_option("-a", "--auth", dest="auth",
+ help="authority name", metavar="HRN", default=None)
+ parser.add_option("-v", "--verbose", action="count", dest="verbose", default=0,
+ help="verbose mode - cumulative")
+ parser.add_option("-D", "--debug",
+ action="store_true", dest="debug", default=False,
+ help="Debug (xml-rpc) protocol messages")
+ # would it make sense to use ~/.ssh/id_rsa as a default here ?
+ parser.add_option("-k", "--private-key",
+ action="store", dest="user_private_key", default=None,
+ help="point to the private key file to use if not yet installed in sfi_dir")
+ parser.add_option("-t", "--timeout", dest="timeout", default=None,
+ help="Amout of time to wait before timing out the request")
+ parser.add_option("-?", "--commands",
+ action="store_true", dest="command_help", default=False,
+ help="one page summary on commands & exit")
+ parser.disable_interspersed_args()
+
+ return parser
+
+
+ def print_help (self):
+ self.sfi_parser.print_help()
+ self.command_parser.print_help()
+