Changeset 19cc408 for fedd


Ignore:
Timestamp:
Oct 3, 2008 4:46:21 PM (16 years ago)
Author:
Ted Faber <faber@…>
Branches:
axis_example, compt_changes, info-ops, master, version-1.30, version-2.00, version-3.01, version-3.02
Children:
ea0a821
Parents:
01073f7
Message:

refactoring

Location:
fedd
Files:
4 added
1 deleted
3 edited
1 moved

Legend:

Unmodified
Added
Removed
  • fedd/fedd.py

    r01073f7 r19cc408  
    1515
    1616from fedd_util import fedd_ssl_context, fedid
    17 from fedd_proj import new_feddservice
     17from fedd_deter_impl import new_feddservice
     18from service_error import *
    1819
    1920# The SSL server here is based on the implementation described at
     
    2728        SSLServer.__init__(self, ME, handler, ssl_ctx)
    2829        self.impl = impl
     30        self.soap_methods = impl.get_soap_services()
     31        self.xmlrpc_methods = impl.get_xmlrpc_services()
    2932
    3033class fedd_soap_handler(BaseHTTPRequestHandler):
    3134    server_version = "ZSI/2.0 fedd/0.1 " + BaseHTTPRequestHandler.server_version
    32 
    3335
    3436    def send_xml(self, text, code=200):
     
    8082        if not self.check_method(ps): return
    8183        try:
    82             resp = self.server.impl.soap_dispatch(ps.body_root.localName, ps,
     84            resp = self.soap_dispatch(ps.body_root.localName, ps,
    8385                    fedid(cert=self.request.get_peer_cert()))
    8486        except Fault, f:
     
    9193            self.send_xml(str(sw))
    9294
     95    def soap_dispatch(self, method, req, fid):
     96        if self.server.soap_methods.has_key(method):
     97            try:
     98                return self.server.soap_methods[method](req, fid)
     99            except service_error, e:
     100                de = ns0.faultType_Def(
     101                        (ns0.faultType_Def.schema,
     102                            "FeddFaultBody")).pyclass()
     103                de._code=e.code
     104                de._errstr=e.code_string()
     105                de._desc=e.desc
     106                if  e.is_server_error():
     107                    raise Fault(Fault.Server, e.code_string(), detail=de)
     108                else:
     109                    raise Fault(Fault.Client, e.code_string(), detail=de)
     110        else:
     111            raise Fault(Fault.Client, "Unknown method: %s" % method)
     112
     113
    93114class fedd_xmlrpc_handler(BaseHTTPRequestHandler):
    94115    server_version = "ZSI/2.0 fedd/0.1 " + BaseHTTPRequestHandler.server_version
     
    120141        if method != None:
    121142            try:
    122                 resp = self.server.impl.xmlrpc_dispatch(method, params,
     143                resp = self.xmlrpc_dispatch(method, params,
    123144                            fedid(cert=self.request.get_peer_cert()))
    124145                data = xmlrpclib.dumps((resp,), encoding='UTF-8',
     
    129150        self.send_xml(data)
    130151
     152
     153    def xmlrpc_dispatch(self, method, req, fid):
     154        if self.server.xmlrpc_methods.has_key(method):
     155            try:
     156                return self.server.xmlrpc_methods[method](req, fid)
     157            except service_error, e:
     158                raise xmlrpclib.Fault(e.code_string(), e.desc)
     159        else:
     160            raise xmlrpclib.Fault(100, "Unknown method: %s" % method)
    131161
    132162class fedd_opts(OptionParser):
  • fedd/fedd_allocate_project.py

    r01073f7 r19cc408  
    1616import subprocess
    1717import tempfile
    18 import copy
    1918
    2019from fedd_services import *
  • fedd/fedd_experiment_control.py

    r01073f7 r19cc408  
    2929from service_error import *
    3030
    31 class fedd_create_experiment_local:
     31class fedd_experiment_control_local:
    3232    scripts = ["fed_bootstrap", "federate.sh", "smbmount.FreeBSD.pl",
    3333        "smbmount.Linux.pl", "make_hosts", "fed-tun.pl", "fed-tun.ucb.pl",
    3434        "fed_evrepeater", "rc.accounts.patch"]
    3535   
    36     def __init__(self,
    37             cert_file=None,
    38             cert_pwd=None,
    39             exp_stem="fed-stem",
    40             debug=False,
    41             muxmax=2,
    42             nthreads=2,
    43             randomize_experiments=False,
    44             scp_exec="/usr/bin/scp",
    45             scripts_dir="./",
    46             splitter=None,
    47             ssh_exec="/usr/bin/ssh",
    48             ssh_identity_file=None,
    49             ssh_keygen="/usr/bin/ssh-keygen",
    50             ssh_pubkey_file=None,
    51             ssh_type="rsa",
    52             tbmap=None,
    53             tclsh="/usr/local/bin/otclsh",
    54             tcl_splitter="/usr/testbed/lib/ns2ir/parse.tcl",
    55             trace_file=None,
    56             trusted_certs=None,
    57             ):
    58         self.scripts = fedd_create_experiment_local.scripts
    59         self.thread_with_rv = fedd_create_experiment_local.pooled_thread
    60         self.thread_pool = fedd_create_experiment_local.thread_pool
    61 
    62         self.cert_file = cert_file
    63         self.cert_pwd = cert_pwd
    64         self.exp_stem = exp_stem
    65         self.debug = debug
    66         self.muxmax = muxmax
    67         self.nthreads = nthreads
    68         self.randomize_experiments = randomize_experiments
    69         self.scp_exec = scp_exec
    70         self.scripts_dir = scripts_dir
    71         self.splitter = splitter
    72         self.ssh_exec=ssh_exec
    73         self.ssh_keygen = ssh_keygen
    74         self.ssh_identity_file = ssh_identity_file
    75         self.ssh_type = ssh_type
    76         self.state = { }
    77         self.tclsh = tclsh
    78         self.tcl_splitter = tcl_splitter
    79         self.tbmap = tbmap
    80         self.trace_file = trace_file
    81         self.trusted_certs=trusted_certs
    82 
    83         self.def_expstart = \
    84                 "sudo -H /bin/sh FEDDIR/fed_bootstrap >& /tmp/federate";
    85         self.def_mexpstart = "sudo -H FEDDIR/make_hosts FEDDIR/hosts";
    86         self.def_gwstart = \
    87                 "sudo -H FEDDIR/fed-tun.pl -f GWCONF>& /tmp/bridge.log";
    88         self.def_mgwstart = \
    89                 "sudo -H FEDDIR/fed-tun.pl -f GWCONF >& /tmp/bridge.log";
    90         self.def_gwimage = "FBSD61-TUNNEL2";
    91         self.def_gwtype = "pc";
    92 
    93 
    94         if ssh_pubkey_file:
    95             try:
    96                 f = open(ssh_pubkey_file, 'r')
    97                 self.ssh_pubkey = f.read()
    98                 f.close()
    99             except IOError:
    100                 raise service_error(service_error.internal,
    101                         "Cannot read sshpubkey")
    102 
    103         # Confirm federation scripts in the right place
    104         for s in self.scripts:
    105             if not os.path.exists(self.scripts_dir + "/" + s):
    106                 raise service_error(service_error.server_config,
    107                         "%s/%s not in local script dir" % (self.scripts_dir, s))
    10836    class thread_pool:
    10937        def __init__(self):
     
    13967            self.changed.notifyAll()
    14068            self.changed.release()
    141 
    142 
    14369
    14470    class pooled_thread(Thread):
     
    177103            if self.pdata:
    178104                self.pdata.terminate()
     105
     106    def __init__(self, config=None):
     107        self.scripts = fedd_experiment_control_local.scripts
     108        self.thread_with_rv = fedd_experiment_control_local.pooled_thread
     109        self.thread_pool = fedd_experiment_control_local.thread_pool
     110
     111        self.cert_file = None
     112        self.cert_pwd = None
     113        self.trusted_certs = None
     114
     115        if config.create_experiment_cert_file:
     116            self.cert_file = config.create_experiment_cert_file
     117            self.cert_pwd = config.create_experiment_cert_pwd
     118        if config.create_experiment_trusted_certs:
     119            self.trusted_certs = config.create_experiment_trusted_certs
     120
     121        if config.proxy_cert_file:
     122            if not self.cert_file:
     123                self.cert_file = config.proxy_cert_file
     124                self.cert_pwd = config.proxy_cert_pwd
     125        if config.proxy_trusted_certs:
     126            if not self.trusted_certs:
     127                self.trusted_certs = config.proxy_trusted_certs
     128
     129        if config.cert_file:
     130            if not self.cert_file:
     131                self.cert_file = config.cert_file
     132                self.cert_pwd = config.cert_pwd
     133        if config.trusted_certs:
     134            if not self.trusted_certs:
     135                self.trusted_certs = config.trusted_certs
     136
     137        self.exp_stem = "fed-stem"
     138        self.debug = config.create_debug
     139        self.muxmax = 2
     140        self.nthreads = 2
     141        self.randomize_experiments = False
     142        self.scp_exec = "/usr/bin/scp"
     143        self.scripts_dir = "/users/faber/testbed/federation"
     144        self.splitter = None
     145        self.ssh_exec="/usr/bin/ssh"
     146        self.ssh_keygen = "/usr/bin/ssh-keygen"
     147        self.ssh_identity_file = None
     148        # XXX
     149        self.ssh_pubkey_file = "/users/faber/.ssh/id_rsa.pub"
     150        self.ssh_type = "rsa"
     151        self.state = { }
     152        self.tclsh = "/usr/local/bin/otclsh"
     153        self.tcl_splitter = "/usr/testbed/lib/ns2ir/parse.tcl"
     154        self.tbmap = {
     155                'deter':'https://users.isi.deterlab.net:23235',
     156                'emulab':'https://users.isi.deterlab.net:23236',
     157                'ucb':'https://users.isi.deterlab.net:23237',
     158                }
     159        self.trace_file = sys.stderr
     160
     161        self.def_expstart = \
     162                "sudo -H /bin/sh FEDDIR/fed_bootstrap >& /tmp/federate";
     163        self.def_mexpstart = "sudo -H FEDDIR/make_hosts FEDDIR/hosts";
     164        self.def_gwstart = \
     165                "sudo -H FEDDIR/fed-tun.pl -f GWCONF>& /tmp/bridge.log";
     166        self.def_mgwstart = \
     167                "sudo -H FEDDIR/fed-tun.pl -f GWCONF >& /tmp/bridge.log";
     168        self.def_gwimage = "FBSD61-TUNNEL2";
     169        self.def_gwtype = "pc";
     170
     171
     172        if self.ssh_pubkey_file:
     173            try:
     174                f = open(self.ssh_pubkey_file, 'r')
     175                self.ssh_pubkey = f.read()
     176                f.close()
     177            except IOError:
     178                raise service_error(service_error.internal,
     179                        "Cannot read sshpubkey")
     180
     181        # Confirm federation scripts in the right place
     182        for s in self.scripts:
     183            if not os.path.exists(self.scripts_dir + "/" + s):
     184                raise service_error(service_error.server_config,
     185                        "%s/%s not in local script dir" % (self.scripts_dir, s))
     186
     187        self.soap_handlers = {\
     188                'Create': make_soap_handler(\
     189                        CreateRequestMessage.typecode,
     190                        getattr(self, "create_experiment"),
     191                        CreateResponseMessage,
     192                        "CreateResponseBody"),
     193                'Vtopo': make_soap_handler(\
     194                        VtopoRequestMessage.typecode,
     195                        getattr(self, "get_vtopo"),
     196                        VtopoResponseMessage,
     197                        "VtopoResponseBody"),
     198                'Vis': make_soap_handler(\
     199                        VisRequestMessage.typecode,
     200                        getattr(self, "get_vis"),
     201                        VisResponseMessage,
     202                        "VisResponseBody"),
     203                'Info': make_soap_handler(\
     204                        InfoRequestMessage.typecode,
     205                        getattr(self, "get_info"),
     206                        InfoResponseMessage,
     207                        "InfoResponseBody"),
     208                'Terminate': make_soap_handler(\
     209                        TerminateRequestMessage.typecode,
     210                        getattr(self, "terminate_experiment"),
     211                        TerminateResponseMessage,
     212                        "TerminateResponseBody"),
     213        }
     214
     215        self.xmlrpc_handlers = {\
     216                'Create': make_xmlrpc_handler(\
     217                        getattr(self, "create_experiment"),
     218                        "CreateResponseBody"),
     219                'Vtopo': make_xmlrpc_handler(\
     220                        getattr(self, "get_vtopo"),
     221                        "VtopoResponseBody"),
     222                'Vis': make_xmlrpc_handler(\
     223                        getattr(self, "get_vis"),
     224                        "VisResponseBody"),
     225                'Info': make_xmlrpc_handler(\
     226                        getattr(self, "get_info"),
     227                        "InfoResponseBody"),
     228                'Terminate': make_xmlrpc_handler(\
     229                        getattr(self, "terminate_experiment"),
     230                        "TerminateResponseBody"),
     231        }
     232
     233    def get_soap_services(self):
     234        return self.soap_handlers
     235
     236    def get_xmlrpc_services(self):
     237        return self.xmlrpc_handlers
    179238
    180239    def copy_file(self, src, dest, size=1024):
     
    650709        # No retry loop here.  Proxy servers must correctly authenticate
    651710        # themselves without help
     711
    652712        try:
    653713            ctx = fedd_ssl_context(self.cert_file,
     
    666726        msg.set_element_RequestAccessRequestBody(
    667727                pack_soap(msg, "RequestAccessRequestBody", req))
     728
    668729        try:
    669730            resp = port.RequestAccess(msg)
  • fedd/fedd_util.py

    r01073f7 r19cc408  
    329329        if keypath: os.remove(keypath)
    330330        if certpath: os.remove(certpath)
     331
     332
     333def make_soap_handler(typecode, method, constructor, body_name):
     334    """
     335    Generate the handler code to unpack and pack SOAP requests and responses
     336    and call the given method.
     337
     338    The code to decapsulate and encapsulate parameters encoded in SOAP is the
     339    same modulo a few parameters.  This is basically a stub compiler for
     340    calling a fedd service trhough a soap interface.  The parameters are the
     341    typecode of the request parameters, the method to call (usually a bound
     342    instance of a method on a fedd service providing class), the constructor of
     343    a response packet and the name of the body element of that packet.  The
     344    handler takes a ParsedSoap object (the request) and returns an instance of
     345    the class created by constructor containing the response.  Failures of the
     346    constructor or badly created constructors will result in None being
     347    returned.
     348    """
     349    def handler(ps, fid):
     350        req = ps.Parse(typecode)
     351
     352        msg = method(unpack_soap(req), fedid)
     353
     354        resp = constructor()
     355        set_element = getattr(resp, "set_element_%s" % body_name, None)
     356        if set_element and callable(set_element):
     357            try:
     358                set_element(pack_soap(resp, body_name, msg))
     359                return resp
     360            except (NameError, TypeError):
     361                return None
     362        else:
     363            return None
     364
     365    return handler
     366
     367def make_xmlrpc_handler(method, body_name, input_binaries=('fedid',),
     368        output_binaries=('fedid',)):
     369    """
     370    Generate the handler code to unpack and pack SOAP requests and responses
     371    and call the given method.
     372
     373    The code to marshall and unmarshall XMLRPC parameters to and from a fedd
     374    service is largely the same.  This helper creates such a handler.  The
     375    parameters are the method name, the name of the body struct that contains
     376    the response asn the list of fields that are encoded as Binary objects in
     377    the input and in the output.  A handler is created that takes the params
     378    response from an xm,lrpclib.loads on the incoming rpc and a fedid and
     379    responds with a hash representing the struct ro be returned to the other
     380    side.  On error None is returned.
     381    """
     382    def handler(params, fid):
     383        p = decapsulate_binaries(params[0], input_binaries)
     384        msg = method(p, fedid)
     385
     386        if msg != None:
     387            return encapsulate_binaries({ body_name: msg }, output_binaries)
     388        else:
     389            return None
     390
     391    return handler
Note: See TracChangeset for help on using the changeset viewer.