Exemplo n.º 1
0
 def setUp(self):
     super(IloRibclTestCaseBeforeRisSupport, self).setUp()
     self.ilo = ribcl.IloClient("x.x.x.x", "admin", "Admin", 60, 443)
Exemplo n.º 2
0
def setup_ilo(user,
              password,
              host,
              tz=None,
              bootmode=None,
              check_privs=True,
              check_propagate=True,
              boot_from_dev=None,
              do_verbose=True,
              debug=False):
    """
        Connect to a host to valdiate its iLO settings.
        If the host doesnt support ilo/ribcl return 1
        else
            report on firmware settings
            optionally check the privileges
            optionally set the timezone
            check NTP propagate setting
            check bootmode and optionally set it
    """
    # Make a connection
    try:
        if (do_verbose):
            verbose('Connecting to %s' % host)
        ilo_client = ribcl.IloClient(host, user, password)
        # if we cant get at least power status we stop.
        ilo_client.get_host_power_status()
    except ribcl.IloConnectionError as e:
        error('Error(%s) connecting to %s: %s' % (type(e), host, e))
        return 1
    except Exception as e:
        error('Error(%s) connecting to %s: %s' % (type(e), host, e))
        return 1

    auth = 'BASIC ' + b64encode(user + ':' + password)
    headers = {'Authorization': auth, 'Content-Type': 'application/json'}

    mp = None
    try:
        if (do_verbose):
            verbose('Getting f/w version')
        info = ilo_client._execute_command('GET_FW_VERSION', 'RIB_INFO',
                                           'read')
        if (debug):
            verbose(info.keys())
        for key in info['GET_FW_VERSION'].keys():
            output("%s: %s" % (key, info['GET_FW_VERSION'][key]))
        # e.g.: 'iLO3' or 'iLO4'
        mp = info['GET_FW_VERSION'].get('MANAGEMENT_PROCESSOR')
    except Exception as e:
        error('Error(%s) getting f/w version from %s: %s' % (type(e), host, e))

    # Get the user privileges - fail if the check was requested and
    # if the required privileges arent present.
    # Its useful to fail early if you get a user without Administrator privs
    if check_privs:
        try:
            if (do_verbose):
                verbose('Getting user info')
            user_dict = {'USER_LOGIN': user}
            info = ilo_client._execute_command('GET_USER', 'USER_INFO', 'read',
                                               user_dict)
            if ('CONFIG_ILO_PRIV' in info['GET_USER']):
                if (info['GET_USER']['CONFIG_ILO_PRIV'] != 'Y'):
                    error('Insufficient privs: CONFIG_ILO_PRIV is required')
                    for key in info['GET_USER'].keys():
                        verbose("%s: %s" % (key, info['GET_USER'][key]))
                    return 1
            else:
                # Maybe its an older iLO?
                pass
        except Exception as e:
            error('Error(%s) getting user info from %s: %s' %
                  (type(e), host, e))

    # Get the timezone - dont fail if it cant be read.
    # UTC is NOT a valid timezone for iLO. Africa/Accra?
    if (do_verbose):
        verbose('Getting network settings')
    try:
        info = ilo_client._execute_command('GET_NETWORK_SETTINGS', 'RIB_INFO',
                                           'read')
        now_tz = info['GET_NETWORK_SETTINGS']['TIMEZONE']['VALUE']
        verbose('Current timezone is: %s' % tz)
        if (tz and now_tz != tz):
            if (do_verbose):
                verbose('Setting timezone=%s' % tz)
            try:
                set_tz(ilo_client, tz)
            except Exception as e:
                error('Error(%s) setting tz %s for %s: %s' %
                      (type(e), args.tz, args.host, e))
    except Exception as e:
        error('Error(%s) getting tz for %s: %s' % (type(e), host, e))

    # Get the propagate flag - dont fail if it cant be read.
    if (do_verbose):
        verbose('Getting global settings')
    try:
        propagate = get_propagate_time(ilo_client)
        verbose('Current time propagate setting is: %s' % propagate)

        if check_propagate and propagate != 'N':
            if (do_verbose):
                verbose('Setting time propagate OFF')
            try:
                set_propagate_time(ilo_client, 'N')
            except Exception as e:
                error('Error(%s) setting time propagate %s for %s: %s' %
                      (type(e), host, e))
            propagate = get_propagate_time(ilo_client)
            if propagate != 'N':
                verbose('Failed setting time propagate OFF')
    except Exception as e:
        error('Error(%s) getting time propagate for %s: %s' %
              (type(e), host, e))

    # bootmode = 'LEGACY'
    current_mode = None
    pending_mode = None
    if mp and mp == 'iLO4':
        try:
            # GET_SUPPORTED_BOOT_MODE = LEGACY_ONLY, UEFI_ONLY,
            #                           LEGACY_UEFI, UNKNOWN
            supported_modes = ilo_client.get_supported_boot_mode()
            if (do_verbose):
                verbose("Supported modes are: %s" % supported_modes)
            # Possible return values are LEGACY, UEFI, or UNKNOWN
            current_mode = ilo_client.get_current_boot_mode()
            pending_mode = ilo_client.get_pending_boot_mode()
            if (do_verbose):
                verbose("Current boot mode is: %s" % current_mode)
                verbose("Pending boot mode is: %s" % pending_mode)
            if bootmode is not None:
                if pending_mode != bootmode:
                    try:
                        if (do_verbose):
                            verbose("Setting *next* boot mode as: %s" %
                                    bootmode)
                        ilo_client.set_pending_boot_mode(bootmode)
                    except Exception as e:
                        error('Error(%s) setting boot_mode to %s for %s: %s' %
                              (type(e), bootmode, host, e))
        except ribcl.IloError as e:
            if 'Feature not supported' in repr(e):
                error(e)
        except Exception as e:
            error('Error(%s) getting current boot mode for %s: %s' %
                  (type(e), host, e))

    _process_bootmode(ilo_client, host, headers, boot_from_dev, current_mode,
                      pending_mode, do_verbose, debug)

    return 0