def _hello_timeout(self): act_time = time.time() act_to = abs(act_time - self.__last_update) if act_to > self.__timeout: if os.path.isdir(self.dir_name): self.log( "timeout of {} > {} reached".format( logging_tools.get_diff_time_str(act_to), logging_tools.get_diff_time_str(self.__timeout))) # if os.path.isfile if os.path.isfile(self.__action.split()[0]): _start_ok, log_lines = process_tools.submit_at_command(self.__action) for line in log_lines: self.log(line) else: self.log( "cannot submit '{}' (command not found)".format( self.__action.split()[0] ), logging_tools.LOG_LEVEL_WARN ) self.__last_update = act_time else: self.log("watch_directory {} no longer present, exiting".format(self.dir_name), logging_tools.LOG_LEVEL_WARN) self.__exit_flag = True
def _call(self, cur_inst): cstat, log_f = process_tools.submit_at_command( "/etc/init.d/nscd restart", 1) for log_line in log_f: self.log(log_line) if cstat: cur_inst.srv_com.set_result( "unable to submit at-command ({:d}, please check logs) to restart nscd" .format(cstat), server_command.SRV_REPLY_STATE_ERROR) else: cur_inst.srv_com.set_result("ok successfully restarted nscd", )
def __call__(self, srv_com, cur_ns): if "arguments:arg0" not in srv_com: srv_com.set_result( "call_script(): missing argument", server_command.SRV_REPLY_STATE_ERROR, ) else: script_name = srv_com["arguments:arg0"].text args = [] while "arguments:arg{:d}".format(len(args) + 1) in srv_com: args.append(srv_com["arguments:arg{:d}".format(len(args) + 1)].text) if os.path.isfile(script_name): if cur_ns.time: cur_ns.use_at = True info_str = "Starting script {} with {}: {}".format( script_name, logging_tools.get_plural("argument", len(args)), " ".join(args)) if cur_ns.use_at: info_str = "{} after {}".format( info_str, logging_tools.get_plural("minute", cur_ns.time)) self.log(info_str) if cur_ns.use_at: c_stat, log_lines = process_tools.submit_at_command( " ".join([script_name] + args), cur_ns.time) ipl = "\n".join(log_lines) else: c_stat, ipl = subprocess.getstatusoutput( " ".join([script_name] + args)) log_lines = ipl.split("\n") self.log(" - gave stat {:d} ({}):".format( c_stat, logging_tools.get_plural("log line", len(log_lines)))) for line in [s_line.strip() for s_line in log_lines]: self.log(" - {}".format(line)) if c_stat: srv_com.set_result( "problem while executing {}: {}".format( script_name, ipl), server_command.SRV_REPLY_STATE_ERROR) else: srv_com.set_result("script {} gave: {}".format( script_name, ipl)) else: srv_com.set_result("script {} not found".format(script_name), server_command.SRV_REPLY_STATE_ERROR)
def _call(self, cur_inst): rsyncd_cf_name = "/etc/rsyncd.conf" # print cur_inst.srv_com.pretty_print() # not_full = opt_dict.get("not_full", 0) def_lines = [ "uid = 0", "gid = 0", "read only = true", "use chroot = true", "transfer logging = false", "log format = %h %o %f %l %b", "log file = /var/log/rsyncd.log", "slp refresh = 300" ] # aer_post_lines ae_post_lines = [] try: in_lines = open("/etc/rsyncd.conf", "r").read().split("\n") except: pass else: aep_found = False for line in in_lines: if line.startswith("### AER-START-POST"): aep_found = True elif line.startswith("### AER-END-POST"): aep_found = False else: if aep_found: ae_post_lines.append(line) # empty image list im_list = [] if False: # not_full: if os.path.isfile(rsyncd_cf_name): im_re = re.compile("^.*\[(?P<image_name>.*)\].*$") com_re = re.compile("^\s*(?P<key>\S+)\s*=\s*(?P<value>.*)$") op_mode = "dl" def_lines, im_dict = ([], {}) for line in [ x.strip() for x in open(rsyncd_cf_name, "r").read().split("\n") if x.strip() ]: im_match = im_re.match(line) if im_match: image_name = im_match.group("image_name") op_mode = "if" im_dict[image_name] = {"path": "/", "exclude": ""} if op_mode == "dl": def_lines.append(line) elif op_mode == "if": com_match = com_re.match(line) if com_match: im_dict[image_name][com_match.group( "key")] = com_match.group("value") if "root" in im_dict: im_list = [("root", im_dict["root"].get("path", "/"), im_dict["root"]["exclude"] and im_dict["root"]["exclude"].split() or ["/proc/", "/sys/"])] num_images, num_rsync = (0, 0) if not im_list: im_list = [ ("root", "/", ["/proc/", "/sys/"]), ] # + opt_dict.get("exclude_dirs", "").split(":"))] extra_info = "" else: extra_info = " (root kept)" _server_idxs = [global_config["SERVER_IDX"]] # check for rsync rsync_dict = {} # for db_rec in call_params.dc.fetchall(): # rsync_dict.setdefault(db_rec["config_name"], {})[db_rec["name"]] = db_rec["value"] rsync_keys = sorted(rsync_dict.keys()) self.log("found %s: %s" % (logging_tools.get_plural( "rsync entry", len(rsync_keys)), ", ".join(rsync_keys) or "---")) # check for validity needed_keys = ["export_rsync", "rsync_name"] rsync_del_keys = [] for rs_key in rsync_keys: if len([ True for x in list(rsync_dict[rs_key].keys()) if x in needed_keys ]) != len(needed_keys): rsync_del_keys.append(rs_key) del rsync_dict[rs_key] if rsync_del_keys: self.log( "deleting %s because of missing keys: %s" % (logging_tools.get_plural( "rsync", len(rsync_del_keys)), ", ".join(rsync_del_keys)), logging_tools.LOG_LEVEL_WARN) rsync_keys = [x for x in rsync_keys if x not in rsync_del_keys] # self.dc.execute("SELECT * FROM image") im_list.extend([(cur_img.name, cur_img.source, []) for cur_img in image.objects.filter(Q(enabled=True))]) for im_name, im_source, im_exclude in im_list: num_images += 1 def_lines.extend( ["", "[%s]" % (im_name), " path = %s" % (im_source)]) if im_exclude: def_lines.append(" exclude = %s" % (" ".join(im_exclude))) for rsync_key in rsync_keys: rsync_stuff = rsync_dict[rsync_key] num_rsync += 1 def_lines.extend([ "", "[%s]" % (rsync_stuff["rsync_name"]), " path = %s" % (rsync_stuff["export_rsync"]) ]) if rsync_stuff.get("excluded_dirs", ""): def_lines.append(" exclude = %s" % (" ".join([ os.path.normpath(x) for x in rsync_stuff["excluded_dirs"].strip().split() ]))) def_lines.append("") # add post lines def_lines.extend( ["### AER-START-POST insert post-rsyncd.conf lines below"] + ae_post_lines + ["### AER-END-POST insert post-rsyncd.conf lines above", ""]) try: open(rsyncd_cf_name, "w").write("\n".join(def_lines)) except IOError: ret_state, ret_str = (server_command.SRV_REPLY_STATE_ERROR, "error creating %s" % (rsyncd_cf_name)) else: cstat, log_f = process_tools.submit_at_command( "/etc/init.d/rsyncd restart", 1) for log_line in log_f: self.log(log_line) if cstat: ret_state, ret_str = ( server_command.SRV_REPLY_STATE_ERROR, "error unable to submit at-command (%d, see logs) to restart rsyncd" % (cstat)) else: ret_state, ret_str = ( server_command.SRV_REPLY_STATE_OK, "ok wrote rsyncd-config file %s for %s%s, %s" % (rsyncd_cf_name, logging_tools.get_plural("image", num_images), extra_info, logging_tools.get_plural("rsync", num_rsync))) cur_inst.set_result(ret_str, ret_state)