updated on 2020-01-22
Google Cloud SDK は、Google Cloud Platform にホストされているリソースとアプリケーションの管理に使用できるツールセット。gcloud
、gsutil
、bq
などのコマンドライン ツールもその一部。
・GCPアカウントが既に開設済であり、任意のプロジェクトが作成されている
・Macユーザー
導入は以下の公式ドキュメントに沿って行います。
しかし、ドキュメントが簡潔に書かれ過ぎている手前、戸惑うか箇所がいくつかあったので、一部始終の手順を残します。
$ python -V
Python 2.7.15
ドキュメントにはインストーラーは任意の場所に配置とあるが、今回はホームディレクトリ配下にgcpというフォルダを作成し、その配下に導入する形で進める。
インストーラーは、macOS 64 ビット(x86_64)の場合、google-cloud-sdk-245.0.0-darwin-x86.tar.gz
$ cd ~/
$ pwd
/Users/myusername myusernameをホームディレクトリと呼ぶ
$ mkdir gcp
$ mv /Users/myusername/Downloads/google-cloud-sdk-245.0.0-darwin-x86.tar.gz /Users/myusername/gcp/
$ cd gcp
$ tar xvf google-cloud-sdk-245.0.0-darwin-x86_64.tar.gz
中に含まれているインストールスクリプト(./google-cloud-sdk/install.sh)を実行。途中幾つかYes/Noを聞かれるので、回答しつつ導入を進める。終盤には必要な「環境変数の指定」に関する設定の記述先の追記も行われている。
MacBook-Pro:gcp $ ./google-cloud-sdk/install.sh
Welcome to the Google Cloud SDK!
pyenv: python2: command not found
The `python2' command exists in these Python versions:
2.7.15
筆者はpyenvでバージョン 3.7.1を固定していたので、エラーが出たが、gcpフォルダ配下では2.7.15を固定にする。
MacBook-Pro:gcp$ pyenv local 2.7.15
MacBook-Pro:gcp$ ./google-cloud-sdk/install.sh Welcome to the Google Cloud SDK! To help improve the quality of this product, we collect anonymized usage data and anonymized stacktraces when crashes are encountered; additional information is available at <https://cloud.google.com/sdk/usage-statistics>. You may choose to opt out of this collection now (by choosing 'N' at the below prompt), or at any time in the future by running the following command: gcloud config set disable_usage_reporting true Do you want to help improve the Google Cloud SDK (Y/n)? Y Your current Cloud SDK version is: 245.0.0 The latest available version is: 276.0.0 ┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ │ Components │ ├──────────────────┬──────────────────────────────────────────────────────┬──────────────────────────┬──────────┤ │ Status │ Name │ ID │ Size │ ├──────────────────┼──────────────────────────────────────────────────────┼──────────────────────────┼──────────┤ │ Update Available │ BigQuery Command Line Tool │ bq │ < 1 MiB │ │ Update Available │ Cloud SDK Core Libraries │ core │ 12.8 MiB │ │ Update Available │ Cloud Storage Command Line Tool │ gsutil │ 3.6 MiB │ │ Not Installed │ App Engine Go Extensions │ app-engine-go │ 4.8 MiB │ │ Not Installed │ Appctl │ appctl │ 18.6 MiB │ │ Not Installed │ Cloud Bigtable Command Line Tool │ cbt │ 7.3 MiB │ │ Not Installed │ Cloud Bigtable Emulator │ bigtable │ 6.6 MiB │ │ Not Installed │ Cloud Datalab Command Line Tool │ datalab │ < 1 MiB │ │ Not Installed │ Cloud Datastore Emulator │ cloud-datastore-emulator │ 18.4 MiB │ │ Not Installed │ Cloud Firestore Emulator │ cloud-firestore-emulator │ 40.4 MiB │ │ Not Installed │ Cloud Pub/Sub Emulator │ pubsub-emulator │ 34.9 MiB │ │ Not Installed │ Cloud SQL Proxy │ cloud_sql_proxy │ 3.7 MiB │ │ Not Installed │ Emulator Reverse Proxy │ emulator-reverse-proxy │ 14.5 MiB │ │ Not Installed │ Google Cloud Build Local Builder │ cloud-build-local │ 5.9 MiB │ │ Not Installed │ Google Container Registry's Docker credential helper │ docker-credential-gcr │ 1.8 MiB │ │ Not Installed │ Minikube │ minikube │ 20.1 MiB │ │ Not Installed │ Skaffold │ skaffold │ 13.3 MiB │ │ Not Installed │ gcloud Alpha Commands │ alpha │ < 1 MiB │ │ Not Installed │ gcloud Beta Commands │ beta │ < 1 MiB │ │ Not Installed │ gcloud app Java Extensions │ app-engine-java │ 62.0 MiB │ │ Not Installed │ gcloud app PHP Extensions │ app-engine-php │ 21.9 MiB │ │ Not Installed │ gcloud app Python Extensions │ app-engine-python │ 6.1 MiB │ │ Not Installed │ gcloud app Python Extensions (Extra Libraries) │ app-engine-python-extras │ 27.1 MiB │ │ Not Installed │ kubectl │ kubectl │ < 1 MiB │ └──────────────────┴──────────────────────────────────────────────────────┴──────────────────────────┴──────────┘ To install or remove components at your current SDK version [245.0.0], run: $ gcloud components install COMPONENT_ID $ gcloud components remove COMPONENT_ID To update your SDK installation to the latest version [276.0.0], run: $ gcloud components update Modify profile to update your $PATH and enable shell command completion? Do you want to continue (Y/n)? Y The Google Cloud SDK installer will now prompt you to update an rc file to bring the Google Cloud CLIs into your environment. Enter a path to an rc file to update, or leave blank to use [/Users/myusername/.bash_profile]: Backing up [/Users/myusername/.bash_profile] to [/Users/myusername/.bash_profile.backup]. [/Users/myusername/.bash_profile] has been updated. ==> Start a new shell for the changes to take effect. For more information on how to get started, please visit: https://cloud.google.com/sdk/docs/quickstarts
上記手順で追記された内容を確認する
MacBook-Pro:gcp $ cat /Users/myusername/.bash_profile ... # The next line updates PATH for the Google Cloud SDK. if [ -f '/Users/myusername/gcp/google-cloud-sdk/path.bash.inc' ]; then . '/Users/myusername/gcp/google-cloud-sdk/path.bash.inc'; fi # The next line enables shell command completion for gcloud. if [ -f '/Users/myusername/gcp/google-cloud-sdk/completion.bash.inc' ]; then . '/Users/myusername/gcp/google-cloud-sdk/completion.bash.inc'; fi
途中、利用したいプロジェクト名を聞かれるので対応する番号を入力して[Enter]を押下します。途中、認証のためにログインを促されるので合わせて実施します。
MacBook-Pro:gcp $ ./google-cloud-sdk/bin/gcloud init Welcome! This command will take you through the configuration of gcloud. Your current configuration has been set to: [default] You can skip diagnostics next time by using the following flag: gcloud init --skip-diagnostics Network diagnostic detects and fixes local network connection issues. Checking network connection...done. Reachability Check passed. Network diagnostic passed (1/1 checks passed). You must log in to continue. Would you like to log in (Y/n)? Y Your browser has been opened to visit: https://accounts.google.com/o/oauth2/auth?xxxxxxxxxxxxxxxxxxxxx
ここで一旦、ブラウザにフォーカスが移りログインを促されるので実施する。また、アカウント選択等も対応するものを選択して進める。
画面操作が完了すると、利用するプロジェクトの選択を促される。選択したプロジェクトやその他情報が出力されるとコマンドのインストールは完了。
Updates are available for some Cloud SDK components. To install them, please run: $ gcloud components update You are logged in as: [GCPにアクセスしているGMailアカウント]. Pick cloud project to use: [1] your_project1 [2] your_project2 [3] Create a new project Please enter numeric choice or text value (must exactly match list item): 1 Your current project has been set to: [your_project1]. Your project default Compute Engine zone has been set to [asia-northeast1-b]. You can change it by running [gcloud config set compute/zone NAME]. Your project default Compute Engine region has been set to [asia-northeast1]. You can change it by running [gcloud config set compute/region NAME]. Created a default .boto configuration file at [/Users/myusername/.boto]. See this file and [https://cloud.google.com/storage/docs/gsutil/commands/config] for more information about configuring Google Cloud Storage. Your Google Cloud SDK is configured and ready to use! * Commands that require authentication will use (GCPにアクセスしているGMailアカウント) by default * Commands will reference project `your_project1` by default * Compute Engine commands will use region `asia-northeast1` by default * Compute Engine commands will use zone `asia-northeast1-b` by default Run `gcloud help config` to learn how to change individual settings This gcloud configuration is called [default]. You can create additional configurations if you work with multiple accounts and/or projects. Run `gcloud topic configurations` to learn more. Some things to try next: * Run `gcloud --help` to see the Cloud Platform services you can interact with. And run `gcloud help COMMAND` to get help on any gcloud command. * Run `gcloud topic --help` to learn about advanced features of the SDK like arg files and output formatting $
コマンドラインツールのバージョン確認はgcloud versionで行います。
MacBook-Pro:gcp myusername$ ./google-cloud-sdk/bin/gcloud version Google Cloud SDK 245.0.0 bq 2.0.43 core 2019.05.03 gsutil 4.38 Updates are available for some Cloud SDK components. To install them, please run: $ gcloud components update
また、コマンドラインツールのアップデートはgcloud components updateで行う。
MacBook-Pro:gcp myusername$ ./google-cloud-sdk/bin/gcloud components update Your current Cloud SDK version is: 245.0.0 You will be upgraded to version: 277.0.0 ┌─────────────────────────────────────────────────────────────────────────────┐ │ These components will be updated. │ ├─────────────────────────────────────────────────────┬────────────┬──────────┤ │ Name │ Version │ Size │ ├─────────────────────────────────────────────────────┼────────────┼──────────┤ │ BigQuery Command Line Tool │ 2.0.52 │ < 1 MiB │ │ BigQuery Command Line Tool (Platform Specific) │ 2.0.51 │ < 1 MiB │ │ Cloud SDK Core Libraries │ 2020.01.17 │ 12.9 MiB │ │ Cloud SDK Core Libraries (Platform Specific) │ 2019.12.17 │ < 1 MiB │ │ Cloud Storage Command Line Tool │ 4.47 │ 3.6 MiB │ │ Cloud Storage Command Line Tool (Platform Specific) │ 4.46 │ < 1 MiB │ │ gcloud cli dependencies │ 2020.01.10 │ 3.3 MiB │ └─────────────────────────────────────────────────────┴────────────┴──────────┘ A lot has changed since your last upgrade. For the latest full release notes, please visit: https://cloud.google.com/sdk/release_notes Do you want to continue (Y/n)? y ╔════════════════════════════════════════════════════════════╗ ╠═ Creating update staging area ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Uninstalling: BigQuery Command Line Tool ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Uninstalling: BigQuery Command Line Tool (Platform Sp... ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Uninstalling: Cloud SDK Core Libraries ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Uninstalling: Cloud SDK Core Libraries (Platform Spec... ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Uninstalling: Cloud Storage Command Line Tool ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Uninstalling: Cloud Storage Command Line Tool (Platfo... ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Uninstalling: gcloud cli dependencies ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Installing: BigQuery Command Line Tool ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Installing: BigQuery Command Line Tool (Platform Spec... ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Installing: Cloud SDK Core Libraries ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Installing: Cloud SDK Core Libraries (Platform Specific) ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Installing: Cloud Storage Command Line Tool ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Installing: Cloud Storage Command Line Tool (Platform... ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Installing: gcloud cli dependencies ═╣ ╠════════════════════════════════════════════════════════════╣ ╠═ Creating backup and activating new installation ═╣ ╚════════════════════════════════════════════════════════════╝ Performing post processing steps...done. Update done! To revert your SDK to the previously installed version, you may run: $ gcloud components update --version 245.0.0
以上で、Google Cloud SDKのインストール完了です。
GoogleCloudPlatformを使う上で、とても便利で、gcloudコマンドを通じてsshやインスタンスの作成など、多くのことがCLIでできてしまいます。
サクッとやってGCPを有効活用できるようになりましょう。