Merge ~harlowja/cloud-init:newer-configobj into cloud-init:master

Proposed by Joshua Harlow on 2016-08-01
Status: Merged
Merged at revision: 80db6eb9d697c21bfab85ab9a0dd5aceee571883
Proposed branch: ~harlowja/cloud-init:newer-configobj
Merge into: cloud-init:master
Diff against target: 90 lines (+26/-15)
3 files modified
cloudinit/config/cc_mcollective.py (+24/-14)
requirements.txt (+1/-1)
tests/unittests/test_handler/test_handler_mcollective.py (+1/-0)
Reviewer Review Type Date Requested Status
cloud-init commiters 2016-08-01 Pending
Review via email: mp+301728@code.launchpad.net
To post a comment you must log in.
Scott Moser (smoser) wrote :

if we declare this as minimum version, i think there is some imrpvoement to ConfigObj usage in
  cloudinit/config/cc_mcollective.py

we had to do this:
    try:
        mcollective_config = ConfigObj(server_cfg, file_error=True)
        existed = True
    except IOError:
        LOG.debug("Did not find file %s", server_cfg)
        mcollective_config = ConfigObj()
        existed = False

it'd be nicer if we had use caught the error with util.load_file and then done ConfigObj with the contents. but an older version of ConfigObj did not support that (i think ancient version, but i wasn't sure what versions were in what distro).

could you make that change also ?

Joshua Harlow (harlowja) wrote :

Sure.

Scott Moser (smoser) wrote :

This is failing tox for me in the mcollective tests.

Joshua Harlow (harlowja) wrote :

Shouldn't be a problem anymore.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/cloudinit/config/cc_mcollective.py b/cloudinit/config/cc_mcollective.py
2index ada535f..b3089f3 100644
3--- a/cloudinit/config/cc_mcollective.py
4+++ b/cloudinit/config/cc_mcollective.py
5@@ -19,6 +19,8 @@
6 # You should have received a copy of the GNU General Public License
7 # along with this program. If not, see <http://www.gnu.org/licenses/>.
8
9+import errno
10+
11 import six
12 from six import BytesIO
13
14@@ -38,16 +40,18 @@ LOG = logging.getLogger(__name__)
15
16 def configure(config, server_cfg=SERVER_CFG,
17 pubcert_file=PUBCERT_FILE, pricert_file=PRICERT_FILE):
18- # Read server.cfg values from the
19- # original file in order to be able to mix the rest up
20+ # Read server.cfg (if it exists) values from the
21+ # original file in order to be able to mix the rest up.
22 try:
23- mcollective_config = ConfigObj(server_cfg, file_error=True)
24- existed = True
25- except IOError:
26- LOG.debug("Did not find file %s", server_cfg)
27- mcollective_config = ConfigObj()
28- existed = False
29-
30+ old_contents = util.load_file(server_cfg, quiet=False, decode=False)
31+ mcollective_config = ConfigObj(BytesIO(old_contents))
32+ except IOError as e:
33+ if e.errno != errno.ENOENT:
34+ raise
35+ else:
36+ LOG.debug("Did not find file %s (starting with an empty"
37+ " config)", server_cfg)
38+ mcollective_config = ConfigObj()
39 for (cfg_name, cfg) in config.items():
40 if cfg_name == 'public-cert':
41 util.write_file(pubcert_file, cfg, mode=0o644)
42@@ -74,12 +78,18 @@ def configure(config, server_cfg=SERVER_CFG,
43 # Otherwise just try to convert it to a string
44 mcollective_config[cfg_name] = str(cfg)
45
46- if existed:
47- # We got all our config as wanted we'll rename
48- # the previous server.cfg and create our new one
49- util.rename(server_cfg, "%s.old" % (server_cfg))
50+ try:
51+ # We got all our config as wanted we'll copy
52+ # the previous server.cfg and overwrite the old with our new one
53+ util.copy(server_cfg, "%s.old" % (server_cfg))
54+ except IOError as e:
55+ if e.errno == errno.ENOENT:
56+ # Doesn't exist to copy...
57+ pass
58+ else:
59+ raise
60
61- # Now we got the whole file, write to disk...
62+ # Now we got the whole (new) file, write to disk...
63 contents = BytesIO()
64 mcollective_config.write(contents)
65 util.write_file(server_cfg, contents.getvalue(), mode=0o644)
66diff --git a/requirements.txt b/requirements.txt
67index cc1dc05..0c4951f 100644
68--- a/requirements.txt
69+++ b/requirements.txt
70@@ -22,7 +22,7 @@ oauthlib
71 # that the built-in config parser is not sufficent (ie
72 # when we need to preserve comments, or do not have a top-level
73 # section)...
74-configobj
75+configobj>=5.0.2
76
77 # All new style configurations are in the yaml format
78 pyyaml
79diff --git a/tests/unittests/test_handler/test_handler_mcollective.py b/tests/unittests/test_handler/test_handler_mcollective.py
80index 8fa0147..c3a5a63 100644
81--- a/tests/unittests/test_handler/test_handler_mcollective.py
82+++ b/tests/unittests/test_handler/test_handler_mcollective.py
83@@ -138,6 +138,7 @@ class TestHandler(t_help.TestCase):
84 def test_mcollective_install(self, mock_util):
85 cc = self._get_cloud('ubuntu')
86 cc.distro = t_help.mock.MagicMock()
87+ mock_util.load_file.return_value = b""
88 mycfg = {'mcollective': {'conf': {'loglevel': 'debug'}}}
89 cc_mcollective.handle('cc_mcollective', mycfg, cc, LOG, [])
90 self.assertTrue(cc.distro.install_packages.called)

Subscribers

People subscribed via source and target branches