Skip to content

Commit 2b321ec

Browse files
committed
Dev: ui_corosync: Improve corosync.show and corosync.edit subcommands
- corosync.show: validate corosync.conf before showing it - corosync.edit: edit a temporary copy of corosync.conf, validate it before saving it, then give the user hints on how to sync to other nodes
1 parent ec25d36 commit 2b321ec

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

crmsh/ui_corosync.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,21 @@ def do_edit(self, context):
121121
'''
122122
cfg = corosync.conf()
123123
try:
124-
utils.edit_file_ext(cfg, template='')
124+
rc = utils.edit_file_ext(cfg, corosync.is_valid_corosync_conf)
125+
if rc and len(utils.list_cluster_nodes()) > 1:
126+
logger.warning(f"\"{cfg}\" has changed, should be synced with other nodes")
127+
logger.info("Use \"crm corosync diff\" to show the difference")
128+
logger.info("Use \"crm corosync push\" to sync")
125129
except IOError as e:
126130
context.fatal_error(str(e))
127131

128132
def do_show(self, context):
129133
'''
130134
Display the corosync configuration.
131135
'''
132-
cfg = corosync.conf()
133-
if not os.path.isfile(cfg):
134-
context.fatal_error("No corosync configuration found on this node.")
135-
utils.page_string(open(cfg).read())
136+
if not corosync.is_valid_corosync_conf():
137+
return False
138+
utils.page_file(corosync.conf())
136139

137140
def do_log(self, context):
138141
'''

crmsh/utils.py

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,31 +1221,26 @@ def edit_file(fname):
12211221
return ext_cmd_nosudo("%s %s" % (config.core.editor, fname))
12221222

12231223

1224-
def edit_file_ext(fname, template=''):
1224+
def edit_file_ext(fname: str, validator: typing.Callable[[typing.IO], bool] = None) -> bool:
12251225
'''
12261226
Edit a file via a temporary file.
12271227
Raises IOError on any error.
1228+
1229+
returns True if the file was changed
12281230
'''
1229-
if not os.path.isfile(fname):
1230-
s = template
1231-
else:
1232-
s = open(fname).read()
1233-
filehash = hash(s)
1234-
tmpfile = str2tmp(s)
1235-
try:
1236-
try:
1237-
if edit_file(tmpfile) != 0:
1238-
return
1239-
s = open(tmpfile, 'r').read()
1240-
if hash(s) == filehash: # file unchanged
1241-
return
1242-
f2 = open(fname, 'w')
1243-
f2.write(s)
1244-
f2.close()
1245-
finally:
1246-
os.unlink(tmpfile)
1247-
except OSError as e:
1248-
raise IOError(e)
1231+
with create_tempfile() as tmpfile:
1232+
shutil.copyfile(fname, tmpfile)
1233+
if edit_file(tmpfile) != 0:
1234+
raise IOError(f"Cannot edit file \"{fname}\"")
1235+
changed_data = read_from_file(tmpfile)
1236+
source_data = read_from_file(fname)
1237+
if hash(changed_data) != hash(source_data):
1238+
if validator and not validator(tmpfile):
1239+
return False
1240+
shutil.copyfile(tmpfile, fname)
1241+
return True
1242+
else:
1243+
return False
12491244

12501245

12511246
def need_pager(s, w, h):

0 commit comments

Comments
 (0)