Redfish:Accessing Redfish enabled systems using Python

From Define Wiki
Jump to navigation Jump to search

Introduction

  • This documents contains preparation on the OS to make redfish runs from the OS command line console.
  • One sample python script is provided, with sample output.
  • As of this writing, only Red Hat 6.x is supported (tested)
  • This document only covers additional steps needed to make redfish run on the OS. It does not cover OS installation instructions.
  • This was done on a vanilla CentOS 6.7 installation

Installation of Chrome Browser

  • Not actually vital (or even necessary for the following) but is useful for when you have to do individual manual test, launching RESTful API commands, etc
  • Installation must be done as root
[root@localhost ~]# cd Downloads/
[root@localhost Downloads]# wget http://chrome.richardlloyd.org.uk/install_chrome.sh
--2016-01-11 03:50:00--  http://chrome.richardlloyd.org.uk/install_chrome.sh
Resolving chrome.richardlloyd.org.uk... 193.110.246.53
Connecting to chrome.richardlloyd.org.uk|193.110.246.53|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 79768 (78K) [application/x-sh]
Saving to: “install_chrome.sh”

100%[================================================================================>] 79,768      --.-K/s   in 0.1s    

2016-01-11 03:50:01 (541 KB/s) - “install_chrome.sh” saved [79768/79768]

[root@localhost Downloads]# chmod +x install_chrome.sh 
[root@localhost Downloads]# ./install_chrome.sh

Installation of Python 2.7

  • Next, you will need to install Python 2.7, since Red Hat default Python version is 2.6
  • Warning: Red Hat OS depends on Python version 2.6, therefore, Python version 2.7 must coexist, and you are not allowed to replace version 2.6
  • This should be done as root
[root@localhost Downloads]# yum -y groupinstall "development tools"
[root@localhost Downloads]# yum -y install zlib-devel openssl openssl-devel
[root@localhost Downloads]# mkdir tmp
[root@localhost Downloads]# cd tmp
[root@localhost tmp]# wget http://python.org/ftp/python/2.7.3/Python-2.7.3.tgz
[root@localhost tmp]# tar zxvf Python-2.7.3.tgz
[root@localhost tmp]# cd Python-2.7.3
[root@localhost Python-2.7.3]# ./configure --prefix=/opt/python2.7 --enable-shared
[root@localhost Python-2.7.3]# make
[root@localhost Python-2.7.3]# make altinstall
[root@localhost Python-2.7.3]# echo "/opt/python2.7/lib" >> /etc/ld.so.conf.d/opt-python2.7.conf
[root@localhost Python-2.7.3]# ldconfig

Setup Path

  • Go to your home directory, then edit your .bashrc file
  • Note: You don’t have to be root to edit .bashrc, since this is your own account’s file
  • Add this line to your .bashrc file:

export PATH=${PATH}:/opt/python2.7/bin

  • For example:
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific aliases and functions
export PATH=${PATH}:/opt/python2.7/bin
  • Then source .bashrc
[jon@localhost ~]$ source ./.bashrc
  • Confirm the Python 2.7 binary directory is now in your path:
[jon@localhost ~]$ echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/jon/bin:/opt/python2.7/bin

Install PIP 2.7

  • Installation must be done as root
[root@localhost ~]# cd /opt/python2.7/bin/
[root@localhost bin]# wget https://bootstrap.pypa.io/get-pip.py
[root@localhost bin]# ./python2.7 get-pip.py
Collecting pip
/tmp/tmpWViYfj/pip.zip/pip/_vendor/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  Downloading pip-7.1.2-py2.py3-none-any.whl (1.1MB)
    100% |████████████████████████████████| 1.1MB 306kB/s 
Collecting setuptools
  Downloading setuptools-19.2-py2.py3-none-any.whl (463kB)
    100% |████████████████████████████████| 466kB 719kB/s 
Collecting wheel
  Downloading wheel-0.26.0-py2.py3-none-any.whl (63kB)
    100% |████████████████████████████████| 65kB 4.5MB/s 
Installing collected packages: pip, setuptools, wheel
Successfully installed pip-7.1.2 setuptools-19.2 wheel-0.26.0
/tmp/tmpWViYfj/pip.zip/pip/_vendor/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  • NB: If wget command does not work, use wget --no-check-certificate command

Python 'requests' package

[root@localhost bin]# ./pip2.7 install requests
Collecting requests
/opt/python2.7/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
  Downloading requests-2.9.1-py2.py3-none-any.whl (501kB)
    100% |████████████████████████████████| 503kB 585kB/s 
Installing collected packages: requests
Successfully installed requests-2.9.1

Sample Script

Script code

  • This is our example script
# File: redfish_sample_cmd.py
# Author: Khushali Shah
# Date: 07-06-2015
#
# Description:
#     This script shows example of how to use redfish using
# command line.

import requests
import pprint
import time
import json
import sys

requests.packages.urllib3.disable_warnings()

if (len(sys.argv) != 2):
    print "\n **** ERROR **** Must pass IP address"
    print "     python2.7 redfish_sample_cmd.py ip_address"
    sys.exit(-1)

ip_address = sys.argv[1]

# Get authentication token.
# Note: 
#     user and password are default values: ADMIN, ADMIN
# Please modify them to match yours.
url = "https://%s/redfish/v1/SessionService/Sessions/" % (ip_address)
payload = {"UserName":"ADMIN","Password":"ADMIN"}
my_headers = {'content-type': 'application/json'}

response = requests.post(url, data=json.dumps(payload), headers=my_headers, verify=False)

# Get the response code, should be 201 
print "\n Response:\n %s" % response

# Next, get authorization token
Token = response.headers['x-auth-token ']
print "\n Authorization token: %s" % (Token)

# Use authorization token for the next command
# We use Chassis command as an example here
url = "https://%s/redfish/v1/Chassis/1" % (ip_address)
token_headers = {'X-Auth-Token': Token}

response1 = requests.get(url, headers=token_headers, verify=False).json()
print "\n Response:\n"
print (json.dumps(response1, indent=2))

Run sample script

[jon@localhost Desktop]$ python2.7 redfish_sample_cmd.py 10.0.0.181

Sample output:

 Response:
 <Response [201]>

 Authorization token: zzsh0qbs624u6pk4tj08ynh1glrmrl5t

 Response:

{
  "@odata.type": "#Chassis.1.0.0.Chassis", 
  "SKU": "", 
  "Name": "Computer System Chassis", 
  "PartNumber": "CSE-219UTS-R1K02P-T", 
  "AssetTag": "NONE", 
  "Links": {
    "ManagedBy": [
      {
        "@odata.id": "/redfish/v1/Managers/1"
      }
    ], 
    "ContainedBy": {
      "@odata.id": "/redfish/v1/Chassis/Rack1"
    }, 
    "ComputerSystems": [
      {
        "@odata.id": "/redfish/v1/Systems/1"
      }
    ]
  }, 
  "SerialNumber": "C219UAD50A20330", 
  "@odata.id": "/redfish/v1/Chassis/1", 
  "@odata.context": "/redfish/v1/$metadata#Chassis/Members/$entity", 
  "Status": {
    "State": "Enabled", 
    "Health": "OK"
  }, 
  "Thermal": {
    "@odata.id": "/redfish/v1/Chassis/1/Thermal"
  }, 
  "Power": {
    "@odata.id": "/redfish/v1/Chassis/1/Power"
  }, 
  "@Redfish.Copyright": "Copyright \u00a9 2014-2015 Distributed Management Task Force, Inc. (DMTF). All rights reserved.", 
  "IndicatorLED": "Off", 
  "Oem": {
    "OemFan": {
      "@odata.type": "#OemFan.Chassis", 
      "FanMode": "Optimal", 
      "FanMode@Redfish.AllowableValues": [
        "FullSpeed", 
        "Optimal", 
        "PUE2", 
        "HeavyIO"
      ]
    }
  }, 
  "ChassisType": "RackMount", 
  "Model": "NONE", 
  "Id": "1", 
  "Manufacturer": "Supermicro"
}

Fixing Python / SSL Warnings

  • Certain Python platforms (specifically, versions of Python earlier than 2.7.9) have restrictions in their ssl module that limit the configuration that urllib3 can apply. In particular, this can cause HTTPS requests that would succeed on more featureful platforms to fail, and can cause certain security features to be unavailable.
  • Additionally, our example script performs unverified HTTPS requests, so you may see errors like the following:
/opt/python2.7/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py:791: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
  InsecureRequestWarning)
  • The general advice seems to be "upgrade to a more modern version of Python" which isn't always possible.
  • If upgrading to a more modern version of Python is not an option, the warnings can be disabled.

Disabling Python / SSL Warnings

import urllib3
urllib3.disable_warnings()
  • However, this won't work in our example script as it uses a separate instance of urllib3, vendored inside of requests. In which case, you'll want to try including the following in your main script (or from wherever requests is imported):
import requests
requests.packages.urllib3.disable_warnings()