Esempio n. 1
0
def check_possibility(fileRequest=None, messageReq=None):
    message = AdvMessage()
    if fileRequest is not None and os.path.isfile(fileRequest):
        message.from_json(fileRequest)
    else:
        if messageReq is not None:
            message = messageReq
        else:
            print("no file")
            exit(1)

    memoryRequired = 0
    cpuRequred = 0
    for component in message.components:
        memoryRequired += component['resources']['memory']
        cpuRequred += component['resources']['cpu']

    values = psutil.virtual_memory()
    available = values.available >> 20  # display in MB format

    if memoryRequired < available and cpuRequred < psutil.cpu_count(
    ) and 1 == 2:
        return True
    else:
        return False
Esempio n. 2
0
def send_delete_app(classLogger, fileRequest=None, app_id=None):
    logger = classLogger.getLogger()
    messaging_adv = Messaging_adv("localhost")
    if fileRequest is not None:
        message_del = AdvMessage()
        message_del.from_json(fileRequest.name)
        message_del.type = 'del'
        messaging_adv.send_user_req(message_del,
                                    local=True)  # send message to controller
        try:
            APP_RUNNING.pop(message_del.app_name)  # remove from app running
        except KeyError:
            print("Key not found")
        logger.submit_message(
            'INFO', "Send request for delete application describe in file: " +
            fileRequest.name)
    else:
        app_del = APP_RUNNING[app_id]
        messaging_adv.send_user_req(app_del,
                                    local=True)  # send message to controller
        try:
            APP_RUNNING.pop(app_del.app_name)  # remove app running
        except KeyError:
            print("Key not found")
        logger.submit_message(
            'INFO', "Send request for delete application: " + app_del.app_name)
Esempio n. 3
0
def create_json_with_request(app, parameters, hostname, path):
    id = app
    if id == 'VLC':
        owner = hostname
        components = []
        componentGUI = Component(
            name="video-gui",
            image="andreamora/imagerepo:vlcnoentrypointwithplugins",
            priority="2",
            resources=Resource(memory=256, cpu=0.5).to_dict(),
            blacklist=["node2"],
            parameter=None)
        components.append(componentGUI.to_dict())
        componentStream = Component(
            name="video-streamer",
            image="andreamora/imagerepo:vlcnoentrypointwithplugins",
            priority="1",
            resources=Resource(memory=1024, cpu=2).to_dict(),
            blacklist=["node2"],
            parameter=path)
        components.append(componentStream.to_dict())

        message = AdvMessage(ID=id, owner=owner, components=components)
        with open('message_ADV.json', 'w') as fp:
            json.dump(message.to_dict(), fp, indent=4)

        return message
    else:
        return "not implemented"
Esempio n. 4
0
def send_request_app(classLogger, fileRequest, app, parameter, path=None):
    logger = classLogger.getLogger()
    messaging_adv = Messaging_adv("localhost")
    if fileRequest is not None:
        if (check_possibility(fileRequest=fileRequest.name) is True):
            # this node can run the entire application
            # TODO: vedere come farlo
            # logger.submit_message('INFO', "Application describe in file " + fileRequest.name + " is run locally")
            # print("Application describe in file " + fileRequest.name + " is run locally")
            # message = AdvMessage()
            # message.from_json(fileRequest.name)
            # # run local
            # return runKubeImpl_local(message)
            pass
        else:
            # this node cannot run the entire application so send request
            print("Send request for application describe in file: " +
                  fileRequest.name)
            logger.submit_message(
                'INFO', "Send request for application describe in file: " +
                fileRequest.name)

            request_message = AdvMessage()
            request_message.from_json(fileRequest.name)
            messaging_adv.send_user_req(
                request_message, local=True)  # send message to controller
            APP_RUNNING[request_message.app_name] = request_message
            return True
    else:
        if app and parameter is not None:
            message = create_request(app, "add", parameter,
                                     socket.gethostname(), path)
            if (check_possibility(messageReq=message) is True):
                # this node can run the entire application
                # TODO: vedere come farlo
                # pod=runKubeImpl_local(message)
                # if pod is not False:
                #     logger.submit_message('INFO', "Application is run locally")
                #     print("Application is run locally")
                #     return pod
                # else:
                #     return False
                pass
            else:
                # this node cannot run the entire application so send request
                print("Send request for application: " + message.app_name)
                logger.submit_message(
                    'INFO',
                    "Send request for application: " + message.app_name)

                messaging_adv.send_user_req(
                    message, local=True)  # send message to controller
                APP_RUNNING[message.app_name] = message
                return True
        else:
            return None
Esempio n. 5
0
def main():
    message = create_json_with_request()

    # create exchange for federation
    # exchange_name = 'drone_exchange'
    # messaging.create_exchange(connection,exchange=exchange_name, exchange_type='direct')

    message = AdvMessage()
    message.from_json(
        path.join(path.dirname(__file__), "Request/advertisement.json"))

    messaging_adv.send_adv(message)
Esempio n. 6
0
def test_produce():

    # for test:
    message = AdvMessage()
    message.from_json(path.join(path.dirname(__file__), configuration.REQUEST))
    messaging_adv.send_user_req(message, local=True)

    sleep(5)

    message_res = resultMessage()
    message_res.from_json(
        path.join(path.dirname(__file__), configuration.RESULT))
    messaging_result.send_result(message_res, local=True)
Esempio n. 7
0
def create_request(app, type, parameters, hostname, path):
    app_name = app + uuid.uuid4().hex[:6].upper()
    type = type
    if app == 'VLC':
        base_node = hostname
        components = []

        func = Function(
            image="andreamora/imagerepo:vlcnoentrypointwithplugins",
            resources=Resource(memory=256, cpu=0.5).to_dict())

        componentGUI = Component(name="video-gui",
                                 function=func.to_dict(),
                                 boot_dependencies=["2"],
                                 nodes_blacklist=["node2"],
                                 nodes_whitelist=["node2"],
                                 parameter=None)
        components.append(componentGUI.to_dict())
        componentStream = Component(name="video-streamer",
                                    function=func.to_dict(),
                                    boot_dependencies=["2"],
                                    nodes_blacklist=["node2"],
                                    nodes_whitelist=["node2"],
                                    parameter=path)
        components.append(componentStream.to_dict())

        message = AdvMessage(app_name=app_name,
                             base_node=base_node,
                             components=components,
                             type=type)
        return message
    else:
        return "not implemented"
Esempio n. 8
0
    def send_adv_from_file(self, file, local=False):
        # connect
        connection_id = self._messaging.connect()

        if local:
            exchange = ''
        else:
            exchange = self.configuration.EXCHANGE

        message = AdvMessage()
        message.from_json(file)

        self._messaging.send_message(connection_id,
                                     self.configuration.QUEUE_ADV, message,
                                     exchange)

        self._messaging.disconnect(connection_id)
Esempio n. 9
0
def create_json_with_request():

    id = "VLC"
    owner = "node1"
    components = []
    component1 = Component("video",
                           "image:tag",
                           "1",
                           resources=Resource(memory=256, cpu=0.5).to_dict(),
                           blacklist=["node1"])
    components.append(component1.to_dict())
    component2 = Component("stream",
                           "image:tag",
                           "2",
                           resources=Resource(memory=1024, cpu=1).to_dict(),
                           blacklist=["node1"])
    components.append(component2.to_dict())

    message = AdvMessage(ID=id, owner=owner, components=components)
    with open('mes.json', 'w') as fp:
        json.dump(message.to_dict(), fp, indent=4)

    return message
Esempio n. 10
0
 def _message_callback_adv(ch, method, properties, body):
     print(" [x] Received " + body.decode())
     self = Messaging_adv()
     message = AdvMessage()
     message.parse_dict(json.loads(body.decode()))
     self._message_handler(message)
Esempio n. 11
0
def runKube(classA, fileRequest, app, parameter, path):
    logger = classA.getLogger()
    if fileRequest is not None:
        if (check_possibility(fileRequest=fileRequest.name) is True):
            logger.submit_message(
                'INFO', "Application describe in file " + fileRequest.name +
                " is run locally")
            print("Application describe in file " + fileRequest.name +
                  " is run locally")
            message = AdvMessage()
            message.from_json(fileRequest.name)
            # run local
            return runKubeImpl_local(message)
        else:
            # this node cannot run the entire application so send request
            print("Send request for application describe in file: " +
                  fileRequest.name)
            logger.submit_message(
                'INFO', "Application describe in file " + fileRequest.name +
                " is run locally")
            adv_messaging.send_from_file_adv(
                fileRequest.name
            )  # send message adv to other nodes for drone orchestration

            # TODO: Dove aspetto il risultato di drone?
            messaging_res = Messaging_result()
            messaging_res.start_consume_result()

            message = AdvMessage()
            message.from_json(fileRequest.name)
            # run using drone result
            return runKubeImpl_distribuitedPods(message)

            # runKubeExample()
    else:
        if app and parameter is not None:
            message = create_json_with_request(app, parameter,
                                               socket.gethostname(), path)
            app_adv_messaging_OLD.send_adv(
                message
            )  # send message adv to other nodes for drone orchestration
            if (check_possibility(messageReq=message) is True):
                # run local
                pod = runKubeImpl_local(message)
                if pod is not False:
                    logger.submit_message('INFO', "Application is run locally")
                    print("Application is run locally")
                    return pod
                else:
                    return False
            else:
                # this node cannot run the entire application so send request
                logger.submit_message(
                    'INFO', "Send request for application: " + message.ID)
                print("Send request for application: " + message.ID)
                app_adv_messaging_OLD.send_adv(
                    message
                )  # send message adv to other nodes for drone orchestration

                # TODO: Dove aspetto il risultato di drone?
                # run using drone result
                return runKubeImpl_distribuitedPods(message)

                # runKubeExample()
        else:
            return None