sfaprotocol is renamed into sfaserverproxy, with class SfaServerProxy
[sfa.git] / sfa / client / sfi.py
old mode 100755 (executable)
new mode 100644 (file)
index cef8b96..c9bde73
@@ -1,6 +1,3 @@
-#! /usr/bin/env python
-
-# sfi -- slice-based facility interface
 
 # xxx NOTE this will soon be reviewed to take advantage of sfaclientlib
 
@@ -34,7 +31,7 @@ from sfa.rspecs.rspec_converter import RSpecConverter
 from sfa.rspecs.version_manager import VersionManager
 from sfa.client.return_value import ReturnValue
 
-import sfa.client.sfaprotocol as sfaprotocol
+from sfa.client.sfaserverproxy import SfaServerProxy, ServerException
 from sfa.client.client_helper import pg_users_arg, sfa_users_arg
 
 AGGREGATE_PORT=12346
@@ -219,9 +216,16 @@ class Sfi:
         ("get_trusted_certs", "cred"),
         ]
 
-    def print_command_help (self):
-        print "%18s %-15s %s"%("command","args","description")
-        print 80*'-'
+    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)
@@ -230,7 +234,11 @@ class Sfi:
             if not doc: doc="*** no doc found ***"
             doc=doc.strip(" \t\n")
             doc=doc.replace("\n","\n"+35*' ')
-            print "%18s %-15s %s"%(command,args,doc)
+            if verbose:
+                print line
+            print format3%(command,args,doc)
+            if verbose:
+                self.create_cmd_parser(command).print_help()
 
     def create_cmd_parser(self, command):
         if command not in self.available_dict:
@@ -240,7 +248,7 @@ class Sfi:
             self.logger.critical(msg)
             sys.exit(2)
 
-        parser = OptionParser(usage="sfi [sfi_options] %s [options] %s" \
+        parser = OptionParser(usage="sfi [sfi_options] %s [cmd_options] %s" \
                                      % (command, self.available_dict[command]))
 
         # user specifies remote aggregate/sm/component                          
@@ -321,14 +329,14 @@ class Sfi:
     def create_parser(self):
 
         # Generate command line parser
-        parser = OptionParser(usage="sfi [options] command [command_options] [command_args]",
+        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", "--slicemgr", dest="sm",
                          help="slice manager", metavar="URL", default=None)
         parser.add_option("-d", "--dir", dest="sfi_dir",
-                         help="config & working directory - default is " + Sfi.default_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)
@@ -368,7 +376,7 @@ class Sfi:
         self.sfi_parser = self.create_parser()
         (options, args) = self.sfi_parser.parse_args()
         if options.command_help: 
-            self.print_command_help()
+            self.print_command_help(options)
             sys.exit(1)
         self.options = options
 
@@ -378,6 +386,7 @@ class Sfi:
  
         if len(args) <= 0:
             self.logger.critical("No command given. Use -h for help.")
+            self.print_command_help(options)
             return -1
     
         command = args[0]
@@ -470,9 +479,9 @@ class Sfi:
        self.cert_file = cert_file
        self.cert = GID(filename=cert_file)
        self.logger.info("Contacting Registry at: %s"%self.reg_url)
-       self.registry = sfaprotocol.server_proxy(self.reg_url, key_file, cert_file, timeout=self.options.timeout, verbose=self.options.debug)  
+       self.registry = SfaServerProxy(self.reg_url, key_file, cert_file, timeout=self.options.timeout, verbose=self.options.debug)  
        self.logger.info("Contacting Slice Manager at: %s"%self.sm_url)
-       self.slicemgr = sfaprotocol.server_proxy(self.sm_url, key_file, cert_file, timeout=self.options.timeout, verbose=self.options.debug)
+       self.slicemgr = SfaServerProxy(self.sm_url, key_file, cert_file, timeout=self.options.timeout, verbose=self.options.debug)
        return
 
     def get_cached_server_version(self, server):
@@ -565,7 +574,7 @@ class Sfi:
             self.logger.info("Getting Registry issued cert")
             self.read_config()
             # *hack.  need to set registry before _get_gid() is called 
-            self.registry = sfaprotocol.server_proxy(self.reg_url, key_file, cert_file, 
+            self.registry = SfaServerProxy(self.reg_url, key_file, cert_file, 
                                                      timeout=self.options.timeout, verbose=self.options.debug)
             gid = self._get_gid(type='user')
             self.registry = None 
@@ -747,7 +756,7 @@ class Sfi:
         host_parts = host.split('/')
         host_parts[0] = host_parts[0] + ":" + str(port)
         url =  "http://%s" %  "/".join(host_parts)    
-        return sfaprotocol.server_proxy(url, keyfile, certfile, timeout=self.options.timeout, 
+        return SfaServerProxy(url, keyfile, certfile, timeout=self.options.timeout, 
                                         verbose=self.options.debug)
 
     # xxx opts could be retrieved in self.options
@@ -872,7 +881,7 @@ or version information about sfi itself
         elif record['type'] in ["slice"]:
             try:
                 cred = self.get_slice_cred(record.get_name()).save_to_string(save_parents=True)
-            except sfaprotocol.ServerException, e:
+            except ServerException, e:
                # XXX smbaker -- once we have better error return codes, update this
                # to do something better than a string compare
                if "Permission error" in e.args[0]:
@@ -1015,10 +1024,12 @@ or currently provisioned resources  (ListResources)
                 creds = [slice_cred]
                 if delegated_cred:
                     creds.append(delegated_cred)
-        call_args = [slice_urn, creds, rspec, users]
+        # do not append users, keys, or slice tags. Anything 
+        # not contained in this request will be removed from the slice 
+        options = {'append': False}
         if self.server_supports_options_arg(server):
-            options = {'call_id': unique_call_id()}
-            call_args.append(options)
+            options['call_id'] = unique_call_id()
+        call_args = [slice_urn, creds, rspec, users, options]
         result = server.CreateSliver(*call_args)
         value = ReturnValue.get_value(result)
         if opts.file is None:
@@ -1264,5 +1275,3 @@ or currently provisioned resources  (ListResources)
             self.logger.debug('Sfi.get_trusted_certs -> %r'%cert.get_subject())
         return 
 
-if __name__ == "__main__":
-    Sfi().main()