From aea822c47a6955d7836248ba80c554677e639cb8 Mon Sep 17 00:00:00 2001 From: Michel Blanc Date: Wed, 20 Feb 2013 12:42:49 +0100 Subject: [PATCH 1/3] Removes exception is sysctl file is missing When syscl file was missing, sysctl module was complaining about it and bailing out. This behaviour prevents usage of /etc/sysctl.d directory, present in some distributions. This patch accepts a missing sysctl.conf file so sysctl.d directory can be used. However, it will bail out if the destination directory doesn't exist. --- sysctl | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/sysctl b/sysctl index 9b2cbebfa51..48c28be8ffb 100644 --- a/sysctl +++ b/sysctl @@ -158,12 +158,14 @@ def sysctl_check(current_step, **sysctl_args): # sysctl file exists and openable ? # TODO choose if prefered to use os.access() instead try/catch on open if current_step == 'before': - try: - f = open(sysctl_args['sysctl_file']) - f.close() - except IOError, e: - return 1, 'unable to open supplied sysctl.conf' - + if not os.access(sysctl_args['sysctl_file'], os.W_OK): + try: + f = open(sysctl_args['sysctl_file'],'w') + f.write('') + f.close() + except IOError, e: + return 1, 'unable to create supplied sysctl file (directory missing)' + # no smart checks at this step ? if sysctl_args['checks'] == 'none': return 0, '' From 05d45fc1faca978abc3b981ffdd0f2e1be23b111 Mon Sep 17 00:00:00 2001 From: Michel Blanc Date: Wed, 20 Feb 2013 12:52:30 +0100 Subject: [PATCH 2/3] Changed when new sysctl file is created When destination sysctl file is missing, it is created. But, for idempotency purposes, the creation process now takes place just before it is used, in the main code path so an empty file is not left over if the code module.fail_jsons before the file is really used. --- sysctl | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/sysctl b/sysctl index 48c28be8ffb..dd5b56d6d7e 100644 --- a/sysctl +++ b/sysctl @@ -155,17 +155,6 @@ def sysctl_check(current_step, **sysctl_args): if not os.access(sysctl_args['key_path'], os.R_OK): return 1, 'key_path is not a readable file, key seems to be uncheckable' - # sysctl file exists and openable ? - # TODO choose if prefered to use os.access() instead try/catch on open - if current_step == 'before': - if not os.access(sysctl_args['sysctl_file'], os.W_OK): - try: - f = open(sysctl_args['sysctl_file'],'w') - f.write('') - f.close() - except IOError, e: - return 1, 'unable to create supplied sysctl file (directory missing)' - # no smart checks at this step ? if sysctl_args['checks'] == 'none': return 0, '' @@ -246,6 +235,14 @@ def main(): if res != 0: module.fail_json(msg='checks_before failed with: ' + msg) + if not os.access(sysctl_args['sysctl_file'], os.W_OK): + try: + f = open(sysctl_args['sysctl_file'],'w') + f.write('') + f.close() + except IOError, e: + module.fail_json(msg='unable to create supplied sysctl file (destination directory probably missing)') + # reading the file for line in open(sysctl_args['sysctl_file'], 'r').readlines(): if not line.strip(): From 8b5df82b74cf687b0abcb3dafa92c9fff449e4a8 Mon Sep 17 00:00:00 2001 From: Michel Blanc Date: Wed, 20 Feb 2013 14:20:27 +0100 Subject: [PATCH 3/3] Removes useless empty string write It s not necessary to write an empty string for the file to be created. --- sysctl | 1 - 1 file changed, 1 deletion(-) diff --git a/sysctl b/sysctl index dd5b56d6d7e..0c06e7989c7 100644 --- a/sysctl +++ b/sysctl @@ -238,7 +238,6 @@ def main(): if not os.access(sysctl_args['sysctl_file'], os.W_OK): try: f = open(sysctl_args['sysctl_file'],'w') - f.write('') f.close() except IOError, e: module.fail_json(msg='unable to create supplied sysctl file (destination directory probably missing)')