George Keishing | e7e9171 | 2021-09-03 11:28:44 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Michael Walsh | 6dbdfef | 2018-05-30 10:35:26 -0500 | [diff] [blame] | 2 | |
| 3 | r""" |
| 4 | This file contains utilities associated with the host OS. |
| 5 | """ |
| 6 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 7 | import os |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 8 | import sys |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 9 | |
George Keishing | b1cfbde | 2018-05-30 11:50:07 -0500 | [diff] [blame] | 10 | sys.path.append(os.path.join(os.path.dirname(__file__), "../lib")) |
| 11 | |
George Keishing | 0967989 | 2022-12-08 08:21:52 -0600 | [diff] [blame] | 12 | import bmc_ssh_utils # NOQA |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 13 | import var_funcs # NOQA |
George Keishing | 37c58c8 | 2022-12-08 07:42:54 -0600 | [diff] [blame] | 14 | |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 15 | |
George Keishing | 1ad128c | 2021-10-28 09:38:37 -0500 | [diff] [blame] | 16 | def get_os_release_info(default_cmd="cat /etc/os-release"): |
Michael Walsh | 6dbdfef | 2018-05-30 10:35:26 -0500 | [diff] [blame] | 17 | r""" |
| 18 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 19 | Get os-release info and return it as a dictionary. |
Michael Walsh | 6dbdfef | 2018-05-30 10:35:26 -0500 | [diff] [blame] | 20 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 21 | An example of the contents of /etc/os-release: |
Michael Walsh | 6dbdfef | 2018-05-30 10:35:26 -0500 | [diff] [blame] | 22 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 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/" |
Michael Walsh | 6dbdfef | 2018-05-30 10:35:26 -0500 | [diff] [blame] | 35 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 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" |
Michael Walsh | 6dbdfef | 2018-05-30 10:35:26 -0500 | [diff] [blame] | 40 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 41 | For the data shown above, this function will return the following |
| 42 | dictionary: |
Michael Walsh | 6dbdfef | 2018-05-30 10:35:26 -0500 | [diff] [blame] | 43 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 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 |
George Keishing | 1ad128c | 2021-10-28 09:38:37 -0500 | [diff] [blame] | 61 | |
| 62 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 63 | . Description of argument(s): |
| 64 | default_cmd A string command to be executed (e.g cat /etc/os-release). |
George Keishing | 1ad128c | 2021-10-28 09:38:37 -0500 | [diff] [blame] | 65 | |
Michael Walsh | 6dbdfef | 2018-05-30 10:35:26 -0500 | [diff] [blame] | 66 | """ |
| 67 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 68 | stdout, stderr, rc = bmc_ssh_utils.os_execute_command(default_cmd) |
Michael Walsh | 6dbdfef | 2018-05-30 10:35:26 -0500 | [diff] [blame] | 69 | |
| 70 | return var_funcs.key_value_outbuf_to_dict(stdout, delim="=", strip='"') |