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:

NameInTypeDescription
hypervisorsbodyarrayAn array of hypervisor information.
cpu_infobodyobjectA dictionary that contains cpu information like arch, model, vendor, features and topology. The content of this field is hypervisor specific.
current_workloadbodyintegerThe 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)
statusbodystringThe status of the hypervisor. One of enabled or disabled.
statebodystringThe state of the hypervisor. One of up or down.
disk_available_leastbodyintegerThe actual free disk on this hypervisor(in GB)
host_ipbodystringThe IP address of the hypervisor’s host.
free_disk_gbbodyintegerThe free disk remaining on this hypervisor(in GB).
free_ram_mbbodyintegerThe free RAM in this hypervisor(in MB).
hypervisor_hostnamebodystringThe hypervisor host name provided by the Nova virt driver. For the Ironic driver, it is the Ironic node uuid
hypervisor_typebodystringThe hypervisor type.
hypervisor_versionbodyintegerThe hypervisor version.
idbodyintegerThe id of the hypervisor. Deprecated in version 2.52
local_gbbodyintegerThe disk in this hypervisor(in GB).
local_gb_usedbodyintegerThe disk used in this hypervisor(in GB).
memory_mbbodyintegerThe memory of this hypervisor(in MB).
memory_mb_usedbodyintegerThe memory used in this hypervisor(in MB).
running_vmsbodyintegerThe number of running vms on this hypervisor.
servers (Optional)bodyarrayA list of server objects. New in version 2.53
servers.uuid (Optional)bodystringThe server ID. New in version 2.53
servers.namebodystringThe server name. New in version 2.53
servicebodyobjectThe hypervisor service object.
service.hostbodystringThe name of the host.
service.idbodyintegerThe id of the service. Deprecated in version 2.52
service.idbodystringThe id of the service as a uuid. New in version 2.53
service.disable_reasonbodystringThe disable reason of the service, null if the service is enabled or disabled without reason provided.
vcpusbodyintegerThe number of vcpu in this hypervisor.
vcpus_usedbodyintegerThe number of vcpu used in this hypervisor.
hypervisor_links (Optional)bodyarrayLinks 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"
        }
    ]
}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值