Esempio n. 1
0
    def on_put(self, req, res):
        """
        PUT: /status?token={None}
        """
        if not Definition.get_str_token() in req.params:
            res.body = "Token is required."
            res.content_type = "String"
            res.status = falcon.HTTP_401
            return

        if req.params[Definition.get_str_token()] == Setting.get_token():
            raw = str(req.stream.read(), 'UTF-8')
            data = eval(raw)

            LookUpTable.update_worker(data)
            SysOut.debug_string("Update worker status ({0})".format(
                data[Definition.get_str_node_name()]))

            res.body = "Okay"
            res.content_type = "String"
            res.status = falcon.HTTP_200
        else:
            res.body = "Invalid token ID."
            res.content_type = "String"
            res.status = falcon.HTTP_401
Esempio n. 2
0
    def on_put(self, req, res):
        """
        PUT: /status?token={None}
        """
        if not Definition.get_str_token() in req.params:
            res.body = "Token is required."
            res.content_type = "String"
            res.status = falcon.HTTP_401
            return

        if Definition.Docker.get_str_finished() in req.params:
            # a container is shutting down, update containers
            # TODO: add some kind of safety mechanism to really make sure no new requests have been sent to this container before acknowledging removal?
            if LookUpTable.remove_container(
                    req.params.get(
                        Definition.Container.get_str_con_image_name()),
                    req.params.get(Definition.Docker.get_str_finished())):
                format_response_string(res, falcon.HTTP_200,
                                       "Container successfully removed")
                # NOTE: container will terminate as soon as it reads this response!
            else:
                format_response_string(
                    res, falcon.HTTP_400,
                    "Could not remove container from table!")
                # NOTE: container will continue as before when it reads this response!
            return

        if req.params[Definition.get_str_token()] == Setting.get_token():
            data = json.loads(
                str(req.stream.read(req.content_length or 0), 'utf-8'))

            LookUpTable.update_worker(data)
            SysOut.debug_string("Update worker status ({0})".format(
                data[Definition.get_str_node_name()]))

            res.body = "Okay"
            res.content_type = "String"
            res.status = falcon.HTTP_200
        else:
            res.body = "Invalid token ID."
            res.content_type = "String"
            res.status = falcon.HTTP_401

        return
Esempio n. 3
0
    def read_cfg_from_file():
        from harmonicIO.general.services import Services
        if not Services.is_file_exist('harmonicIO/worker/configuration.json'):
            SysOut.terminate_string(
                'harmonicIO/worker/configuration.json does not exist')
        else:
            with open('harmonicIO/worker/configuration.json', 'rt') as t:
                import json
                cfg = json.loads(t.read())

                try:
                    from harmonicIO.general.definition import Definition
                    # Check for the json structure
                    if  Definition.get_str_node_name() in cfg and \
                        Definition.get_str_node_port() in cfg and \
                        Definition.get_str_data_port_range() in cfg and \
                        Definition.get_str_idle_time() in cfg and \
                        Definition.get_str_master_addr() in cfg and \
                        Definition.get_str_master_port() in cfg and \
                        Definition.get_str_node_external_addr() in cfg and \
                        Definition.get_str_node_internal_addr():
                        # Check port number is int or not
                        if not isinstance(cfg[Definition.get_str_node_port()],
                                          int):
                            SysOut.terminate_string(
                                "Node port must be integer.")
                        elif not isinstance(
                                cfg[Definition.get_str_data_port_range()],
                                list):
                            SysOut.terminate_string("Port range must be list.")
                        elif not (isinstance(cfg[Definition.get_str_data_port_range()][0], int) and \
                                  isinstance(cfg[Definition.get_str_data_port_range()][1], int)):
                            SysOut.terminate_string(
                                "Port range must be integer.")
                        elif not isinstance(
                                cfg[Definition.get_str_master_port()], int):
                            SysOut.terminate_string(
                                "Master port must be integer.")
                        elif len(cfg[
                                Definition.get_str_data_port_range()]) != 2:
                            SysOut.terminate_string(
                                "Port range must compost of two elements: start, stop."
                            )
                        elif not isinstance(
                                cfg[Definition.get_str_idle_time()], int):
                            SysOut.terminate_string(
                                "Idle time must be integer.")
                        elif cfg[Definition.get_str_data_port_range()][0] > \
                             cfg[Definition.get_str_data_port_range()][1]:
                            SysOut.terminate_string(
                                "Start port range must greater than stop port range."
                            )
                        else:
                            Setting.set_node_addr()
                            import multiprocessing
                            Setting.__node_name = cfg[
                                Definition.get_str_node_name()].strip()
                            Setting.__node_port = cfg[
                                Definition.get_str_node_port()]
                            Setting.__node_data_port_start = cfg[
                                Definition.get_str_data_port_range()][0]
                            Setting.__node_data_port_stop = cfg[
                                Definition.get_str_data_port_range()][1]
                            Setting.__std_idle_time = cfg[
                                Definition.get_str_idle_time()]
                            Setting.__master_addr = cfg[
                                Definition.get_str_master_addr()].strip()
                            Setting.__master_port = cfg[
                                Definition.get_str_master_port()]
                            Setting.__node_external_addr = cfg[
                                Definition.get_str_node_external_addr()].strip(
                                ).lower()

                            # Check for auto node name
                            if Setting.__node_name.lower() == "auto":
                                # Get node name from host name
                                import socket
                                Setting.__node_name = socket.gethostname()

                            # Check for overriding node address
                            if cfg[Definition.get_str_node_internal_addr()] and \
                               cfg[Definition.get_str_node_internal_addr()] != "auto":
                                # Set node name automatically from hostname
                                from harmonicIO.general.services import Services

                                if Services.is_valid_ipv4(cfg[Definition.get_str_node_internal_addr()]) or \
                                   Services.is_valid_ipv6(cfg[Definition.get_str_node_internal_addr()]):
                                    Setting.__node_internal_addr = cfg[
                                        Definition.get_str_node_internal_addr(
                                        )]

                            # Check for node address validity
                            if Setting.get_node_external_addr() != "none":
                                from harmonicIO.general.services import Services

                                if Services.is_valid_ipv4(Setting.get_node_external_addr()) or \
                                   Services.is_valid_ipv6(Setting.get_node_external_addr()):
                                    SysOut.out_string(
                                        "By pass request with external address."
                                    )
                                else:
                                    SysOut.terminate_string(
                                        "Invaliid external ip address!")
                            else:
                                Setting.__node_external_addr = None

                            SysOut.out_string("Load setting successful.")
                    else:
                        SysOut.terminate_string(
                            "Required parameters are not present.")
                except Exception as e:
                    print(e)
                    SysOut.terminate_string(
                        "Invalid data in configuration file.")
Esempio n. 4
0
    def read_cfg_from_file():
        from harmonicIO.general.services import Services, SysOut
        if not Services.is_file_exist('harmonicIO/master/configuration.json'):
            SysOut.terminate_string(
                'harmonicIO/master/configuration.json does not exist!')
        else:
            with open('harmonicIO/master/configuration.json', 'rt') as t:
                import json
                cfg = json.loads(t.read())

                try:
                    from harmonicIO.general.definition import Definition
                    # Check for the json structure
                    if  Definition.get_str_node_name() in cfg and \
                        Definition.get_str_node_port() in cfg and \
                        Definition.get_str_master_addr() in cfg and \
                        Definition.get_str_data_port_range() in cfg and \
                        Definition.get_str_idle_time() in cfg:
                        # Check port number is int or not
                        if not isinstance(cfg[Definition.get_str_node_port()],
                                          int):
                            SysOut.terminate_string(
                                "Node port must be integer!")
                        if not isinstance(
                                cfg[Definition.get_str_master_addr()], str):
                            SysOut.terminate_string(
                                "Master address must be string!")
                        elif not isinstance(
                                cfg[Definition.get_str_data_port_range()],
                                list):
                            SysOut.terminate_string("Port range must be list!")
                        elif not (isinstance(cfg[Definition.get_str_data_port_range()][0], int) and \
                                  isinstance(cfg[Definition.get_str_data_port_range()][1], int)):
                            SysOut.terminate_string(
                                "Port range must be integer!")
                        elif len(cfg[
                                Definition.get_str_data_port_range()]) != 2:
                            SysOut.terminate_string(
                                "Port range must compost of two elements: start, stop!"
                            )
                        elif not isinstance(
                                cfg[Definition.get_str_idle_time()], int):
                            SysOut.terminate_string(
                                "Idle time must be integer!")
                        elif cfg[Definition.get_str_data_port_range()][0] > \
                             cfg[Definition.get_str_data_port_range()][1]:
                            SysOut.terminate_string(
                                "Start port range must greater than stop port range!"
                            )
                        else:
                            Setting.__node_name = cfg[
                                Definition.get_str_node_name()].strip()
                            Setting.__node_port = cfg[
                                Definition.get_str_node_port()]
                            Setting.__node_data_port_start = cfg[
                                Definition.get_str_data_port_range()][0]
                            Setting.__node_data_port_stop = cfg[
                                Definition.get_str_data_port_range()][1]
                            Setting.__std_idle_time = cfg[
                                Definition.get_str_idle_time()]
                            SysOut.out_string("Load setting successful.")

                        try:
                            if cfg[Definition.get_str_master_addr()].lower(
                            ) == "auto":
                                Setting.__node_addr = Services.get_host_name_i(
                                )
                                SysOut.out_string(
                                    "Assigning master ip address automatically."
                                )
                            elif Services.is_valid_ipv4(cfg[Definition.get_str_master_addr()]) or \
                                 Services.is_valid_ipv6(cfg[Definition.get_str_master_addr()]):
                                Setting.set_node_addr(
                                    cfg[Definition.get_str_master_addr()])
                            else:
                                SysOut.terminate_string(
                                    "Invalid master IP address format!")

                        except:
                            SysOut.terminate_string(
                                "Cannot assign IP address to the master!")

                    else:
                        SysOut.terminate_string(
                            "Invalid data in configuration file.")
                except:
                    SysOut.terminate_string(
                        "Invalid data in configuration file.")