autoload sfi_config
[sfa.git] / sfa / client / sfi.py
index d3c320f..9cb40aa 100755 (executable)
@@ -1,21 +1,22 @@
 #! /usr/bin/env python
-from __future__ import with_statement
 
 # sfi -- slice-based facility interface
 
 import sys
 import os, os.path
 import tempfile
+import traceback
 from types import StringTypes, ListType
 from optparse import OptionParser
 
 from sfa.trust.certificate import Keypair, Certificate
 from sfa.trust.credential import Credential
-from sfa.trust.gid import create_uuid
 
-from sfa.util.geniclient import GeniClient, ServerException
+from sfa.util.geniclient import GeniClient
 from sfa.util.record import *
 from sfa.util.rspec import Rspec
+from sfa.util.xmlrpcprotocol import ServerException
+from sfa.util.config import Config
 
 # xxx todo xxx auto-load ~/.sfi/sfi_config
 
@@ -35,64 +36,69 @@ def set_servers(options):
    global user
    global authority
 
+   config_file = sfi_dir + os.sep + "sfi_config"
+   try:
+      config = Config (config_file)
+   except:
+      print "Failed to read configuration file",config_file
+      print "Make sure to remove the export clauses and to add quotes"
+      if not options.verbose:
+         print "Re-run with -v for more details"
+      else:
+         traceback.print_exc()
+      sys.exit(1)
+
+   errors=0
    # Set SliceMgr URL
    if (options.sm is not None):
       sm_url = options.sm
-   elif ("SFI_SM" in os.environ):
-      sm_url = os.environ["SFI_SM"]
+   elif hasattr(config,"SFI_SM"):
+      sm_url = config.SFI_SM
    else:
-      print "No Known Slice Manager"
-      print "Try:"
-      print "    export SFI_SM=http://your.slicemanager.url:12347/"
-      print "Or add this argument to the command line:"
-      print "    --slicemgr=http://your.slicemanager.url:12347/"
-      sys.exit(1)
+      print "You need to set e.g. SFI_SM='http://your.slicemanager.url:12347/' in %s"%config_file
+      errors +=1 
 
    # Set Registry URL
    if (options.registry is not None):
       reg_url = options.registry
-   elif ("SFI_REGISTRY" in os.environ):
-      reg_url = os.environ["SFI_REGISTRY"]
+   elif hasattr(config,"SFI_REGISTRY"):
+      reg_url = config.SFI_REGISTRY
    else:
-      print "No Known Registry Server"
-      print "Try:"
-      print "    export SFI_REGISTRY=http://your.registry.url:12345/"
-      print "Or add this argument to the command line:"
-      print "    --registry=http://your.registry.url:12345/"
-      sys.exit(1)
-
-   if options.verbose :
-      print "Contacting Slice Manager at:", sm_url
-      print "Contacting Registry at:", reg_url
+      print "You need to set e.g. SFI_REGISTRY='http://your.registry.url:12345/' in %s"%config_file
+      errors +=1 
 
    # Set user HRN
    if (options.user is not None):
       user = options.user
-   elif ("SFI_USER" in os.environ):
-      user = os.environ["SFI_USER"]
+   elif hasattr(config,"SFI_USER"):
+      user = config.SFI_USER
    else:
-      print "No Known User Name"
-      print "Try:"
-      print "    export SFI_USER=$SFI_AUTH.username"
-      print "Or add this argument to the command line:"
-      print "    --user=username"
-      sys.exit(1)
+      print "You need to set e.g. SFI_USER='plc.princeton.username' in %s"%config_file
+      errors +=1 
 
    # Set authority HRN
    if (options.auth is not None):
       authority = options.auth
-   elif ("SFI_AUTH" in os.environ):
-      authority = os.environ["SFI_AUTH"]
+   elif hasattr(config,"SFI_AUTH"):
+      authority = config.SFI_AUTH
    else:
-      authority = None
+      print "You need to set e.g. SFI_AUTH='plc.princeton' in %s"%config_file
+      errors +=1 
+
+   if errors:
+      sys.exit(1)
+
+   if options.verbose :
+      print "Contacting Slice Manager at:", sm_url
+      print "Contacting Registry at:", reg_url
 
    # Get key and certificate
    key_file = get_key_file()
    cert_file = get_cert_file(key_file)
 
    # Establish connection to server(s)
-   slicemgr = GeniClient(sm_url, key_file, cert_file)
-   registry = GeniClient(reg_url, key_file, cert_file)
+   slicemgr = GeniClient(sm_url, key_file, cert_file, options.protocol)
+   registry = GeniClient(reg_url, key_file, cert_file, options.protocol)
    return
 
 #
@@ -340,6 +346,9 @@ def create_parser():
    parser.add_option("-v", "--verbose",
         action="store_true", dest="verbose", default=False,
         help="verbose mode")
+   parser.add_option("-p", "--protocol",
+        dest="protocol", default="xmlrpc",
+        help="RPC protocol (xmlrpc or soap)")
    parser.disable_interspersed_args()
 
    return parser
@@ -397,7 +406,11 @@ def main():
 def list(opts, args):
    global registry
    user_cred = get_user_cred()
-   list = registry.list(user_cred, args[0])
+   try:
+      list = registry.list(user_cred, args[0])
+   except IndexError:
+      raise Exception, "Not enough parameters for the 'list' command"
+      
    # filter on person, slice, site, node, etc.  
    # THis really should be in the filter_records funct def comment...
    list = filter_records(opts.type, list)
@@ -582,8 +595,7 @@ def create(opts, args):
    slice_hrn = args[0]
    slice_cred = get_slice_cred(slice_hrn)
    rspec_file = get_rspec_file(args[1])
-   with open(rspec_file) as f:
-      rspec = f.read()
+   rspec=open(rspec_file).read()
    return slicemgr.create_slice(slice_cred, slice_hrn, rspec)
 
 # delete named slice