1#!/usr/bin/env python3
2
3r"""
4This file contains utilities associated with the host OS.
5"""
6
7import os
8import sys
9
10sys.path.append(os.path.join(os.path.dirname(__file__), "../lib"))
11
12import bmc_ssh_utils  # NOQA
13import var_funcs  # NOQA
14
15
16def get_os_release_info(default_cmd="cat /etc/os-release"):
17    r"""
18
19        Get os-release info and return it as a dictionary.
20
21        An example of the contents of /etc/os-release:
22
23        NAME="Red Hat Enterprise Linux Server"
24        VERSION="7.5 (Maipo)"
25        ID="rhel"
26        ID_LIKE="fedora"
27        VARIANT="Server"
28        VARIANT_ID="server"
29        VERSION_ID="7.5"
30        PRETTY_NAME="Red Hat Enterprise Linux Server 7.5 Beta (Maipo)"
31        ANSI_COLOR="0;31"
32        CPE_NAME="cpe:/o:redhat:enterprise_linux:7.5:beta:server"
33        HOME_URL="https://www.redhat.com/"
34        BUG_REPORT_URL="https://bugzilla.redhat.com/"
35
36        REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 7"
37        REDHAT_BUGZILLA_PRODUCT_VERSION=7.5
38        REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux"
39        REDHAT_SUPPORT_PRODUCT_VERSION="7.5 Beta"
40
41        For the data shown above, this function will return the following
42        dictionary:
43
44        result:
45          [name]:                             Red Hat Enterprise Linux Server
46          [version]:                          7.5 (Maipo)
47          [id]:                               rhel
48          [id_like]:                          fedora
49          [variant]:                          Server
50          [variant_id]:                       server
51          [version_id]:                       7.5
52          [pretty_name]:                      Red Hat Enterprise Linux Server 7.5 Beta (Maipo)
53          [ansi_color]:                       0;31
54          [cpe_name]:                         cpe:/o:redhat:enterprise_linux:7.5:beta:server
55          [home_url]:                         https://www.redhat.com/
56          [bug_report_url]:                   https://bugzilla.redhat.com/
57          [redhat_bugzilla_product]:          Red Hat Enterprise Linux 7
58          [redhat_bugzilla_product_version]:  7.5
59          [redhat_support_product]:           Red Hat Enterprise Linux
60          [redhat_support_product_version]:   7.5 Beta
61
62
63    .   Description of argument(s):
64        default_cmd    A string command to be executed (e.g cat /etc/os-release).
65
66    """
67
68    stdout, stderr, rc = bmc_ssh_utils.os_execute_command(default_cmd)
69
70    return var_funcs.key_value_outbuf_to_dict(stdout, delim="=", strip='"')
71