OpenStack 初探(三) -- OpenStack API 初探(呼叫API獲取OpenStack資訊)
本篇部落格緊接前兩篇《OpenStack All-In-One模式部署(初學OpenStack必備)》和《在OpenStack中launch一個虛擬機器例項》,當然亦可獨立閱讀,介紹如何呼叫OpenStack的Restful API來獲取系統的一些資訊。僅僅只是初探,給出了兩個API的呼叫全過程。
最開始寫這篇的時候,是寫給同事查閱的,英文版的(但是是極簡單的英文)。後續會給出中文版,先粘貼出來。
1.Some environment variables setting
1.1 Download OpenStack RC file. Login to the OpenStack dashboard as administrator. Choose the admin project (the button is on the left-top of the main page) and then download the OpenStack RC V3 file (the button is on the right-top of the main page).
1.2 Modify the RC file:
1.2.1 Comment the three lines below:
#echo “Please enter your OpenStack Password for project $OS_PROJECT_NAME as user $OS_USERNAME: ”
#read -sr OS_PASSWORD_INPUT
#export OS_PASSWORD=$OS_PASSWORD_INPUT
1.2.2 Add one line:
export OS_PASSWORD=”passwd”
(Replace ‘passwd’ to the password of current log in administrator user)
1.2.3 Additional code:
if [ -z $OS_PROJECT_DOMAIN_NAME ]; then
export OS_PROJECT_DOMAIN_NAME=Default
fi
2.Call Restful API to get the host system information
2.1 Source the RC file fixed in step 1 to set particular environment variables.
Command: source ‘RcFile.sh’
(Replace the ‘RcFile.sh’ to your RC file name)
2.2 Call Restful API to get authentic token. (Here is a sample of curl command)
curl -s -i POST \$OS_AUTH_URL/auth/tokens?nocatalog -H "Content-Type: application/json" -d '{"auth":{"identity":{"methods":["password"],"password":{"user":{"domain":{"name":"'"\$OS_USER_DOMAIN_NAME"'"},"name":"'"\$OS_USERNAME"'","password":"'"\$OS_PASSWORD"'"}}},"scope":{"project":{"domain":{"name":"'"\$OS_PROJECT_DOMAIN_NAME"'"},"name":"'"\$OS_PROJECT_NAME"'"}}}}
The OS_AUTH_URL, OS_USER_DOMAIN_NAME, OS_PASSWORD, OS_PROJECT_DOMAIN_NAME and OS_PROJECT_NAME in this command are defined in the RC file. And they have been sourced into running context in last step.
2.3 Set OS_TOKEN environment variable.
2.3.1 Extract the value of ‘X-Subject-Token’ from the response header of the request in step 2.
2.3.2 export OS_TOKEN=”$XSubjectTokenData”
(Replace $XSubjectTokenData to the value of ‘X-Subject-Toke’.)
2.4 Call Restful API to list all hypervisors’ information.
2.4.1 Extract destination host address from ‘OS_AUTH_URL’
The OS_AUTH_URL is a string like “http://10.62.235.105:5000/v3”. We need extract the host address below from it.
HostAddr=http://10.62.235.105
2.4.2 Call API
curl -s -H “X-Auth-Token: $OS_TOKEN” $HostAddr:8774/v2.1/$OS_PROJECT_ID/os-hypervisors/detail
The ‘HosAddr’ is set in last step.
Use JsonTool of python to beauty print the output of this command:
{
"hypervisors": [
{
"cpu_info": "{\"vendor\": \"Intel\", \"model\": \"Broadwell\", \"arch\": \"x86_64\", \"features\": [\"pge\", \"avx\", \"xsaveopt\", \"clflush\", \"sep\", \"rtm\", \"tsc_adjust\", \"vme\", \"dtes64\", \"invpcid\", \"tsc\", \"fsgsbase\", \"xsave\", \"smap\", \"bmi2\", \"vmx\", \"erms\", \"xtpr\", \"cmov\", \"hle\", \"smep\", \"pcid\", \"est\", \"pat\", \"monitor\", \"smx\", \"pbe\", \"lm\", \"msr\", \"adx\", \"3dnowprefetch\", \"nx\", \"fxsr\", \"syscall\", \"tm\", \"sse4.1\", \"pae\", \"sse4.2\", \"pclmuldq\", \"acpi\", \"fma\", \"pni\", \"tsc-deadline\", \"popcnt\", \"mmx\", \"osxsave\", \"cx8\", \"mce\", \"de\", \"rdtscp\", \"ht\", \"dca\", \"lahf_lm\", \"abm\", \"rdseed\", \"pdcm\", \"mca\", \"pdpe1gb\", \"mbm_local\", \"sse\", \"f16c\", \"pse\", \"ds\", \"invtsc\", \"mbm_total\", \"tm2\", \"avx2\", \"aes\", \"sse2\", \"ss\", \"ds_cpl\", \"arat\", \"bmi1\", \"apic\", \"ssse3\", \"fpu\", \"cx16\", \"pse36\", \"mtrr\", \"movbe\", \"rdrand\", \"cmt\", \"x2apic\"], \"topology\": {\"cores\": 10, \"cells\": 2, \"threads\": 2, \"sockets\": 1}}",
"current_workload": 0,
"disk_available_least": 136,
"free_disk_gb": 371,
"free_ram_mb": 253346,
"host_ip": "10.62.235.105",
"hypervisor_hostname": "localhost.localdomain",
"hypervisor_type": "QEMU",
"hypervisor_version": 2009000,
"id": 1,
"local_gb": 499,
"local_gb_used": 238,
"memory_mb": 262050,
"memory_mb_used": 56602,
"running_vms": 1,
"service": {
"disabled_reason": null,
"host": "localhost.localdomain",
"id": 32
},
"state": "up",
"status": "enabled",
"vcpus": 40,
"vcpus_used": 2
}
]
}
Each field meaning:
Name | In | Type | Description |
---|---|---|---|
hypervisors | body | array | An array of hypervisor information. |
cpu_info | body | object | A dictionary that contains cpu information like arch, model, vendor, features and topology. The content of this field is hypervisor specific. |
current_workload | body | integer | The current_workload is the number of tasks the hypervisor is responsible for. This will be equal or greater than the number of active VMs on the system (it can be greater when VMs are being deleted and the hypervisor is still cleaning up) |
status | body | string | The status of the hypervisor. One of enabled or disabled. |
state | body | string | The state of the hypervisor. One of up or down. |
disk_available_least | body | integer | The actual free disk on this hypervisor(in GB) |
host_ip | body | string | The IP address of the hypervisor’s host. |
free_disk_gb | body | integer | The free disk remaining on this hypervisor(in GB). |
free_ram_mb | body | integer | The free RAM in this hypervisor(in MB). |
hypervisor_hostname | body | string | The hypervisor host name provided by the Nova virt driver. For the Ironic driver, it is the Ironic node uuid |
hypervisor_type | body | string | The hypervisor type. |
hypervisor_version | body | integer | The hypervisor version. |
id | body | integer | The id of the hypervisor. Deprecated in version 2.52 |
local_gb | body | integer | The disk in this hypervisor(in GB). |
local_gb_used | body | integer | The disk used in this hypervisor(in GB). |
memory_mb | body | integer | The memory of this hypervisor(in MB). |
memory_mb_used | body | integer | The memory used in this hypervisor(in MB). |
running_vms | body | integer | The number of running vms on this hypervisor. |
servers (Optional) | body | array | A list of server objects. New in version 2.53 |
servers.uuid (Optional) | body | string | The server ID. New in version 2.53 |
servers.name | body | string | The server name. New in version 2.53 |
service | body | object | The hypervisor service object. |
service.host | body | string | The name of the host. |
service.id | body | integer | The id of the service. Deprecated in version 2.52 |
service.id | body | string | The id of the service as a uuid. New in version 2.53 |
service.disable_reason | body | string | The disable reason of the service, null if the service is enabled or disabled without reason provided. |
vcpus | body | integer | The number of vcpu in this hypervisor. |
vcpus_used | body | integer | The number of vcpu used in this hypervisor. |
hypervisor_links (Optional) | body | array | Links to the hypervisors resource. See API Guide / Links and References for more info. New in version 2.33 |
3. Call Restful API to list all instances in the OpenStack.
3.1 The all pre-operations are same to Step 1 to Step 2.3.
3.2 The term of instance is called server on OpenStack. The Restful API is below:
curl -s -H "X-Auth-Token: $OS_TOKEN" $HostAddr:8774/v2.1/$OS_PROJECT_ID/servers
Use JsonTool to beauty show the result as below:
{
"servers": [
{
"id": "57ea68f5-127b-4171-9edb-0cf824eecb57",
"links": [
{
"href": "http://10.62.235.105:8774/v2.1/ace44a62c3ab4c1dbdb320b5c4f40518/servers/57ea68f5-127b-4171-9edb-0cf824eecb57",
"rel": "self"
},
{
"href": "http://10.62.235.105:8774/ace44a62c3ab4c1dbdb320b5c4f40518/servers/57ea68f5-127b-4171-9edb-0cf824eecb57",
"rel": "bookmark"
}
],
"name": "v750OsUpgradeTest"
}
]
}
The all instances are shown as the array “servers”. We only care the name of each server.
The above result shows that there is only one instance in current OpenStack. And its name is “v750OsUpgradeTest”.
3.3 List the details of each instance.
curl -s -H "X-Auth-Token: $OS_TOKEN" $HostAddr:8774/v2.1//$OS_PROJECT_ID/detail | python -m json.tool
The result:
{
"servers": [
{
"OS-DCF:diskConfig": "AUTO",
"OS-EXT-AZ:availability_zone": "nova",
"OS-EXT-STS:power_state": 1,
"OS-EXT-STS:task_state": null,
"OS-EXT-STS:vm_state": "active",
"OS-SRV-USG:launched_at": "2017-11-15T17:36:36.000000",
"OS-SRV-USG:terminated_at": null,
"accessIPv4": "",
"accessIPv6": "",
"addresses": {
"external_network": [
{
"OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:1f:53:03",
"OS-EXT-IPS:type": "fixed",
"addr": "10.62.235.110",
"version": 4
}
]
},
"config_drive": "",
"created": "2017-11-15T17:34:53Z",
"flavor": {
"id": "604e722b-ceb5-4a94-a40a-640fdb570a5e",
"links": [
{
"href": "http://10.62.235.105:8774/ace44a62c3ab4c1dbdb320b5c4f40518/flavors/604e722b-ceb5-4a94-a40a-640fdb570a5e",
"rel": "bookmark"
}
]
},
"hostId": "bfa38a8fd399e73b47731e4d2ec2f43ab9b2834436b854c0ecf86a1f",
"id": "57ea68f5-127b-4171-9edb-0cf824eecb57",
"image": {
"id": "f2539194-b274-4049-9373-2ccd51b1dfbd",
"links": [
{
"href": "http://10.62.235.105:8774/ace44a62c3ab4c1dbdb320b5c4f40518/images/f2539194-b274-4049-9373-2ccd51b1dfbd",
"rel": "bookmark"
}
]
},
"key_name": null,
"links": [
{
"href": "http://10.62.235.105:8774/v2.1/ace44a62c3ab4c1dbdb320b5c4f40518/servers/57ea68f5-127b-4171-9edb-0cf824eecb57",
"rel": "self"
},
{
"href": "http://10.62.235.105:8774/ace44a62c3ab4c1dbdb320b5c4f40518/servers/57ea68f5-127b-4171-9edb-0cf824eecb57",
"rel": "bookmark"
}
],
"metadata": {},
"name": "v750OsUpgradeTest",
"os-extended-volumes:volumes_attached": [
{
"id": "a68b421c-3cc9-4918-a79d-60147a55f032"
},
{
"id": "8adb9dd3-a46e-4372-94b3-4f99b9fbc984"
},
{
"id": "b4c0e20d-824d-4ed4-9e8b-cb8499b1a72b"
}
],
"progress": 0,
"security_groups": [
{
"name": "default"
}
],
"status": "ACTIVE",
"tenant_id": "ace44a62c3ab4c1dbdb320b5c4f40518",
"updated": "2017-11-15T17:41:41Z",
"user_id": "cdcb30d5cb684395a1319abee73780d6"
}
]
}