Beispiel #1
0
    def post(self):

        parser = reqparse.RequestParser()
        data = request.get_json()
        etcdman = EtcdManagement()
        resp_list = []
        for dict_obj in data:
            try:
                etcdman.write(new_key=dict_obj["key"], value=dict_obj["value"])
            except:
                resp_list.append({
                    "key": dict_obj["key"],
                    "status": "failed",
                    "code": 1000
                })
                continue
            resp_list.append({
                "key":
                dict_obj["key"],
                "status":
                "success",
                "mod_revision":
                etcdman.get_mod_revision(dict_obj['key'])
            })
        return jsonify(resp_list)
Beispiel #2
0
    def post(self):

        mod_revision = None
        parser = reqparse.RequestParser()

        parser.add_argument('key', required=True)
        parser.add_argument('value', required=True)

        args = parser.parse_args()
        logger = Logger(filename = "kvs_wrapper", \
          logger_name = "Etcd_kvs post", \
          dirname="/aux1/ockvsman/logs/")
        logger.info("Handle params from web.")
        etcd_manager = EtcdManagement()
        try:
            etcd_manager.write(new_key=args['key'], value=args['value'])
            mod_revision = etcd_manager.get_mod_revision(args['key'])
        except:
            logger.info("Request can't be executed, Error code => 1000")
            return {
                'message': "Request can't be registered",
                "code": 1000
            }, 500

        logger.info(
            "Key was registered successfully, mod_revision => {}".format(
                mod_revision))
        logger.clear_handler()
        return {
            'message': 'Key was registered successfully',
            'mod_revision': mod_revision
        }, 200
Beispiel #3
0
    def post(self):

        parser = reqparse.RequestParser()
        parser.add_argument('etcd_key', required=True)
        args = parser.parse_args()

        etcd_manager = EtcdManagement()
        key_data = None
        try:
            key_data = etcd_manager.read_key(args['etcd_key'])
            return {
                "value": key_data,
                "mod_revision": etcd_manager.get_mod_revision(args['etcd_key'])
            }, 200
        except:
            return {'message': "Key can't be get!!", "code": 1001}, 404
Beispiel #4
0
    def post(self):

        parser = reqparse.RequestParser()

        parser.add_argument('key', required=True)
        parser.add_argument('value', required=True)

        args = parser.parse_args()
        etcd_manager = EtcdManagement()
        try:
            etcd_manager.write(new_key=args['key'], value=args['value'])
            mod_revision = etcd_manager.get_mod_revision(args['key'])
            return {
                'message': 'Key was registered successfully',
                "mod_revision": mod_revision
            }, 200

        except:
            return {'message': "Request can't be executed", "code": 1000}, 500
Beispiel #5
0
class ProcessConfig():
    """
	- Processes a given etcd config key:
	- checks if local node is affected by this config change
	- generates new config file
	- replaces markers with their respective values
	- executes the set of commands related to this host
	"""
    def __init__(self, event, status_path, initial):
        log = Logger(filename = "confman", \
                                                      logger_name = "Process Config", \
                                                      dirname="/aux1/occonfman/")
        self.etcd = EtcdManagement()

        self.initial = initial
        if (self.initial):
            self.event_value = json.loads(event[0].decode("utf-8"))
            self.event_revision = str(
                self.etcd.get_mod_revision(event[1].key.decode("utf-8")))
            print(self.event_revision)
            log.info(f"Event revision is: {self.event_revision}")
        else:
            self.event_value = json.loads(event.value)
            self.event_revision = event.mod_revision

        self.hostname = os.uname()[1]
        self.status_path = status_path
        log.clear_handler()

    def check_affected(self, hosts):
        """ 
		If node hostname is not within 
		the etcd config's command keys,
		then the config change is ignored.
		"""
        if self.hostname in hosts.keys():
            return 0

        return 1

    def process_config(self):

        log = Logger(filename = "confman", \
                                                      logger_name = "Process Config", \
                                                      dirname="/aux1/occonfman/")
        if (self.check_affected(self.event_value["commands"])):
            print('This change does not concern my host: {}'.format(
                self.hostname))
            log.info("This config change does not concern my host {}".format(
                self.hostname))
            log.clear_handler()
            return ''
        log.info("Config change: {}".format(self.event_value["path"]))
        config_path = self.event_value["path"]
        content = self.apply_markers(self.event_value["content"])
        self.write_config(config_path, content)
        res = self.execute_command()
        log.clear_handler()

        return (res)

    def apply_markers(self, content):
        """
		Using jinja2 template engine to replace markers within the config content
		"""

        log = Logger(filename = "confman", \
                                                      logger_name = "Process Config", \
                                                      dirname="/aux1/occonfman/")
        content_ready = content
        if "markers" in self.event_value.keys():
            for host in self.event_value["markers"]:
                if self.hostname in self.event_value["markers"].keys():
                    template = jinja2.Template(content)
                    log.info("Replacing markers for {}".format(self.hostname))
                    log.clear_handler()
                    content_ready = template.render(
                        self.event_value["markers"][self.hostname])
        log.clear_handler()

        return content_ready

    def write_config(self, config_path, content):

        log = Logger(filename = "confman", \
                                                      logger_name = "Process Config", \
                                                      dirname="/aux1/occonfman/")
        try:
            with open(config_path, 'w') as conf:
                conf.write(content)
                conf.close()
        except:
            print(f"Could not write config file: { config_path }")
            log.error("Could not write config file {}".format(config_path))

        log.clear_handler()

    def execute_command(self):
        """
		Executes all commands found in commands object
		Returns the output of executed commands
		"""

        results = {}
        log = Logger(filename = "confman", \
                                                      logger_name = "Process Config", \
                                                      dirname="/aux1/occonfman/")

        if self.hostname in self.event_value["commands"].keys():
            for command in self.event_value["commands"][self.hostname]:
                log.info("Executing command {}".format(command))
                res = run(command,
                          stdout=PIPE,
                          stderr=PIPE,
                          universal_newlines=True,
                          shell=True)
                log.info("Command output: {}".format(res.stdout))
                log.clear_handler()
                results[command] = res.stdout

        self.return_status(results)
        log.clear_handler()

        return results

    def return_status(self, results):
        """
		Writes status key in etcd
		Status key containes the executed command output as value
		"""

        log = Logger(filename = "confman", \
                                                      logger_name = "Process Config", \
                                                      dirname="/aux1/occonfman/")
        stat_path = self.status_path.rstrip(
            '/') + self.event_value["path"] + '/' + str(
                self.event_revision) + '/' + self.hostname
        print(stat_path)
        log.info("Writing status key: {} , value: {}".format(
            stat_path, results))
        log.clear_handler()
        self.etcd.write(stat_path, str(results))