Write keys file to temp location to avoid potential corruption

reviewable/pr18780/r1
Stephen Fromm 12 years ago
parent 115dcbe34b
commit 4bce10c303

@ -68,6 +68,8 @@ import sys
import os import os
import pwd import pwd
import os.path import os.path
import tempfile
import shutil
def keyfile(user, write=False): def keyfile(user, write=False):
""" """
@ -114,11 +116,16 @@ def readkeys(filename):
f.close() f.close()
return keys return keys
def writekeys( filename, keys): def writekeys(module, filename, keys):
f = open(filename,"w") fd, tmp_path = tempfile.mkstemp()
f.writelines( (key + "\n" for key in keys) ) f = open(tmp_path,"w")
try:
f.writelines( (key + "\n" for key in keys) )
except IOError, e:
module.fail_json(msg="Failed to write to file %s: %s" % (tmp_path, str(e)))
f.close() f.close()
shutil.move(tmp_path, filename)
def enforce_state(module, params): def enforce_state(module, params):
""" """
@ -139,13 +146,13 @@ def enforce_state(module, params):
if present: if present:
module.exit_json(changed=False) module.exit_json(changed=False)
keys.append(key) keys.append(key)
writekeys(keyfile(user,write=True), keys) writekeys(module, keyfile(user,write=True), keys)
elif state=="absent": elif state=="absent":
if not present: if not present:
module.exit_json(changed=False) module.exit_json(changed=False)
keys.remove(key) keys.remove(key)
writekeys(keyfile(user,write=True), keys) writekeys(module, keyfile(user,write=True), keys)
params['changed'] = True params['changed'] = True
return params return params

Loading…
Cancel
Save