def send_redfish_request(serverid,
                         uri,
                         service_port=0,
                         method="GET",
                         data=None,
                         encrypted=True,
                         connect_timeout=2):
    """
    Send a Redfish request to the REST service on a system.
    
    :param serverid: The ID for the server to send the request to.
    :param uri: The resource to access.
    :param service_port: The port the REST service is listening on.
    :param method: The HTTP request type to execute.
    :param data: The data to send with the request.
    :param encrypted: Flag indicating if HTTPS should be used for the requested instead of plain
        HTTP.
    :param connect_timeout: The number of seconds to wait for a connection before timing out the
    request.
        
    :return The data returned from the Redfish request.
    """

    access = get_blade_map_library()

    user = create_string_buffer(48)
    passwd = create_string_buffer(48)
    status = access.get_server_rest_access(byref(user), byref(passwd))
    if (status != 0):
        raise RuntimeError(
            "Failed to get server access credentials: {0}".format(status))

    try:
        ip = get_hostname_by_serverid(serverid)
        cmd = [
            "curl", "-sS", "-k", "-i", "-u",
            "{0}:{1}".format(user.value, passwd.value), "-X", method,
            "--connect-timeout", "{0}".format(connect_timeout)
        ]
        if (data):
            cmd.extend([
                "-d", "{0}".format(data), "-H",
                "Content-Type: application/json"
            ])
        cmd.append("{0}://{1}{2}{3}".format(
            "https" if (encrypted) else "http", ip, "" if
            (not service_port) else ":{0}".format(service_port), uri))

        response = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
        status, body = parse_http_response(response)
        if (status >= 300):
            raise rest_http_error(status, body)

        return body

    except subprocess.CalledProcessError as error:
        raise RuntimeError(error.output.strip())
Ejemplo n.º 2
0
def get_switch_ip_address():
    """
    Get the IP address for the REST interface of the management switch.
    
    Return:
        The switch IP address.
    """

    switchaccess_binary = get_blade_map_library()

    ip = create_string_buffer(20)

    if (switchaccess_binary.get_switch_address(ip) != 0):
        raise RuntimeError("Failed to get the switch IP address.")

    return ip.value
Ejemplo n.º 3
0
def get_switch_access():
    """
    Get the login credentials for the management switch.
    
    Return:
        The user name and password needed to access the switch.
    """

    switchaccess_binary = get_blade_map_library()

    username = create_string_buffer(48)
    password = create_string_buffer(48)

    if (switchaccess_binary.get_switch_access(username, password) != 0):
        raise RuntimeError("Failed to get switch access credentials.")

    return username.value, password.value
def get_sftp_access():
    """
    Get the login credentials for SFTP access to the server.
    
    :return The username and password to use for SFTP access.
    """

    blade_lib = get_blade_map_library()

    user = create_string_buffer(48)
    passwd = create_string_buffer(48)
    status = blade_lib.get_server_console_access(user, passwd)

    if (status != 0):
        raise RuntimeError("Failed to get SFTP login credentials.")

    return user.value, passwd.value
Ejemplo n.º 5
0
def get_server_access(isconsole):
    try:
        blademap_binary = get_blade_map_library ()    
        
        username = create_string_buffer(48)  
        password = create_string_buffer(48)

        if (isconsole != 1):
            ret = blademap_binary.get_server_command_access(username, password)
        else:
            ret = blademap_binary.get_server_console_access(username, password) 
        
        if ret != 0: 
            return None
        else:
            return username.value, password.value
        
    except Exception, e:  
        print("get_server_access() Exception ", e)    
        return None
Ejemplo n.º 6
0
def get_hostname_by_serverid(serverid):
    try:
        if (int(serverid) < 0) or (int(serverid) > 48):
            return -1

        blademap_binary = get_blade_map_library ()   
            
        hostname = create_string_buffer(48)  
        
        ret = blademap_binary.get_server_address(int(serverid), hostname) 
        
        if ret != 0: 
            return -1
        else:
            return hostname.value
        
    except Exception, e:  
        print("get_hostname_by_serverid() Exception ", e)
        
        return -1