打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
OpenStack API Quick Start(1)

 OpenStack API Quick Start

 OpenStack API Quick Start

The OpenStack system has several key projects that are separate installations but can work together depending on your cloud needs: OpenStack Compute, OpenStack Object Storage, OpenStack Identity Service, and OpenStack Image Store. With the TryStack OpenStack installation, the OpenStack Compute, OpenStack Identity, and OpenStack Image Store projects are all working together in the background of the installation.

OpenStack由几个关键的项目组成,它们彼此独立,但又可以一起工作(具体需要使用其中的哪些,取决于你的云的需求),这些项目包括  :OpenStack Compute, OpenStack Object Storage, OpenStack Identity Service, and OpenStack Image Store(即OpenStack计算、OpenStack对象存储,、OpenStack身份识别、 OpenStack图片存储)。TryStack这个OpenStack 测试平台的背后,正运行着上述几个项目。

 OpenStack API Introduction

OpenStack API 介绍

This page covers the basics for talking to your OpenStack cloud through the Compute API after authorizing with the Identity Service API. You can then build a cloud by launching images and assigning metadata to instances, all through the API. For an API reference of all the possible commands, see the OpenStack Compute API v2 specification and the Identity Service 2.0 specification published at docs.openstack.org/api.

本页通过介绍OpenStack Compute的 API(在调用计算API之前,需先通过身份识别API)向你讲述了OpenStack云。

然后,通过这些API,您可以构建一个云,来提供图片和元数据存储服务实例。查看OpenStack 计算API v2.0规范与身份识别v2..0规范,

可以得到一个API参考的所有可能的指令。


Getting Credentials

取得证书

Credentials are a combination of your user name, password, and what tenant (or project) your cloud is running under. You only need to generate an additional token if you are interacting with your cloud directly with API endpoints, and not with a client. Your cloud administrator can give you a user name and a password as well as your tenant identifier so you can generate authorization tokens. You can also get the tenant ID from the Dashboard URLs, for example             https://trystack.org/dash/296/images/ indicates a tenant ID of 296.

凭证是你的用户名,密码,你的云运行的租户(或项目)的一个组合。如果你与云是通过API直接交互而不是通过一个客户端,你只需要生成一个额外的令牌。你的云管理员可以分配给你用户名和密码以及您的租户标识符,这样你就可以生成授权令牌。你也可以从仪表板的url取得你的租户ID,比如 https://trystack.org/dash/296/images/ indicates a tenant ID of 296.


These tokens are typically good for 24 hours, and when the token expires, you will find out with a 401 (Unauthorized) error and can request another token programmatically. The general work flow goes something like this:

这些令牌通常有效期是24小时,当令牌到期后,你会发现访问时,报401(未经授权)错误, 此时,可以通过编程的方式,再去申请另一个令牌。一般的工作流程是这样的:


  1. Begin API requests by asking for an authorization token from the endpoint your cloud administrator gave you, typically http://hostname:port/v2.0/tokens. You send your user name, password, and what group or account you are with (the "tenant" in auth-speak).

    1. 开始调用API时,需要通过你的云管理员给你的端口得到授权令牌,典型的比如 http://hostname:port/v2.0/tokens. 你把用户名、密码、授权的租户ID发送给服务器。 比如,如下所示:
    curl -k -X 'POST' -v https://arm.trystack.org:5443/v2.0/tokens -d '{"auth":{"passwordCredentials":{"username": "joecool", "password":"coolword"}, "tenantId":"5"}}' -H 'Content-type: application/json'
  2. The server returns a response in which the 24-hours token is contained. Use that token to send API requests with the X-Auth-Token included as an header field.

    2. 服务器会返回一个24小时内有效的令牌. 然后,通过在请求头中加上X-Auth-Tocken字段信息(字段的值就是此令牌),使用该令牌发送API请求。例如
    curl -k -D - -H "X-Auth-Token: 7d2f63fd-4dcc-4752-8e9b-1d08f989cc00" -X 'GET' -v https://arm.trystack.org:9774/v1.1/296/extensions  -H 'Content-type: application/json'
  3. Repeatedly send API requests with that token in the X-Auth-Token header until either: 1) the job's done or 2) you get a 401 (Unauthorized) code in return.

     3.  重复多次发送API请求时,都需要在请求头中带上X-Auth-Token头域(此域的值为令牌),除非下面两种情况之一出现:1)工作完成了 2)请求返回401(未经授权)错误.


  4. Request a token again when you get a 401 response or until the script's job is done.

    当收到一个 4011(未经授权)错误返回中,或者脚本工作已完成,则需要重新申请一个令牌。

For a typical OpenStack deployment running the Identity Service you can request a token with this command in cURL if you know your tenantId:

对于一个运行有Identity Service典型部署的OpenStack ,如果你知道你的租户ID,则可以通过下面的curl指令来取得令牌:

$ curl -X 'POST' -v https://arm.trystack.org:5443/v2.0/tokens -d '{"auth":{"passwordCredentials":{"username": "joecool", "password":"coolword"}, "tenantId":"5"}}' -H 'Content-type: application/json'         

In return, you should get a 200 OK response with a token in the form of "id": "cd427a33-bb4a-4079-a6d7-0ae148bdeda9" and an expiration date 24 hours from now. Here's what it looks like, the exact response may vary from cloud-to-cloud:

 作为回报,你将得到一个200 OK响应,响应中包含令牌与24小时的过期时间,其中令牌的形式为“id”:“cd427a33 - bb4a - 4079 a6d7 - 0 - ae148bdeda9”。大体上返回是这样,但不同的云平台会有所不同:(参见下面的json格式的数据)

{    "access": {        "serviceCatalog": [            {                "endpoints": [                    {                        "adminURL": "https://arm.trystack.org:9774/v1.1/1",                         "internalURL": "https://arm.trystack.org:9774/v1.1/1",                         "publicURL": "https://arm.trystack.org:9774/v1.1/1",                         "region": "RegionOne"                    }                ],                 "name": "nova", //OpenStack nova组件服务                "type": "compute" //计算服务            },             {                "endpoints": [                    {                        "adminURL": "https://GLANCE_API_IS_NOT_DISCLOSED/v1.1/1",                         "internalURL": "https://GLANCE_API_IS_NOT_DISCLOSED/v1.1/1",                         "publicURL": "https://GLANCE_API_IS_NOT_DISCLOSED/v1.1/1",                         "region": "RegionOne"                    }                ],                 "name": "glance", //OpenStack Glance组件服务                "type": "image"   //图形服务            },             {                "endpoints": [                    {                        "adminURL": "https://arm.trystack.org:5443/v2.0",                         "internalURL": "https://keystone.trystack.org:5000/v2.0",                         "publicURL": "https://keystone.trystack.org:5000/v2.0",                         "region": "RegionOne"                    }                ],                 "name": "keystone", //OpenStack 身份、令牌、分类及策略服务                "type": "identity"  //身份识别            }        ],         "token": {            "expires": "2012-02-15T19:32:21", //24小时内的有效期,即失效时间            "id": "5df9d45d-d198-4222-9b4c-7a280aa35666", //token令牌            "tenant": {  //租户信息,包括id,name                "id": "1",                 "name": "admin"            }        },         "user": {            "id": "14",             "name": "joecool",             "roles": [                {                    "id": "2",                     "name": "Member",                     "tenantId": "1"                }            ]        }    }    }    

If you don't know your tenantId, you can send a request with an empty tenantId, such as this JSON example:

如果你不清楚你的租户ID,你可以通过发送一个带空串的租户ID的请求来得到,就像下面的json格式示例:

'{
    "auth":{
        "passwordCredentials":{
                "username": "joecool", //用户名 
                "password":"coolword"  //密码(注:这里是直接发送密码明文吗???)
         }, 
        "tenantId":""  //空串tenantID
     }
 }'        

Then, with the auth token that returns, fill in a request using the X-Auth-Token header as an authentication to get your tenantId:

在取得返回的授权令牌后,使用此令牌作为x认证令牌头填充请求头,作为一个身份论证的请求来得到你的租户ID。如下所示:

curl -H "X-Auth-Token:6de6d45d-d198-4222-9b4c-7a280aa24888" http://arm.trystack.org:5000/v2.0/tenants

In return, you get a list of tenants:

在返回结果中,你可以得到一批租户信息的列表,如下所示的json格式数据:

{    "tenants_links": [],    "tenants": [        {             "enabled": "true",  //是否有效的标识  true:有效  false:失效             "description": "joecool", //描述            "name": "joecool",  //租户名            "id": "tenantnnnnnn" //租户ID        }    ]}

Use the resulting token to make a new POST request containing the tenantId so you can retrieve endpoints:

使用返回结果中的令牌与租户ID,发起一个新的HTTP POST请求,从而取得对应的服务端口endpoint.。如下所示:

curl -k -X 'POST' -v http://arm.trystack.org:5000/v2.0/tokens -d             '{"auth":{"passwordCredentials":{"username": "usern4me", "password":"passwerd"}, "tenantId":"tenantnnnnnn"}}' -H 'Content-type: application/json'

The resulting JSON contains a list of endpoints, for example:

返回的json格式数据中,包括一个endpoints的列表,示例如下:

{    "endpoints": [ //服务接口的endpoints        {            "adminURL": "http://10.225.0.8:8774/v2/tenantnnnnnn",            "region": "Calxeda-AUS1",            "internalURL": "http://10.225.0.8:8774/v2/tenantnnnnnn", //内网地址            "publicURL": "http://208.123.85.197:8774/v2/tenantnnnnnn" //外网地址        }    ],    "endpoints_links": [],    "type": "compute", //OpenStack计算处理服务    "name": "nova" //OpenStack nova Compute组件}

In addition, you can see a valid token matched to your user and tenantId:

此外,你可以得到一个与你的用户名与租户ID匹配的、有效的令牌。如下所示:

{    "access": {        "token": {            "expires": "2012-10-31T17: 13: 12Z", //有效结止时间            "id": "new_token***", //令牌            "tenant": {                "enabled": true,  //租户状态                "id": "tenantnnnnnn", //租户ID                "name": "joecool", //用户名                "description": "joecool"            }        }}

 Sending Requests to the API

向API发送请求

You have a couple of options for sending requests to OpenStack through an API. Developers and testers may prefer to use cURL, the command-line tool from http://curl.haxx.se/. With cURL you can send HTTP requests and receive responses back from the command line.

你可以通过两种方式向OpenStack发送请求来调用其API。开发人员和测试人员 可能会倾向于使用cURL(cURL命令行工具可以从http://curl.haxx.se/查阅)。通过cURL工具,你可以从命令行发送HTTP请求和接收服务端响应。

If you like to use a more graphical interface, the REST client for Firefox also works well for testing and trying out commands, see https://addons.mozilla.org/en-US/firefox/addon/restclient/. You can also download and install rest-client, a Java application to test RESTful web services, from http://code.google.com/p/rest-client/.

如果你喜欢使用图型用户界面,也可以通过使用Firefox浏览器的REST client工具来测试OpenStack的AP(具体可参见https://addons.mozilla.org/en-US/firefox/addon/restclient/.)      你也可以下载安装一个基于JAVA的REST web service客户端,下载地址 http://code.google.com/p/rest-client/.


You need to generate a token as shown above if you use cURL or a REST client.

如果你使用cURL或者REST客户端来进行测试,你也是需要事先生成一个令牌(方法如上所述,不再垒述)。

It's also recommended that you install and use a Command-Line-Client (CLI) such as python-novaclient, which is documented in CLI guide.

此外,还建议你安装并使用命令行客户端,比如python-novaclient(它的命令行客户端使用文档见http://docs.openstack.org/cli/quick-start/content/index.html

Here's an example of curl commands that can check your list of servers.

下面是一个检查服务器列表的curl命令的例子:

curl -v -H "X-Auth-Token:tokengoeshere" http://208.123.85.197:8774/v2/tenantnnnnnn/servers

Here's what you get in return if you have a running server.

如果你已经有正在运行的服务器,下面是一个返回的结果JSON示例:

{    "servers": [        {            "id": "server***",            "links": [                {                    "href": "http://208.123.85.197:8774/v2/tenantnnnnnn/servers/server***",                    "rel": "self"                },                {                    "href": "http://208.123.85.197:8774/tenantnnnnnn/servers/server***",                    "rel": "bookmark"                }            ],            "name": "Staging Server"        }    ]}

 Setting Up python-novaclient

安装python-novaclient

For more serious scripting work, you can use a client like the python-novaclient client. The python-novaclient implements Compute API through a command-line interface. You only need a user name and password to use the python-novaclient tool.

对于比较繁重的脚本开发工作,你可以使用像python-novaclient这样的客户端工具。python-novaclient可以直接通过命令行接口来调用OpenStack Compute API. 你仅只需要用户名和密码,就可以使用python-novaclient。

Installing the python-novaclient gives you a nova shell command that enables Compute API interactions from the command line. You install the client, and then provide your user name and password, set as environment variables for convenience, and then you can have the ability to send commands to your cloud on the command-line.

安装python-novaclient后,它提供了一个与OpenStack Compute API进行命令行交互的nova shell命令。当你安装了python-novaclient客户端,提供了用户名和密码,设置好了环境变量,接着就可以通过命令行向你的云平台发送命令了。

To install python-novaclient, grab the stable/nova version like so.

选择最新的、稳定的python-novaclient版本进行安装。例如:

$ pip install -e git+https://github.com/openstack/python-novaclient.git#egg=python-novaclient    

The CLI guide offers more detailed install instructions including how to source your credentials.

CLI指南(http://docs.openstack.org/cli/quick-start/content/index.html)提供更详细的安装说明,包括如何产生你的凭证。

 Listing Images

列出图片服务

Before you can go about the business of building your cloud, you want to know what images are available to you by asking the image service what kinds of configurations are available. The image service could be compared to iTunes for your cloud - you can view the playlist of images before using your favorite image to create a new instance in the cloud. To get the list of images, their names, status, and ID, use this command:

在构建你的云的业务之前,你一定想知道:图片是如何可用地、图片服务的配置又是怎么样地? 图片服务云常被比作iTunes,通过iTunes,你可以查看之前的播放列表的图片,使用您喜欢的图片在云平台创建一个新的实例.  获取图片的列表、名字、状态和ID ,可以使用这个命令:

$ nova image-list

+----+--------------------------------------+--------+--------+| ID |                 Name                 | Status | Server |+----+--------------------------------------+--------+--------+| 12 | natty-server-cloudimg-amd64-kernel   | ACTIVE |        || 13 | natty-server-cloudimg-amd64          | ACTIVE |        || 14 | oneiric-server-cloudimg-amd64-kernel | ACTIVE |        || 15 | oneiric-server-cloudimg-amd64        | ACTIVE |        |+----+--------------------------------------+--------+--------+        

Next you need to know the relative sizes of each of these.

接下来,你需要知道下面这些东东的相对大小.

With the information about what is available to you, you can choose the combination of image and flavor to create your virtual servers and launch instances.

在了解了哪些是可用的信息后,你可以兼顾图片和版本特性两个方面来选择创建虚拟机,同时启动这些实例。

 Listing Flavors

列出版本特性

You also need to know the ID of the flavor To get the list of flavors, their names, status, and ID, use this command:

你还需要了解nova的版本特性的ID。得到nova的版本特性、名称、状态和ID,使用下面的指令:

$ nova flavor-list        +----+-----------+-----------+------+-----------+------+-------+-------------+| ID |    Name   | Memory_MB | Disk | Ephemeral | Swap | VCPUs | RXTX_Factor |+----+-----------+-----------+------+-----------+------+-------+-------------+| 1  | m1.tiny   | 512       | 0    | N/A       | 0    | 1     |             || 2  | m1.small  | 2048      | 20   | N/A       | 0    | 1     |             || 3  | m1.medium | 4096      | 40   | N/A       | 0    | 2     |             || 4  | m1.large  | 8192      | 80   | N/A       | 0    | 4     |             || 5  | m1.xlarge | 16384     | 160  | N/A       | 0    | 8     |             |+----+-----------+-----------+------+-----------+------+-------+-------------+            

With the information about what is available to you, you can choose the combination of image and flavor to create your virtual servers and launch instances.

(译注:这段貌似重复了,貌似是官网搞错了吧 ^_^)

 Launching Instances

启动实例

To launch a server, you choose an image you want to match up to a size, find the ID for the image and the ID for the flavor so you can size it, and create the command with the IDs. From the information we got previously, we know that an Ubuntu Natty image has an ID of 13, and if you want to start small with about 2 GB of memory and 20 GB of disk space, you'd choose the m1.small flavor which has an ID of 2 when using the 1.1 API on TryStack. Put those parameters in with the "boot" command and you can create a new virtual server. 

选择一个与你所想的大小匹配的图片服务,启动它,找到图片服务的ID,以及ID所对应的版本特性,以更你可以调整它的大小,同时还可以用ID来创建指令。从前面我们得到的信息来看,我们知道:Ubuntu Natty 图片服务的ID编号是13,如果你想基于2GB内存、20GB磁盘来启动一个较小的图片服务用于OpenStack的测试平台,你最好选择ID为2、版本特性标记为m1.small的1.1 版本的nova.  把这些参数放在启动命令中,你将可以创建另一个新的虚拟服务器。

Note

When using an endpoint that supports 1.1 of the Compute API, you can launch instances with an ID. When using an endpoint that supports v2 of the Compute API, you must use the UUID to launch an instance.

                注:当使用支持v1.1 的OpenStack Compute API时,你需要通过ID来启动服务实例。 而当使用支持v2 版本的OpenStack Compute API时,你必须使用UUID来启动一个服务实例。
 $ nova boot --flavor=2 --image=13 testserver  +-----------+--------------------------------------+|  Property |                Value                 |+-----------+--------------------------------------+| adminPass | ****************                     || created   | 2011-09-01T21:40:41Z                 || flavor    | m1.small                             || hostId    |                                      || id        | 1805                                 || image     | natty                                || metadata  | {}                                   || name      | testserver                           || progress  | 0                                    || status    | BUILD                                || updated   | 2011-09-01T21:40:41Z                 || uuid      | ce044452-f22e-4ea4-a3ec-d1cde80cf996 |+-----------+--------------------------------------+ 

Now, you can view this server in your new cloud by using the nova list command:

现在,你可以通过下面的指令来查看你的新的云平台的服务器:

$ nova list+------+------------+--------+--------------------------------+|  ID  |    Name    | Status |            Networks            |+------+------------+--------+--------------------------------+| 1805 | testserver | ACTIVE | private=10.4.96.81             |+------+------------+--------+--------------------------------+            

There are three statuses you may see - ACTIVE, BUILDING, and UNKNOWN. The BUILDING status is transient and you likely will not see it. If you see UNKNOWN, run nova list again until it goes away.

你将可能看到三个状态:活跃中、正在构建、未知的。“正在构建”的状态是一个短暂的过程,你可能不会看到它。如果你看到的是“未知”状态,请再次运行nova列表,直到它消失为止。

To view all the information about a particular server, use nova show with the ID of the server that you got from the nova list command.

如果想查看关于一个特定的服务器的所有信息,请使用指令: nova show 服务器ID (其中,服务器ID可以通过 nova list 指令获得) 

$ nova show 1805+-----------------+----------------------------------------------------------+|     Property    |                          Value                           |+-----------------+----------------------------------------------------------+| created         | 2011-09-01T21:40:41Z                                     || flavor          | m1.small                                                 || hostId          | 58a7430169aa42cde5ce2456b0cb5bb5ac1ab0703bab6420e8a49e6e || id              | 1805                                                     || image           | natty                                                    || metadata        | {}                                                       || name            | testserver                                               || private network | 10.4.96.81                                               || progress        | 100                                                      || status          | ACTIVE                                                   || updated         | 2011-09-01T21:40:46Z                                     || uuid            | ce044452-f22e-4ea4-a3ec-d1cde80cf996                     |+-----------------+----------------------------------------------------------+ 

You can now launch that image again, but add more information to the server when you boot it so that you can more easily identify it amongst your ever-growing elastic cloud. Use the -meta option with a key=value pair, where you can make up the string for both the key and the value. For example, you could add a description and also the creator of the server.

你可以再次重新启动你的图片服务器,但是在你启动时需添加更多的关于此服务器的信息,以便于在你的日益增长的弹性云平台上,能更容易识别它。使用key-value键值对的元选项,你可以附加上这个字符串的键和值信息。比如,你可以添加服务器的描述和创建者。

$ nova boot --flavor=2 --image=13 testserver --meta description='Use for testing purposes' --meta creator=joecool
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
OpenStack 升级实践:风险、收益、步骤、策略、问题@云技术社区分享
Openstack开发者入门(二)
(转)解读Openstack Identity API v2.0
Centos6.5 搭建openstack
OpenStack从入门到放弃
【博文推荐】如何进行 OpenStack 的性能测试及平台功能性测试
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服