
客户机环境:
[opc@py-ol-75~]$ cat etc/oracle-release
OracleLinux Server release 7.5
[opc@py-ol-75~]$ uname -r
4.14.35-1818.3.3.el7uek.x86_64
安装设置Python
OCIPython SDK支持Python 2.7.5、3.5和更高的版本。
OracleLinux 7.5上已经缺省安装了Python 2.7.5,可以直接使用,本文就用2.7.5版来完成所有的操作。
[opc@py-ol-75~]$ which python
/usr/bin/python
[opc@py-ol-75~]$ python -V
Python2.7.5
[root@py-ol-75 opc]#yum install python-pip -y
[root@py-ol-75opc]# which pip
/bin/pip
[root@py-ol-75opc]# pip -V
pip 8.1.2from /usr/lib/python2.7/site-packages (python 2.7)
将PIP升级到最新版本
[root@py-ol-75opc]# pip install --upgrade pip
[root@py-ol-75opc]# pip -V
pip 18.1from /usr/lib/python2.7/site-packages/pip (python 2.7)
已经将pip从8.12升级到18.1。
下载和安装OCIPython SDK
注:Python SDK需要用到cryptography.io库,所以在安装Python SDK前,先确保cryptography库已经安装。
如果没有安装cryptography库,可以使用pip安装:
[root@py-ol-75opc]# pip install cryptography
Requirementalready satisfied: cryptography in /usr/lib64/python2.7/site-packages (1.7.2)
Requirementalready satisfied: idna>=2.0 in /usr/lib/python2.7/site-packages (fromcryptography) (2.5)
Requirementalready satisfied: pyasn1>=0.1.8 in /usr/lib/python2.7/site-packages (fromcryptography) (0.1.9)
Requirementalready satisfied: six>=1.4.1 in /usr/lib/python2.7/site-packages (fromcryptography) (1.9.0)
Requirementalready satisfied: setuptools in /usr/lib/python2.7/site-packages (fromcryptography) (18.0.1)
Requirementalready satisfied: enum34 in /usr/lib/python2.7/site-packages (fromcryptography) (1.0.4)
Requirementalready satisfied: ipaddress in /usr/lib/python2.7/site-packages (fromcryptography) (1.0.16)
Requirementalready satisfied: cffi>=1.4.1 in /usr/lib64/python2.7/site-packages (fromcryptography) (1.9.1)
Requirementalready satisfied: pycparser in /usr/lib/python2.7/site-packages (fromcffi>=1.4.1->cryptography) (2.14)
说明该库已经安装。
设置虚拟化环境
Oracle强烈建议在一个虚拟化环境中运行OCI Python SDK。虚拟化环境可以隔离SDK的依赖关系,并避免与已经安装的其他Python包发生任何潜在冲突。在Linux环境中,可以使用pip安装virtualenv 。
[root@py-ol-75opc]# pip install virtualenv
Collectingvirtualenv
Downloading https://files.pythonhosted.org/packages/7c/17/9b7b6cddfd255388b58c61e25b091047f6814183e1d63741c8df8dcd65a2/virtualenv-16.1.0-py2.py3-none-any.whl(1.9MB)
100% |████████████████████████████████|1.9MB 9.0MB/s
Installingcollected packages: virtualenv
Successfullyinstalled virtualenv-16.1.0
创建虚拟化环境
[root@py-ol-75opc]# virtualenv oci_sdk_env
Newpython executable in /home/opc/oci_sdk_env/bin/python2
Alsocreating executable in /home/opc/oci_sdk_env/bin/python
Installingsetuptools, pip, wheel...
done.
激活虚拟化环境
[root@py-ol-75opc]# source oci_sdk_env/bin/activate
(oci_sdk_env)[root@py-ol-75 opc]#
安装OCI PythonSDK
有两种方法安装,一种是直接用pip安装oci,另一种是从GitHub上下载SDK的zip包,解压后,通过用pip安装whl文件的方式安装。在这里我们采用第一种方法安装。
(oci_sdk_env)[root@py-ol-75 opc]# pip install oci
Collectingoci
Downloading https://files.pythonhosted.org/packages/8f/c4/85b7283e077868007bfa2daccd3d92bbaa73afe3fff1ca4891d27bf18269/oci-2.1.2-py2.py3-none-any.whl(1.5MB)
100% |████████████████████████████████|1.5MB 9.9MB/s
Successfullyinstalled asn1crypto-0.24.0 certifi-2018.11.29 cffi-1.11.5 configparser-3.5.0cryptography-2.4.2 enum34-1.1.6 idna-2.8 ipaddress-1.0.22 oci-2.1.2pyOpenSSL-17.4.0 pycparser-2.19 python-dateutil-2.7.3 pytz-2018.7 six-1.11.0
(oci_sdk_env)[root@py-ol-75 opc]# pip show oci
Name: oci
Version:2.1.2
Summary:Oracle Cloud Infrastructure Python SDK
Home-page:https://oracle-cloud-infrastructure-python-sdk.readthedocs.io/en/latest/index.html
Author:Oracle
Author-email:joe.levy@oracle.com
License:Universal Permissive License 1.0 or Apache License 2.0
Location:/home/opc/oci_sdk_env/lib/python2.7/site-packages
Requires:cryptography, pyOpenSSL, configparser, pytz, python-dateutil, certifi
Required-by:
注:OCI包已经安装,版本2.1.2。
验证OpenSSL版本
PythonSDK支持OpenSSL 1.0.1或更新的版本。使用以下命令验证已安装的OpenSSL版本。
(oci_sdk_env)[root@py-ol-75 opc]# python -c "import ssl;print(ssl.OPENSSL_VERSION)"
OpenSSL 1.0.2k-fips 26 Jan 2017
注:如果版本低于1.0.1,可以使用以下命令绕开版本问题:
# pip install requests[security]==2.18.4
至此OCI Python SDK已经配置完毕,现在我们可以使用Python SDK来管理OCI上的资源了。
使用Python SDK管理OCI上的资源,需要提前具备以下前置条件。
拥有一个Oracle Cloud Infrastructure账号。
在OCI账号下创建一个操作用户,把这个用户分配到一个group中,并授予相应的权限。
需要一对密钥来进行Signing API requests验证。在第二步中创建操作用户后,在该操作用户的主界面中,用公钥创建一个API Key,记住APK Key的Fingerprint,同时保存好私钥。在使用Python SDK管理OCI的资源时,需要使用操作用户、私钥和Fingerprint等信息,才能得到操作OCI资源的权限,并进行相关的操作。
注:在此提供一个shell 脚本,可以用来生成一套用于SigningAPI request的密钥。
############ genkey.sh ############ Begin
#!/bin/bash-x
echo"This script will create API keys for use with OCI in ~/.oci"
read -p"If there is already API keys present on the system then ctrl+c -otherwise press [Enter]"
mkdir -p~/.oci
cd ~/.oci
opensslgenrsa -out ~/.oci/oci_api_key.pem 2048
chmod0700 ~/.oci
chmod0600 ~/.oci/oci_api_key.pem
opensslrsa -pubout -in ~/.oci/oci_api_key.pem -out ~/.oci/oci_api_key_public.pem
cat~/.oci/oci_api_key_public.pem
opensslrsa -pubout -outform DER -in ~/.oci/oci_api_key.pem 2>/dev/null | opensslmd5 -c
opensslrsa -pubout -outform DER -in ~/.oci/oci_api_key.pem 2>/dev/null | opensslmd5 -c > ~/.oci/oci_api_key_fingerprint
############ genkey.sh ############ End
在linux中运行这个脚本后,将生成3个文件,其中:
公钥:oci_api_key_public.pem
私钥:oci_api_key.pem
Fingerprint:oci_api_key_fingerprint
测试Python SDK连接OCI
oci使用一个简单的字典来构建客户机和其他组件。我们可以手工构建这些配置文件,或者oci可以解析和验证配置文件。
下面是oci的配置模板:
>>>from oci.config import from_file
>>>config = from_file()
注:函数from_file有两个参数,file_location(config文件的存放路径,缺省值为’~/.oci/config‘)和profile_name(配置名,缺省值为'DEFAULT')。
我们可以在Python的交互界面,通过help命令来查看包和函数帮助信息。
# python
>>>help('oci.config.from_file')
Help onfunction from_file in oci.config:
oci.config.from_file= from_file(file_location='~/.oci/config', profile_name='DEFAULT')
Create a config dict from a file.
:param file_location: Path to the configfile. Defaults to ~/.oci/config and witha fallback to ~/.oraclebmc/config.
:param profile_name: The profile to loadfrom the config file. Defaults to"DEFAULT"
:return: A config dict that can be used tocreate clients.
(END)
Configure文件示例
##########
[DEFAULT]
user=ocid1.user.oc1..aaaaaaaat5nvwcna5j6aqzjcaty5eqbb6qt2jvpkanghtgdaqedqw3rynjq
fingerprint=20:3b:97:13:55:1c:5b:0d:d3:37:d8:50:4e:c5:3a:34
key_file=~/.oci/oci_api_key.pem
tenancy=ocid1.tenancy.oc1..aaaaaaaaba3pv6wkcr4jqae5f15p2b2m2yt2j6rx32uzr4h25vqstifsfdsq
region=us-ashburn-1
##########
注:
[DEFAULT]:DEFAULT预置信息,为缺省值。
user= 为OCI 操作用户的信息,这里为用户的OCID信息,可以从OCI的web 界面中获得。
fingerprint=为公钥的fingerprint,前面步骤中提供了genkey.sh脚本可以生成。
key_file=为密钥的存放路径,前面步骤中提供了genkey.sh脚本可以生成。
tenancy= OCI租户信息,这里为租户的OCID信息,可以从OCI的web界面中获得。
region= OCI资源所在的区域信息,前面步骤中提供了genkey.sh脚本可以生成。
编写测试脚本testoci.py,测试连接OCI
(oci_sdk_env)[root@py-ol-75 example]# cat testoci.py
importoci
config =oci.config.from_file("/home/opc/.oci/config","DEFAULT")
identity= oci.identity.IdentityClient(config)
user =identity.get_user(config["user"]).data
print(user)
注:配置文件为/home/opc/.oci/config,指定使用DEFAULT预置信息,该脚本连接OCI后,将打印出该操作用户的详细信息。
将config文件和公钥文件上传到相应的目录:
[root@py-ol-75opc]# mkdir .oci
[root@py-ol-75opc]# cd .oci
[root@py-ol-75.oci]# pwd
/home/opc/.oci
[root@py-ol-75.oci]# ll
total 8
-rw-r--r--.1 root root 300 Dec 5 03:40 config
-rw-r--r--.1 root root 1704 Dec 5 03:38oci_api_key.pem
执行测试脚本
(oci_sdk_env)[root@py-ol-75 example]# python testoci.py
{
"compartment_id":"ocid1.tenancy.oc1..aaaaaaaavhvs33qikfq76eui6j6ulg7osbsngq2x7kbh4i33ytfica66m7eq",
"defined_tags": {},
"description":"cheney.yu@outlook.com",
"freeform_tags": {},
"id":"ocid1.user.oc1..aaaaaaaavif7uld7rbcconhikqozr4lxalo5pkoqieos4zdmcyann6ltithq",
"inactive_status": null,
"lifecycle_state":"ACTIVE",
"name":"cheney.yu@outlook.com",
"time_created":"2018-10-30T07:02:05.982000+00:00"
}
成功输出操作用户的信息。




