def config_exists(self, name): if not utils.is_filename_sane(name): return False if not os.path.isfile("%s/%s" % (userdir.CRMCONF_DIR, name)): common_err("%s: no such config" % name) return False return True
def do_new(self, context, *args): "usage: new [<shadow_cib>] [withstatus] [force] [empty]" argl = list(args) opt_l = utils.fetch_opts(argl, ["force", "--force", "withstatus", "empty"]) if len(argl) > 1: context.fatal_error("Unexpected argument(s): " + ' '.join(argl)) name = None if argl: name = argl[0] if not utils.is_filename_sane(name): context.fatal_error("Bad filename: " + name) if name in (constants.tmp_cib_prompt, constants.live_cib_prompt): context.fatal_error("Shadow name '%s' is not allowed" % (name)) del argl[0] constants.tmp_cib = False else: fd, fname = tmpfiles.create(dir=xmlutil.cib_shadow_dir(), prefix="shadow.crmsh_") name = os.path.basename(fname).replace("shadow.", "") constants.tmp_cib = True if "empty" in opt_l: new_cmd = "%s -e '%s'" % (self.extcmd, name) else: new_cmd = "%s -c '%s'" % (self.extcmd, name) if constants.tmp_cib or config.core.force or "force" in opt_l or "--force" in opt_l: new_cmd = "%s --force" % new_cmd if utils.ext_cmd(new_cmd) == 0: context.info("%s shadow CIB created" % name) self.do_use(context, name) if "withstatus" in opt_l: cib_status.load("shadow:%s" % name)
def do_new(self, context, *args): "usage: new [<shadow_cib>] [withstatus] [force] [empty]" argl = list(args) opt_l = utils.fetch_opts(argl, ["force", "--force", "withstatus", "empty"]) if len(argl) > 1: context.fatal_error("Unexpected argument(s): " + ','.join(argl)) name = None if argl: name = argl[0] if not utils.is_filename_sane(name): context.fatal_error("Bad filename: " + name) if name in (vars.tmp_cib_prompt, vars.live_cib_prompt): context.fatal_error("Shadow name '%s' is not allowed" % (name)) del argl[0] vars.tmp_cib = False else: fd, fname = tmpfiles.create(dir=xmlutil.cib_shadow_dir(), prefix="shadow.crmsh_") name = os.path.basename(fname).replace("shadow.", "") vars.tmp_cib = True if "empty" in opt_l: new_cmd = "%s -e '%s'" % (self.extcmd, name) else: new_cmd = "%s -c '%s'" % (self.extcmd, name) if vars.tmp_cib or config.core.force or "force" in opt_l or "--force" in opt_l: new_cmd = "%s --force" % new_cmd if utils.ext_cmd(new_cmd) == 0: context.info("%s shadow CIB created" % name) self.do_use(context, name) if "withstatus" in opt_l: cib_status.load("shadow:%s" % name)
def do_new(self, context, name, *args): "usage: new <config> <template> [<template> ...] [params name=value ...]" if not utils.is_filename_sane(name): return False if os.path.isfile("%s/%s" % (userdir.CRMCONF_DIR, name)): common_err("config %s exists; delete it first" % name) return False lt = LoadTemplate(name) rc = True mode = 0 params = {} for s in args: if mode == 0 and s == "params": params["id"] = name mode = 1 elif mode == 1: a = s.split('=') if len(a) != 2: syntax_err(args, context='new') rc = False else: params[a[0]] = a[1] elif not lt.load_template(s): rc = False if rc: lt.post_process(params) if not rc or not lt.write_config(name): return False self.curr_conf = name
def do_use(self, context, name='', withstatus=''): "usage: use [<shadow_cib>] [withstatus]" # check the name argument if name and not utils.is_filename_sane(name): context.fatal_error("Bad filename: " + name) if name and name != "live": if not os.access(xmlutil.shadowfile(name), os.F_OK): context.fatal_error("%s: no such shadow CIB" % name) if withstatus and withstatus != "withstatus": context.fatal_error("Expected 'withstatus', got '%s'" % (withstatus)) # If invoked from configure # take special precautions if not context.previous_level_is("cibconfig"): return self._use(name, withstatus) if not cib_factory.has_cib_changed(): ret = self._use(name, withstatus) # new CIB: refresh the CIB factory cib_factory.refresh() return ret saved_cib = utils.get_cib_in_use() self._use(name, '') # don't load the status yet if not cib_factory.is_current_cib_equal(silent=True): # user made changes and now wants to switch to a # different and unequal CIB; we refuse to cooperate context.error_message( "the requested CIB is different from the current one") if config.core.force: context.info("CIB overwrite forced") elif not utils.ask( "All changes will be dropped. Do you want to proceed?"): self._use(saved_cib, '') # revert to the previous CIB return False return self._use(name, withstatus) # now load the status too
def do_use(self, context, name='', withstatus=''): "usage: use [<shadow_cib>] [withstatus]" # check the name argument if name and not utils.is_filename_sane(name): context.fatal_error("Bad filename: " + name) if name and name != "live": if not os.access(xmlutil.shadowfile(name), os.F_OK): context.fatal_error("%s: no such shadow CIB" % name) if withstatus and withstatus != "withstatus": context.fatal_error("Expected 'withstatus', got '%s'" % (withstatus)) # If invoked from configure # take special precautions if not context.previous_level_is("cibconfig"): return self._use(name, withstatus) if not cib_factory.has_cib_changed(): ret = self._use(name, withstatus) # new CIB: refresh the CIB factory cib_factory.refresh() return ret saved_cib = utils.get_cib_in_use() self._use(name, '') # don't load the status yet if not cib_factory.is_current_cib_equal(silent=True): # user made changes and now wants to switch to a # different and unequal CIB; we refuse to cooperate context.error_message("the requested CIB is different from the current one") if config.core.force: context.info("CIB overwrite forced") elif not utils.ask("All changes will be dropped. Do you want to proceed?"): self._use(saved_cib, '') # revert to the previous CIB return False return self._use(name, withstatus) # now load the status too
def do_reset(self, context, name): "usage: reset <shadow_cib>" if not utils.is_filename_sane(name): context.fatal_error("Bad filename: " + name) if utils.ext_cmd("%s -r '%s'" % (self.extcmd, name)) == 0: context.info("copied live CIB to %s" % name) else: context.fatal_error("failed to copy live CIB to %s" % name)
def do_delete(self, context, name): "usage: delete <shadow_cib>" if not utils.is_filename_sane(name): context.fatal_error("Bad filename: " + name) if utils.get_cib_in_use() == name: context.fatal_error("%s shadow CIB is in use" % name) if utils.ext_cmd("%s -D '%s' --force" % (self.extcmd, name)) == 0: context.info("%s shadow CIB deleted" % name) else: context.fatal_error("failed to delete %s shadow CIB" % name)
def do_commit(self, context, name=None): "usage: commit [<shadow_cib>]" if name and not utils.is_filename_sane(name): context.fatal_error("Bad filename: " + name) if not name: name = utils.get_cib_in_use() if not name: context.fatal_error("There is nothing to commit") if utils.ext_cmd("%s -C '%s' --force" % (self.extcmd, name)) == 0: context.info("committed '%s' shadow CIB to the cluster" % name) else: context.fatal_error("failed to commit the %s shadow CIB" % name) if vars.tmp_cib: self._use('', '')
def do_commit(self, context, name=None): "usage: commit [<shadow_cib>]" if name and not utils.is_filename_sane(name): context.fatal_error("Bad filename: " + name) if not name: name = utils.get_cib_in_use() if not name: context.fatal_error("There is nothing to commit") if utils.ext_cmd("%s -C '%s' --force" % (self.extcmd, name)) == 0: context.info("committed '%s' shadow CIB to the cluster" % name) else: context.fatal_error("failed to commit the %s shadow CIB" % name) if constants.tmp_cib: self._use('', '')
def test_sanity(): sane_paths = ['foo/bar', 'foo', '/foo/bar', 'foo0', 'foo_bar', 'foo-bar', '0foo', '.foo', 'foo.bar'] insane_paths = ['#foo', 'foo?', 'foo*', 'foo$', 'foo[bar]', 'foo`', "foo'", 'foo/*'] for p in sane_paths: assert utils.is_path_sane(p) for p in insane_paths: assert not utils.is_path_sane(p) sane_filenames = ['foo', '0foo', '0', '.foo'] insane_filenames = ['foo/bar'] for p in sane_filenames: assert utils.is_filename_sane(p) for p in insane_filenames: assert not utils.is_filename_sane(p) sane_names = ['foo'] insane_names = ["f'o"] for n in sane_names: assert utils.is_name_sane(n) assert utils.is_value_sane(n) for n in insane_names: assert not utils.is_name_sane(n) assert not utils.is_value_sane(n)
def do_import(self, context, infile, name=None): "usage: import {<file>|<number>} [<shadow>]" if name and not utils.is_filename_sane(name): context.fatal_error("Bad filename: " + name) # where's the input? if not os.access(infile, os.F_OK): if "/" in infile: context.fatal_error(str(infile) + ": no such file") infile = self._find_pe(context, infile) if not name: name = os.path.basename(infile).replace(".bz2", "") if not xmlutil.pe2shadow(infile, name): context.fatal_error("Error copying PE file to shadow: %s -> %s" % (infile, name)) # use the shadow and load the status from there return self.do_use(context, name, "withstatus")
def do_session(self, context, subcmd=None, name=None): "usage: session [{save|load|delete} <name> | pack [<name>] | update | list]" self._init_source() if not subcmd: print "current session: %s" % self.current_session return True # verify arguments if subcmd not in ("save", "load", "pack", "delete", "list", "update"): common_err("unknown history session subcmd: %s" % subcmd) return False if name: if subcmd not in ("save", "load", "pack", "delete"): syntax_err(subcmd, context='session') return False if not utils.is_filename_sane(name): return False elif subcmd not in ("list", "update", "pack"): syntax_err(subcmd, context='session') return False elif subcmd in ("update", "pack") and not self.current_session: common_err("need to load a history session before update/pack") return False # do work if not name: # some commands work on the existing session name = self.current_session rc = crm_report().manage_session(subcmd, name) # set source appropriately if rc and subcmd in ("save", "load"): options.history = crm_report().get_source() crm_report().prepare_source() self.current_session = name elif rc and subcmd == "delete": if name == self.current_session: common_info( "current history session deleted, setting source to live") self._set_source("live") return rc
def do_session(self, context, subcmd=None, name=None): "usage: session [{save|load|delete} <name> | pack [<name>] | update | list]" self._init_source() if not subcmd: print "current session: %s" % self.current_session return True # verify arguments if subcmd not in ("save", "load", "pack", "delete", "list", "update"): common_err("unknown history session subcmd: %s" % subcmd) return False if name: if subcmd not in ("save", "load", "pack", "delete"): syntax_err(subcmd, context='session') return False if not utils.is_filename_sane(name): return False elif subcmd not in ("list", "update", "pack"): syntax_err(subcmd, context='session') return False elif subcmd in ("update", "pack") and not self.current_session: common_err("need to load a history session before update/pack") return False # do work if not name: # some commands work on the existing session name = self.current_session rc = crm_report.manage_session(subcmd, name) # set source appropriately if rc and subcmd in ("save", "load"): options.history = crm_report.get_source() crm_report.prepare_source() self.current_session = name elif rc and subcmd == "delete": if name == self.current_session: common_info("current history session deleted, setting source to live") self._set_source("live") return rc