Пример #1
0
    def __exec_salt(self, sid, module, parameter, res):
        utils.log("INFO", "Loading state ID '%s' from module '%s' ..."%(sid,module),('__exec_salt',self))

        # init
        cs = None
        result = FAIL

        # Watch prepare
        try:
            watch_map = self.__state_adaptor.watch
            utils.log("DEBUG", "StateAdaptor watch map loaded",('__exec_salt',self))
        except Exception:
            watch_map = WATCH
            utils.log("DEBUG", "Default watch map loaded",('__exec_salt',self))
        rerun = watch_map.get(module,{}).get("rerun",False)

        # dry-run
        if rerun is True:
            try:
                # state convert
                utils.log("INFO", "Dry-run: Begin to convert salt states...", ('__exec_salt', self))
                salt_states = self.__state_adaptor.convert(sid, module, copy.deepcopy(parameter))

                # exec salt states
                utils.log("INFO", "Dry-run: Begin to execute salt states...", ('__exec_salt', self))
                (result, comment, out_log) = self.__state_runner.exec_salt(salt_states)
            except Exception as err:
                utils.log("ERROR", str(err), ('__exec_salt',self))
                res['result'] = FAIL
                res['comment'] += "Internal error."
                res['out_log'] += "Dry-run: %s"%err
                return
            else:
                res['result'] = result
                res['comment'] += comment
                res['out_log'] += out_log

        # Enable watch
        try:
            parameter, watchs = self.__enable_watch(parameter, watch_map, module, sid)
        except Exception as e:
            err = "Internal error while watch process on watched file: %s"%(e)
            utils.log("ERROR", err,('__exec_salt',self))
            res['result'] = FAIL
            res['comment'] += "Internal error on watched file."
            res['out_log'] += "%s"%err
            return
        watch_valid = parameter.get("watch",False)

        # state exec
        if (not rerun) or (watchs and watch_valid):
            try:
                # state convert
                utils.log("INFO", "Begin to convert salt states...", ('__exec_salt', self))
                if self.__config['module']['mod_tag'] == "v2014-04-15":
                    salt_states = self.__state_adaptor.convert(sid, module, parameter, self.__state_runner.os_type)
                else:
                    salt_states = self.__state_adaptor.convert(sid, module, parameter)

                # exec salt state
                utils.log("INFO", "Begin to execute salt states...", ('__exec_salt', self))
                (result, comment, out_log) = self.__state_runner.exec_salt(salt_states)
            except Exception as err:
                utils.log("ERROR", str(err), ('__exec_salt',self))
                res['result'] = FAIL
                res['comment'] += "Internal error."
                res['out_log'] += "%s"%err
                return
            else:
                res['result'] = result
                res['comment'] += comment
                res['out_log'] += out_log

        # Persist watch
        if result and watchs:
            for watch in watchs:
                if watch_map[module].get("file"):
                    watch = os.path.join(watch,watch_map[module]['file'])
                try:
                    cs = Checksum(watch,sid,self.__config['global']['watch'])
                    if cs.update(edit=True,tfirst=watch_map[module].get("tfirst",True)) is not None:
                        utils.log("INFO", "New checksum stored for file %s"%(cs.filepath()),('__exec_salt',self))
                    else:
                        utils.log("INFO", "Checksum for file %s unchanged"%(cs.filepath()),('__exec_salt',self))
                except Exception as e:
                    utils.log("WARNING", "Failed to store new checksum for file %s: %s"%(cs.filepath(),e),('__exec_salt',self))

        # end log
        utils.log("INFO", "State ID '%s' from module '%s' done, result '%s'"%(sid,module,result),('__exec_salt',self))
        utils.log("DEBUG", "State out log='%s'"%(out_log),('__exec_salt',self))
        utils.log("DEBUG", "State comment='%s'"%(comment),('__exec_salt',self))
Пример #2
0
    def __exec_salt(self, sid, module, parameter, res):
        utils.log("INFO", "Loading state ID '%s' from module '%s' ..."%(sid,module),('__exec_salt',self))

        # init
        cs = None
        result = FAIL

        # Watch prepare
        try:
            watch_map = self.__state_adaptor.watch
            utils.log("DEBUG", "StateAdaptor watch map loaded",('__exec_salt',self))
        except Exception:
            watch_map = WATCH
            utils.log("DEBUG", "Default watch map loaded",('__exec_salt',self))
        rerun = watch_map.get(module,{}).get("rerun",False)

        # dry-run
        if rerun is True:
            try:
                # state convert
                utils.log("INFO", "Dry-run: Begin to convert salt states...", ('__exec_salt', self))
                salt_states = self.__state_adaptor.convert(sid, module, copy.deepcopy(parameter))

                # exec salt states
                utils.log("INFO", "Dry-run: Begin to execute salt states...", ('__exec_salt', self))
                (result, comment, out_log) = self.__state_runner.exec_salt(salt_states)
            except Exception as err:
                utils.log("ERROR", str(err), ('__exec_salt',self))
                res['result'] = FAIL
                res['comment'] += "Internal error."
                res['out_log'] += "Dry-run: %s"%err
                return
            else:
                res['result'] = result
                res['comment'] += comment
                res['out_log'] += out_log

        # Enable watch
        try:
            parameter, watchs = self.__enable_watch(parameter, watch_map, module, sid)
        except Exception as e:
            err = "Internal error while watch process on watched file: %s"%(e)
            utils.log("ERROR", err,('__exec_salt',self))
            res['result'] = FAIL
            res['comment'] += "Internal error on watched file."
            res['out_log'] += "%s"%err
            return
        watch_valid = parameter.get("watch",False)

        # state exec
        if (not rerun) or (watchs and watch_valid):
            try:
                # state convert
                utils.log("INFO", "Begin to convert salt states...", ('__exec_salt', self))
                if self.__config['module']['mod_tag'] == "v2014-04-15":
                    salt_states = self.__state_adaptor.convert(sid, module, parameter, self.__state_runner.os_type)
                else:
                    salt_states = self.__state_adaptor.convert(sid, module, parameter)

                # exec salt state
                utils.log("INFO", "Begin to execute salt states...", ('__exec_salt', self))
                (result, comment, out_log) = self.__state_runner.exec_salt(salt_states)
            except Exception as err:
                utils.log("ERROR", str(err), ('__exec_salt',self))
                res['result'] = FAIL
                res['comment'] += "Internal error."
                res['out_log'] += "%s"%err
                return
            else:
                res['result'] = result
                res['comment'] += comment
                res['out_log'] += out_log

        # Persist watch
        if result and watchs:
            for watch in watchs:
                if watch_map[module].get("file"):
                    watch = os.path.join(watch,watch_map[module]['file'])
                try:
                    cs = Checksum(watch,sid,self.__config['global']['watch'])
                    if cs.update(edit=True,tfirst=watch_map[module].get("tfirst",True)) is not None:
                        utils.log("INFO", "New checksum stored for file %s"%(cs.filepath()),('__exec_salt',self))
                    else:
                        utils.log("INFO", "Checksum for file %s unchanged"%(cs.filepath()),('__exec_salt',self))
                except Exception as e:
                    utils.log("WARNING", "Failed to store new checksum for file %s: %s"%(cs.filepath(),e),('__exec_salt',self))

        # end log
        utils.log("INFO", "State ID '%s' from module '%s' done, result '%s'"%(sid,module,result),('__exec_salt',self))
        utils.log("DEBUG", "State out log='%s'"%(out_log),('__exec_salt',self))
        utils.log("DEBUG", "State comment='%s'"%(comment),('__exec_salt',self))