Merge lp:~shraddha-pandhe/cloud-init/debug-module into lp:~cloud-init-dev/cloud-init/trunk

Proposed by Shraddha Pandhe
Status: Merged
Merged at revision: 898
Proposed branch: lp:~shraddha-pandhe/cloud-init/debug-module
Merge into: lp:~cloud-init-dev/cloud-init/trunk
Diff against target: 80 lines (+76/-0)
1 file modified
cloudinit/config/cc_debug.py (+76/-0)
To merge this branch: bzr merge lp:~shraddha-pandhe/cloud-init/debug-module
Reviewer Review Type Date Requested Status
Joshua Harlow Pending
Review via email: mp+198119@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Scott Moser (smoser) wrote :

Neat!

I have a few nitpicks / suggestions:
 * namespace the 'verbose' in config to appear under a 'debug'. Ie:
    #cloud-config
    debug:
     verbose: True
 * Allow me to set the target filename to write to:
    #cloud-config
    debug:
     output: /tmp/debug.out
 * possibly we could combine the 2 and just drop 'verbose'. Do nothing if 'output' isn't set, rather than if verbose is set to true.

 * lastly, allow argument to set 'output'.
   Ie, i'd like to be able to do:
     cloud-init single --frequency=always --name=debug /tmp/debug.out
     cat debug.out

make sense?
Thanks!

898. By Shraddha Pandhe

bug: 1258619 added namespace for config options, output file, commandline argument for output file

Revision history for this message
Joshua Harlow (harlowja) wrote :

One change, _format_yaml seems similar to util.yaml_dumps, seems like just can use that instead.

899. By Shraddha Pandhe

essage

900. By Shraddha Pandhe

Removed method _format_yaml

901. By Shraddha Pandhe

Removed yaml import

Revision history for this message
Joshua Harlow (harlowja) wrote :

Seems ok with me. Thanks for this, is useful as a simple way of debugging what cloud-init is doing (or the data it contains) when that is needed (and you don't have access to the log file).

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'cloudinit/config/cc_debug.py'
2--- cloudinit/config/cc_debug.py 1970-01-01 00:00:00 +0000
3+++ cloudinit/config/cc_debug.py 2013-12-11 23:56:25 +0000
4@@ -0,0 +1,76 @@
5+# vi: ts=4 expandtab
6+#
7+# Copyright (C) 2013 Yahoo! Inc.
8+#
9+# This program is free software: you can redistribute it and/or modify
10+# it under the terms of the GNU General Public License version 3, as
11+# published by the Free Software Foundation.
12+#
13+# This program is distributed in the hope that it will be useful,
14+# but WITHOUT ANY WARRANTY; without even the implied warranty of
15+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+# GNU General Public License for more details.
17+#
18+# You should have received a copy of the GNU General Public License
19+# along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
21+from StringIO import StringIO
22+from cloudinit import util
23+from cloudinit import type_utils
24+import copy
25+
26+
27+def _make_header(text):
28+ header = StringIO()
29+ header.write("-" * 80)
30+ header.write("\n")
31+ header.write(text.center(80, ' '))
32+ header.write("\n")
33+ header.write("-" * 80)
34+ header.write("\n")
35+ return header.getvalue()
36+
37+
38+def handle(name, cfg, cloud, log, args):
39+ verbose = util.get_cfg_by_path(cfg, ('debug','verbose'), default=True)
40+ if not verbose:
41+ log.debug(("Skipping module named %s,"
42+ " verbose printing disabled"), name)
43+ return
44+ out_file = None
45+ if args:
46+ out_file = args[0]
47+ else:
48+ out_file = util.get_cfg_by_path(cfg, ('debug','output'))
49+ # Clean out some keys that we just don't care about showing...
50+ dump_cfg = copy.deepcopy(cfg)
51+ for k in ['log_cfgs']:
52+ dump_cfg.pop(k, None)
53+ all_keys = list(dump_cfg.keys())
54+ for k in all_keys:
55+ if k.startswith("_"):
56+ dump_cfg.pop(k, None)
57+ # Now dump it...
58+ to_print = StringIO()
59+ to_print.write(_make_header("Config"))
60+ to_print.write(util.yaml_dumps(dump_cfg))
61+ to_print.write("\n")
62+ to_print.write(_make_header("MetaData"))
63+ to_print.write(util.yaml_dumps(cloud.datasource.metadata))
64+ to_print.write("\n")
65+ to_print.write(_make_header("Misc"))
66+ to_print.write("Datasource: %s\n" % (type_utils.obj_name(cloud.datasource)))
67+ to_print.write("Distro: %s\n" % (type_utils.obj_name(cloud.distro)))
68+ to_print.write("Hostname: %s\n" % (cloud.get_hostname(True)))
69+ to_print.write("Instance ID: %s\n" % (cloud.get_instance_id()))
70+ to_print.write("Locale: %s\n" % (cloud.get_locale()))
71+ to_print.write("Launch IDX: %s\n" % (cloud.launch_index))
72+ contents = to_print.getvalue()
73+ content_to_file = []
74+ for line in contents.splitlines():
75+ line = "ci-info: %s\n" % (line)
76+ content_to_file.append(line)
77+ if out_file:
78+ util.write_file(out_file, "".join(content_to_file), 0644, "w")
79+ else:
80+ util.multi_log("".join(content_to_file), console=True, stderr=False)