Ejemplo n.º 1
0
class _RemoteServer(_Server):
    """ API through which it is possible to invoke services directly on other remote servers or clusters.
    """
    repr_to_avoid = ['api_password']

    def __init__(self, cluster_id, cluster_name, name, preferred_address, port,
                 crypto_use_tls, api_password, up_status):
        self.cluster_id = cluster_id
        self.cluster_name = cluster_name
        self.name = name
        self.preferred_address = preferred_address
        self.port = port
        self.crypto_use_tls = crypto_use_tls
        self.api_password = api_password
        self.up_status = up_status
        self.address = 'http{}://{}:{}'.format(
            's' if self.crypto_use_tls else '', self.preferred_address,
            self.port)
        self.invoker = AnyServiceInvoker(self.address, '/zato/internal/invoke',
                                         (api_user, self.api_password))
        self.ping_address = '{}/zato/ping'.format(self.address)
        self.ping_timeout = 2

# ################################################################################################################################

    def invoke(self, service, request=None, *args, **kwargs):

        # Ping the remote server to quickly find out if it is still available
        requests_get(self.ping_address, timeout=self.ping_timeout)

        response = self.invoker.invoke(service, request, *args, **kwargs)

        if response.ok:
            return response.data
        else:
            raise Exception(response.details)

# ################################################################################################################################

    def invoke_all_pids(self, service, request=None, *args, **kwargs):

        # Ping the remote server to quickly find out if it is still available
        requests_get(self.ping_address, timeout=self.ping_timeout)

        return self.invoker.invoke(service,
                                   request,
                                   all_pids=True,
                                   *args,
                                   **kwargs)

# ################################################################################################################################

    def invoke_async(self, service, request=None, *args, **kwargs):
        return self.invoker.invoke_async(service, request, *args, **kwargs)
Ejemplo n.º 2
0
    def handle(self):

        # Let's prepare as much as we can upfront.
        sec_def = self.worker_store.basic_auth_get('admin.invoke').config
        channel = self.worker_store.get_channel_plain_http('admin.invoke.json')
        out = {}

        with closing(self.odb.session()) as session:
            for item in server_list(session, self.server.cluster_id, False):
                server_info = out.setdefault(item.name, {})
                server_info['cluster_name'] = item.cluster_name

                server_info['up_mod_date'] = item.up_mod_date.isoformat() if item.up_status == SERVER_UP_STATUS.RUNNING else None
                server_info['last_join_mod_date'] = item.last_join_mod_date.isoformat() if \
                    item.last_join_status == SERVER_JOIN_STATUS.ACCEPTED else None

                for name in 'id', 'name', 'bind_host', 'bind_port', 'last_join_status', 'last_join_mod_by', 'up_status':
                    server_info[name] = getattr(item, name)

                if item.up_status == SERVER_UP_STATUS.RUNNING:

                    client = AnyServiceInvoker(
                        'http://{}:{}'.format(item.bind_host, item.bind_port),
                        channel.url_path, (sec_def.username, sec_def.password))
                    response = client.invoke('zato.info.get-server-info')
                    if response.ok:
                        response = loads(response.inner.text)['zato_service_invoke_response']['response'].decode('base64')
                        response = loads(response)['response']
                        server_info['info'] = loads(response['info'])
                    else:
                        self.logger.warn(response)

        self.response.content_type = 'application/json'
        self.response.payload = dumps(out)
Ejemplo n.º 3
0
Archivo: server.py Proyecto: znavy/zato
class _RemoteServer(_Server):
    """ API through which it is possible to invoke services directly on other remote servers or clusters.
    """
    def __init__(self, cluster_id, cluster_name, name, preferred_address, port, crypto_use_tls, api_password):
        self.cluster_id = cluster_id
        self.cluster_name = cluster_name
        self.name = name
        self.preferred_address = preferred_address
        self.port = port
        self.crypto_use_tls = crypto_use_tls
        self.api_password = api_password
        self.address = 'http{}://{}:{}'.format(
            's' if self.crypto_use_tls else '', self.preferred_address, self.port)

        self.invoker = AnyServiceInvoker(self.address, '/zato/internal/invoke', (api_user, self.api_password))

    def invoke(self, service, request=None, *args, **kwargs):
        response = self.invoker.invoke(service, request, *args, **kwargs)

        if response.ok:
            return response.data
        else:
            raise Exception(response.details)

    def invoke_async(self, service, request=None, *args, **kwargs):
        return self.invoker.invoke_async(service, request, *args, **kwargs)
Ejemplo n.º 4
0
    def handle(self):

        # Let's prepare as much as we can upfront.
        sec_def = self.worker_store.basic_auth_get('admin.invoke').config
        channel = self.worker_store.get_channel_plain_http('admin.invoke.json')
        out = {}

        with closing(self.odb.session()) as session:
            for item in server_list(session, self.server.cluster_id, None, None, False):
                server_info = out.setdefault(item.name, {})
                server_info['cluster_name'] = item.cluster_name

                server_info['up_mod_date'] = item.up_mod_date.isoformat() if item.up_status == SERVER_UP_STATUS.RUNNING else None
                server_info['last_join_mod_date'] = item.last_join_mod_date.isoformat() if \
                    item.last_join_status == SERVER_JOIN_STATUS.ACCEPTED else None

                for name in 'id', 'name', 'bind_host', 'bind_port', 'last_join_status', 'last_join_mod_by', 'up_status':
                    server_info[name] = getattr(item, name)

                if item.up_status == SERVER_UP_STATUS.RUNNING:

                    client = AnyServiceInvoker(
                        'http://{}:{}'.format(item.bind_host, item.bind_port),
                        channel.url_path, (sec_def.username, sec_def.password))
                    response = client.invoke('zato.info.get-server-info')
                    if response.ok:
                        response = loads(response.inner.text)['zato_service_invoke_response']['response'].decode('base64')
                        response = loads(response)['response']
                        server_info['info'] = loads(response['info'])
                    else:
                        self.logger.warn(response)

        self.response.content_type = 'application/json'
        self.response.payload = dumps(out)
Ejemplo n.º 5
0
class ChannelUtils:
    def __init__(self, logger=None):
        try:
            self.address = 'http://localhost:11223'
            self.path = '/zato/admin/invoke'
            self.auth = ('admin.invoke', os.environ["ZATOPASS"])
            self.client = AnyServiceInvoker(self.address, self.path, self.auth)
            self.channeldict = None
            self.loggerobj = logger
        except KeyError:
            print "Please set the environment variable ZATOPASS"
            self.log('Please set the environment variable ZATOPASS')
            sys.exit(1)

    def get_channels(self):
        self.channeldict = {}

        payload = {"cluster_id": 1, "connection": "channel", "transport": "plain_http"}
        response = self.client.invoke('zato.http-soap.get-list', payload=payload)

        if response.ok:
            d = response.data
            for c in d:
                self.channeldict[c[ u'name']]=c[ u'id']
            return self.channeldict
        else:
            self.log('ERROR getting channel dictionary')
            return {}

    def delete_channel(self, id):
        self.channeldict = {}

        payload = {"id": id}
        response = self.client.invoke('zato.http-soap.delete', payload=payload)

        if response.ok:
            pass
        else:
            print(response.details)
            return {}

    def add_channel(self, channelname, servicename, url, method, skipifexists=False):
        if self.channeldict is None:
            self.get_channels()

        if skipifexists == False and channelname in self.channeldict:
            self.delete_channel(id=self.channeldict[channelname])

        payload = {}
        payload[u'connection'] = u'channel'
        payload[u'name'] = channelname
        payload[u'cluster_id'] = 1
        payload[u'transport'] = u'plain_http'
        payload[u'url_path'] = url
        payload[u'service'] = servicename
        payload[u'method'] = method
        payload[u'data_format'] = u'json'
        payload[u'is_active']=True
        payload[u'is_internal']=False

        response = self.client.invoke('zato.http-soap.create', payload=payload)

        if response.ok:
            pass
        else:
            print(response.details)
            return {}

    def log(self, msg):
        if self.loggerobj is not None:
            self.loggerobj.info(msg)
        else:
            print msg

    def configure_service_file(self, filepath):
        source = ''
        self.log('Processing {}'.format(filepath))
        with open(filepath, 'r') as f:
            source = f.read()
        p = ast.parse(source)

        classes = [node.name for node in ast.walk(p) if isinstance(node, ast.ClassDef)]
        for classname in classes:
            if classname != 'SimpleIO':
                classobj = load_from_file(filepath, classname)

                namemethod = getattr(classobj, 'get_name')
                servicename = namemethod()

                channelmethod = getattr(classobj, 'get_channels')
                channels = channelmethod()

                for channel in channels:
                    self.log('Adding channel {}'.format(channel['name']))
                    self.add_channel(channel['name'], servicename, channel['path'], channel['method'])