def __enable_watch(self, parameter, watch_map, module, sid): watch_key = None watchs = None if watch_map.get(module): watch_key = (watch_map[module].get("file_key") if watch_map[module].get("file_key") else watch_map[module].get("dir_key")) if parameter and type(parameter) is dict and watch_key and parameter.get(watch_key): if hasattr(watch_map[module].get("action"), '__call__'): watchs = watch_map[module]["action"](self.__config,parameter) else: watchs = parameter.get(watch_key) if type(watchs) is str or type(watchs) is unicode: watchs = [watchs] if type(watchs) is list: utils.log("DEBUG", "Watched state detected",('__enable_watch',self)) if "watch" in parameter: del parameter["watch"] for watch in watchs: if watch_map[module].get("file"): watch = os.path.join(watch,watch_map[module]['file']) utils.log("DEBUG", "Watched file '%s' found"%(watch),('__enable_watch',self)) cs = Checksum(watch,sid,self.__config['global']['watch']) if cs.update(edit=False, tfirst=watch_map[module].get("tfirst",True)): parameter["watch"] = True utils.log("INFO","Watch event triggered, replacing standard action ...",('__enable_watch',self)) return parameter, watchs
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))