File tree Expand file tree Collapse file tree 2 files changed +25
-26
lines changed Expand file tree Collapse file tree 2 files changed +25
-26
lines changed Original file line number Diff line number Diff line change @@ -121,18 +121,21 @@ def do_edit(self, context):
121
121
'''
122
122
cfg = corosync .conf ()
123
123
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" )
125
129
except IOError as e :
126
130
context .fatal_error (str (e ))
127
131
128
132
def do_show (self , context ):
129
133
'''
130
134
Display the corosync configuration.
131
135
'''
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 ())
136
139
137
140
def do_log (self , context ):
138
141
'''
Original file line number Diff line number Diff line change @@ -1230,31 +1230,27 @@ def edit_file(fname):
1230
1230
return ext_cmd_nosudo ("%s %s" % (config .core .editor , fname ))
1231
1231
1232
1232
1233
- def edit_file_ext (fname , template = '' ) :
1233
+ def edit_file_ext (fname : str , validator : typing . Callable [[ typing . IO ], bool ] = None ) -> bool :
1234
1234
'''
1235
1235
Edit a file via a temporary file.
1236
1236
Raises IOError on any error.
1237
+
1238
+ returns True if the file was changed
1237
1239
'''
1238
- if not os .path .isfile (fname ):
1239
- s = template
1240
- else :
1241
- s = open (fname ).read ()
1242
- filehash = hash (s )
1243
- tmpfile = str2tmp (s )
1244
- try :
1245
- try :
1246
- if edit_file (tmpfile ) != 0 :
1247
- return
1248
- s = open (tmpfile , 'r' ).read ()
1249
- if hash (s ) == filehash : # file unchanged
1250
- return
1251
- f2 = open (fname , 'w' )
1252
- f2 .write (s )
1253
- f2 .close ()
1254
- finally :
1255
- os .unlink (tmpfile )
1256
- except OSError as e :
1257
- raise IOError (e )
1240
+ with create_tempfile () as tmpfile :
1241
+ shutil .copyfile (fname , tmpfile )
1242
+ if edit_file (tmpfile ) != 0 :
1243
+ raise IOError (f"Cannot edit file \" { fname } \" " )
1244
+ changed_data = read_from_file (tmpfile )
1245
+ source_data = read_from_file (fname )
1246
+ if changed_data != source_data :
1247
+ if validator and not validator (tmpfile ):
1248
+ return False
1249
+ # The original file needs to be replaced atomically
1250
+ str2file (changed_data , fname )
1251
+ return True
1252
+ else :
1253
+ return False
1258
1254
1259
1255
1260
1256
def need_pager (s , w , h ):
You can’t perform that action at this time.
0 commit comments