first draft of 'sfi myslice' actually tested against a manifold v2 api server
authorThierry Parmentelat <thierry.parmentelat@inria.fr>
Thu, 23 May 2013 12:16:54 +0000 (14:16 +0200)
committerThierry Parmentelat <thierry.parmentelat@inria.fr>
Thu, 23 May 2013 12:50:26 +0000 (14:50 +0200)
Conflicts:
sfa/client/sfi.py

sfa/client/manifolduploader.py
sfa/client/sfi.py

index 9df009a..e5bb41a 100755 (executable)
@@ -35,35 +35,45 @@ class ManifoldUploader:
         self._username=username
         self._password=password
         self.logger=logger
+        self._proxy=None
 
     def username (self):
-        if not self._username: 
+        if not self._username:
             self._username=raw_input("Enter your manifold username: ")
-        return self._username            
+        return self._username
 
     def password (self):
-        if not self._password: 
+        if not self._password:
             username=self.username()
             self._password=getpass.getpass("Enter password for manifold user %s: "%username)
-        return self._password            
+        return self._password
 
     def platform (self):
-        if not self._platform: 
+        if not self._platform:
             self._platform=raw_input("Enter your manifold platform [%s]: "%DEFAULT_PLATFORM)
             if self._platform.strip()=="": self._platform = DEFAULT_PLATFORM
-        return self._platform            
+        return self._platform
 
     def url (self):
-        if not self._url: 
+        if not self._url:
             self._url=raw_input("Enter the URL for your manifold API [%s]: "%DEFAULT_URL)
             if self._url.strip()=="": self._url = DEFAULT_URL
-        return self._url            
+        return self._url
+
+    def prompt_all(self):
+        self.username(); self.password(); self.platform(); self.url()
+
+    def proxy (self):
+        if not self._proxy:
+            url=self.url()
+            self.logger.debug("Connecting manifold url %s"%url)
+            self._proxy = xmlrpclib.ServerProxy(url, allow_none = True)
+        return self._proxy
 
     # does the job for one credential
     # expects the credential (string) and an optional message for reporting
     # return True upon success and False otherwise
     def upload (self, delegated_credential, message=None):
-        url=self.url()
         platform=self.platform()
         username=self.username()
         password=self.password()
@@ -71,32 +81,34 @@ class ManifoldUploader:
         if not message: message=""
 
         try:
+#            manifold=self.proxy()
+            url=self.url()
             self.logger.debug("Connecting manifold url %s"%url)
             manifold = xmlrpclib.Server(url, allow_none = 1)
             # the code for a V2 interface
-            query= { 'action':       'update',
-                     'fact_table':   'local:account',
-                     'filters':      [ ['platform', '=', platform] ] ,
-                     'params':       {'credential': delegated_credential, },
+            query= { 'action':     'update',
+                     'object':     'local:account',
+                     'filters':    [ ['platform', '=', platform] ] ,
+                     'params':     {'credential': delegated_credential, },
                      }
             try:
-                self.logger.debug("Trying v2 method Update %s"%message)
+                self.logger.debug("Trying v2 method Update@%s %s"%(platform,message))
                 retcod2=manifold.Update (auth, query)
             except Exception,e:
                 # xxx we need a constant constant for UNKNOWN, how about using 1
                 MANIFOLD_UNKNOWN=1
-                retcod2={'code':MANIFOLD_UNKNOWN,'output':"%s"%e}
+                retcod2={'code':MANIFOLD_UNKNOWN,'description':"%s"%e}
             if retcod2['code']==0:
                 info=""
                 if message: info += message+" "
                 info += 'v2 upload OK'
                 self.logger.info(info)
                 return True
-            #print delegated_credential, "upload failed,",retcod['output'], \
+            #print delegated_credential, "upload failed,",retcod['description'], \
             #    "with code",retcod['code']
             # the code for V1
             try:
-                self.logger.debug("Trying v1 method AddCredential %s"%message)
+                self.logger.debug("Trying v1 method AddCredential@%s %s"%(platform,message))
                 retcod1=manifold.AddCredential(auth, delegated_credential, platform)
             except Exception,e:
                 retcod1=e
@@ -109,7 +121,12 @@ class ManifoldUploader:
             # everything has failed, let's report
             if message: self.logger.error("Could not upload %s"%message)
             else: self.logger.error("Could not upload credential")
-            self.logger.info("  V2 Update returned code %s and error %s"%(retcod2['code'],retcod2['output']))
+            if 'code' in retcod2 and 'description' in retcod2:
+                self.logger.info("  V2 Update returned code %s and error >>%s<<"%(retcod2['code'],retcod2['description']))
+                self.logger.debug("****** full retcod2")
+                for (k,v) in retcod2.items(): self.logger.debug("**** %s: %s"%(k,v))
+            else:
+                self.logger.info("  V2 Update returned %s"%retcod2)
             self.logger.info("  V1 AddCredential returned code %s (expected 1)"%retcod1)
             return False
         except Exception, e:
@@ -133,12 +150,17 @@ def main ():
                          help='the manifold username')
     parser.add_argument ('-P','--password',dest='password',action='store',default=None,
                          help='the manifold password')
+    parser.add_argument ('-v','--verbose',dest='verbose',action='count',default=0,
+                         help='more and more verbose')
     args = parser.parse_args ()
     
     from sfa.util.sfalogging import sfi_logger
+    sfi_logger.enable_console()
+    sfi_logger.setLevelFromOptVerbose(args.verbose)
     uploader = ManifoldUploader (url=args.url, platform=args.platform,
                                  username=args.username, password=args.password,
                                  logger=sfi_logger)
+
     for filename in args.credential_files:
         with file(filename) as f:
             result=uploader.upload (f.read(),filename)
index 2a6b7ad..3c125bc 100644 (file)
@@ -299,7 +299,7 @@ class Sfi:
             print line
         else:
             print line
-            self.create_global_parser().print_help()
+            self.create_parser_global().print_help()
         # preserve order from the code
         for command in commands_list:
             (doc, args_string, example) = commands_dict[command]
@@ -308,7 +308,7 @@ class Sfi:
             doc=doc.replace("\n","\n"+35*' ')
             print format3%(command,args_string,doc)
             if verbose:
-                self.create_command_parser(command).print_help()
+                self.create_parser_command(command).print_help()
             
     ### now if a known command was found we can be more verbose on that one
     def print_help (self):
@@ -323,7 +323,7 @@ class Sfi:
             print "\n==================== %s example(s)"%self.command
             print example
 
-    def create_global_parser(self):
+    def create_parser_global(self):
         # Generate command line parser
         parser = OptionParser(add_help_option=False,
                               usage="sfi [sfi_options] command [cmd_options] [cmd_args]",
@@ -365,7 +365,7 @@ class Sfi:
         return parser
         
 
-    def create_command_parser(self, command):
+    def create_parser_command(self, command):
         if command not in commands_dict:
             msg="Invalid command\n"
             msg+="Commands: "
@@ -386,6 +386,14 @@ class Sfi:
             parser.add_option('-m', '--myslice', dest='myslice', action='store_true', default=False,
                               help='how myslice config variables as well')
 
+        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")
+
         if command in ("add", "update"):
             parser.add_option('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)')
             parser.add_option('-t', '--type', dest='type', metavar='<type>', help='object type', default=None)
@@ -476,15 +484,11 @@ class Sfi:
            parser.add_option("-A","--to-authority",dest='delegate_to_authority',action='store_true',default=False,
                              help="""by default the mandatory argument is expected to be a user, 
 use this if you mean an authority instead""")
-        
-        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")
 
+        if command in ("myslice"):
+            parser.add_option("-p","--password",dest='password',action='store',default=None,
+                              help="specify mainfold password on the command line")
+        
         return parser
 
         
@@ -499,7 +503,7 @@ use this if you mean an authority instead""")
         return method(command_options, command_args)
 
     def main(self):
-        self.sfi_parser = self.create_global_parser()
+        self.sfi_parser = self.create_parser_global()
         (options, args) = self.sfi_parser.parse_args()
         if options.help: 
             self.print_commands_help(options)
@@ -522,7 +526,7 @@ use this if you mean an authority instead""")
             sys.exit(1)
         # second pass options parsing
         self.command=command
-        self.command_parser = self.create_command_parser(command)
+        self.command_parser = self.create_parser_command(command)
         (command_options, command_args) = self.command_parser.parse_args(args[1:])
         if command_options.help:
             self.print_help()
@@ -1547,8 +1551,12 @@ $ sfi m
         delegatee_hrn=myslice_dict['delegate']
         hrn_delegated_credentials = []
         for (hrn, htype, credential) in hrn_credentials:
-            sfi_logger.info("Computing delegated credential for %s (%s)"%(hrn,htype))
-            hrn_delegated_credentials.append ((hrn, htype, self.client_bootstrap.delegate_credential_string (credential, delegatee_hrn, delegatee_type),))
+            delegated_credential = self.client_bootstrap.delegate_credential_string (credential, delegatee_hrn, delegatee_type)
+            hrn_delegated_credentials.append ((hrn, htype, delegated_credential, ))
+            # inspect
+            inspect=Credential(string=delegated_credential)
+            expire_datetime=inspect.get_expiration()
+            sfi_logger.info("Delegated credential for %s (%s) expires on %s"%(hrn,htype,expire_datetime))
 
         # (f) and finally upload them to manifold server
         # xxx todo add an option so the password can be set on the command line
@@ -1556,11 +1564,12 @@ $ sfi m
         uploader = ManifoldUploader (logger=sfi_logger,
                                      url=myslice_dict['backend'],
                                      platform=myslice_dict['platform'],
-                                     username=myslice_dict['username'])
+                                     username=myslice_dict['username'],
+                                     password=options.password)
         uploader.prompt_all()
         for (hrn,htype,delegated_credential) in hrn_delegated_credentials:
             sfi_logger.info("Uploading delegated credential for %s (%s)"%(hrn,htype))
-            uploader.upload(delegated_credential,message=hrn)
+            uploader.upload(delegated_credential,message="%s (%s)"%(hrn,htype))
         # at first I thought we would want to save these,
         # like 'sfi delegate does' but on second thought
         # it is probably not helpful as people would not