summaryrefslogtreecommitdiff
path: root/cloudinit/config/cc_debug.py
blob: c51818c3ecc7a43a1710df63ff2e4c96ff5c0e53 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Copyright (C) 2013 Yahoo! Inc.
#
# This file is part of cloud-init. See LICENSE file for license information.

"""Debug: Helper to debug cloud-init *internal* datastructures."""

import copy
from io import StringIO
from textwrap import dedent

from cloudinit import safeyaml, type_utils, util
from cloudinit.config.schema import MetaSchema, get_meta_doc
from cloudinit.distros import ALL_DISTROS
from cloudinit.settings import PER_INSTANCE

SKIP_KEYS = frozenset(["log_cfgs"])

MODULE_DESCRIPTION = """\
This module will enable for outputting various internal information that
cloud-init sources provide to either a file or to the output console/log
location that this cloud-init has been configured with when running.

.. note::
    Log configurations are not output.
"""

meta: MetaSchema = {
    "id": "cc_debug",
    "name": "Debug",
    "title": "Helper to debug cloud-init *internal* datastructures",
    "description": MODULE_DESCRIPTION,
    "distros": [ALL_DISTROS],
    "frequency": PER_INSTANCE,
    "examples": [
        dedent(
            """\
            debug:
              verbose: true
              output: /tmp/my_debug.log
            """
        )
    ],
}

__doc__ = get_meta_doc(meta)


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 _dumps(obj):
    text = safeyaml.dumps(obj, explicit_start=False, explicit_end=False)
    return text.rstrip()


def handle(name, cfg, cloud, log, args):
    """Handler method activated by cloud-init."""
    verbose = util.get_cfg_by_path(cfg, ("debug", "verbose"), default=True)
    if args:
        # if args are provided (from cmdline) then explicitly set verbose
        out_file = args[0]
        verbose = True
    else:
        out_file = util.get_cfg_by_path(cfg, ("debug", "output"))

    if not verbose:
        log.debug("Skipping module named %s, verbose printing disabled", name)
        return
    # Clean out some keys that we just don't care about showing...
    dump_cfg = copy.deepcopy(cfg)
    for k in SKIP_KEYS:
        dump_cfg.pop(k, None)
    all_keys = list(dump_cfg)
    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(_dumps(dump_cfg))
    to_print.write("\n")
    to_print.write(_make_header("MetaData"))
    to_print.write(_dumps(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()
    content_to_file = []
    for line in contents.splitlines():
        line = "ci-info: %s\n" % (line)
        content_to_file.append(line)
    if out_file:
        util.write_file(out_file, "".join(content_to_file), 0o644, "w")
    else:
        util.multi_log("".join(content_to_file), console=True, stderr=False)


# vi: ts=4 expandtab