GCPのローカル開発環境を効率化する方法(Mac版)
最近 Google Cloud Platform を上のサービスを開発する機会に恵まれ、Datastore、BigtableやPub/SubなどGCPのクラウドサービスを利用する機会が増えてきました。
ローカル開発をする際にエミュレーターを使っているのですが、公式ドキュメント通りに環境を構築すると毎回ターミナルコマンド起動する必要があり非常に面倒です。
PCの起動時に自動でエミュレーターが立ち上がるように開発環境を改善してみました。
今回は Datastoreエミュレーターを使ってサンプルを記述します。
Datastore エミュレーターのインストール
まずドキュメント通りに gcloud クライアントをダウンロードします
Cloud SDK のインストール | Cloud SDK のドキュメント | Google Cloud
そしてDatastoreのエミュレーターをインストールし、起動できることを確認します、
gcloud components install cloud-datastore-emulator # インストール gcloud beta emulators datastore start # 起動
自動起動設定の準備
今回は LaunchAgent を使ってPC起動時にエミュレーターが起動するようします。
まずはLaunchAgent で扱いやすいように /usr/local にシンボリックリンクを生成しましょう。
sudo ln -s ~/google-cloud-sdk /usr/local/google-cloud-sdk # google-cloud-sdk のパスはダウンロードしたパスに変更してください
自動起動の設定
最後にPCが立ち上がった際に自動でエミュレーターが立ち上がるようにLaunchAgentの設定を記述します。
emacs ~/Library/LaunchAgents/gcloud.emulator.datastore.plist
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>KeepAlive</key> <true/> <key>Label</key> <string>gcloud.emulator.datastore</string> <key>ProgramArguments</key> <array> <string>/usr/local/google-cloud-sdk/bin/gcloud</string> <string>beta</string> <string>emulators</string> <string>datastore</string> <string>start</string> </array> <key>RunAtLoad</key> <true/> </dict> </plist>
以上で設定が完了しました。
まとめ
※ 今回の設定は、私が手動で起動しているのを見かねた先輩が設定してくれたものです。ありがとうございました。