def _get_iscsi_config(node): path = iscsi_get_config_path_node(node) parser = iscsidParser() dop = DictOp() dop.addconf(MODULE, parser.read_conf(path)) return dop
def _new_lines(self, search_key, new_key): try: attrs = self.dop.get(self._module, search_key, with_attr=True) action = attrs['action'] iscomment = attrs['comment'] val = attrs['value'] except: action = self.dop.action(self._module, search_key) iscomment = self.dop.iscomment(self._module, search_key) val = self.dop.get(self._module, search_key) pass #print val dop = DictOp() dop.addconf('__', {}) if action == "delete": dop.add('__', new_key, val) dop.delete('__', new_key) elif action == "set": dop.set('__', new_key, val) else: dop.add('__', new_key, val) if iscomment is True: dop.comment('__', new_key) #preprint_r(dop.getconf('__')) new_lines = self._value_to_lines(dop.getconf('__')) #print "\n".join(new_lines) return new_lines
def _pre_write_conf(self, conf_arr={}): # Change permission to be able to read/write data kss group. if os.path.exists(COLLECTD_DATA_DIR): if os.getuid() == 0: r_chgrp(COLLECTD_DATA_DIR, KARESANSUI_GROUP) r_chmod(COLLECTD_DATA_DIR, "g+rwx") r_chmod(COLLECTD_DATA_DIR, "o-rwx") dop = DictOp() dop.addconf("__", conf_arr) if dop.isset("__", ["python"]) is True: dop.cdp_unset("__", ["python", "Plugin", "python", "@ORDERS"], multiple_file=True) orders = [] orders.append(['Encoding']) orders.append(['LogTraces']) orders.append(['Interactive']) orders.append(['ModulePath']) orders.append(['Import']) orders.append(['Module']) dop.cdp_set("__", ["python", "Plugin", "python", "@ORDERS"], orders, is_opt_multi=True, multiple_file=True) return dop.getconf("__")
def __init__(self): self.dop = DictOp() self.dop.addconf(self._module, {}) self.parser = Parser() self.base_parser_name = self.parser.__class__.__name__ pass
def convert_old_style(self, conf_arr): dop = DictOp() dop.addconf("__",{}) orders = [] for cnt in range(0,20): try: try: exec("action = conf_arr['ADDRESS%d']['action']" % cnt) except: action = None exec("address = conf_arr['ADDRESS%d']['value']" % cnt) exec("netmask = conf_arr['NETMASK%d']['value']" % cnt) exec("gateway = conf_arr['GATEWAY%d']['value']" % cnt) target = "%s/%s" % (address,netmask,) net = NetworkAddress(target) try: target = net.cidr except: pass dop.add("__",[target],gateway) if action == "delete": dop.delete("__",[target]) orders.append([target]) except: pass if len(orders) != 0: dop.add("__",['@ORDERS'],orders) return dop.getconf("__")
def __init__(self, paths=[]): self.dop = DictOp() self.dop.addconf(self._module, {}) self.set_source_file(paths) self.opt_uni = ['PIDFile'] self.opt_multi = ['LoadPlugin', 'Include'] self.opt_sect = ['Directory', 'VirtualHost', 'View']
def __init__(self): self.dop = DictOp() self.dop.addconf(self._module,{}) self.base_parser_name = Parser.__name__ self.encoding = "UTF-8" pass
def __init__(self): self.dop = DictOp() self.dop.addconf(self._module, {}) self.parser = Parser() self.exclude_device_regex = "\.old|\.bak|\.rpm.*|lo|\.20" self.exclude_device_regex = "\.old|\.bak|\.rpm.*|\.20" self.base_parser_name = self.parser.__class__.__name__ pass
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(1) dop = DictOp() modules = opts.module.split(":") files = opts.file.split(":") retval = True cnt = 0 for _mod in modules: _file = files[cnt] try: exec( "from karesansui.lib.parser.%s import %sParser as Parser" % ( _mod, _mod, )) self.up_progress(5) parser = Parser() self.up_progress(5) if opts.raw is True: raw_str = "Config_Raw_%s = {}\n" % (_mod, ) for _src in parser.source_file(): raw_str = "%sConfig_Raw_%s['%s'] = \"\"\"%s\"\"\"\n" % ( raw_str, _mod, _src, open(_src).read()) ConfigFile(_file).write(raw_str) else: # 設定ファイルの読み込み extra_args = {} extra_args["include"] = opts.include conf_arr = parser.read_conf(extra_args=extra_args) dop.addconf(_mod, conf_arr) #dop.preprint_r(_mod) # 辞書配列ファイルに書き込み _var = "Config_Dict_%s" % (_mod, ) if opts.php is True: _str = python_dict_to_php_array( dop.getconf(_mod), _var) ConfigFile(_file).write(_str) else: ConfigFile(_file).write("%s = %s\n" % ( _var, str(dop.getconf(_mod)), )) finally: cnt = cnt + 1 self.up_progress(10) return True
def __init__(self): self.dop = DictOp() self.dop.addconf(self._module,{}) self.parser = Parser() self.parser.set_delim("=") self.parser.set_new_delim("=") self.parser.set_multidefine(False) self.base_parser_name = self.parser.__class__.__name__ pass
def __init__(self): self.dop = DictOp() self.dop.addconf(self._module,{}) self.parser = Parser() self.parser.set_delim("[ \t]+") self.parser.set_new_delim("\t") self.parser.set_comment("#") self.base_parser_name = self.parser.__class__.__name__ pass
def convert_new_style(self, conf_arr): dop = DictOp() dop.addconf("__",{}) orders = [] try: old_orders = conf_arr['@ORDERS']['value'] except: old_orders = [] cnt = 0 for _k,_v in conf_arr.iteritems(): if _k[0] != "@": net = NetworkAddress(_k) try: ipaddr = net.ipaddr netmask = net.netmask gateway = _v['value'] try: action = _v['action'] except: action = None try: index = old_orders.index([_k]) except: index = cnt dop.add("__",["ADDRESS%d" % index],ipaddr) if action == "delete": dop.delete("__",["ADDRESS%d" % index]) orders.insert(cnt*3+0,["ADDRESS%d" % index]) dop.add("__",["NETMASK%d" % index],netmask) if action == "delete": dop.delete("__",["NETMASK%d" % index]) orders.insert(cnt*3+1,["NETMASK%d" % index]) dop.add("__",["GATEWAY%d" % index],gateway) if action == "delete": dop.delete("__",["GATEWAY%d" % index]) orders.insert(cnt*3+2,["GATEWAY%d" % index]) cnt = cnt + 1 except: pass if len(orders) != 0: dop.add("__",['@ORDERS'],orders) return dop.getconf("__")
def __init__(self): self.dop = DictOp() self.dop.addconf(self._module,{}) self.exclude_device_regex = "\.old|\.bak|\.rpm.*|\.20" self.parser = Parser() self.config_style = self.detect_config_style() if self.config_style == "old": self.parser.set_delim(" via ") self.parser.set_new_delim(" via ") else: self.parser.set_delim("=") self.parser.set_new_delim("=") self.base_parser_name = self.parser.__class__.__name__ pass
def __init__(self): self.dop = DictOp() self.dop.addconf(self._module, {}) self.parser = Parser() self.parser.set_delim("[ \t]+") self.parser.set_new_delim("\t") self.parser.set_comment("#") self.base_parser_name = self.parser.__class__.__name__ from karesansui.lib.parser.collectd import collectdParser collectdp = collectdParser() self.parser.set_opt_uni(collectdp.opt_uni) self.parser.set_opt_multi(collectdp.opt_multi) self.parser.set_opt_sect(collectdp.opt_sect) pass
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) dev_list = comma_split(opts.dev) if len(dev_list) < 2: # TRANSLATORS: # bondingするためのdeviceが少ないです raise KssCommandOptException( 'ERROR: Small device for bonding. - dev=%s' % (opts.dev)) interface_list = get_ifconfig_info() for dev in dev_list: if dev not in interface_list: raise KssCommandOptException( 'ERROR: Bonding target device not found. - dev=%s' % (dev)) if opts.primary not in dev_list: raise KssCommandOptException( 'ERROR: Primary device not found in bonding device. - primary=%s dev=%s' % (opts.primary, opts.dev)) exist_bond_max_num = -1 exist_bond_list = get_ifconfig_info("regex:^bond") for bond_name in exist_bond_list.keys(): try: num = int(bond_name.replace("bond", "")) except ValueError: continue if exist_bond_max_num < num: exist_bond_max_num = num self.up_progress(10) physical_bond_name = "bond%s" % (exist_bond_max_num + 1) bridge_bond_name = "bondbr%s" % (exist_bond_max_num + 1) bond_options = '"mode=%s primary=%s miimon=%s"' % ( opts.mode, opts.primary, BONDING_CONFIG_MII_DEFAULT) self.up_progress(10) dop = DictOp() ifcfg_parser = ifcfgParser() modprobe_parser = modprobe_confParser() dop.addconf("ifcfg", ifcfg_parser.read_conf()) if dop.getconf("ifcfg") == {}: raise KssCommandException('Failure read network config file.') dop.addconf("modprobe_conf", modprobe_parser.read_conf()) if dop.getconf("modprobe_conf") == {}: raise KssCommandException('Failure read modprobe config file.') self.up_progress(10) eth_conf_copykey = [ "HWADDR", "BOOTPROTO", "ONBOOT", "USERCTL", ] bond_conf_nocopykey = [ "TYPE", "HWADDR", "MACADDR", "ETHTOOL_OPTS", "ESSID", "CHANNEL", ] self.up_progress(10) for dev in dev_list: conf = dop.get("ifcfg", dev) if dev == opts.primary: primary_conf = copy.deepcopy(conf) dop.unset("ifcfg", dev) dop.set("ifcfg", [dev, "DEVICE"], conf["DEVICE"]["value"]) for key in eth_conf_copykey: if key in conf: dop.set("ifcfg", [dev, key], conf[key]["value"]) dop.set("ifcfg", [dev, "MASTER"], physical_bond_name) dop.set("ifcfg", [dev, "SLAVE"], "yes") dop.set("ifcfg", [dev, "BOOTPROTO"], "none") if dop.get("ifcfg", "p%s" % (dev)): hwaddr = dop.get("ifcfg", ["p%s" % (dev), "HWADDR"]) if hwaddr: dop.set("ifcfg", [dev, "HWADDR"], hwaddr) dop.unset("ifcfg", "p%s" % (dev)) for key in bond_conf_nocopykey: if key in primary_conf: del primary_conf[key] dop.set("ifcfg", bridge_bond_name, primary_conf) dop.set("ifcfg", [bridge_bond_name, "DEVICE"], bridge_bond_name) dop.set("ifcfg", [bridge_bond_name, "TYPE"], "Bridge") dop.set("ifcfg", [physical_bond_name, "DEVICE"], physical_bond_name) dop.set("ifcfg", [physical_bond_name, "BRIDGE"], bridge_bond_name) dop.set("ifcfg", [physical_bond_name, "BOOTPROTO"], "none") dop.set("ifcfg", [physical_bond_name, "ONBOOT"], dop.get("ifcfg", [bridge_bond_name, "ONBOOT"])) dop.set("ifcfg", [physical_bond_name, "BONDING_OPTS"], bond_options) self.up_progress(10) dop.set("modprobe_conf", ["alias", physical_bond_name], "bonding") for dev in dev_list: if os.path.isfile("%s/ifcfg-%s" % (NETWORK_IFCFG_DIR, dev)): copy_file("%s/ifcfg-%s" % (NETWORK_IFCFG_DIR, dev), VENDOR_DATA_BONDING_EVACUATION_DIR) if os.path.isfile("%s/ifcfg-p%s" % (NETWORK_IFCFG_DIR, dev)): move_file("%s/ifcfg-p%s" % (NETWORK_IFCFG_DIR, dev), VENDOR_DATA_BONDING_EVACUATION_DIR) if ifcfg_parser.write_conf(dop.getconf("ifcfg")) is False: raise KssCommandException('Failure write network config file.') if modprobe_parser.write_conf(dop.getconf("modprobe_conf")) is False: raise KssCommandException('Failure write modprobe config file.') self.up_progress(10) # # Delete bridge device # bridge_list = get_bridge_info() for dev in dev_list: if dev in bridge_list: ifdown_cmd = ( NETWORK_IFDOWN_COMMAND, dev, ) (ifdown_rc, ifdown_res) = execute_command(ifdown_cmd) if ifdown_rc != 0: raise KssCommandException( 'Failure stop interface. interface:%s' % (dev)) for brif in bridge_list[dev]: brctl_delif_cmd = ( NETWORK_BRCTL_COMMAND, "delif", dev, brif, ) (brctl_rc, brctl_res) = execute_command(brctl_delif_cmd) if brctl_rc != 0: raise KssCommandException( 'Failure delete bridge port. bridge:%s port:%s' % (dev, brif)) brctl_delbr_cmd = ( NETWORK_BRCTL_COMMAND, "delbr", dev, ) (brctl_rc, brctl_res) = execute_command(brctl_delbr_cmd) if brctl_rc != 0: raise KssCommandException( 'Failure delete bridge. bridge:%s' % (dev, brif)) self.up_progress(10) # # Restart network # network_restart_cmd = ( NETWORK_COMMAND, "restart", ) (net_rc, net_res) = execute_command(network_restart_cmd) if net_rc != 0: raise KssCommandException('Failure restart network.') self.logger.info("Created bonding device. - dev=%s bond=%s" % (opts.dev, bridge_bond_name)) print >> sys.stdout, _("Created bonding device. - dev=%s bond=%s" % (opts.dev, bridge_bond_name)) return True
def __init__(self, paths=[]): self.dop = DictOp() self.dop.addconf(self._module, {}) self.set_source_file(paths)
try: self.dop.addconf("parser",{}) self.dop.set("parser",[PARSER_HOSTS_CONF],conf_arr) #self.dop.preprint_r("parser") arr = self.dop.getconf("parser") self.parser.write_conf(arr,dryrun=dryrun) except: pass return retval """ """ if __name__ == '__main__': """Testing """ parser = hostsParser() dop = DictOp() dop.addconf("dum",parser.read_conf()) dop.add("dum",['key'],['value',[['comment foo','comment bar'],'comment hoge']]) print dop.cdp_get("dum",['key']) print dop.cdp_get_pre_comment("dum",['key']) print dop.cdp_get_post_comment("dum",['key']) print dop.cdp_set("dum",['key'],"value2") print dop.cdp_set_pre_comment("dum",['key'],["comment foo2","comment bar2","a"]) print dop.cdp_set_post_comment("dum",['key'],"comment fuga") conf = dop.getconf("dum") preprint_r(conf) parser.write_conf(conf,dryrun=True)
def __init__(self): self.dop = DictOp() self.dop.addconf(self._module,{}) self.parser = Parser() self.parser.set_delim("[ \t]+") self.parser.set_new_delim("\t") self.parser.set_comment("#") self.base_parser_name = self.parser.__class__.__name__ self.opt_uni = ['Hostname', 'FQDNLookup', 'BaseDir', 'PIDFile', 'Target', 'Host', 'Key', 'LogLevel', 'Plugin', 'Subject', 'SMTPPort', 'SMTPServer', 'URL', 'Type', 'Chain'] self.opt_multi = ['Include', 'LoadPlugin', 'Collect', 'DriverOption', 'GetCapacity', 'GetSnapshot', 'Irq', 'JVMArg', 'Listen', 'PreCacheChain', 'PostCacheChain', 'Query', 'Recipient', 'Sensor', 'Server', 'WatchAdd'] self.opt_sect = ['Plugin', 'LoadPlugin', 'Threshold', 'Type', 'Chain', 'Data', 'Database', 'Directory', 'Disks', 'File', 'Host', 'Key', 'Match', 'Metric', 'Module', 'Page', 'Query', 'Recursor', 'Result', 'Router', 'Rule', 'Server', 'System', 'Table', 'Target', 'URL', 'View', 'VolumePerf', 'VolumeUsage', 'WAFL'] self.parser.set_opt_uni(self.opt_uni) self.parser.set_opt_multi(self.opt_multi) self.parser.set_opt_sect(self.opt_sect) pass
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) config_path = iscsi_get_config_path(opts.host, opts.iqn, opts.port, opts.tpgt) parser = iscsidParser() dop = DictOp() dop.addconf("new", parser.read_conf(config_path)) self.up_progress(10) dop.cdp_set("new", ISCSI_CONFIG_KEY_AUTH_METHOD, opts.auth) if opts.auth == ISCSI_CONFIG_VALUE_AUTH_METHOD_CHAP: password = "" if opts.password is not None: password = opts.password elif opts.password_file is not None and is_readable( opts.password_file): try: fp = open(opts.password_file, "r") try: fcntl.lockf(fp.fileno(), fcntl.LOCK_SH) try: password = fp.readline().strip("\n") finally: fcntl.lockf(fp.fileno(), fcntl.LOCK_UN) self.up_progress(10) finally: fp.close() except: raise KssCommandException('Failed to read file. - target host=%s password_file=%s' \ % (opts.host,opts.password_file)) try: os.remove(opts.password_file) except: raise KssCommandException('Failed to remove file. - target host=%s password_file=%s' \ % (opts.host,opts.password_file)) dop.cdp_set("new", ISCSI_CONFIG_KEY_AUTH_USER, opts.user) dop.cdp_set("new", ISCSI_CONFIG_KEY_AUTH_PASSWORD, password) else: dop.comment("new", ISCSI_CONFIG_KEY_AUTH_USER) dop.comment("new", ISCSI_CONFIG_KEY_AUTH_PASSWORD) self.up_progress(10) if opts.autostart: dop.cdp_set("new", ISCSI_CONFIG_KEY_SATRTUP, ISCSI_CONFIG_VALUE_SATRTUP_ON) else: dop.cdp_set("new", ISCSI_CONFIG_KEY_SATRTUP, ISCSI_CONFIG_VALUE_SATRTUP_OFF) self.up_progress(10) parser.write_conf(dop.getconf("new"), config_path) self.up_progress(30) self.logger.info("Updated iSCSI node. - host=%s iqn=%s" % (opts.host, opts.iqn)) print >> sys.stdout, _("Updated iSCSI node. - host=%s iqn=%s") % ( opts.host, opts.iqn) return True
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) exist_bond_list = get_ifconfig_info("regex:^bond") if opts.dev not in exist_bond_list: raise KssCommandOptException( 'Target bonding device not found. target=%s' % opts.dev) self.up_progress(10) dop = DictOp() ifcfg_parser = ifcfgParser() dop.addconf("ifcfg", ifcfg_parser.read_conf()) if dop.getconf("ifcfg") == {}: raise KssCommandException('Failure read network config file.') if dop.get("ifcfg", opts.dev) is False: raise KssCommandException('Target device ifcfg file not found.') self.up_progress(10) restore_dev_list = [] for dev in dop.getconf("ifcfg").keys(): if dop.get("ifcfg", [dev, "MASTER"]) == opts.dev: restore_dev_list.append(dev) self.up_progress(10) if opts.succession is True: bond_bridge = dop.get("ifcfg", [opts.dev, "BRIDGE"]) bond_dev = opts.dev if bond_bridge: bond_dev = bond_bridge ipaddr = dop.get("ifcfg", [bond_dev, "IPADDR"]) netmask = dop.get("ifcfg", [bond_dev, "NETMASK"]) gateway = dop.get("ifcfg", [bond_dev, "GATEWAY"]) bonding_opts = dop.get("ifcfg", [opts.dev, "BONDING_OPTS"]) bonding_opts = bonding_opts.strip('"') primary_dev = None for combination in bonding_opts.split(" "): if re.match("primary", combination): (key, val) = combination.split("=") val = val.strip() primary_dev = val self.up_progress(10) for restore_dev in restore_dev_list: if move_file( "%s/ifcfg-%s" % (VENDOR_DATA_BONDING_EVACUATION_DIR, restore_dev), NETWORK_IFCFG_DIR) is False: raise KssCommandException('Failure restore ifcfg file.') if os.path.isfile( "%s/ifcfg-p%s" % (VENDOR_DATA_BONDING_EVACUATION_DIR, restore_dev)): if move_file( "%s/ifcfg-p%s" % (VENDOR_DATA_BONDING_EVACUATION_DIR, restore_dev), NETWORK_IFCFG_DIR) is False: raise KssCommandException('Failure restore ifcfg file.') self.up_progress(10) if opts.succession is True and primary_dev is not None: dop = DictOp() ifcfg_parser = ifcfgParser() dop.addconf("ifcfg", ifcfg_parser.read_conf()) if dop.getconf("ifcfg") == {}: raise KssCommandException('Failure read network config file.') if ipaddr: dop.set("ifcfg", [primary_dev, "IPADDR"], ipaddr) if netmask: dop.set("ifcfg", [primary_dev, "NETMASK"], netmask) if gateway: dop.set("ifcfg", [primary_dev, "GATEWAY"], gateway) if ifcfg_parser.write_conf(dop.getconf("ifcfg")) is False: raise KssCommandException('Failure write network config file.') self.up_progress(10) remove_file("%s/ifcfg-%s" % (NETWORK_IFCFG_DIR, opts.dev)) self.up_progress(10) dop = DictOp() modprobe_parser = modprobe_confParser() dop.addconf("modprobe_conf", modprobe_parser.read_conf()) if dop.getconf("modprobe_conf") == {}: raise KssCommandException('Failure read modprobe config file.') dop.unset("modprobe_conf", ["alias", opts.dev]) if modprobe_parser.write_conf(dop.getconf("modprobe_conf")) is False: raise KssCommandException('Failure write modprobe config file.') self.up_progress(10) # # Delete bridge device # bridge_list = get_bridge_info() bond_bridge = None for bridge in bridge_list: if opts.dev in bridge_list[bridge]: bond_bridge = bridge if bond_bridge: ifdown_cmd = ( NETWORK_IFDOWN_COMMAND, bond_bridge, ) (ifdown_rc, ifdown_res) = execute_command(ifdown_cmd) if ifdown_rc != 0: raise KssCommandException( 'Failure stop interface. interface:%s' % (dev)) for brif in bridge_list[bond_bridge]: brctl_delif_cmd = ( NETWORK_BRCTL_COMMAND, "delif", bond_bridge, brif, ) (brctl_rc, brctl_res) = execute_command(brctl_delif_cmd) if brctl_rc != 0: raise KssCommandException( 'Failure delete bridge port. bridge:%s port:%s' % (dev, brif)) brctl_delbr_cmd = ( NETWORK_BRCTL_COMMAND, "delbr", bond_bridge, ) (brctl_rc, brctl_res) = execute_command(brctl_delbr_cmd) if brctl_rc != 0: raise KssCommandException('Failure delete bridge. bridge:%s' % (dev, brif)) remove_file("%s/ifcfg-%s" % (NETWORK_IFCFG_DIR, bond_bridge)) # # Unload bonding module # remove_bonding_cmd = ( SYSTEM_COMMAND_REMOVE_MODULE, "bonding", ) (rmmod_rc, rmmod_res) = execute_command(remove_bonding_cmd) if rmmod_rc != 0: raise KssCommandException('Failure remove bonding module.') # # Restart network # network_restart_cmd = ( NETWORK_COMMAND, "restart", ) (net_rc, net_res) = execute_command(network_restart_cmd) if net_rc != 0: raise KssCommandException('Failure restart network.') self.logger.info("Deleted bonding device. - bond=%s dev=%s" % (opts.dev, ','.join(restore_dev_list))) print >> sys.stdout, _("Deleted bonding device. - bond=%s dev=%s" % (opts.dev, ','.join(restore_dev_list))) return True
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(10) original_parser = iscsidParser() new_parser = iscsidParser() dop = DictOp() dop.addconf("original", original_parser.read_conf()) dop.addconf("new", new_parser.read_conf()) self.up_progress(10) dop.cdp_set("new", ISCSI_CONFIG_KEY_AUTH_METHOD, opts.auth) if opts.auth == ISCSI_CONFIG_VALUE_AUTH_METHOD_CHAP: password = "" if opts.password is not None: password = opts.password elif opts.password_file is not None and is_readable( opts.password_file): try: fp = open(opts.password_file, "r") try: fcntl.lockf(fp.fileno(), fcntl.LOCK_SH) try: password = fp.readline().strip("\n") finally: fcntl.lockf(fp.fileno(), fcntl.LOCK_UN) self.up_progress(10) finally: fp.close() except: raise KssCommandException('Failed to read file. - target host=%s password_file=%s' \ % (opts.host,opts.password_file)) try: os.remove(opts.password_file) except: raise KssCommandException('Failed to remove file. - target host=%s password_file=%s' \ % (opts.host,opts.password_file)) dop.cdp_set("new", ISCSI_CONFIG_KEY_AUTH_METHOD, opts.auth) dop.cdp_set("new", ISCSI_CONFIG_KEY_AUTH_USER, opts.user) dop.cdp_set("new", ISCSI_CONFIG_KEY_AUTH_PASSWORD, password) else: dop.comment("new", ISCSI_CONFIG_KEY_AUTH_USER) dop.comment("new", ISCSI_CONFIG_KEY_AUTH_PASSWORD) self.up_progress(10) if opts.autostart: dop.cdp_set("new", ISCSI_CONFIG_KEY_SATRTUP, ISCSI_CONFIG_VALUE_SATRTUP_ON) else: dop.cdp_set("new", ISCSI_CONFIG_KEY_SATRTUP, ISCSI_CONFIG_VALUE_SATRTUP_OFF) new_parser.write_conf(dop.getconf("new")) self.up_progress(10) discovery_command_args = (ISCSI_CMD, ISCSI_CMD_OPTION_MODE, ISCSI_CMD_OPTION_MODE_DISCOVERY, ISCSI_CMD_OPTION_TYPE, ISCSI_CMD_OPTION_TYPE_SENDTARGETS, ISCSI_CMD_OPTION_PORTAL, opts.host) (discovery_rc, discovery_res) = execute_command(discovery_command_args) self.up_progress(10) original_parser.write_conf(dop.getconf("original")) self.up_progress(10) if discovery_rc != 0: raise KssCommandException( 'Failed to add iSCSI. - host=%s message=%s' % (opts.host, discovery_res)) if discovery_res == []: raise KssCommandException( 'Failed to add iSCSI. - host=%s message=No exist permit iSCSI disk for target.' % (opts.host)) for node_line in discovery_res: if not node_line: continue try: node = iscsi_parse_node(node_line) except: self.logger.warn( 'Failed to parse iSCSI discovery command response. message="%s"' % (node_line)) continue self.logger.info("%s" % (iscsi_print_format_node(node))) print >> sys.stdout, _("%s") % (iscsi_print_format_node(node)) return True
def _read_conf(self, lines, level=0): dop = DictOp() dop.addconf("__", {}) pre_comment = [] # 設定の前のコメント リスト配列 post_comment = None # 設定行のコメント 文字列 _in_section = False _res = [] for _aline in lines: if _in_section is True: regex = "[ \t]*(?P<comment>#*)[ \t]*</(?P<key>%s)>" % _section_key #'|'.join(self.opt_sect) _regex = re.compile(r"%s" % regex) m = _regex.match(_aline) if m: _comment = m.group('comment') _key = m.group('key').strip() values = self.build_value(self._read_conf(_res, level), pre_comment, post_comment) dop.set("__", [_key, _section_val], values) if _comment != "": dop.comment("__", [_key, _section_val]) if level == 1: self.orders.append([_key, _section_val]) pre_comment = [] post_comment = None _in_section = False _res = [] level = level - 1 else: _res.append(_aline) else: _aline = _aline.rstrip('\r\n') if _aline.strip() == "": pre_comment.append(_aline) continue match = False for _type in ['uni', 'multi', 'sect']: exec("regex = '|'.join(self.opt_%s)" % _type) if _type == "sect": regex = "[ \t]*(?P<comment>#*)[ \t]*<(?P<key>%s)(?P<section>.*)>" % regex elif _type == "multi": regex = "[ \t]*(?P<comment>#*)[ \t]*(?P<key>%s)[ \t]+(?P<value>.+)" % regex elif _type == "uni": regex = "[ \t]*(?P<comment>#*)[ \t]*(?P<key>%s)[ \t]+(?P<value>.+)" % regex _regex = re.compile(r"%s" % regex) m = _regex.match(_aline) if m: match = True _comment = m.group('comment') _key = m.group('key').strip() if _type == "sect": _section_key = _key _section_val = re.sub(r"[\"']", "", m.group('section').strip()) _in_section = True level = level + 1 elif _type == "multi": _value = m.group('value').strip() if _value.find(self._comment) > 0: post_comment = _value[_value.find(self._comment ):] _value = re.sub("%s$" % post_comment, "", _value).rstrip() values = self.build_value(_value, pre_comment, post_comment) dop.set("__", [_key, _value], values) if _comment != "": dop.comment("__", [_key, _value]) if level == 0: self.orders.append([_key, _value]) pre_comment = [] post_comment = None elif _type == "uni": _value = m.group('value').strip() if _value.find(self._comment) > 0: post_comment = _value[_value.find(self._comment ):] _value = re.sub("%s$" % post_comment, "", _value).rstrip() values = self.build_value(_value, pre_comment, post_comment) dop.set("__", [_key], values) if _comment != "": dop.comment("__", [_key]) if level == 0: self.orders.append([_key]) pre_comment = [] post_comment = None break if match is False: # ブラケットディレクティブのパラメータは除外する (よって、ブラケットディレクティブは全ての定義が必要!) # example: "<undefined_directive 'foobar'>" regex_exclude1 = "[ \t]*(?P<comment>#*)[ \t]*(?P<key>%s)[ \t]" % '|'.join( self.opt_sect) _regex_exclude1 = re.compile(r"%s" % regex_exclude1) # 未定義のパラメータの値がクオートせずにスペース区切りで3つ以上の値を指定している場合はコメント行とみなす # example: "# Read this configuration file" regex_exclude2 = "[ \t]*#+[ \t]*[^ \t]+([ \t]+[^ \t]+){3,}" _regex_exclude2 = re.compile(r"%s" % regex_exclude2) # 未定義のパラメータの値がクオートせずにスペース区切りで2つ以上の値を指定していて、かつ、最後が:で終わる場合はコメント行とみなす # example: "# Read this configuration:" regex_exclude3 = "[ \t]*#+[ \t]*[^ \t]+([ \t]+[^ \t]+){2,}:$" _regex_exclude3 = re.compile(r"%s" % regex_exclude3) # 未定義のパラメータの値が0個以上で、かつ、最後が:で終わる場合はコメント行とみなす # example: "# Read #" regex_exclude4 = "[ \t]*#+[ \t]*[^ \t]+.+[ \t]+#+$" _regex_exclude4 = re.compile(r"%s" % regex_exclude4) m1 = _regex_exclude1.match(_aline) m2 = _regex_exclude2.match(_aline) m3 = _regex_exclude3.match(_aline) m4 = _regex_exclude4.match(_aline) if not m1 and not m2 and not m3 and not m4: # opt_xxxに未定義のパラメータはuniパラメータとする regex = "[ \t]*(?P<comment>#*)[ \t]*(?P<key>[A-Z][^ \t]+)[ \t]+(?P<value>.+)" _regex = re.compile(r"%s" % regex) m = _regex.match(_aline) if m: _comment = m.group('comment') _key = m.group('key').strip() _value = m.group('value').strip() if _value.find(self._comment) > 0: post_comment = _value[_value.find(self._comment ):] _value = re.sub("%s$" % post_comment, "", _value).rstrip() values = self.build_value(_value, pre_comment, post_comment) dop.set("__", [_key], values) if _comment != "": dop.comment("__", [_key]) if level == 0: self.orders.append([_key]) pre_comment = [] post_comment = None match = True if match is False: if _aline.lstrip()[0:1] == self._comment: footer_regex = re.compile(self._footer) m = footer_regex.search(_aline) if not m: comment = _aline[_aline.find(self._comment):] pre_comment.append(comment) continue if len(pre_comment) > 0: eof_key = "%sEOF" % (self._reserved_key_prefix, ) new_value = self.build_value("", pre_comment, post_comment) dop.set("__", [eof_key], new_value) return dop.getconf("__")
def read_conf(modules, webobj=None, machine=None, extra_args={}): """<comment-ja> 設定ファイルパーサー(モジュール)により設定ファイルの内容を 辞書配列操作クラスに渡し、そのオブジェクトを返す @param modules: モジュールのリスト配列 @param webobj: @param machine: @type modules: list @rtype: object dict_op @return: 辞書配列操作オブジェクト </comment-ja> <comment-en> TODO: English Comment </comment-en> """ if type(modules) == str: modules = [modules] options = {"module" : ":".join(modules)} try: options['include'] = extra_args['include'] except: pass #cmd_name = u"Get Settings - %s" % ":".join(modules) cmd_name = u"Get Settings" if type(webobj) == types.InstanceType: from karesansui.db.model._2pysilhouette import Job, JobGroup, \ JOBGROUP_TYPE from karesansui.db.access._2pysilhouette import jg_findby1, jg_save,corp from karesansui.db.access._2pysilhouette import save_job_collaboration from karesansui.db.access.machine2jobgroup import new as m2j_new from pysilhouette.command import dict2command _cmd = dict2command( "%s/%s" % (karesansui.config['application.bin.dir'], CONFIGURE_COMMAND_READ), options) jobgroup = JobGroup(cmd_name, karesansui.sheconf['env.uniqkey']) jobgroup.jobs.append(Job('%s command' % cmd_name, 0, _cmd)) jobgroup.type = JOBGROUP_TYPE['PARALLEL'] _machine2jobgroup = m2j_new(machine=machine, jobgroup_id=-1, uniq_key=karesansui.sheconf['env.uniqkey'], created_user=webobj.me, modified_user=webobj.me, ) if corp(webobj.orm, webobj.pysilhouette.orm,_machine2jobgroup, jobgroup) is False: webobj.logger.debug("%s command failed. Return to timeout" % (cmd_name)) return False cmd_res = jobgroup.jobs[0].action_stdout else: from karesansui.lib.utils import execute_command opts_str = "" for x in options.keys(): if options[x] is None: opts_str += "--%s " % x else: opts_str += "--%s=%s " % (x, options[x]) _cmd = "%s/bin/%s %s" % (KARESANSUI_PREFIX, CONFIGURE_COMMAND_READ, opts_str.strip(),) command_args = _cmd.strip().split(" ") (rc,res) = execute_command(command_args) if rc != 0: return False cmd_res = "\n".join(res) dop = DictOp() try: exec(cmd_res) except Exception: return False for module in modules: try: exec("dop.addconf('%s',Config_Dict_%s)" % (module,module,)) except: pass return dop
def _value_to_lines(self, conf_arr, level=0): lines = [] orders_key = "%sORDERS" % (self._reserved_key_prefix, ) dop = DictOp() dop.addconf("__", conf_arr) for _k, _v in dop.getconf("__").iteritems(): action = dop.action("__", [_k]) if action == "delete": continue iscomment = dop.iscomment("__", [_k]) value = dop.get("__", [_k]) if type(value) == list: _val = value[0] if type(_val) != dict: _pre_comment = value[1][0] _post_comment = value[1][1] pre_comment = [] try: for _aline in _pre_comment: if _aline.strip() == "": pass elif _aline[0:1] != self._comment: _prefix = "" if level > 0: _prefix += str_repeat(self._indent, level) _prefix += self._comment _aline = "%s %s" % ( _prefix, _aline, ) pre_comment.append(_aline) except: pass if len(pre_comment) > 0: #preprint_r(pre_comment) lines = lines + pre_comment post_comment = _post_comment try: if post_comment is not None and post_comment[ 0:1] != self._comment: post_comment = "%s %s" % ( self._comment, post_comment, ) except: pass else: pass else: _val = value _prefix = "" if iscomment is True: _prefix += self._comment if level > 0: _prefix += str_repeat(self._indent, level) if type(_val) == dict: # ORDER順に設定する orders = [] try: old_orders = _val[orders_key]['value'] except: old_orders = [] for kk in old_orders: if type(kk) is list: orders.append(kk[0]) elif type(kk) is str: orders.append(kk) for kk in _val.keys(): if not kk in orders: orders.append(kk) #for _k2,_v2 in _val.iteritems(): for _k2 in orders: if _k2 == orders_key: continue _v2 = _val[_k2] sub_value = {} sub_value[_k2] = _v2 try: iscomment = sub_value[_k2]['comment'] except: iscomment = False try: action = sub_value[_k2]['action'] except: action = "" if action == "delete": continue #try: # sub_value[_k2]['value'][1][0] # lines = lines + sub_value[_k2]['value'][1][0] #except: # pass is_sect = False if _k in self.opt_multi and _k2 == _v2["value"][0]: for _k3, _v3 in sub_value.iteritems(): try: iscomment3 = sub_value[_k3]['comment'] except: iscomment3 = iscomment try: action3 = sub_value[_k3]['action'] except: action3 = "" _prefix = "" if iscomment is True: _prefix += self._comment if level > 0: _prefix += str_repeat(self._indent, level) lines.append("%s%-18s%s%s" % (_prefix, _k, self._new_delim, _k2)) elif _k in self.opt_sect: is_sect = True _prefix = "" if iscomment is True: _prefix += self._comment if level > 0: _prefix += str_repeat(self._indent, level) if _k2 == "": lines.append("%s<%s>" % (_prefix, _k)) else: lines.append("%s<%s \"%s\">" % (_prefix, _k, _k2)) new_level = level + 1 new_lines = self._value_to_lines(sub_value, level=new_level) for _aline in new_lines: _prefix2 = "" if iscomment is True: _prefix2 += self._comment new_aline = "%s%s" % ( _prefix2, _aline, ) new_aline = re.sub("^%s+" % self._comment, self._comment, new_aline) lines.append(new_aline) #lines = lines + new_lines if is_sect is True: lines.append("%s</%s>" % ( _prefix, _k, )) else: aline = "" if _k in self.opt_multi: aline += "%s%-18s%s%s" % ( _prefix, _k, self._new_delim, _val, ) else: if re.match("^[A-Z]+[a-z]", _k): aline += "%s%-18s%s%s" % ( _prefix, _k, self._new_delim, _val, ) if post_comment is not None: aline = "%s %s" % ( aline, post_comment, ) if aline != "": lines.append(aline) return lines
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(1) uniq_id = time.strftime("%Y%m%d%H%M%S", time.localtime()) if opts.pre_command is not None: if opts.pre_command[0:4] == "b64:": command = base64_decode(opts.pre_command[4:]) else: command = opts.pre_command self.logger.info("execute command - %s" % command) (_ret,_res) = execute_command(command.split()) if _ret != 0: error_msg = "execute error - %s" % command self.logger.error(error_msg) #raise KssCommandOptException("ERROR: %s" % error_msg) dop = DictOp() modules = opts.module.split(":") files = opts.file.split(":") source_files = [] retval = True cnt = 0 for _mod in modules: _file = files[cnt] try: exec("from karesansui.lib.parser.%s import %sParser as Parser" % (_mod,_mod,)) self.up_progress(5) parser = Parser() # 辞書オペレータに追加 self.up_progress(5) if opts.php is True: conf_arr = php_array_to_python_dict(open(_file).read()) else: exec("conf_arr = %s" % open(_file).read()) dop.addconf(_mod,conf_arr) """ 必要ならここで配列操作 通常は、配列操作後の辞書が_fileに書き込まれているので必要ない dop.add (_mod,"foo","bar") dop.delete(_mod,"foo") """ # 設定ファイル一覧に作成(バックアップ用) self.up_progress(5) source_file = parser.source_file() for _afile in source_file: _bak_afile = "%s.%s" % (_afile,uniq_id) copy_file(_afile,_bak_afile) source_files = source_files + source_file # 辞書に戻す self.up_progress(5) conf_arr = dop.getconf(_mod) #dop.preprint_r(_mod) # 設定ファイルに書き込み self.up_progress(5) extra_args = {} extra_args["include"] = opts.include if opts.early_exit is True: retval = retval and parser.write_conf(conf_arr,extra_args=extra_args) else: retval = parser.write_conf(conf_arr,extra_args=extra_args) and retval if opts.delete is True: os.unlink(_file) finally: cnt = cnt + 1 if retval is False: for _afile in source_files: _bak_afile = "%s.%s" % (_afile,uniq_id) os.unlink(_afile) copy_file(_bak_afile,_afile) os.unlink(_bak_afile) raise KssCommandOptException("ERROR: write configure failure") for _afile in source_files: _bak_afile = "%s.%s" % (_afile,uniq_id) os.unlink(_bak_afile) if opts.post_command is not None: if opts.post_command[0:4] == "b64:": command = base64_decode(opts.post_command[4:]) else: command = opts.post_command self.logger.info("execute command - %s" % command) (_ret,_res) = execute_command(command.split()) if _ret != 0: error_msg = "execute error - %s" % command self.logger.error(error_msg) raise KssCommandOptException("ERROR: %s" % error_msg) self.up_progress(10) return True
def process(self): (opts, args) = getopts() chkopts(opts) self.up_progress(1) uniq_id = time.strftime("%Y%m%d%H%M%S", time.localtime()) if opts.pre_command is not None: if opts.pre_command[0:4] == "b64:": command = base64_decode(opts.pre_command[4:]) else: command = opts.pre_command self.logger.info("execute command - %s" % command) if opts.dry_run is True: print "" print ">>>Execute pre command: %s" % command print "" else: (_ret,_res) = execute_command(command.split()) if _ret != 0: error_msg = "execute error - %s" % command self.logger.error(error_msg) #raise KssCommandOptException("ERROR: %s" % error_msg) self.up_progress(5) from karesansui.lib.parser.collectd import collectdParser from karesansui.lib.parser.collectdplugin import collectdpluginParser dop = DictOp() collectd_parser = collectdParser() dop.addconf("collectd",collectd_parser.read_conf()) collectdplugin_parser = collectdpluginParser() extra_args = {"include":"^(%s)$" % "|".join(COLLECTD_PLUGINS)} dop.addconf("collectdplugin",collectdplugin_parser.read_conf(extra_args=extra_args)) initialize_collectd_settings(dop=dop,force=opts.force,reverse=opts.reverse) retval = collectd_parser.write_conf(dop.getconf("collectd"),dryrun=opts.dry_run) retval = collectdplugin_parser.write_conf(dop.getconf("collectdplugin"),extra_args=extra_args,dryrun=opts.dry_run) self.up_progress(30) if opts.post_command is not None: if opts.post_command[0:4] == "b64:": command = base64_decode(opts.post_command[4:]) else: command = opts.post_command self.logger.info("execute command - %s" % command) if opts.dry_run is True: print "" print ">>>Execute post command: %s" % command print "" else: (_ret,_res) = execute_command(command.split()) if _ret != 0: error_msg = "execute error - %s" % command self.logger.error(error_msg) raise KssCommandOptException("ERROR: %s" % error_msg) self.up_progress(10) return True