VagrantでGCE(Google Compute Engine)インスタンスを作成
VagrantでGCE(Google Compute Engine)用のプラグインがありましたので早速試してみました。mitchellh / vagrant-googleのReadmeでサクっと…いきませんでした。
いきなりプラグインのインストールで「The plugin ‘vagrant-google’ could not be found in local or remote repositories. Please check the name of the plugin and try again.」って怒られます。
なんかよくわかりませんが、このissueに対応法が書いてましたのでやってみたらプラグインのインストールはできました。
日本語で解説しますと(Vagrantはインストール済みという前提で)まずmitchellh / vagrant-googleをCloneします。
$ git clone git@github.com:mitchellh/vagrant-google.git Cloning into 'vagrant-google'... remote: Reusing existing pack: 296, done. remote: Total 296 (delta 0), reused 0 (delta 0) Receiving objects: 100% (296/296), 61.48 KiB | 0 bytes/s, done. Resolving deltas: 100% (174/174), done. Checking connectivity... done``` vagrant-google.gemspecがある階層(Cloneしたルートですが)を開いて以下のようにします。
$ cd vagrant-google/ $ gem build vagrant-google.gemspec WARNING: licenses is empty Successfully built RubyGem Name: vagrant-google Version: 0.1.1 File: vagrant-google-0.1.1.gem $ vagrant plugin install vagrant-google-0.1.1.gem Installing the ‘vagrant-google-0.1.1.gem’ plugin. This can take a few minutes… Installed the plugin ‘mini_portile (0.5.2)’! $ vagrant box add gce google.box Downloading or copying the box… Extracting box…e: 0/s, Estimated time remaining: –:–:–) Successfully added box ‘gce’ with provider ‘google’!```
さあ、これで多分インスタンスをサクっと作れるようになったと思うので早速Vagrantfileを作ります。Cloneしたディレクトリ内にExampleがあるので、まずは最もシンプルなやつで試してみましょう。
※本当はここまででMac OS X Mavericks で Vagrant がこける時の対処法に散々ハマってました。よくわからないエラーで落ちてたのでVirtualboxとVagrantを再インストールしたら行けました。あ、あと、その前にもfogがコンフリクトとか言ってたのでgemを全部アンインストールしたり(これは多分環境依存かな)結構大がかりでした。
とりあえずVagrantfileは以下のようになるんですが…
GCE(Google Compute Engine)を触ったこと無いとハマると思います。
まずプロジェクトIDは以下の画像の通り普通にプロジェクトのIDです。

次にGCE(Google Compute Engine)のAPIを有効にするのを忘れずに。

次にサービスアカウントなるものを作ります。以下の画像の「新しいクライアントIDを作成」ボタンをクリックして表示されるダイアログで「サービスアカウント」を選択して作成してください。

作成後、プライベートキーファイルのダウンロードが始まるので適当な場所に落としつつ同時に表示されるパスフレーズ(秘密キーのパスワード)もコピペしておきます。
で、Vagrantfileのgoogle.google_client_emailには、ここで発行される「サービスアカウント」のメールアドレスを入れます。
また、Vagrantfileのgoogle.google_key_locationには、ダイアログウィザードの途中で落ちてきたプライベートキーへのパスを入れます。
で、ここからが問題でVagrantfileのテンプレではSSHのユーザ名にローカルPCのユーザ名を指定していますがコレっていつどこで教えるんだろう?
とりあえず手動でインスタンス作成してみると「SSH」というボタンが現れました。

これボタンなんですけど押すと「gcutil –service_version=“v1” –project=“プロジェクト名” ssh –zone=“ゾーン名” “インスタンス名”」と、このコマンドを叩けとしか書いてません。
しょうがないのでgcutilをココからインストールして言われた通りコマンドを叩くと何か怒られます。
$ gcutil --service_version="v1" --project=<project-name> ssh --zone=<zone-name> <instance-name> There is no currently active account. You can activate an already-authorized account with $ gcloud config set account <account> or authorize a new account with $ gcloud auth login or any of the other gcloud auth subcommands with $ gcloud auth --help``` アカウントをconfig setしてauth loginしろと。とりあえず言われた通りに...
$ gcloud config set account $ gcloud auth login```
するとブラウザでOAuth認証画面が開く。あーGoogleでよくあるよくある…
ブラウザでの認証が終わるとプロジェクトID訊かれるのでID入れると何かOKっぽいこと言われます。
Your browser has been opened to visit: https://accounts.google.com/o/oauth2/auth?scope=... You can list your projects and create new ones in the Google Cloud console at https://cloud.google.com/console. If you have a project ready, you can enter it now. Enter a cloud project id (or leave blank to not set): <your-project-id> You are logged in as <your-gmail>.``` で、先ほどのSSH接続らしきコマンドを打つと勝手に鍵を作られます。ここで先ほどコピペしたパスフレーズ(秘密キーのパスワード)を訊かれるので入れます。その後、結構長いこと待たされます(数分間)
$ gcutil –service_version=“v1” –project= ssh –zone= WARNING: You don’t have an ssh key for Google Compute Engine. Creating one now… Enter passphrase (empty for no passphrase): Enter same passphrase again:```
で、やっとSSHログインできるんですがリモートのユーザ名が勝手にローカルのユーザ名になってます。
[<ローカルのユーザ名>@test~]$``` ということでVagrantfileのSSHユーザ名はそのままで良くて、SSHの秘密鍵はGoogleが勝手に作った鍵(恐らく「google_compute_engine」というファイル名)へのパスを入れます。 つまりプロジェクト毎に必ず最初に1個、手動でVMを作る必要があるってことですね! これで、やっとVagrant upできます!
$ vagrant up –provider=google Bringing machine ‘default’ up with ‘google’ provider… [default] Warning! The Google provider doesn’t support any of the Vagrant high-level network configurations (config.vm.network). They will be silently ignored. [default] Launching an instance with the following settings… [default] – Name: new [default] – Type: n1-standard-1 [default] – Image: debian-7-wheezy-v20130617 [default] – Zone: us-central1-a [default] – Network: default [default] – Metadata: ‘{}’ [default] Waiting for instance to become “ready”… [default] Machine is booted and ready for use! [default] Waiting for SSH to become available… [default] Machine is ready for SSH access!```
よし、問題なく完了。管理コンソールに表示されてます。

SSH接続も…
``` $ vagrant ssh Linux new 3.3.8-gcg-201305291443 #1 SMP Wed May 29 14:49:59 PDT 2013 x86_64
The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. <ローカルのユーザ名>@new:~$```
いやー意外と疲れた…