VM インスタンスへファイルアップロードするのに、ssh key を作成したり FW ポート開けたりめんどい。
いろいろ試した結果 Cloud ソースリポジトリと gcloud を使うのが一番楽だと結論。
すぐやり方を忘れてしまうので、備忘録として残しておく。
やりたいこと
- ローカルで開発した Python コードを GCP Cloud ソースリポジトリへ push
- VM インスタンスでソースリポジトリから pull する
本記事での環境
ローカル開発環境側
bash-3.2$ uname -a Darwin MacBook-Pro.local 20.3.0 Darwin Kernel Version 20.3.0: Thu Jan 21 00:07:06 PST 2021; root:xnu-7195.81.3~1/RELEASE_X86_64 x86_64
GCP VMインスタンス側
$ uname -a Linux utility-server-001 5.4.0-1037-gcp #40-Ubuntu SMP Fri Feb 5 11:57:53 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
mac に gcloud sdk をインストールする
brew install google-cloud-sdk
でインストールします。
bash-3.2$ brew install google-cloud-sdk Error: homebrew-core is a shallow clone. To `brew update`, first run: git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow ★ This restriction has been made on GitHub's request because updating shallow clones is an extremely expensive operation due to the tree layout and traffic of Homebrew/homebrew-core and Homebrew/homebrew-cask. We don't do this for you automatically to avoid repeatedly performing an expensive unshallow operation in CI systems (which should instead be fixed to not use shallow clones). Sorry for the inconvenience! ==> Caveats google-cloud-sdk is installed at /usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk. Add your profile: for bash users source "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.bash.inc" source "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/completion.bash.inc" for zsh users source "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.zsh.inc" source "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/completion.zsh.inc" for fish users source "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.fish.inc" ==> Downloading https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.tar.g ######################################################################## 100.0% Warning: No checksum defined for cask 'google-cloud-sdk', skipping verification. ==> Installing dependencies: python@3.9 ==> Downloading https://homebrew.bintray.com/bottles/gdbm-1.18.1_1.big_sur.bottl ==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/36b492f1b0910367dd394 ######################################################################## 100.0% ==> Downloading https://homebrew.bintray.com/bottles/openssl%401.1-1.1.1i.big_su ==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/8008537d37a7f09eedbcd ######################################################################## 100.0% ==> Downloading https://homebrew.bintray.com/bottles/readline-8.1.big_sur.bottle ==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/2cc3a9582e3c7e21eb3c2 ######################################################################## 100.0% ==> Downloading https://homebrew.bintray.com/bottles/sqlite-3.34.0.big_sur.bottl ==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/cdf256befc0752680a674 ######################################################################## 100.0% ==> Downloading https://homebrew.bintray.com/bottles/python%403.9-3.9.1_3.big_su ==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/46933d4a92652c8980f00 ######################################################################## 100.0% Error: python@3.9 3.7.7 is already installed To upgrade to 3.9.1_3, run `brew upgrade python@3.9`. ★
ん? brew を shallow clone じゃなくするのと python 3.9 にしてよ、と言われたので、
$ git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow```
と
$ brew upgrade python@3.9
を実行してから再び実行。
bash-3.2$ brew install google-cloud-sdk ==> Caveats google-cloud-sdk is installed at /usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk. Add your profile: for bash users source "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.bash.inc" source "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/completion.bash.inc" for zsh users source "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.zsh.inc" source "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/completion.zsh.inc" for fish users source "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.fish.inc" ==> Downloading https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.tar.gz Already downloaded: /Users/hiroaki/Library/Caches/Homebrew/downloads/cda39f18c3069c1a4ac0cd5b1d91541af945cd331eba8443c58ec5c890343c0a--google-cloud-sdk.tar.gz Warning: No checksum defined for cask 'google-cloud-sdk', skipping verification. All formula dependencies satisfied. ==> Installing Cask google-cloud-sdk Beginning update. This process may take several minutes. Your current Cloud SDK version is: 332.0.0 Installing components from version: 332.0.0 ┌─────────────────────────────────────────────────────────────────────────────┐ │ These components will be installed. │ ├─────────────────────────────────────────────────────┬────────────┬──────────┤ │ Name │ Version │ Size │ ├─────────────────────────────────────────────────────┼────────────┼──────────┤ │ BigQuery Command Line Tool │ 2.0.65 │ < 1 MiB │ │ BigQuery Command Line Tool (Platform Specific) │ 2.0.65 │ < 1 MiB │ │ Cloud SDK Core Libraries (Platform Specific) │ 2021.03.12 │ < 1 MiB │ │ Cloud Storage Command Line Tool │ 4.59 │ 3.9 MiB │ │ Cloud Storage Command Line Tool (Platform Specific) │ 4.59 │ < 1 MiB │ │ anthoscli │ 0.2.7 │ 52.0 MiB │ │ gcloud cli dependencies │ 2020.06.12 │ < 1 MiB │ └─────────────────────────────────────────────────────┴────────────┴──────────┘ For the latest full release notes, please visit: https://cloud.google.com/sdk/release_notes ╔════════════════════════════════════════════════════════════╗ ╠═ Creating update staging area ═╣ ╚════════════════════════════════════════════════════════════╝ ╔════════════════════════════════════════════════════════════╗ ╠═ Installing: BigQuery Command Line Tool ═╣ ╚════════════════════════════════════════════════════════════╝ ╔════════════════════════════════════════════════════════════╗ ╠═ Installing: BigQuery Command Line Tool (Platform Spec... ═╣ ╚════════════════════════════════════════════════════════════╝ ╔════════════════════════════════════════════════════════════╗ ╠═ Installing: Cloud SDK Core Libraries (Platform Specific) ═╣ ╚════════════════════════════════════════════════════════════╝ ╔════════════════════════════════════════════════════════════╗ ╠═ Installing: Cloud Storage Command Line Tool ═╣ ╚════════════════════════════════════════════════════════════╝ ╔════════════════════════════════════════════════════════════╗ ╠═ Installing: Cloud Storage Command Line Tool (Platform... ═╣ ╚════════════════════════════════════════════════════════════╝ ╔════════════════════════════════════════════════════════════╗ ╠═ Installing: Default set of gcloud commands ═╣ ╚════════════════════════════════════════════════════════════╝ ╔════════════════════════════════════════════════════════════╗ ╠═ Installing: anthoscli ═╣ ╚════════════════════════════════════════════════════════════╝ ╔════════════════════════════════════════════════════════════╗ ╠═ Installing: anthoscli ═╣ ╚════════════════════════════════════════════════════════════╝ ╔════════════════════════════════════════════════════════════╗ ╠═ Installing: gcloud cli dependencies ═╣ ╚════════════════════════════════════════════════════════════╝ ╔════════════════════════════════════════════════════════════╗ ╠═ Creating backup and activating new installation ═╣ ╚════════════════════════════════════════════════════════════╝ Performing post processing steps... ........................................................................done. Update done! ==> Source [/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/completion.bash.inc] in your profile to enable shell command completion for gcloud. ==> Source [/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.bash.inc] in your profile to add the Google Cloud SDK command line tools to your $PATH. 🍺 google-cloud-sdk was successfully installed!
今度はきれいに入りました。
bash_profile にパスをセット
インストール途中で表示される記述の通りに bash_profile に追記します。
google-cloud-sdk is installed at /usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk. Add your profile: for bash users source "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.bash.inc" source "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/completion.bash.inc" for zsh users source "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.zsh.inc" source "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/completion.zsh.inc" for fish users source "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.fish.inc"
$ vi ~/.bash_profile
でファイルを開き以下の 2 行を追記。
source "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.bash.inc" source "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-casdk/completion.bash.inc"
source ~/.bash_profile
で .bash_profile を再読み込みします。
gcloud のバージョンを確認
bash-3.2$ gcloud --version Google Cloud SDK 332.0.0 bq 2.0.65 core 2021.03.12 gsutil 4.59
無事に gcloud インストールが完了しました。
Cloud ソースリポジトリを作成
次に Cloud ソースリポジトリを作成していきます。
クラウドコンソールで Cloud Source Repositories を開き「開始」をクリック
リポジトリの作成
以下を入力してリポジトリを作成します。
- 新しいリポジトリの作成
- リポジトリ名:任意の名称
- プロジェクト:既存のプロジェクトを選択(無ければ新規)
リポジトリにコードを追加
現在リポジトリは空なので追加する方法を選択します。
- リポジトリにコードを push するオプションを選択
- 「ローカル Git リポジトリへのリポジトリのクローンを作成する」を選択
- 使用する認証方法
- 「Google Cloud SDK」を表示
認証方法としては以下の手順が必要です。
- Google Cloud SDK のインストール:上記で実施済み
- 認証情報の指定:
gcloud init
gcloud init
コマンドを実行するとブラウザが開きログイン認証を求められたり、どのプロジェクトを利用するかが CLI で応答を求められます。
質問に答えていくだけで認証が完了します。
- ローカル Git リポジトリへのクローン:
gcloud source repos clone {リポジトリ名} --project={プロジェクト名}
- 新しいローカル Git リポジトリへの切り替え:
cd {リポジトリ名}
git init
を実行し初期化を行います- リポジトリに登録する python コードを格納
git add.
を実行し python コードをステージングへアップ` git commit -m "first commit"
でコミット- 作成したリポジトリへ push:
git push -u origin master
これで、リポジトリへのコードの push が完了。
bash-3.2$ git push -u origin master Enumerating objects: 29, done. Counting objects: 100% (29/29), done. Delta compression using up to 8 threads Compressing objects: 100% (29/29), done. Writing objects: 100% (29/29), 12.41 KiB | 1.77 MiB/s, done. Total 29 (delta 6), reused 0 (delta 0) remote: Resolving deltas: 100% (6/6) To https://source.developers.google.com/p/{プロジェクト名}/r/{リポジトリ名} * [new branch] master -> master Branch 'master' set up to track remote branch 'master' from 'origin'.
先程の Cloud ソースリポジトリ画面を更新すると、ローカルに合ったファイルがリポジトリにエントリされています。
GCP VM インスタンス側の設定
Cloud Source Repositories のアクセス権限を付与する
Cloud Console 画面の VM インスタンス で「Cloud API アクセス スコープ」を修正します。
- Cloud Source Repositories:読み取り以上の設定へ
アクセス権限の修正は VM 停止中でないと出来ないことに注意してください。
VM インスタンスで gcloud コマンドを使いレポジトリの clone 作成
VM インスタンス上でレポジトリのクローンを作成します。
Cloud ソースリポジトリ画面の「クローンを作成」をクリックすると gcloud(Google Cloud SDK)を利用したクローン作成コマンドが表示されます。
$ gcloud source repos clone team-creative-lab-001 --project=teamcreativelab001 Cloning into '/home/team_create_lab/team-creative-lab-001'... remote: Total 29 (delta 7), reused 29 (delta 7) Unpacking objects: 100% (29/29), 12.23 KiB | 963.00 KiB/s, done. Project [teamcreativelab001] repository [team-creative-lab-001] was cloned to [/home/team_create_lab/team-creative-lab-001].
成功するとプロジェクトのディレクトリとその配下にソースコードがコピーされてきます。