LoginSignup
37
33

More than 5 years have passed since last update.

gcloud confing configurations コマンドでconfig設定を切り替える

Posted at

機能が追加されたみたい

gcloud 2015.09.21 からgcloud config configurationsコマンドがGAになり、gcloud config list実行時に以下設定が追加されるようになった。

[meta]
active_config = default

どうもgcloud config listで設定している設定ごと切り替えられるようになったらしい。
業務で複数のprojectやzoneをglcoud config setコマンドで行ったり来たりしていたので使えそうだ、ということで使えるのか設定・検証してみた。

設定方法

configurationの作成

gcloud config configurations create CONFIGURATION名
兎にも角にもまずはconfigurationを作ってみる。名前は適当でいいだろう。

% gcloud config configurations create hoge                                       
Created [hoge].
To use this configuration, activate it using `gcloud config configurations activate`.

命名規則違反するとクッソ怒られる。

% gcloud config configurations create HOGE                                                                
ERROR: (gcloud.config.configurations.create) Invalid name [HOGE] for a configuration.  Except for special cases (NONE), configuration names start with a lower case letter and contain only lower case letters a-z, digits 0-9, and hyphens '-'.

configurationの切り替え

gcloud config configurations activate CONFIGURATION名
長い。alias必須だろこんなもん。

% gcloud config configurations activate hoge                                                              
Activated [hoge].

ちなみに作ったばっかりのconfigurationにはactive_config以外の設定は入っていない。

% gcloud config list                                                                                      
[meta]
active_config = hoge

gcloud config set コマンドで記載してもいいんだけど(本来はそれが正攻法のはず)だが、面倒なのでファイルに直接書いちゃう。
~/.config/gcloud/configurationsの中にconfig_CONFIGURATION名のファイル名があるので今までの設定であるconfig_defaultを真似て直接書いちゃえます。

configurationの確認

gcloud config configurations list
作ったconfigurationsと今どのconfigurationが有効なのか確認できます。

NAME                  IS_ACTIVE
default               False
hoge                  True

これ情報どこから取ってきてるかというと~/.config/gcloud/configurationsからのようです。
touch ~/.config/gcloud/configurations/config_fugaって実行したらfuga出てきちゃったからね。内緒だよ。

% touch ~/.config/gcloud/configurations/config_fuga
% gcloud config configurations list
NAME                  IS_ACTIVE
default               False
fuga                  False
hoge                  True

この機能の使い所とは

今持ってるサービスではenvironment/partitionごとにVMインスタンス群を持っていて、複数存在するGCP projectとzoneの組み合わせと紐付けて作業をする必要があります。
今回environment/partitionの名前でconfigurationを作ってしまえばGCP projectやzoneが何かを気にすることなく作業ができそうだな、ということで使い始めています。
あとはGKEのcluster([container]cluster)の切り替えなんかにも使えそうですね。

ちなみにaliasはgcloud config configurationsを略したらgccなのでこれはないな、と思いgcfgあたりが妥当かなぁと思ってます。

37
33
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
37
33