Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Hey Siri, ConoHaでサーバー立てて!

Last updated at Posted at 2024-12-07

Hey Siri, ConoHaでサーバー立てて!

前置き

普段開発をされているエンジニアの方であれば、ちょっとした検証の際に一時的にサーバーを立てることはよくあることだと思います。

しかし、"ちょっと"のためにダッシュボードにログインして、サーバーを作成して、SSHの設定をして、といった手順を踏むのは面倒です。部屋の電気をつけるような感覚で、Hey Siri!の一声でサーバーを立てられたら便利ですよね...?

そんなわけで、今回はConoHaのAPIとSiriを使って、サーバーを立ててみます。

準備

環境

構築環境は以下の通りです。

  • ConoHa VPS
    • Ubuntu 22.04
    • 2Core / 1GB / 100GB プラン
  • Shorcuts
    • iPhone 15
    • iOS 18.1.1
    • Siri Enabled
    • a-Shell 1.15.8

ConoHaのアカウントを作成

ConoHaのアカウントをお持ちでない方は、こちらからアカウントを作成してください。

※上のリンクは招待リンクです。招待リンクを踏みたくない方は公式サイトからアカウントを作成してください。

APIの設定

ConoHaのAPIを使うためには、APIの設定が必要です。まずは、ConoHaのダッシュボードにログインし、APIの設定を行います。

APIユーザーを作成して、以下の情報をメモしておきます。

  • テナント情報
    • テナントID
  • APIユーザー
    • ユーザーID
    • パスワード

api.png

OSとプランの選択

VPSの作成には、OSとプランの選択が必要です。
一度手動でAPIを叩いて、OSとプランのIDを取得しておきます。

1. トークンの取得

各APIにはAPIユーザーの認証情報ではなく、認証情報から取得できるトークンを使用してアクセスします。まずはトークンを取得します。

レスポンスヘッダーのうち、X-Subject-Tokenの値がトークンとなります。これを次のリクエストで使用します。トークンの有効期限は24時間です。

get_token
curl -i -X POST \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-d '{
  "auth": {
    "identity": {
      "methods": ["password"],
      "password": {
        "user": {
          "id": "APIユーザーID",
          "password": "パスワード"
        }
      }
    },
    "scope": {
      "project": {
        "id": "テナントID"
      }
    }
  }
}' \
https://identity.c3j1.conoha.io/v3/auth/tokens
get_token_response_header
HTTP/1.1 201 Created
server: nginx
date: Thu, 05 Dec 2024 22:50:03 GMT
content-type: application/json
content-length: 2709
vary: X-Auth-Token
x-subject-token: xxx

2. OSイメージの取得

先ほど取得したトークンを用いて、使用可能なイメージの一覧を取得します。今回はUbuntu 22.04を使用するので、レスポンスからvmi-ubuntu-22.04-amd64を探し、イメージIDをメモしておきます。

get_images
curl -X GET \
-H "Accept: application/json" \
-H "X-Auth-Token: トークン" \
https://image-service.c3j1.conoha.io/v2/images?limit=200
get_images_response
{
	"images": [
    {
      // 省略
    },
		{
			"status": "active",
			"name": "vmi-ubuntu-22.04-amd64",
			"tags": [
				"service_type=vps",
				"dst_name=Ubuntu",
				"dst_version=22.04",
				"display_order=150"
			],
			"container_format": "ovf",
			"created_at": "2023-10-25T09:55:09Z",
			"disk_format": "qcow2",
			"updated_at": "2023-11-22T03:06:57Z",
			"visibility": "public",
			"self": "/v2/images/51609119-6b0a-48dc-b061-2a70d75f8fe0",
			"min_disk": 100,
			"protected": false,
			"id": "51609119-6b0a-48dc-b061-2a70d75f8fe0",
			"file": "/v2/images/51609119-6b0a-48dc-b061-2a70d75f8fe0/file",
			"checksum": "3a0d3834019f4782c924050611e83a08",
			"os_type": "linux",
			"os_hash_algo": "sha512",
			"os_hash_value": "421b02a4d6be8d4dc3a575fef503a6289c782029cecd707e542f85431d6929ca97ec3a3f1d826b99f7f33bb3f48fe571b32e6188a2d172ca4ebf2d444a12870a",
			"os_hidden": false,
			"owner": "ad53f93815c44822ada4bd2be9e7b4ae",
			"size": 6983843840,
			"min_ram": 1024,
			"schema": "/v2/schemas/image",
			"virtual_size": null,
			"architecture": "x86_64"
		},
    {
      // 省略
    }
	],
	"schema": "/v2/schemas/images",
	"first": "/v2/images?limit=200"
}

3. プランの取得

プランの一覧を取得します。今回は2Core / 1GB / 100GBプランを使用するので、レスポンスからg2l-t-c2m1を探し、プランIDをメモしておきます。

get_flavors
curl -X GET \
-H "Accept: application/json" \
-H "X-Auth-Token: トークン" \
https://compute.c3j1.conoha.io/v2.1/flavors
get_flavors_response
{
	"flavors": [
    {
      // 省略
    },
		{
			"id": "f2a77529-1815-43a2-bc14-1f3f6b09079c",
			"name": "g2l-t-c2m1",
			"links": [
				{
					"rel": "self",
					"href": "https://compute.c3j1.conoha.io/v2.1/flavors/f2a77529-1815-43a2-bc14-1f3f6b09079c"
				},
				{
					"rel": "bookmark",
					"href": "https://compute.c3j1.conoha.io/flavors/f2a77529-1815-43a2-bc14-1f3f6b09079c"
				}
			]
		},
    {
      // 省略
    }
	]
}

4. SSHキーの作成

サーバー接続時に使用するSSHキーを作成します。privatekeyを保存しておきます。

create_ssh_key
curl -X POST \
-H "Accept: application/json" \
-H "X-Auth-Token: トークン" \
-d '{"keypair": {"name": "ubuntu-2204-ssh"}}' \
https://compute.c3j1.conoha.io/v2.1/os-keypairs
create_ssh_key_response
{
	"keypair": {
		"private_key": "-----BEGIN RSA PRIVATE KEY-----\n**********************\n-----END RSA PRIVATE KEY-----\n",
		"name": "ubuntu-2204-ssh",
		"public_key": "xxx",
		"fingerprint": "xxx",
		"user_id": "xxx"
	}
}

ショートカットの作成

スクリプトの作成

1. トークンの取得

先ほどと同様にして、トークンを取得します。

2. ボリュームの作成

VPSの起動用ボリュームを作成します。ボリュームIDをメモしておきます。

create_volume
curl -X POST \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "X-Auth-Token: トークン" \
-d '{
  "volume": {
    "size": 100,
    "description": null,
    "name": "ubuntu-2204-vol1",
    "volume_type": "c3j1-ds02-boot",
    "imageRef": "イメージID"
  }
}' \
https://block-storage.c3j1.conoha.io/v3/テナントID/volumes
create_volume_response
{
	"volume": {
		"id": "xxx",
		"status": "creating",
		"size": 100,
		"availability_zone": "nova",
		"created_at": "2024-12-06T00:52:47.297902",
		"updated_at": null,
		"name": "ubuntu-2204-vol1",
		"description": null,
		"volume_type": "c3j1-ds02-boot",
		"snapshot_id": null,
		"source_volid": null,
		"metadata": {
			"via_api": "ext-api"
		},
		"links": [
			{
				"rel": "self",
				"href": "xxx"
			},
			{
				"rel": "bookmark",
				"href": "xxx"
			}
		],
		"user_id": "xxx",
		"bootable": "false",
		"encrypted": false,
		"replication_status": null,
		"consistencygroup_id": null,
		"multiattach": false,
		"attachments": []
	}
}

3. サーバーの作成

取得したトークンを用いて、サーバーを作成します。サーバーIDをメモしておきます。

create_server
curl -X POST \
-H "Accept: application/json" \
-H "X-Auth-Token: トークン" \
-d '{
  "server": {
    "flavorRef": "プランID",
    "adminPass": "管理者パスワード",
    "block_device_mapping_v2": [
      {
        "uuid": "ボリュームID"
      }
    ],
    "metadata": {
      "instance_name_tag": "ubuntu-2204-srv1",
    },
    "security_groups": [
      {
        "name": "IPv4v6-SSH"
      }
    ]
    "key_name": "ubuntu-2204-ssh",
  }
}' \
https://compute.c3j1.conoha.io/v2.1/servers

4. サーバー詳細の取得

サーバーの詳細を取得します。IPv4アドレスをメモしておきます。

get_server_detail
curl -X GET \
-H "Accept: application/json" \
-H "X-Auth-Token: トークン" \
https://compute.c3j1.conoha.io/v2.1/servers/サーバーID
get_server_detail_response
{
	"server": {
    // 省略
		"addresses": {
			"ext-v4v6-xxx": [
				{
					"version": 6,
					"addr": "xxx",
					"OS-EXT-IPS:type": "fixed",
					"OS-EXT-IPS-MAC:mac_addr": "xxx"
				},
				{
					"version": 4,
					"addr": "xxx",
					"OS-EXT-IPS:type": "fixed",
					"OS-EXT-IPS-MAC:mac_addr": "xxx"
				}
			]
		},
    // 省略
	}
}

ショートカットの作成

ショートカットアプリを開き、新しいショートカットを作成します。

後でSiriで呼び出す際にショートカットの名前を指定するため、わかりやすい名前をつけておきます。

また、ショートカットアプリの「URLの内容を取得」アクションではレスポンスヘッダーを取得できないため、a-Shell経由でcurlコマンドを実行します。

以下のようなショートカットとなります。画像をクリックすると別タブで開きます。

完成したら、一度実行してa-Shellへのアクセスを許可してください。一度許可すれば、次回以降は自動で許可されます(ショートカットの設定から設定を変更可能)。

ショートカットをホーム画面に追加することで、Siriから呼び出せるようになります。
ホーム画面追加時に指定した名前(例:ConoHaでサーバー立てて)でSiriに話しかけることで、ショートカットを実行できます。

Hey Siri! ConoHaでサーバー立てて!

準備が出来たので、実際にやってみましょう。

Hey Siri! ConoHaでサーバー立てて!

Siriがショートカットを実行し、サーバーが立ち上がりました!

作成しておいたSSH鍵でサーバーにログインして、自由に作業できます。

まとめ

今回はConoHaのAPIとSiriを使って、サーバーを立ててみました。

ConoHaのAPIでは他にも様々な機能が提供されているので、ぜひ自分専用のショートカットを作ってみてください!

ps iOSのショートカットの標準機能ではシークレットを扱えません(今回使用した認証情報などは、ショートカットアプリからいつでも確認できてしまいます)。業務利用の際は十分にお気を付けください。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?