2015年12月12日土曜日

Blue BoxのAPI使ってみる

Blue BoxはOpenStackそのものなので、OpenStack APIを使って外部からいろいろと操作することが可能です。

前提ソフトウェアのインストール

テストしたPC環境はWindows 8.1 64bit (Surface 3)。
まずはPythonを導入します。
Python 2.6以上を推奨しているが、Python 3は現時点ではサポートされていない模様。
安定している2.7をダウンロードします。

https://www.python.org/downloads/release/python-279/

このURLにある

Windows x86-64 MSI installer

をダウンロードして実行すればインストールできます。
パスを自動追加すると便利なので、インストール画面でパスの自動追加を選択しておきましょう。
コマンドプロンプトを管理者モードで実行し、「python」を実行できればインストールされていることを確認できます。

続いて、pipをインストールします。
もしすでに含まれているようでしたら不要です。







pip

というコマンドを入力してエラーになるようでしたらコマンドプロンプトから以下のコマンドを実行します。

easy_install pip




OpenStackクライアントを導入する

コマンドプロンプトから以下のコマンドを実行します。

pip install python-openstackclient


途中コンパイルエラー等のメッセージが出力されますが、最後に以下のようなメッセージが出力されればインストールが完了しています。

Successfully installed python-openstackclient python-cinderclient cliff python-novaclient six python-neutronclient Babel python-keystoneclient stevedore cliff-tablib oslo.config oslo.i18n pbr requests oslo.utils os-client-config python-glan ceclient simplejson PrettyTable argparse pyparsing cmd2 unicodecsv PyYAML iso8601 oslo.serialization netaddr pytz debtcollector tablib monotonic netifaces keystoneauth1 appdirs warlock msgpack-python wrapt jsonschema jsonpatch functools32 jsonpointer
Cleaning up...



環境変数の定義



OpenStackのダッシュボードからOpenStack.RCファイルをダウンロードします。
左側のメニューから「Access & Security」を選び、上部のタブから「API Access」を選ぶとダウンロードボタンが表示されるので、それをクリックします。











"Demo Project-openrc.sh"という名前のファイルがダウンロードされます。
(注)ファイル名の先頭はプロジェクト名です。実際の環境に合わせて読み替えてください。


#!/bin/bash

# To use an Openstack cloud you need to authenticate against keystone, which
# returns a **Token** and **Service Catalog**.  The catalog contains the
# endpoint for all services the user/tenant has access to - including nova,
# glance, keystone, swift.
#
# *NOTE*: Using the 2.0 *auth api* does not mean that compute api is 2.0.  We
# will use the 1.1 *compute api*
export OS_AUTH_URL=https://icos-sea.openstack.blueboxgrid.com:5001/v2.0

# With the addition of Keystone we have standardized on the term **tenant**
# as the entity that owns the resources.
export OS_TENANT_ID=162342da7f4b42adb7adfcb47e3746fd
export OS_TENANT_NAME="Demo Project"

# In addition to the owning entity (tenant), openstack stores the entity
# performing the action as the **user**.
export OS_USERNAME="matsuos"

# With Keystone you pass the keystone password.
echo "Please enter your OpenStack Password: "
read -sr OS_PASSWORD_INPUT
export OS_PASSWORD=$OS_PASSWORD_INPUT

# If your configuration has multiple regions, we set that information here.
# OS_REGION_NAME is optional and only valid in certain environments.
export OS_REGION_NAME="RegionOne"
# Don't leave a blank variable, unset it if it was empty
if [ -z "$OS_REGION_NAME" ]; then unset OS_REGION_NAME; fi


これはBashスクリプトなので、Windowsで実行するためには修正が必要です。
筆者は次のようなバッチファイルを作成しました。


set OS_AUTH_URL=https://icos-sea.openstack.blueboxgrid.com:5001/v2.0
set OS_TENANT_NAME=Demo\ Project
set OS_USERNAME=matsuos
set OS_PASSWORD=XXXXXXXX    (実際のパスワードを入力)


(注)プロジェクト名にブランク(スペース)が含まれている場合は、\(半角の円マーク、英語環境ならバックスラッシュ)をスペースの前に付けてあげる必要があります。


それでは、OpenStack APIを実行してみましょう。



C:\Users\matsuo\Dev\bb>nova list
+--------------------------------------+-----------------------------------------------------+---------+------------+-------------+-------------------------
--------------------+
| ID                                   | Name                                                | Status  | Task State | Power State | Networks
                    |
+--------------------------------------+-----------------------------------------------------+---------+------------+-------------+-------------------------
--------------------+
| 860f7a44-a63d-4ab2-a859-c44ac3f93375 | AdamTest                                            | SHUTOFF | -          | Shutdown    | internal=10.230.40.143
                    |
| 4c551808-0988-4493-92c3-0ff77ebbb147 | Helen. Testing-4c551808-0988-4493-92c3-0ff77ebbb147 | ACTIVE  | -          | Running     | internal=10.230.41.119;
ed_1=192.168.136.12 |
| 774f0fb9-503c-41c2-9e27-f25a1b88762c | Helen. Testing-774f0fb9-503c-41c2-9e27-f25a1b88762c | ACTIVE  | -          | Running     | internal=10.230.41.120;
ed_1=192.168.136.11 |
| ecb151ec-1e8b-4f97-a8f9-df7d6cabf066 | RakeshC                                             | ACTIVE  | -          | Running     | internal=10.230.40.62
                    |
| ed3994a2-da77-4ec1-97a2-bedd88e37dcb | RakeshU                                             | ACTIVE  | -          | Running     | internal=10.230.40.63
                    |
| b6b953c6-55aa-4f73-95ec-7042530965fd | kurajesh                                            | ACTIVE  | -          | Running     | internal=10.230.40.132
                    |
| 53b7a812-7ce4-4f03-89b9-40781596e4d4 | matsuos-1                                           | ACTIVE  | -          | Running     | internal=10.230.41.127
                    |
| 54dcac56-5489-4325-80d9-29df0d4cd3c9 | rhj-u14-1                                           | ACTIVE  | -          | Running     | internal=10.230.41.129
                    |
| 2a19190f-6ae9-4302-b036-f12ec10f84bb | test-clement                                        | ACTIVE  | -          | Running     | internal=10.230.41.128;
rnal=208.85.144.165 |
+--------------------------------------+-----------------------------------------------------+---------+------------+-------------+-------------------------
--------------------+

C:\Users\matsuo\Dev\bb>




0 件のコメント:

コメントを投稿