2015年10月13日火曜日

IBM Cloud OpenStack Service (ICOS)をコマンドラインインタフェースから制御する

IBM Cloud OpenStack Services (ICOS)をコマンドラインから操作してみよう。

参考にした文献はICOSのDocument

Using the command line client

http://open.ibmcloud.com/documentation/getting-started-with-command-line.html

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

テストした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をインストールする。
Python 3系列には含まれているが、Python 2.7は下記のコマンドでインストールする必要がある。
コマンドプロンプトから以下のコマンドを実行する。

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ファイルをダウンロードする。
左側のメニューから「アクセスおよびセキュリティ」を選び、上部のタブから「APIアクセス」を選ぶとダウンロードボタンが表示されるので、それをクリックする。




RCファイルの中身は下記のようになっている。

#!/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://192.168.101.10:5000/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=8386fc36185a45b487adba9225c2fad8
export OS_TENANT_NAME="Demo3"

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

# 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用に描き直す必要がある。
筆者の環境では、env.batという名前のバッチファイルを作成した。


c:\Users\IBM_ADMIN\Dev\OpenStack>type env.bat
set OS_AUTH_URL=https://192.168.101.10:5000/v2.0
set OS_TENANT_ID=8386fc36185a45b487adba9225c2fad8
set OS_TENANT_NAME=Demo3
set OS_USERNAME=<Userid>
set OS_PASSWORD=<Password>
set OS_REGION_NAME=RegionOne


 UseridとPasswordには実際のものを代入してほしい。


SSL証明書をインストールする

APIの認証(実際はKeystoneによる認証)にはSSL証明書が必要。

以下のURLから証明書をダウンロードする。

http://open.ibmcloud.com/documentation/_downloads/ca.pem

ダウンロードした証明書を以下のディレクトリに置く。
C:\Python27\Lib\site-packages\requests

このディレクトリはpython-openstackclientをインストールした際にできたもの。
ここにcacert.pemという証明書がある。

ダウンロードした証明書をこのcacert.pemの後ろに追加する。テキストエディタで追記してもよいが、以下のコマンドが簡単である。

C:\Python27\Lib\site-packages\requests> copy cacert.pem+ca.pem cacert.pem


(別の方法)

上記の方法はPython言語の中に証明書を導入する方法だが、そうはせずに、自分の環境に証明書を置き、証明書の場所を直接指定する方法もある。

例えば、C:\Users\IBM_ADMIN\Dev\OpenStackというディレクトリを作成し、そこにダウンロードしたca.pemを置く。
そして、RCファイルの最後に以下の行を追加する。

set OS_CACERT=C:\Users\IBM_ADMIN\Dev\OpenStack\ca.pem


OpenStackコマンドを実行する


RCファイルを実行すれば、OpenStackコマンドを使えるようになる。



C:\Users\IBM_ADMIN\Dev\OpenStack>nova list
No handlers could be found for logger "keystoneclient.auth.identity.generic.base"
+--------------------------------------+---------------------------+--------+------------+-------------+----------------
----------------------+
| ID                                   | Name                      | Status | Task State | Power State | Networks
                      |
+--------------------------------------+---------------------------+--------+------------+-------------+----------------
----------------------+
| 09ea4f34-cae1-4977-8529-9710a63cd820 | Cent OS Instance 20151001 | ACTIVE | -          | Running     | Shared-Custom-N
etwork4=192.168.1.195 |
| dbc823ba-7aaf-4a9d-af54-f3c6c2747e03 | CentOS_20150921           | ACTIVE | -          | Running     | Shared-Custom-N
etwork1=15.15.15.7    |
| 56b9269b-180e-45a5-8d78-7e4b2758c466 | Win2012JP                 | ACTIVE | -          | Running     | Shared-Custom-N
etwork4=192.168.1.190 |
+--------------------------------------+---------------------------+--------+------------+-------------+----------------
----------------------+