def get_os_release_info():
    r"""
    Get release info from the OS and return as a dictionary.

    Example:

    ${release_info}=  Get OS Release Info
    Rprint Vars  release_info

    Output:
    release_info:
      [name]:                                         Red Hat Enterprise Linux Server
      [version]:                                      7.6 (Maipo)
      [id]:                                           rhel
      [id_like]:                                      fedora
      [variant]:                                      Server
      [variant_id]:                                   server
      [version_id]:                                   7.6
      [pretty_name]:                                  Red Hat Enterprise Linux Server 7.6 (Maipo)
      [ansi_color]:                                   0;31
      [cpe_name]:                                     cpe:/o:redhat:enterprise_linux:7.6:GA:server
      [home_url]:                                     https://www.redhat.com/
      [bug_report_url]:                               https://bugzilla.redhat.com/
      [redhat_bugzilla_product]:                      Red Hat Enterprise Linux 7
      [redhat_bugzilla_product_version]:              7.6
      [redhat_support_product]:                       Red Hat Enterprise Linux
      [redhat_support_product_version]:               7.6
    """

    out_buf, stderr, rc = bsu.os_execute_command('cat /etc/os-release')
    return vf.key_value_outbuf_to_dict(out_buf, delim="=", strip='"')
Exemple #2
0
def get_os_release_info():
    r"""

    Get os-release info and return it as a dictionary.

    An example of the contents of /etc/os-release:

    NAME="Red Hat Enterprise Linux Server"
    VERSION="7.5 (Maipo)"
    ID="rhel"
    ID_LIKE="fedora"
    VARIANT="Server"
    VARIANT_ID="server"
    VERSION_ID="7.5"
    PRETTY_NAME="Red Hat Enterprise Linux Server 7.5 Beta (Maipo)"
    ANSI_COLOR="0;31"
    CPE_NAME="cpe:/o:redhat:enterprise_linux:7.5:beta:server"
    HOME_URL="https://www.redhat.com/"
    BUG_REPORT_URL="https://bugzilla.redhat.com/"

    REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 7"
    REDHAT_BUGZILLA_PRODUCT_VERSION=7.5
    REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux"
    REDHAT_SUPPORT_PRODUCT_VERSION="7.5 Beta"

    For the data shown above, this function will return the following
    dictionary:

    result:
      [name]:                             Red Hat Enterprise Linux Server
      [version]:                          7.5 (Maipo)
      [id]:                               rhel
      [id_like]:                          fedora
      [variant]:                          Server
      [variant_id]:                       server
      [version_id]:                       7.5
      [pretty_name]:                      Red Hat Enterprise Linux Server 7.5 Beta (Maipo)
      [ansi_color]:                       0;31
      [cpe_name]:                         cpe:/o:redhat:enterprise_linux:7.5:beta:server
      [home_url]:                         https://www.redhat.com/
      [bug_report_url]:                   https://bugzilla.redhat.com/
      [redhat_bugzilla_product]:          Red Hat Enterprise Linux 7
      [redhat_bugzilla_product_version]:  7.5
      [redhat_support_product]:           Red Hat Enterprise Linux
      [redhat_support_product_version]:   7.5 Beta
    """

    stdout, stderr, rc =\
        bmc_ssh_utils.os_execute_command("cat /etc/os-release")

    return var_funcs.key_value_outbuf_to_dict(stdout, delim="=", strip='"')
Exemple #3
0
def execute_ipmi_cmd(cmd_string,
                     ipmi_cmd_type='inband',
                     print_output=1,
                     ignore_err=0):
    r"""
    Run the given command string as an IPMI command and return the stdout,
    stderr and the return code.

    Description of argument(s):
    cmd_string                      The command string to be run as an IPMI
                                    command.
    ipmi_cmd_type                   'inband' or 'external'.
    print_output                    If this is set, this function will print
                                    the stdout/stderr generated by
                                    the IPMI command.
    ignore_err                      Ignore error means that a failure
                                    encountered by running the command
                                    string will not be raised as a python
                                    exception.
    """

    if ipmi_cmd_type == 'inband':
        IPMI_INBAND_CMD = BuiltIn().get_variable_value("${IPMI_INBAND_CMD}")
        cmd_buf = IPMI_INBAND_CMD + " " + cmd_string
        return bsu.os_execute_command(cmd_buf,
                                      print_out=print_output,
                                      ignore_err=ignore_err)

    if ipmi_cmd_type == 'external':
        cmd_buf = BuiltIn().get_variable_value("${IPMI_EXT_CMD}")
        IPMI_USER_OPTIONS =\
            BuiltIn().get_variable_value("${IPMI_USER_OPTIONS}")
        if IPMI_USER_OPTIONS != "":
            cmd_buf += " " + IPMI_USER_OPTIONS
        cmd_buf += " -P " + BuiltIn().get_variable_value("${IPMI_PASSWORD}")
        cmd_buf += " " + BuiltIn().get_variable_value("${HOST}")
        cmd_buf += " " + BuiltIn().get_variable_value("${OPENBMC_HOST}")
        cmd_buf += " " + cmd_string
        rc, stdout, stderr = gc.shell_cmd(cmd_buf,
                                          print_output=print_output,
                                          ignore_err=ignore_err,
                                          return_stderr=1)
        return stdout, stderr, rc
Exemple #4
0
def execute_ipmi_cmd(cmd_string,
                     ipmi_cmd_type='inband',
                     print_output=1,
                     ignore_err=0,
                     **options):
    r"""
    Run the given command string as an IPMI command and return the stdout,
    stderr and the return code.

    Description of argument(s):
    cmd_string                      The command string to be run as an IPMI
                                    command.
    ipmi_cmd_type                   'inband' or 'external'.
    print_output                    If this is set, this function will print
                                    the stdout/stderr generated by
                                    the IPMI command.
    ignore_err                      Ignore error means that a failure
                                    encountered by running the command
                                    string will not be raised as a python
                                    exception.
    options                         These are passed directly to the
                                    create_ipmi_ext_command_string function.
                                    See that function's prolog for details.
    """

    if ipmi_cmd_type == 'inband':
        IPMI_INBAND_CMD = BuiltIn().get_variable_value("${IPMI_INBAND_CMD}")
        cmd_buf = IPMI_INBAND_CMD + " " + cmd_string
        return bsu.os_execute_command(cmd_buf,
                                      print_out=print_output,
                                      ignore_err=ignore_err)

    if ipmi_cmd_type == 'external':
        cmd_buf = ic.create_ipmi_ext_command_string(cmd_string, **options)
        rc, stdout, stderr = gc.shell_cmd(cmd_buf,
                                          print_output=print_output,
                                          ignore_err=ignore_err,
                                          return_stderr=1)
        return stdout, stderr, rc
Exemple #5
0
def get_os_state(os_host="",
                 os_username="",
                 os_password="",
                 req_states=default_os_req_states,
                 os_up=True,
                 quiet=None):
    r"""
    Get component states for the operating system such as ping, login,
    etc, put them into a dictionary and return them to the caller.

    Note that all substate values are strings.

    Description of argument(s):
    os_host      The DNS name or IP address of the operating system.
                 This defaults to global ${OS_HOST}.
    os_username  The username to be used to login to the OS.
                 This defaults to global ${OS_USERNAME}.
    os_password  The password to be used to login to the OS.
                 This defaults to global ${OS_PASSWORD}.
    req_states   This is a list of states whose values are being requested by
                 the caller.
    os_up        If the caller knows that the os can't possibly be up, it can
                 improve performance by passing os_up=False.  This function
                 will then simply return default values for all requested os
                 sub states.
    quiet        Indicates whether status details (e.g. curl commands) should
                 be written to the console.
                 Defaults to either global value of ${QUIET} or to 1.
    """

    quiet = int(gp.get_var_value(quiet, 0))

    # Set parm defaults where necessary and validate all parms.
    if os_host == "":
        os_host = BuiltIn().get_variable_value("${OS_HOST}")
    error_message = gv.valid_value(os_host, invalid_values=[None, ""])
    if error_message != "":
        BuiltIn().fail(gp.sprint_error(error_message))

    if os_username == "":
        os_username = BuiltIn().get_variable_value("${OS_USERNAME}")
    error_message = gv.valid_value(os_username, invalid_values=[None, ""])
    if error_message != "":
        BuiltIn().fail(gp.sprint_error(error_message))

    if os_password == "":
        os_password = BuiltIn().get_variable_value("${OS_PASSWORD}")
    error_message = gv.valid_value(os_password, invalid_values=[None, ""])
    if error_message != "":
        BuiltIn().fail(gp.sprint_error(error_message))

    invalid_req_states = [sub_state for sub_state in req_states
                          if sub_state not in valid_os_req_states]
    if len(invalid_req_states) > 0:
        error_message = "The following req_states are not supported:\n" +\
            gp.sprint_var(invalid_req_states)
        BuiltIn().fail(gp.sprint_error(error_message))

    # Initialize all substate values supported by this function.
    os_ping = 0
    os_login = 0
    os_run_cmd = 0

    if os_up:
        if 'os_ping' in req_states:
            # See if the OS pings.
            rc, out_buf = gc.shell_cmd("ping -c 1 -w 2 " + os_host,
                                       print_output=0, show_err=0,
                                       ignore_err=1)
            if rc == 0:
                os_ping = 1

        # Programming note: All attributes which do not require an ssh login
        # should have been processed by this point.
        master_req_login = ['os_login', 'os_run_cmd']
        req_login = [sub_state for sub_state in req_states if sub_state in
                     master_req_login]
        must_login = (len(req_login) > 0)

        if must_login:
            output, stderr, rc = bsu.os_execute_command("uptime", quiet=quiet,
                                                        ignore_err=1,
                                                        time_out=20)
            if rc == 0:
                os_login = 1
                os_run_cmd = 1
            else:
                gp.dprint_vars(output, stderr)
                gp.dprint_vars(rc, 1)

    os_state = DotDict()
    for sub_state in req_states:
        cmd_buf = "os_state['" + sub_state + "'] = str(" + sub_state + ")"
        exec(cmd_buf)

    return os_state
def get_hard_disk_info(device="/dev/sdb"):
    r"""
    Get firmware information for the given device on the OS and return it as a
    dictionary.

    Description of argument(s):
    device                          The device to be passed to the hdparm and
                                    lsblk commands (e.g. "/dev/sdb").

    Example result:

    sda_info:
      [model_number]:                        MTFDDAK1T9TCB 00LY461 00LY570IBM
      [serial_number]:                       179C413F
      [firmware_revision]:                   MJ06
      [transport]:                           Serial, ATA8-AST, SATA 1.0a, SATA II Extensions, SATA Rev 2.5,
                                             SATA Rev 2.6, SATA Rev 3.0
      [used]:                                unknown (minor revision code 0x006d)
      [supported]:                           enhanced erase
      [likely_used]:                         10
      [lba_user_addressable_sectors]:        268435455
      [lba48_user_addressable_sectors]:      3750748848
      [logical_sector_size]:                 512 bytes
      [physical_sector_size]:                4096 bytes
      [logical_sector-0_offset]:             0 bytes
      [device_size_with_m_=_1024*1024]:      1831420 MBytes
      [device_size_with_m_=_1000*1000]:      1920383 MBytes (1920 GB)
      [form_factor]:                         2.5 inch
      [nominal_media_rotation_rate]:         Solid State Device
      [queue_depth]:                         32
      [standby_timer_values]:                spec'd by Standard, with device specific minimum
      [r/w_multiple_sector_transfer]:        Max = 16 Current = 16
      [advanced_power_management_level]:     254
      [dma]:                                 mdma0 mdma1 mdma2 udma0 udma1 udma2 udma3 udma4 udma5 *udma6
      [cycle_time]:                          no flow control=120ns IORDY flow control=120ns
      [pio]:                                 pio0 pio1 pio2 pio3 pio4
      [security]:
      [not_expired]:                         security count
      [logical_unit_wwn_device_identifier]:  500a0751179c413f
      [naa]:                                 5
      [ieee_oui]:                            00a075
      [unique_id]:                           1179c413f
      [checksum]:                            correct
      [name]:                                sda1
      [maj:min]:                             8:1
      [rm]:                                  1
      [size]:                                4M
      [ro]:                                  0
      [type]:                                part
      [mountpoint]:

    """

    cmd_buf = "hdparm -I " + device + " | egrep \":.+\" | sed -re" +\
        " \"s/[ \t]+/ /g\""
    stdout, stderr, rc = bsu.os_execute_command(cmd_buf)

    firmware_dict = vf.key_value_outbuf_to_dict(stdout)

    cmd_buf = "lsblk -P " + device + " | sed -re 's/\" /\"\\n/g'"
    stdout, stderr, rc = bsu.os_execute_command(cmd_buf)
    firmware_dict.update(vf.key_value_outbuf_to_dict(stdout, delim='=',
                                                     strip=" \""))

    return firmware_dict
def get_os_ethtool(interface_name):
    r"""
    Get OS 'ethtool' output for the given interface_name and return it as a
    dictionary.

    Settings for enP52p1s0f0:
          Supported ports: [ TP ]
          Supported link modes:   10baseT/Half 10baseT/Full
                                  100baseT/Half 100baseT/Full
                                  1000baseT/Half 1000baseT/Full
          Supported pause frame use: No
          Supports auto-negotiation: Yes
          Supported FEC modes: Not reported
          Advertised link modes:  10baseT/Half 10baseT/Full
                                  100baseT/Half 100baseT/Full
                                  1000baseT/Half 1000baseT/Full
          Advertised pause frame use: Symmetric
          Advertised auto-negotiation: Yes
          Advertised FEC modes: Not reported
          Speed: Unknown!
          Duplex: Unknown! (255)
          Port: Twisted Pair
          PHYAD: 1
          Transceiver: internal
          Auto-negotiation: on
          MDI-X: Unknown
          Supports Wake-on: g
          Wake-on: g
          Current message level: 0x000000ff (255)
                                 drv probe link timer ifdown ifup rx_err tx_err
          Link detected: no

    Given that data, this function will return the following dictionary.

    ethtool_dict:
      [supported_ports]:             [ TP ]
      [supported_link_modes]:
        [supported_link_modes][0]:   10baseT/Half 10baseT/Full
        [supported_link_modes][1]:   100baseT/Half 100baseT/Full
        [supported_link_modes][2]:   1000baseT/Half 1000baseT/Full
      [supported_pause_frame_use]:   No
      [supports_auto-negotiation]:   Yes
      [supported_fec_modes]:         Not reported
      [advertised_link_modes]:
        [advertised_link_modes][0]:  10baseT/Half 10baseT/Full
        [advertised_link_modes][1]:  100baseT/Half 100baseT/Full
        [advertised_link_modes][2]:  1000baseT/Half 1000baseT/Full
      [advertised_pause_frame_use]:  Symmetric
      [advertised_auto-negotiation]: Yes
      [advertised_fec_modes]:        Not reported
      [speed]:                       Unknown!
      [duplex]:                      Unknown! (255)
      [port]:                        Twisted Pair
      [phyad]:                       1
      [transceiver]:                 internal
      [auto-negotiation]:            on
      [mdi-x]:                       Unknown
      [supports_wake-on]:            g
      [wake-on]:                     g
      [current_message_level]:       0x000000ff (255)
      [drv_probe_link_timer_ifdown_ifup_rx_err_tx_err]:<blank>
      [link_detected]:               no
    """

    # Using sed and tail to massage the data a bit before running
    # key_value_outbuf_to_dict.
    cmd_buf = "ethtool " + interface_name +\
        " | sed -re 's/(.* link modes:)(.*)/\\1\\n\\2/g' | tail -n +2"
    stdout, stderr, rc = bsu.os_execute_command(cmd_buf)
    result = vf.key_value_outbuf_to_dict(stdout, process_indent=1, strip=" \t")

    return result
def get_os_ethtool(interface_name):
    r"""
    Get OS 'ethtool' output for the given interface_name and return it as a
    dictionary.

    Settings for enP52p1s0f0:
          Supported ports: [ TP ]
          Supported link modes:   10baseT/Half 10baseT/Full
                                  100baseT/Half 100baseT/Full
                                  1000baseT/Half 1000baseT/Full
          Supported pause frame use: No
          Supports auto-negotiation: Yes
          Supported FEC modes: Not reported
          Advertised link modes:  10baseT/Half 10baseT/Full
                                  100baseT/Half 100baseT/Full
                                  1000baseT/Half 1000baseT/Full
          Advertised pause frame use: Symmetric
          Advertised auto-negotiation: Yes
          Advertised FEC modes: Not reported
          Speed: Unknown!
          Duplex: Unknown! (255)
          Port: Twisted Pair
          PHYAD: 1
          Transceiver: internal
          Auto-negotiation: on
          MDI-X: Unknown
          Supports Wake-on: g
          Wake-on: g
          Current message level: 0x000000ff (255)
                                 drv probe link timer ifdown ifup rx_err tx_err
          Link detected: no

    Given that data, this function will return the following dictionary.

    ethtool_dict:
      [supported_ports]:             [ TP ]
      [supported_link_modes]:
        [supported_link_modes][0]:   10baseT/Half 10baseT/Full
        [supported_link_modes][1]:   100baseT/Half 100baseT/Full
        [supported_link_modes][2]:   1000baseT/Half 1000baseT/Full
      [supported_pause_frame_use]:   No
      [supports_auto-negotiation]:   Yes
      [supported_fec_modes]:         Not reported
      [advertised_link_modes]:
        [advertised_link_modes][0]:  10baseT/Half 10baseT/Full
        [advertised_link_modes][1]:  100baseT/Half 100baseT/Full
        [advertised_link_modes][2]:  1000baseT/Half 1000baseT/Full
      [advertised_pause_frame_use]:  Symmetric
      [advertised_auto-negotiation]: Yes
      [advertised_fec_modes]:        Not reported
      [speed]:                       Unknown!
      [duplex]:                      Unknown! (255)
      [port]:                        Twisted Pair
      [phyad]:                       1
      [transceiver]:                 internal
      [auto-negotiation]:            on
      [mdi-x]:                       Unknown
      [supports_wake-on]:            g
      [wake-on]:                     g
      [current_message_level]:       0x000000ff (255)
      [drv_probe_link_timer_ifdown_ifup_rx_err_tx_err]:<blank>
      [link_detected]:               no
    """

    # Using sed and tail to massage the data a bit before running
    # key_value_outbuf_to_dict.
    cmd_buf = "ethtool " + interface_name +\
        " | sed -re 's/(.* link modes:)(.*)/\\1\\n\\2/g' | tail -n +2"
    stdout, stderr, rc = bsu.os_execute_command(cmd_buf)
    result = vf.key_value_outbuf_to_dict(stdout, process_indent=1, strip=" \t")

    return result
Exemple #9
0
def get_hard_disk_info(device="/dev/sdb"):
    r"""
    Get firmware information for the given device on the OS and return it as a
    dictionary.

    Description of argument(s):
    device                          The device to be passed to the hdparm and
                                    lsblk commands (e.g. "/dev/sdb").

    Example result:

    sda_info:
      [model_number]:                        MTFDDAK1T9TCB 00LY461 00LY570XXX
      [serial_number]:                       179C413F
      [firmware_revision]:                   MJ06
      [transport]:                           Serial, ATA8-AST, SATA 1.0a, SATA II Extensions, SATA Rev 2.5,
                                             SATA Rev 2.6, SATA Rev 3.0
      [used]:                                unknown (minor revision code 0x006d)
      [supported]:                           enhanced erase
      [likely_used]:                         10
      [lba_user_addressable_sectors]:        268435455
      [lba48_user_addressable_sectors]:      3750748848
      [logical_sector_size]:                 512 bytes
      [physical_sector_size]:                4096 bytes
      [logical_sector-0_offset]:             0 bytes
      [device_size_with_m_=_1024*1024]:      1831420 MBytes
      [device_size_with_m_=_1000*1000]:      1920383 MBytes (1920 GB)
      [form_factor]:                         2.5 inch
      [nominal_media_rotation_rate]:         Solid State Device
      [queue_depth]:                         32
      [standby_timer_values]:                spec'd by Standard, with device specific minimum
      [r/w_multiple_sector_transfer]:        Max = 16 Current = 16
      [advanced_power_management_level]:     254
      [dma]:                                 mdma0 mdma1 mdma2 udma0 udma1 udma2 udma3 udma4 udma5 *udma6
      [cycle_time]:                          no flow control=120ns IORDY flow control=120ns
      [pio]:                                 pio0 pio1 pio2 pio3 pio4
      [security]:
      [not_expired]:                         security count
      [logical_unit_wwn_device_identifier]:  500a0751179c413f
      [naa]:                                 5
      [ieee_oui]:                            00a075
      [unique_id]:                           1179c413f
      [checksum]:                            correct
      [name]:                                sda1
      [maj:min]:                             8:1
      [rm]:                                  1
      [size]:                                4M
      [ro]:                                  0
      [type]:                                part
      [mountpoint]:

    """

    cmd_buf = "hdparm -I " + device + " | egrep \":.+\" | sed -re" +\
        " \"s/[ \t]+/ /g\""
    stdout, stderr, rc = bsu.os_execute_command(cmd_buf)

    firmware_dict = vf.key_value_outbuf_to_dict(stdout)

    cmd_buf = "lsblk -P " + device + " | sed -re 's/\" /\"\\n/g'"
    stdout, stderr, rc = bsu.os_execute_command(cmd_buf)
    firmware_dict.update(
        vf.key_value_outbuf_to_dict(stdout, delim='=', strip=" \""))

    return firmware_dict
Exemple #10
0
def get_lan_print_dict():
    r"""
    Get IPMI 'lan print' output and return it as a dictionary.

    Here is an example of the IPMI lan print output:

    Set in Progress         : Set Complete
    Auth Type Support       : MD5
    Auth Type Enable        : Callback : MD5
                            : User     : MD5
                            : Operator : MD5
                            : Admin    : MD5
                            : OEM      : MD5
    IP Address Source       : Static Address
    IP Address              : x.x.x.x
    Subnet Mask             : x.x.x.x
    MAC Address             : xx:xx:xx:xx:xx:xx
    Default Gateway IP      : x.x.x.x
    802.1q VLAN ID          : Disabled
    Cipher Suite Priv Max   : Not Available
    Bad Password Threshold  : Not Available

    Given that data, this function will return the following dictionary.

    lan_print_dict:
      [Set in Progress]:                              Set Complete
      [Auth Type Support]:                            MD5
      [Auth Type Enable]:
        [Callback]:                                   MD5
        [User]:                                       MD5
        [Operator]:                                   MD5
        [Admin]:                                      MD5
        [OEM]:                                        MD5
      [IP Address Source]:                            Static Address
      [IP Address]:                                   x.x.x.x
      [Subnet Mask]:                                  x.x.x.x
      [MAC Address]:                                  xx:xx:xx:xx:xx:xx
      [Default Gateway IP]:                           x.x.x.x
      [802.1q VLAN ID]:                               Disabled
      [Cipher Suite Priv Max]:                        Not Available
      [Bad Password Threshold]:                       Not Available

    """

    IPMI_INBAND_CMD = BuiltIn().get_variable_value("${IPMI_INBAND_CMD}")

    # Notice in the example of data above that 'Auth Type Enable' needs some
    # special processing.  We essentially want to isolate its data and remove
    # the 'Auth Type Enable' string so that key_value_outbuf_to_dict can
    # process it as a sub-dictionary.
    cmd_buf = IPMI_INBAND_CMD + " lan print | grep -E '^(Auth Type Enable)" +\
        "?[ ]+: ' | sed -re 's/^(Auth Type Enable)?[ ]+: //g'"
    stdout1, stderr, rc = bsu.os_execute_command(cmd_buf)

    # Now get the remainder of the data and exclude the lines with no field
    # names (i.e. the 'Auth Type Enable' sub-fields).
    cmd_buf = IPMI_INBAND_CMD + " lan print | grep -E -v '^[ ]+: '"
    stdout2, stderr, rc = bsu.os_execute_command(cmd_buf)

    # Make auth_type_enable_dict sub-dictionary...
    auth_type_enable_dict = vf.key_value_outbuf_to_dict(stdout1,
                                                        to_lower=0,
                                                        underscores=0)

    # Create the lan_print_dict...
    lan_print_dict = vf.key_value_outbuf_to_dict(stdout2,
                                                 to_lower=0,
                                                 underscores=0)
    # Re-assign 'Auth Type Enable' to contain the auth_type_enable_dict.
    lan_print_dict['Auth Type Enable'] = auth_type_enable_dict

    return lan_print_dict