コマンドラインツール「sacloud」でさくらのクラウドを操作する 2ページ

エンドポイントおよびアクセストークンを登録する

 sacloud CLIを利用するには、まずさくらのクラウドAPIを利用するためのURLと、アクセストークンを登録する必要がある。これらの作業は、「sacloud config」コマンドで実行する。

 まずURLの登録だが、さくらのクラウドのゾーンごとにこのURLは異なる。APIドキュメントに記載されているが、第1ゾーンの場合は「https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/」、第2ゾーンの場合は「https://secure.sakura.ad.jp/cloud/zone/is1b/api/cloud/1.1/」となる。使用しているゾーンに応じて、以下のようにしてこれらURLを登録する。

第1ゾーンの場合:
$ sacloud config --apiRoot="https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/"
第2ゾーンの場合:
$ sacloud config --apiRoot="https://secure.sakura.ad.jp/cloud/zone/is1b/api/cloud/1.1/"

 続いて、アクセストークン情報を登録する。

$ sacloud config --accessToken=<アクセストークン> --accessTokenSecret=<アクセストークンシークレット>

 以上で設定は完了だ。設定されている情報は次のようにして確認できる。

$ sacloud config
/Users/foo/.sacloudcfg.json:
{
  "apiRoot": "https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/",
  "accessToken": "<アクセストークン>",
  "accessTokenSecret": "<アクセストークンシークレット>"
}

 最後に正しく設定が行えているかどうかを、作成されている仮想サーバーを表示する「sacloud show server」」コマンドを実行して確認しておこう。正しく設定できていれば、以下のようにサーバー一覧が表示される。

$ sacloud show server
GET https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/server.json?{"Count":0} -> 200 OK (1/1) ~0.645sec
+--------------+-------------------------------+--------+---------------------+
| id           | name                          | status | created at          |
+--------------+-------------------------------+--------+---------------------+
| 112500152369 | centos 48-120                 | down   | 2013-04-05 12:46:25 |
+--------------+-------------------------------+--------+---------------------+
 :
 :

サーバーの情報を確認する

 続いては、sacloudでAPIを実行する例を紹介していこう。まずはサーバーの情報などを確認するコマンドだ。先ほど、「sacloud show server」コマンドで作成されている仮想サーバーの一覧を表示したが、サーバーIDを引数として「show server」コマンドを実行すると、指定したサーバーについての詳細を確認できる。

$ sacloud show server 112500152369
GET https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/server/112500152369.json?{"Count":0} -> 200 OK (1/1) ~0.397sec
+-------------------+-------------------------------------------------------------+
| id                | 112500152369                                                |
+-------------------+-------------------------------------------------------------+
| zone              | 31001:is1a                                                  |
+-------------------+-------------------------------------------------------------+
| name              | centos 48-120                                               |
+-------------------+-------------------------------------------------------------+
| tags              | @virtio-net-pci, @keyboard-us                               |
+-------------------+-------------------------------------------------------------+
| plan              | 1:(旧)プラン1                                               |
+-------------------+-------------------------------------------------------------+
| cpu               | 1                                                           |
+-------------------+-------------------------------------------------------------+
| memory            | 2048MB                                                      |
+-------------------+-------------------------------------------------------------+
| status            | down                                                        |
+-------------------+-------------------------------------------------------------+
| status changed at | 2014-03-09 21:28:48                                         |
+-------------------+-------------------------------------------------------------+
| hypervisor        |                                                             |
+-------------------+-------------------------------------------------------------+
| disk              | 112500152368:名称未設定 サーバ 13dda3aa184, virtio, 20480MB |
+-------------------+-------------------------------------------------------------+
| interface         | 112500152370:**:**:**:**:**:** -> ***.***.**.***            |
+-------------------+-------------------------------------------------------------+
| created at        | 2013-04-05 12:46:25                                         |
+-------------------+-------------------------------------------------------------+

 なお、sacloudコマンドはデフォルトでは表形式で出力を行うが、「–csv」オプションを付けることでCSV形式で、「–tsv」オプションを付けることでTSV形式で、「–json」オプションを付けることでJSON形式で出力が可能だ。これらはスクリプトなどで出力を加工する場合などに便利だ。

$ sacloud --json show server 112500152369
[{
  "requestInfo": {
    "time": 1397396161193,
    "method": "GET",
    "url": "https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/server/112500152369.json?{\"Count\":0}",
    "path": "server/112500152369"
  },
  "request": {
    "Count": 0
  },
  "responseInfo": {
    "time": 1397396161553,
    "latency": 360,
    "length": 1957,
    "serial": "66493241c0670b52489191b7ba8b53ae",
    "status": 200,
    "statusText": "OK",
    "type": "resource",
    "key": "server"
  },
  "response": {
    "server": {
      "id": "112500152369",
      "name": "centos 48-120",
      "hostName": "localhost",
      "description": "",
      "serviceClass": "cloud/plan/1",
      "createdAt": "2013-04-05T12:46:25+09:00",
      "icon": {
        "id": "112300511981",
        "url": "https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/icon/112300511981.png",
        "name": "CentOS",
        "scope": "shared"
      },
      "serverPlan": {
        "id": 1,
        "name": "(旧)プラン1",
        "cpu": 1,
        "memoryMB": 2048,
        "serviceClass": "cloud/plan/1",
        "availability": "disabled"
      },
      "zone": {
        "id": 31001,
        "name": "is1a",
        "description": "石狩第1ゾーン",
        "vncProxy": {
          "hostName": "sac-is1a-ssl.sakura.ad.jp",
          "ipAddress": "133.242.31.244"
        },
        "ftpServer": {
          "hostName": "sac-is1a-ssl.sakura.ad.jp",
          "ipAddress": "133.242.31.244"
        },
        "region": {
          "id": 310,
          "name": "石狩",
          "description": "石狩",
          "nameServers": [
            "133.242.0.3",
            "133.242.0.4"
          ]
        }
      },
      "instance": {
        "server": {
          "id": "112500152369"
        },
        "status": "down",
        "beforeStatus": "cleaning",
        "statusChangedAt": "2014-03-09T21:28:48+09:00",
        "migrationProgress": null,
        "migrationSchedule": null,
        "isMigrating": null,
        "migrationAllowed": null,
        "modifiedAt": "2014-03-09T21:28:48+09:00",
        "host": null,
        "cdrom": null,
        "cdromStorage": null
      },
      "disks": [
        {
          "id": "112500152368",
          "name": "名称未設定 サーバ 13dda3aa184",
          "connection": "virtio",
          "connectionOrder": 1,
          "reinstallCount": 0,
          "availability": "available",
          "sizeMB": 20480,
          "storage": {
            "id": "3100196005",
            "mountIndex": "3100196005",
            "class": "iscsi1204"
          },
          "bundleInfo": null
        }
      ],
      "interfaces": [
        {
          "id": "112500152370",
          "macAddress": "**:**:**:**:**:**",
          "ipAddress": "***.***.**.***",
          "userIPAddress": null,
          "hostName": null,
          "switch": {
            "id": "112400128561",
            "name": "sac-is1a-rt1-***.***.**.*",
            "scope": "shared",
            "subnet": {
              "id": null,
              "networkAddress": "***.***.**.*",
              "networkMaskLen": 24,
              "defaultRoute": "***.***.**.*",
              "internet": {
                "bandWidthMbps": 100
              }
            },
            "userSubnet": {
              "defaultRoute": "***.***.**.*",
              "networkMaskLen": 24
            }
          },
          "packetFilter": null
        }
      ],
      "appliance": null,
      "tags": [
        "@virtio-net-pci",
        "@keyboard-us"
      ]
    },
    "is_ok": true
  }
}]