summaryrefslogtreecommitdiff
path: root/cloudinit/config/cc_debug.py
blob: 85dc5c581125a7a528d1aed26fb7ec9152791c03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# vi: ts=4 expandtab
#
#    Copyright (C) 2013 Yahoo! Inc.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License version 3, as
#    published by the Free Software Foundation.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

from StringIO import StringIO
from cloudinit import util
from cloudinit import type_utils
import copy
import yaml


def _format_yaml(obj):
    try:
        formatted = yaml.safe_dump(obj,
                                   line_break="\n",
                                   indent=4,
                                   explicit_start=True,
                                   explicit_end=True,
                                   default_flow_style=False)
        return formatted.strip()
    except:
        return "???"


def _make_header(text):
    header = StringIO()
    header.write("-" * 80)
    header.write("\n")
    header.write(text.center(80, ' '))
    header.write("\n")
    header.write("-" * 80)
    header.write("\n")
    return header.getvalue()


def handle(name, cfg, cloud, log, args):
    verbose = util.get_cfg_by_path(cfg, ('debug','verbose'), default=True)
    if not verbose:
        log.debug(("Skipping module named %s,"
                   " verbose printing disabled"), name)
        return
    out_file = None
    if args:
        out_file = args[0]
    else:
        out_file = util.get_cfg_by_path(cfg, ('debug','output'))
    # Clean out some keys that we just don't care about showing...
    dump_cfg = copy.deepcopy(cfg)
    for k in ['log_cfgs']:
        dump_cfg.pop(k, None)
    all_keys = list(dump_cfg.keys())
    for k in all_keys:
        if k.startswith("_"):
            dump_cfg.pop(k, None)
    # Now dump it...
    to_print = StringIO()
    to_print.write(_make_header("Config"))
    to_print.write(_format_yaml(dump_cfg))
    to_print.write("\n")
    to_print.write(_make_header("MetaData"))
    to_print.write(_format_yaml(cloud.datasource.metadata))
    to_print.write("\n")
    to_print.write(_make_header("Misc"))
    to_print.write("Datasource: %s\n" % (type_utils.obj_name(cloud.datasource)))
    to_print.write("Distro: %s\n" % (type_utils.obj_name(cloud.distro)))
    to_print.write("Hostname: %s\n" % (cloud.get_hostname(True)))
    to_print.write("Instance ID: %s\n" % (cloud.get_instance_id()))
    to_print.write("Locale: %s\n" % (cloud.get_locale()))
    to_print.write("Launch IDX: %s\n" % (cloud.launch_index))
    contents = to_print.getvalue()
    for line in contents.splitlines():
        line = "ci-info: %s\n" % (line)
        if out_file:
            util.write_file(out_file, line, 0644, "a")
        else:
            util.multi_log(line, console=True, stderr=False)