from sfa.client.client_helper import pg_users_arg, sfa_users_arg
from sfa.client.return_value import ReturnValue
from sfa.client.candidates import Candidates
+from sfa.client.manifolduploader import ManifoldUploader
CM_PORT=12346
import uuid
def unique_call_id(): return uuid.uuid4().urn
+########## a simple model for maintaing 3 doc attributes per command (instead of just one)
+# essentially for the methods that implement a subcommand like sfi list
+# we need to keep track of
+# (*) doc a few lines that tell what it does, still located in __doc__
+# (*) args_string a simple one-liner that describes mandatory arguments
+# (*) example well, one or several releant examples
+#
+# since __doc__ only accounts for one, we use this simple mechanism below
+# however we keep doc in place for easier migration
+
+from functools import wraps
+
+# we use a list as well as a dict so we can keep track of the order
+commands_list=[]
+commands_dict={}
+
+def register_command (args_string, example):
+ def wrap(m):
+ name=getattr(m,'__name__')
+ doc=getattr(m,'__doc__',"-- missing doc --")
+ doc=doc.strip(" \t\n")
+ commands_list.append(name)
+ commands_dict[name]=(doc, args_string, example)
+ @wraps(m)
+ def new_method (*args, **kwds): return m(*args, **kwds)
+ return new_method
+ return wrap
+
+##########
+
class Sfi:
# dirty hack to make this class usable from the outside
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"),
- ("resources", ""),
- ("describe", "slice_hrn"),
- ("allocate", "slice_hrn rspec"),
- ("provision", "slice_hrn"),
- ("action", "slice_hrn action"),
- ("delete", "slice_hrn"),
- ("status", "slice_hrn"),
- ("renew", "slice_hrn time"),
- ("shutdown", "slice_hrn"),
- ("delegate", "to_hrn"),
- ("gid", "[name]"),
- ("trusted", "cred"),
- ("config", ""),
- ]
-
- def print_command_help (self, options):
+ self.command=None
+
+ ### suitable if no reasonable command has been provided
+ def print_commands_help (self, options):
verbose=getattr(options,'verbose')
format3="%18s %-15s %s"
line=80*'-'
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*' ')
+ # preserve order from the code
+ for command in commands_list:
+ (doc, args_string, example) = commands_dict[command]
if verbose:
print line
- print format3%(command,args,doc)
+ doc=doc.replace("\n","\n"+35*' ')
+ print format3%(command,args_string,doc)
if verbose:
self.create_command_parser(command).print_help()
+
+ ### now if a known command was found we can be more verbose on that one
+ def print_help (self):
+ print "==================== Generic sfi usage"
+ self.sfi_parser.print_help()
+ (doc,_,example)=commands_dict[self.command]
+ print "\n==================== Purpose of %s"%self.command
+ print doc
+ print "\n==================== Specific usage for %s"%self.command
+ self.command_parser.print_help()
+ if example:
+ print "\n==================== %s example(s)"%self.command
+ print example
def create_command_parser(self, command):
- if command not in self.available_dict:
+ if command not in commands_dict:
msg="Invalid command\n"
msg+="Commands: "
- msg += ','.join(self.available_names)
+ msg += ','.join(commands_list)
self.logger.critical(msg)
sys.exit(2)
- parser = OptionParser(usage="sfi [sfi_options] %s [cmd_options] %s" \
- % (command, self.available_dict[command]))
+ # retrieve args_string
+ (_, args_string, __) = commands_dict[command]
+
+ parser = OptionParser(add_help_option=False,
+ usage="sfi [sfi_options] %s [cmd_options] %s"
+ % (command, args_string))
+ parser.add_option ("-h","--help",dest='help',action='store_true',default=False,
+ help="Summary of one command usage")
if command in ("add", "update"):
parser.add_option('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)')
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 = OptionParser(add_help_option=False,
+ usage="sfi [sfi_options] command [cmd_options] [cmd_args]",
+ description="Commands: %s"%(" ".join(commands_list)))
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="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,
+ parser.add_option("-h", "--help",
+ action="store_true", dest="help", default=False,
help="one page summary on commands & exit")
parser.disable_interspersed_args()
return parser
- def print_help (self):
- print "==================== Generic sfi usage"
- self.sfi_parser.print_help()
- print "==================== Specific command usage"
- self.command_parser.print_help()
-
#
# Main: parse arguments and dispatch to command
#
def dispatch(self, command, command_options, command_args):
- method=getattr(self, command,None)
+ method=getattr(self, command, None)
if not method:
print "Unknown command %s"%command
return
def main(self):
self.sfi_parser = self.create_parser()
(options, args) = self.sfi_parser.parse_args()
- if options.command_help:
- self.print_command_help(options)
+ if options.help:
+ self.print_commands_help(options)
sys.exit(1)
self.options = options
if len(args) <= 0:
self.logger.critical("No command given. Use -h for help.")
- self.print_command_help(options)
+ self.print_commands_help(options)
return -1
# complete / find unique match with command set
- command_candidates = Candidates (self.available_names)
+ command_candidates = Candidates (commands_list)
input = args[0]
command = command_candidates.only_match(input)
if not command:
- self.print_command_help(options)
+ self.print_commands_help(options)
sys.exit(1)
# second pass options parsing
+ self.command=command
self.command_parser = self.create_command_parser(command)
(command_options, command_args) = self.command_parser.parse_args(args[1:])
+ if command_options.help:
+ self.print_help()
+ sys.exit(1)
self.command_options = command_options
self.read_config ()
self.bootstrap ()
- self.logger.debug("Command=%s" % command)
+ self.logger.debug("Command=%s" % self.command)
try:
self.dispatch(command, command_options, command_args)
+ except SystemExit:
+ return 1
except:
self.logger.log_exc ("sfi command %s failed"%command)
- sys.exit(1)
+ return 1
- return
+ return 0
####################
def read_config(self):
# we need to preload the sections we want parsed
# from the shell config
config.add_section('sfi')
+ # sface users should be able to use this same file to configure their stuff
config.add_section('sface')
+ # manifold users should be able to specify the details
+ # of their backend server here for 'sfi myslice'
+ config.add_section('myslice')
config.load(config_file)
# back up old config
shutil.move(config_file, shell_config_file)
# Registry-related commands
#==========================================================================
+ @register_command("","")
def version(self, options, args):
"""
display an SFA server version (GetVersion)
-or version information about sfi itself
+ or version information about sfi itself
"""
if options.version_local:
version=version_core()
pprinter = PrettyPrinter(indent=4)
pprinter.pprint(version)
+ @register_command("authority","")
def list(self, options, args):
"""
list entries in named authority registry (List)
save_records_to_file(options.file, list, options.fileformat)
return
+ @register_command("name","")
def show(self, options, args):
"""
show details about named registry record (Resolve)
save_records_to_file(options.file, record_dicts, options.fileformat)
return
+ @register_command("[record]","")
def add(self, options, args):
"add record into registry by using the command options (Recommended) or from xml file (Register)"
auth_cred = self.my_authority_credential_string()
record_dict['last_name'] = record_dict['hrn']
return self.registry().Register(record_dict, auth_cred)
+ @register_command("[record]","")
def update(self, options, args):
"update record into registry by using the command options (Recommended) or from xml file (Update)"
record_dict = {}
show_credentials(cred)
return self.registry().Update(record_dict, cred)
+ @register_command("name","")
def remove(self, options, args):
"remove registry record by name (Remove)"
auth_cred = self.my_authority_credential_string()
# Slice-related commands
# ==================================================================
+ @register_command("","")
def slices(self, options, args):
"list instantiated slices (ListSlices) - returns urn's"
server = self.sliceapi()
return
# show rspec for named slice
+ @register_command("","")
def resources(self, options, args):
"""
discover available resources (ListResources)
return
+ @register_command("slice_hrn","")
def describe(self, options, args):
"""
- shows currently allocated/provisioned resources of the named slice or set of slivers (Describe)
+ shows currently allocated/provisioned resources
+ of the named slice or set of slivers (Describe)
"""
server = self.sliceapi()
return
+ @register_command("slice_hrn","")
def delete(self, options, args):
"""
de-allocate and de-provision all or named slivers of the slice (Delete)
print value
return value
+ @register_command("slice_hrn rspec","")
def allocate(self, options, args):
"""
allocate resources to the named slice (Allocate)
save_rspec_to_file (value, options.file)
if (self.options.raw is None) and (options.file is None):
print value
-
return value
+ @register_command("slice_hrn","")
def provision(self, options, args):
"""
provision already allocated resources of named slice (Provision)
print value
return value
+ @register_command("slice_hrn","")
def status(self, options, args):
"""
retrieve the status of the slivers belonging to tne named slice (Status)
save_raw_to_file(result, self.options.raw, self.options.rawformat, self.options.rawbanner)
else:
print value
+ # Thierry: seemed to be missing
+ return value
- # reset named slice
+ @register_command("slice_hrn action","")
def action(self, options, args):
"""
- Perform the named operational action on the named slivers
+ Perform the named operational action on these slivers
"""
server = self.sliceapi()
api_options = {}
print value
return value
+ @register_command("slice_hrn time","")
def renew(self, options, args):
"""
renew slice (RenewSliver)
return value
+ @register_command("slice_hrn","")
def shutdown(self, options, args):
"""
shutdown named slice (Shutdown)
return value
+ @register_command("[name]","")
def gid(self, options, args):
"""
Create a GID (CreateGid)
GID(string=gid).save_to_file(filename)
+ @register_command("to_hrn","""$ sfi delegate -u -p -s ple.inria.heartbeat -s ple.inria.omftest ple.upmc.slicebrowser
+
+ will locally create a set of delegated credentials for the benefit of ple.upmc.slicebrowser
+ the set of credentials in the scope for this call would be
+ (*) ple.inria.thierry_parmentelat.user_for_ple.upmc.slicebrowser.user.cred
+ as per -u/--user
+ (*) ple.inria.pi_for_ple.upmc.slicebrowser.user.cred
+ as per -p/--pi
+ (*) ple.inria.heartbeat.slice_for_ple.upmc.slicebrowser.user.cred
+ (*) ple.inria.omftest.slice_for_ple.upmc.slicebrowser.user.cred
+ because of the two -s options
+
+""")
def delegate (self, options, args):
"""
(locally) create delegate credential for use by given hrn
+ make sure to check for 'sfi myslice' instead if you plan
+ on using MySlice
"""
if len(args) != 1:
self.print_help()
delegated_credential.save_to_file(filename, save_parents=True)
self.logger.info("delegated credential for %s to %s and wrote to %s"%(message,to_hrn,filename))
+ ####################
+ @register_command("","""$ less +/myslice sfi_config
+[myslice]
+backend = 'http://manifold.pl.sophia.inria.fr:7080'
+# the HRN that myslice uses, so that we are delegating to
+delegate = 'ple.upmc.slicebrowser'
+# platform - this is a myslice concept
+platform = 'ple'
+# username - as of this writing (May 2013) a simple login name
+user = 'thierry'
+
+$ sfi myslice
+
+ will first collect the slices that you are part of, then make sure
+ all your credentials are up-to-date (read: refresh expired ones)
+ then compute delegated credentials for user 'ple.upmc.slicebrowser'
+ and upload them all on myslice backend, using 'platform' and 'user'.
+ A password will be prompted for the upload part.
+"""
+) # register_command
+ def myslice (self, options, args):
+
+ """ This helper is for refreshing your credentials at myslice; it will
+ * compute all the slices that you currently have credentials on
+ * refresh all your credentials (you as a user and pi, your slices)
+ * upload them to the manifold backend server
+ for last phase, sfi_config is read to look for the [myslice] section,
+ and namely the 'backend', 'delegate' and 'user' settings"""
+
+ ##########
+ if len(args)>0:
+ self.print_help()
+ sys.exit(1)
+ # ManifoldUploader
+ pass
+
+ @register_command("cred","")
def trusted(self, options, args):
"""
- return uhe trusted certs at this interface (get_trusted_certs)
+ return the trusted certs at this interface (get_trusted_certs)
"""
trusted_certs = self.registry().get_trusted_certs()
for trusted_cert in trusted_certs:
self.logger.debug('Sfi.trusted -> %r'%cert.get_subject())
return
+ @register_command("","")
def config (self, options, args):
"Display contents of current config"
self.show_config()