X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfa%2Fclient%2Fsfi.py;h=094c6be4f1eae0b7b751f5cc06f867c4b09c1950;hb=b2d3e37b8c36a60f2449c235e0b4f7d2942e3292;hp=8fe234904adfd6439dd8fab0007498e0366840fd;hpb=27091c5dbe38674fee5e91d153315449027d3061;p=sfa.git diff --git a/sfa/client/sfi.py b/sfa/client/sfi.py index 8fe23490..094c6be4 100755 --- a/sfa/client/sfi.py +++ b/sfa/client/sfi.py @@ -10,13 +10,12 @@ import traceback import socket import random import datetime +import zlib from lxml import etree from StringIO import StringIO from types import StringTypes, ListType from optparse import OptionParser -import zlib - -from sfa.util.sfalogging import sfa_logger,sfa_logger_goes_to_console +from sfa.util.sfalogging import sfi_logger from sfa.trust.certificate import Keypair, Certificate from sfa.trust.gid import GID from sfa.trust.credential import Credential @@ -26,6 +25,9 @@ from sfa.util.xrn import Xrn, get_leaf, get_authority, hrn_to_urn import sfa.util.xmlrpcprotocol as xmlrpcprotocol from sfa.util.config import Config from sfa.util.version import version_core +from sfa.util.cache import Cache +from sfa.rspecs.rspec_version import RSpecVersion +from sfa.rspecs.pg_rspec import pg_rspec_request_version AGGREGATE_PORT=12346 CM_PORT=12346 @@ -134,14 +136,15 @@ class Sfi: for opt in Sfi.required_options: if not hasattr(options,opt): setattr(options,opt,None) if not hasattr(options,'sfi_dir'): options.sfi_dir=os.path.expanduser("~/.sfi/") + self.sfi_dir = options.sfi_dir self.options = options self.slicemgr = None self.registry = None self.user = None self.authority = None self.hashrequest = False - sfa_logger_goes_to_console() - self.logger=sfa_logger() + self.logger = sfi_logger + self.logger.enable_console() def create_cmd_parser(self, command, additional_cmdargs=None): cmdargs = {"list": "authority", @@ -209,6 +212,10 @@ class Sfi: 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) + if command in ("resources", "show", "list"): parser.add_option("-o", "--output", dest="file", @@ -272,7 +279,7 @@ class Sfi: return parser - + def read_config(self): config_file = self.options.sfi_dir + os.sep + "sfi_config" try: @@ -340,15 +347,56 @@ class Sfi: self.key = Keypair(filename=key_file) self.key_file = key_file self.cert_file = cert_file - self.cert = GID(filename=cert_file) - # Establish connection to server(s) + self.cert = GID(filename=cert_file) self.logger.info("Contacting Registry at: %s"%self.reg_url) self.registry = xmlrpcprotocol.get_server(self.reg_url, key_file, cert_file, self.options) self.logger.info("Contacting Slice Manager at: %s"%self.sm_url) self.slicemgr = xmlrpcprotocol.get_server(self.sm_url, key_file, cert_file, self.options) - return - + + def get_cached_server_version(self, server): + # check local cache first + cache = None + version = None + cache_file = self.sfi_dir + os.path.sep + 'sfi_cache.dat' + cache_key = server.url + "-version" + try: + cache = Cache(cache_file) + except IOError: + cache = Cache() + self.logger.info("Local cache not found at: %s" % cache_file) + + if cache: + version = cache.get(cache_key) + + if not version: + version = server.GetVersion() + # cache version for 24 hours + cache.add(cache_key, version, ttl= 60*60*24) + self.logger.info("Updating cache file %s" % cache_file) + cache.save_to_file(cache_file) + + + return version + + + def server_supports_call_id_arg(self, server): + """ + Returns true if server support the optional call_id arg, false otherwise. + """ + server_version = self.get_cached_server_version(server) + if 'sfa' in server_version: + code_tag = server_version['code_tag'] + code_tag_parts = code_tag.split("-") + + version_parts = code_tag_parts[0].split(".") + major, minor = version_parts[0], version_parts[1] + rev = code_tag_parts[1] + if int(major) > 1: + if int(minor) > 0 or int(rev) > 20: + return True + return False + # # Get various credential and spec files # @@ -390,6 +438,7 @@ class Sfi: cert.sign() self.logger.info("Writing self-signed certificate to %s"%cert_file) cert.save_to_file(cert_file) + self.cert = cert # try to get registry issued cert try: self.logger.info("Getting Registry issued cert") @@ -402,7 +451,7 @@ class Sfi: gid.save_to_file(cert_file) except: self.logger.info("Failed to download Registry issued cert") - + return cert_file def get_cached_gid(self, file): @@ -435,18 +484,23 @@ class Sfi: hrn = self.user gidfile = os.path.join(self.options.sfi_dir, hrn + ".gid") + print gidfile gid = self.get_cached_gid(gidfile) if not gid: user_cred = self.get_user_cred() records = self.registry.Resolve(hrn, user_cred.save_to_string(save_parents=True)) - record = None + if not records: + raise RecordNotFound(args[0]) + record = records[0] if type: + record=None for rec in records: - if type == record['type']: + if type == rec['type']: record = rec if not record: raise RecordNotFound(args[0]) - gid = GID(string=records[0]['gid']) + + gid = GID(string=record['gid']) self.logger.info("Writing gid to %s"%gidfile) gid.save_to_file(filename=gidfile) return gid @@ -632,7 +686,7 @@ class Sfi: record = SliceRecord(dict=record) elif record['type'] in ['node']: record = NodeRecord(dict=record) - elif record['type'] in ['authority', 'ma', 'sa']: + elif record['type'].startswith('authority'): record = AuthorityRecord(dict=record) else: record = SfaRecord(dict=record) @@ -640,7 +694,7 @@ class Sfi: record.dump() else: print record.save_to_string() - + if opts.file: file = opts.file if not file.startswith(os.sep): @@ -758,6 +812,8 @@ class Sfi: """ trusted_certs = self.registry.get_trusted_certs() for trusted_cert in trusted_certs: + gid = GID(string=trusted_cert) + gid.dump() cert = Certificate(string=trusted_cert) self.logger.debug('Sfi.get_trusted_certs -> %r'%cert.get_subject()) return @@ -791,7 +847,6 @@ class Sfi: # ================================================================== # Slice-related commands # ================================================================== - def version(self, opts, args): if opts.version_local: @@ -831,7 +886,7 @@ class Sfi: if args: cred = self.get_slice_cred(args[0]).save_to_string(save_parents=True) hrn = args[0] - call_options = {'geni_slice_urn': hrn_to_urn(hrn, 'slice')} + call_options = {'geni_slice_urn': hrn_to_urn(hrn, 'slice')} else: cred = user_cred hrn = None @@ -841,10 +896,22 @@ class Sfi: delegated_cred = self.delegate_cred(cred, get_authority(self.authority)) creds.append(delegated_cred) if opts.rspec_version: - #call_options['rspec_version'] = opts.rspec_version - call_options['rspec_version'] = {'type': opts.rspec_version, 'version': "2"} - result = server.ListResources(creds, call_options,unique_call_id()) - #result = server.ListResources(creds, call_options) + server_version = self.get_cached_server_version(server) + if 'sfa' in server_version: + # just request the version the client wants + call_options['rspec_version'] = dict(RSpecVersion(opts.rspec_version)) + else: + # this must be a protogeni aggregate. We should request a v2 ad rspec + # regardless of what the client user requested + call_options['rspec_version'] = dict(pg_rspec_request_version) + #panos add info options + if opts.info: + call_options['info'] = opts.info + + call_args = [creds, call_options] + if self.server_supports_call_id_arg(server): + call_args.append(unique_call_id()) + result = server.ListResources(*call_args) format = opts.format if opts.file is None: display_rspec(result, format) @@ -874,7 +941,7 @@ class Sfi: # }] users = [] server = self.get_server_from_opts(opts) - version = server.GetVersion() + version = self.get_cached_server_version(server) if 'sfa' not in version: # need to pass along user keys if this request is going to a ProtoGENI aggregate # ProtoGeni Aggregates will only install the keys of the user that is issuing the @@ -890,8 +957,13 @@ class Sfi: for user_record in user_records: if 'keys' in user_record: user['keys'].extend(user_record['keys']) - users.append(user) - result = server.CreateSliver(slice_urn, creds, rspec, users, unique_call_id()) + users.append(user) + + call_args = [slice_urn, creds, rspec, users] + if self.server_supports_call_id_arg(server): + call_args.append(unique_call_id()) + + result = server.CreateSliver(*call_args) print result return result @@ -958,8 +1030,12 @@ class Sfi: delegated_cred = self.delegate_cred(slice_cred, get_authority(self.authority)) creds.append(delegated_cred) server = self.get_server_from_opts(opts) - return server.DeleteSliver(slice_urn, creds, unique_call_id()) - + + call_args = [slice_urn, creds] + if self.server_supports_call_id_arg(server): + call_args.append(unique_call_id()) + return server.DeleteSliver(*call_args) + # start named slice def start(self, opts, args): slice_hrn = args[0] @@ -1006,7 +1082,11 @@ class Sfi: delegated_cred = self.delegate_cred(slice_cred, get_authority(self.authority)) creds.append(delegated_cred) time = args[1] - return server.RenewSliver(slice_urn, creds, time, unique_call_id()) + + call_args = [slice_urn, creds, time] + if self.server_supports_call_id_arg(server): + call_args.append(unique_call_id()) + return server.RenewSliver(*call_args) def status(self, opts, args): @@ -1018,7 +1098,10 @@ class Sfi: delegated_cred = self.delegate_cred(slice_cred, get_authority(self.authority)) creds.append(delegated_cred) server = self.get_server_from_opts(opts) - print server.SliverStatus(slice_urn, creds, unique_call_id()) + call_args = [slice_urn, creds] + if self.server_supports_call_id_arg(server): + call_args.append(unique_call_id()) + print server.SliverStatus(*call_args) def shutdown(self, opts, args): @@ -1057,13 +1140,12 @@ class Sfi: (cmd_opts, cmd_args) = self.cmd_parser.parse_args(args[1:]) self.set_servers() - self.logger.info("Command=%s" % command) if command in ("resources"): self.logger.debug("resources cmd_opts %s" % cmd_opts.format) elif command in ("list", "show", "remove"): self.logger.debug("cmd_opts.type %s" % cmd_opts.type) - self.logger.debug('cmd_args %s',cmd_args) + self.logger.debug('cmd_args %s' % cmd_args) try: self.dispatch(command, cmd_opts, cmd_args)