本文整理汇总了Python中karesansui.lib.dict_op.DictOp.getconf方法的典型用法代码示例。如果您正苦于以下问题:Python DictOp.getconf方法的具体用法?Python DictOp.getconf怎么用?Python DictOp.getconf使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类karesansui.lib.dict_op.DictOp
的用法示例。
在下文中一共展示了DictOp.getconf方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from karesansui.lib.dict_op import DictOp [as 别名]
# 或者: from karesansui.lib.dict_op.DictOp import getconf [as 别名]
class iscsidParser:
_module = "hosts"
def __init__(self):
self.dop = DictOp()
self.dop.addconf(self._module,{})
self.parser = Parser()
self.parser.set_delim("[ \t]*=[ \t]*")
self.parser.set_new_delim(" = ")
self.parser.set_comment("#")
self.base_parser_name = self.parser.__class__.__name__
pass
def set_footer(self, footer=""):
self.parser.set_footer(footer)
def source_file(self):
retval = [ISCSI_DEFAULT_CONFIG_PATH]
return retval
def read_conf(self, conf_path=None, extra_args=None):
retval = {}
if conf_path is None:
conf_path = ISCSI_DEFAULT_CONFIG_PATH
self.parser.set_source_file([conf_path])
conf_arr = self.parser.read_conf()
try:
self.dop.addconf(self._module,conf_arr[conf_path]['value'])
except:
pass
self.dop.set(self._module,['@BASE_PARSER'],self.base_parser_name)
#self.dop.preprint_r(self._module)
return self.dop.getconf(self._module)
def write_conf(self, conf_arr={}, conf_path=None, extra_args=None, dryrun=False):
retval = True
if conf_path is None:
conf_path = ISCSI_DEFAULT_CONFIG_PATH
try:
self.dop.addconf("parser",{})
self.dop.set("parser",[conf_path],conf_arr)
#self.dop.preprint_r("parser")
arr = self.dop.getconf("parser")
self.parser.write_conf(arr,dryrun=dryrun)
except:
pass
return retval
示例2: __init__
# 需要导入模块: from karesansui.lib.dict_op import DictOp [as 别名]
# 或者: from karesansui.lib.dict_op.DictOp import getconf [as 别名]
class modprobe_confParser:
_module = "modprobe_conf"
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_comment("#")
self.base_parser_name = self.parser.__class__.__name__
pass
def source_file(self):
retval = [PARSER_MODPROBE_CONF]
return retval
def read_conf(self,extra_args=None):
retval = {}
self.parser.set_source_file([PARSER_MODPROBE_CONF])
conf_arr = self.parser.read_conf()
try:
self.dop.addconf(self._module,conf_arr[PARSER_MODPROBE_CONF]['value'])
except:
pass
self.dop.set(self._module,['@BASE_PARSER'],self.base_parser_name)
#self.dop.preprint_r(self._module)
return self.dop.getconf(self._module)
def write_conf(self,conf_arr={},extra_args=None,dryrun=False):
retval = True
try:
self.dop.addconf("parser",{})
self.dop.set("parser",[PARSER_MODPROBE_CONF],conf_arr)
#self.dop.preprint_r("parser")
arr = self.dop.getconf("parser")
self.parser.write_conf(arr,dryrun=dryrun)
except:
pass
return retval
示例3: _new_lines
# 需要导入模块: from karesansui.lib.dict_op import DictOp [as 别名]
# 或者: from karesansui.lib.dict_op.DictOp import getconf [as 别名]
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
示例4: convert_old_style
# 需要导入模块: from karesansui.lib.dict_op import DictOp [as 别名]
# 或者: from karesansui.lib.dict_op.DictOp import getconf [as 别名]
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("__")
示例5: __init__
# 需要导入模块: from karesansui.lib.dict_op import DictOp [as 别名]
# 或者: from karesansui.lib.dict_op.DictOp import getconf [as 别名]
class nullParser:
_comment = ""
_module = "null_parser"
def __init__(self,paths=[]):
self.dop = DictOp()
self.dop.addconf(self._module,{})
self.set_source_file(paths)
def set_comment(self, comment=""):
self._comment = comment
def set_source_file(self,paths=[]):
if type(paths) == str:
paths = [paths]
self.paths = paths
return True
def get_source_file(self):
return self.paths
def source_file(self):
return self.get_source_file()
def read_conf(self):
return self.dop.getconf(self._module)
def write_conf(self,conf_arr={},dryrun=False):
retval = True
return retval
示例6: process
# 需要导入模块: from karesansui.lib.dict_op import DictOp [as 别名]
# 或者: from karesansui.lib.dict_op.DictOp import getconf [as 别名]
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
示例7: convert_new_style
# 需要导入模块: from karesansui.lib.dict_op import DictOp [as 别名]
# 或者: from karesansui.lib.dict_op.DictOp import getconf [as 别名]
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("__")
示例8: _pre_write_conf
# 需要导入模块: from karesansui.lib.dict_op import DictOp [as 别名]
# 或者: from karesansui.lib.dict_op.DictOp import getconf [as 别名]
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("__")
示例9: write_conf
# 需要导入模块: from karesansui.lib.dict_op import DictOp [as 别名]
# 或者: from karesansui.lib.dict_op.DictOp import getconf [as 别名]
#self.dop.preprint_r(self._module)
return self.dop.getconf(self._module)
def write_conf(self,conf_arr={},extra_args=None,dryrun=False):
retval = True
try:
self.dop.addconf("parser",{})
self.dop.set("parser",[PARSER_RESOLV_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 = resolvParser()
dop = DictOp()
dop.addconf("dum",parser.read_conf())
dop.comment("dum","search")
dop.comment("dum","nameserver")
dop.add("dum","domain","example.com localdomain")
conf = dop.getconf("dum")
parser.write_conf(conf,dryrun=True)
示例10: process
# 需要导入模块: from karesansui.lib.dict_op import DictOp [as 别名]
# 或者: from karesansui.lib.dict_op.DictOp import getconf [as 别名]
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
示例11: __init__
# 需要导入模块: from karesansui.lib.dict_op import DictOp [as 别名]
# 或者: from karesansui.lib.dict_op.DictOp import getconf [as 别名]
class xmlLikeConfParser:
_delim = "[ \t]+"
_new_delim = " "
_comment = "#"
_reserved_key_prefix = "@"
_indent = " "
_module = "xml_like_conf_parser"
_footer = "-- Generated by karesansui"
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 set_opt_uni(self, opts):
self.opt_uni = opts
def set_opt_multi(self, opts):
self.opt_multi = opts
def set_opt_sect(self, opts):
self.opt_sect = opts
def set_delim(self, delim=" "):
self._delim = delim
def set_new_delim(self, delim=" "):
self._new_delim = delim
def set_comment(self, comment="#"):
self._comment = comment
def set_reserved_key_prefix(self, prefix="@"):
self._reserved_key_prefix = prefix
def set_footer(self, footer=""):
self._footer = footer
def set_source_file(self,paths=[]):
if type(paths) == str:
paths = [paths]
self.paths = paths
return True
def get_source_file(self):
return self.paths
def source_file(self):
return self.get_source_file()
def build_value(self, value=None, precomment=[], postcomment=None):
if type(precomment) == str:
precomment = [precomment]
return [value, [precomment, postcomment], ]
def read_conf(self):
retval = {}
orders_key = "%sORDERS" % (self._reserved_key_prefix,)
for _afile in self.source_file():
res = ConfigFile(_afile).read()
self.orders = []
self.dop.set(self._module,[_afile],self._read_conf(res))
self.dop.set(self._module,[_afile,orders_key],self.orders)
return self.dop.getconf(self._module)
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
#.........这里部分代码省略.........
示例12: process
# 需要导入模块: from karesansui.lib.dict_op import DictOp [as 别名]
# 或者: from karesansui.lib.dict_op.DictOp import getconf [as 别名]
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
示例13: _value_to_lines
# 需要导入模块: from karesansui.lib.dict_op import DictOp [as 别名]
# 或者: from karesansui.lib.dict_op.DictOp import getconf [as 别名]
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 = ""
#.........这里部分代码省略.........
示例14: emlParser
# 需要导入模块: from karesansui.lib.dict_op import DictOp [as 别名]
# 或者: from karesansui.lib.dict_op.DictOp import getconf [as 别名]
#.........这里部分代码省略.........
def parse(self,file=None,message=None):
retval = {}
try:
if file is not None and os.path.exists(file):
message = open(file).read()
mail = email.Parser.Parser().parsestr(message)
headers = mail._headers
msgs = self._parse_mail(mail)
if type(msgs['body']) is list:
rawbody = ""
if file is not None and os.path.exists(file):
f = open(file)
line = f.readline()
while line:
line = f.readline()
if line.rstrip() == "":
rawbody += line
rawbody += ''.join(f.readlines())
break
f.close
else:
in_body = False
for line in message.split("\n"):
if line.rstrip() == "":
in_body = True
if in_body is True:
rawbody += line + "\n"
elif type(msgs['body']) is str:
rawbody = msgs['body']
retval["headers"] = headers
retval["msgs"] = msgs
retval["rawbody"] = rawbody
except:
pass
#preprint_r(retval)
return retval
def read_conf(self,extra_args=None):
retval = {}
self.dop.addconf(self._module,{})
try:
self.set_source_file(extra_args["file"])
for _file in self.source_file():
if os.path.exists(_file):
mail = self.parse(file=_file)
headers = mail['headers']
msgs = mail['msgs']
rawbody = mail['rawbody']
self.dop.add(self._module,[_file,'@msgs'] ,msgs)
self.dop.add(self._module,[_file,'@headers'],headers)
for _header in headers:
self.dop.add(self._module,[_file,'header',_header[0]],_header[1])
self.dop.add(self._module,[_file,'rawbody'] ,rawbody)
except:
pass
try:
message = extra_args["message"]
try:
message = message.encode(self.encoding)
except:
pass
if message != "" and message is not None:
mail = self.parse(message=message)
headers = mail['headers']
msgs = mail['msgs']
rawbody = mail['rawbody']
self.dop.add(self._module,["@message",'@msgs'] ,msgs)
self.dop.add(self._module,["@message",'@headers'],headers)
for _header in headers:
self.dop.add(self._module,["@message",'header',_header[0]],_header[1])
self.dop.add(self._module,["@message",'rawbody'] ,rawbody)
except:
pass
return self.dop.getconf(self._module)
def write_conf(self,conf_arr={},extra_args=None,dryrun=False):
retval = True
return retval
示例15: process
# 需要导入模块: from karesansui.lib.dict_op import DictOp [as 别名]
# 或者: from karesansui.lib.dict_op.DictOp import getconf [as 别名]
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