ようへいの日々精進XP

よかろうもん

超メモで走り切る 2015 年(6) LXD REST API を使った Test Kitchen のドライバを作ってみたメモ

tl;dr

LXD の REST API を使った Test Kitchen のドライバを作ってみたのでメモ。

既に LXD には kitchen-lxd_cli というドライバが存在していて、特別な理由が無い以外は kitchen-lxd_cli のドライバの利用をオススメしたい。(コンテナのリソース制御等も設定出来たりするので)

尚、今回、こちら の記事にて...

Test-Kitchen のバージョン1.4以降ではVMの調達に関連する処理を、独立したモジュールとして扱えるようになりました。

と書かれていたので、これはいいネタになると思って、LXD REST APIクライアントライブラリっぽいものを作りつつ、そのライブラリを利用して仕上げてみた。

尚... LXD の REST API は絶賛開発中とのことですので、ここに書いてある内容は本記事を書いた時点の内容となりますのでご注意くださいませ。


参考


memo

作ったもの

github.com

試した環境

  • ホスト環境
$ cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.3 LTS"

$ lxd --version
0.25
  • 教材

github.com

インストール

$ git clone https://github.com/inokappa/oreno_lxdapi.git
$ cd oreno_lxdapi
$ bundle install
$ rake install:local
  • ドライバのインストール
$ git clone https://github.com/inokappa/kitchen-lxd_api.git
$ cd kitchen-lxd_api
$ bundle install
$ rake install:local

設定

  • 事前にコンテナを作成する
$ lxc remote add images images.linuxcontainers.org
$ lxc launch images:ubuntu/trusty/amd64 oreno-ubuntu
$ lxc exec oreno-ubuntu -- apt-get -y install openssh-server
$ lxc stop oreno-ubuntu
$ lxc publish oreno-ubuntu --alias=oreno-ubuntu-image
  • .kitchen.local.yml
---
driver:
  name: lxd_api

provisioner:
  name: ansible_playbook
  roles_path: roles

platforms:
  - name: oreno-ubuntu-14.04
    driver_plugin: lxd_api
    driver_config:
      container_image: oreno-ubuntu-image
      container_name: kitchen-container
      username: kitchen

suites:
  - name: default
    provisioner:
      playbook: default.yml
      hosts: default

verifier:
  name: shell
  command: rspec -c -f d -I serverspec serverspec/common_spec.rb

kitchen-verifier-shell が手放せなくなりそう。

デモ

  • kitchen list
$ kitchen list
Instance                   Driver  Provisioner      Verifier  Transport  Last Action
default-oreno-ubuntu-1404  LxdApi  AnsiblePlaybook  Shell     Ssh        <Not Created>
  • kitchen create
$ kitchen create
-----> Starting Kitchen (v1.4.2)
-----> Creating <default-oreno-ubuntu-1404>...
       Create Container...
       Run Container...
still starting...
still starting...
       Set Username and Upload Public key...
       Change Permission...
       Get Container IP address...
       Finished creating <default-oreno-ubuntu-1404> (0m8.74s).
-----> Kitchen is finished. (0m8.79s)

kitchen list で確認。

$ kitchen list
Instance                   Driver  Provisioner      Verifier  Transport  Last Action
default-oreno-ubuntu-1404  LxdApi  AnsiblePlaybook  Shell     Ssh        Created

一応、lxc list でコンテナ一覧を確認。

$ lxc list
+-------------------+---------+------------------+------+-----------+-----------+
|       NAME        |  STATE  |       IPV4       | IPV6 | EPHEMERAL | SNAPSHOTS |
+-------------------+---------+------------------+------+-----------+-----------+
| kitchen-container | RUNNING | 10.0.x.xx (eth0) |      | YES       |         0 |
+-------------------+---------+------------------+------+-----------+-----------+
| oreno-ubuntu      | STOPPED |                  |      | NO        |         0 |
+-------------------+---------+------------------+------+-----------+-----------+

コンテナが作成されている。

  • kitchen login
$ kitchen login
Welcome to Ubuntu 14.04.3 LTS (GNU/Linux 3.13.0-74-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

kitchen@kitchen-container:~$
  • kitchen converge
$ kitchen converge
-----> Starting Kitchen (v1.4.2)
-----> Converging <default-oreno-ubuntu-1404>...
       Preparing files for transfer
       Preparing playbook
       Preparing inventory file
       Preparing modules
       nothing to do for modules

(snip)

       PLAY [all] ******************************************************************** 
       
       GATHERING FACTS *************************************************************** 
       ok: [localhost]
       
       TASK: [common | file path=/tmp/sample.txt state=touch mode=0644] ************** 
       changed: [localhost]
       
       PLAY RECAP ******************************************************************** 
       localhost                  : ok=2    changed=1    unreachable=0    failed=0   
       
       Finished converging <default-oreno-ubuntu-1404> (2m10.34s).
-----> Kitchen is finished. (2m10.38s)
zlib(finalizer): the stream was freed prematurely.
  • kitchen verify
$ kitchen verify
-----> Starting Kitchen (v1.4.2)
-----> Setting up <default-oreno-ubuntu-1404>...
       Finished setting up <default-oreno-ubuntu-1404> (0m0.00s).
-----> Verifying <default-oreno-ubuntu-1404>...
       [Shell] Verify on instance=#<Kitchen::Instance:0x007f0021231090> with state={:username=>"kitchen", :hostname=>"10.0.x.xxx", :last_action=>"setup"}


File "/tmp/sample.txt"
  should be file

Finished in 0.32268 seconds (files took 0.87866 seconds to load)
1 example, 0 failures

       Finished verifying <default-oreno-ubuntu-1404> (0m1.28s).
-----> Kitchen is finished. (0m1.33s)
  • kitchen destory
$ kitchen destroy
-----> Starting Kitchen (v1.4.2)
-----> Destroying <default-oreno-ubuntu-1404>...
       Destroy Container...
       Finished destroying <default-oreno-ubuntu-1404> (0m0.01s).
-----> Kitchen is finished. (0m0.05s)

一応、lxc list でコンテナ一覧を確認。

$ lxc list
+--------------+---------+------+------+-----------+-----------+
|     NAME     |  STATE  | IPV4 | IPV6 | EPHEMERAL | SNAPSHOTS |
+--------------+---------+------+------+-----------+-----------+
| oreno-ubuntu | STOPPED |      |      | NO        |         0 |
+--------------+---------+------+------+-----------+-----------+

コンテナが停止と共に削除されている。


最後に...

安心してください...作れますよ

冒頭にも書いたが、test-kitchen の改善によりドライバが書きやすくなったことを体感した。

ドライバ書くにあたってのメモ

  • Kitchen::Driver::Base をロードしよう
  • kitchen_driver_api_version 2 も付けておこう
  • 基本的には createdestroyメソッドに処理を書く
  • state という変数にハッシュでコンテナの情報を色々と突っ込むことが出来る

Chef とか Ansible の...

レシピの書き方も一向に上達しないのにも関わらず、周辺のエコシステムに触手を伸ばしてしまって申し訳ございません。