LoginSignup
143
108

More than 5 years have passed since last update.

gcloud configで複数の設定を持って切り替える

Posted at

お題

1つのGCPプロジェクトを相手に設定を持つだけであれば、ここに記載があるようにgcloud initで1度設定してしまえばいい。
でも、複数のGCPプロジェクトを切り替えながら作業をする必要がある場合、困る。
なので、表題のことができるようにしたい。

作業環境

# OS

$ cat /etc/os-release 
NAME="Ubuntu"
VERSION="17.10 (Artful Aardvark)"

# gcloud

$ gcloud version
Google Cloud SDK 219.0.1
app-engine-go 
app-engine-java 1.9.65
app-engine-python 1.9.76
beta 2018.07.16
bigtable 
bq 2.0.34
cbt 
cloud-build-local 
cloud-datastore-emulator 2.0.2
cloud_sql_proxy 
container-builder-local 
core 2018.09.28
docker-credential-gcr 
gsutil 4.34
kubectl 2018.09.17
pubsub-emulator 2018.02.02

実践

現状確認

$ gcloud config list
[compute]
region = asia-northeast1
zone = asia-northeast1-a
[core]
account = xxxx@example.com
disable_usage_reporting = False
project = 【プロジェクトID】

Your active configuration is: [default]

設定追加方法を探る

とりあえず、gcloud configの他のメニューを探る。

$ gcloud config
ERROR: (gcloud.config) Command name argument expected.
Usage: gcloud config [optional flags] <group | command>
  group may be           configurations
  command may be         get-value | list | set | unset

For detailed information on this command and its flags, run:
  gcloud config --help

get-valuesetunsetは違いそうなので、configurationsを試してみる。

$ gcloud config configurations
ERROR: (gcloud.config.configurations) Command name argument expected.
Usage: gcloud config configurations [optional flags] <command>
  command may be         activate | create | delete | describe | list

For detailed information on this command and its flags, run:
  gcloud config configurations --help

まだよくわからないので、↑の指示に従い、helpを見てみる。

$ gcloud config configurations --help

すると、

NAME
    gcloud config configurations - manage the set of gcloud named
        configurations

SYNOPSIS
    gcloud config configurations COMMAND [GCLOUD_WIDE_FLAG ...]

DESCRIPTION
    Manage the set of gcloud named configurations.

    See gcloud topic configurations for an overview of named configurations.

GCLOUD WIDE FLAGS
    These flags are available to all commands: --account, --configuration,
    --flatten, --format, --help, --log-http, --project, --quiet, --trace-token,
    --user-output-enabled, --verbosity. Run $ gcloud help for details.

COMMANDS
    COMMAND is one of the following:

     activate
        Activates an existing named configuration.

     create
        Creates a new named configuration.

     delete
        Deletes a named configuration.

     describe
        Describes a named configuration by listing its properties.

     list
        Lists existing named configurations.

NOTES
    This variant is also available:

        $ gcloud beta config configurations

Manage the set of gcloud named configurations.」とあるので、これで問題なさそう。
createactivateを使うことで複数の設定を持って切り替えが実現できると思われる。

新しい設定を追加

$ gcloud config configurations create
ERROR: (gcloud.config.configurations.create) argument CONFIGURATION_NAME: Must be specified.
Usage: gcloud config configurations create CONFIGURATION_NAME [optional flags]
  optional flags may be  --activate | --help

For detailed information on this command and its flags, run:
  gcloud config configurations create --help

あれっ、エラー。あ、新しい設定の名前を指定する必要があるのか。
設定の名前は何にするもんだろうか。プロジェクトIDと同じものを入れたりするのかな。
とりあえず今回は、適当に「config2」としておく。

$ gcloud config configurations create config2
Created [config2].
Activated [config2].

ん? これで終わり?
勝手な想像では、gcloud init同様、プロジェクトIDを入力させたりという対話モードに入るのかと思ってた。

$ gcloud config list
[core]
disable_usage_reporting = False

Your active configuration is: [config2]

設定の切り替え

使う設定は確かに新しいものに変わった。
もとの設定は、どうなった???
gcloud config configurationsのメニューに調べる手段がありそう。
というか、大抵、listというサブコマンドはあるので、まずはそこから。

$ gcloud config configurations list
NAME     IS_ACTIVE  ACCOUNT               PROJECT         DEFAULT_ZONE       DEFAULT_REGION
config2  True
default  False      xxxx@example.com     【プロジェクトID】  asia-northeast1-a  asia-northeast1

あった。元に戻すのは、たぶん、さっき当たりをつけたactivateだろう。

$ gcloud config configurations activate default
Activated [default].
$
$ gcloud config configurations list
NAME     IS_ACTIVE  ACCOUNT               PROJECT         DEFAULT_ZONE       DEFAULT_REGION
config2  False
default  True       xxxx@example.com     【プロジェクトID】  asia-northeast1-a  asia-northeast1
$
$ gcloud config list
[compute]
region = asia-northeast1
zone = asia-northeast1-a
[core]
account = xxxx@gmail.com
disable_usage_reporting = False
project = 【プロジェクトID】

Your active configuration is: [default]

よし、切り替わった。

追加した設定にプロジェクトを紐付ける

いったん追加した設定をアクティブにする。

$ gcloud config configurations activate config2
Activated [config2].
$
$ gcloud config list
[core]
disable_usage_reporting = False

Your active configuration is: [config2]

defaultの設定にあるようにアカウントやプロジェクトIDを紐付けるには、どうしたらいいのか?
gcloud config configurationsのヘルプを見る限り、editのサブコマンドは存在しない。。。
というわけで、一段あがってgcloud configのヘルプを確認してみる。

$ gcloud config --help

すると、

NAME
    gcloud config - view and edit Cloud SDK properties
 〜省略〜
COMMANDS
   〜省略〜
     set
        Set a Cloud SDK property.
     unset
        Unset a Cloud SDK property.

AVAILABLE PROPERTIES
     core
         account
            Account gcloud should use for authentication. Run gcloud auth list
            to see your currently available accounts.
     〜省略〜
         project
            Project ID of the Cloud Platform project to operate on by default.
            This can be overridden by using the global --project flag.
     〜省略〜
     compute
         region
            Default region to use when working with regional Compute Engine
            resources. When a --region flag is required but not provided, the
            command will fall back to this value, if set. To see valid choices,
            run gcloud compute regions list.
     〜省略〜
         zone
            Default zone to use when working with zonal Compute Engine
            resources. When a --zone flag is required but not provided, the
            command will fall back to this value, if set. To see valid choices,
            run gcloud compute zones list.
     〜省略〜

あぁ、これで正解だね。
しかし、ひとつひとつ設定していくのか・・・。

setのお作法もチェックしておく。

$ gcloud config set --help
NAME
    gcloud config set - set a Cloud SDK property

SYNOPSIS
    gcloud config set SECTION/PROPERTY VALUE [--installation]
        [GCLOUD_WIDE_FLAG ...]

はい。
それでは、新しく追加した設定「config2」に対し、「default」の設定を参考にsetしていく。

$ gcloud config set compute/region asia-northeast1
Updated property [compute/region].
$
$ gcloud config set compute/zone asia-northeast1-a
Updated property [compute/zone].
$
$ gcloud config set core/account tttt@example.com
Updated property [core/account].
$
$ gcloud config set core/project 【新プロジェクトID】
Updated property [core/project].
$
$ gcloud config set core/disable_usage_reporting False
Updated property [core/disable_usage_reporting].

すると、こうなる。

$ gcloud config list
[compute]
region = asia-northeast1
zone = asia-northeast1-a
[core]
account = tttt@example.com
disable_usage_reporting = False
project = 【新プロジェクトID】

Your active configuration is: [config2]

新プロジェクトにgcloudコマンドで接続できるか確認

$ gcloud projects list
ERROR: (gcloud.projects.list) Your current active account [tttt@example.com] does not have any valid credentials
Please run:

  $ gcloud auth login

to obtain new credentials, or if you have already logged in with a
different account:

  $ gcloud config set account ACCOUNT

to select an already authenticated account to use.

そう、新プロジェクトのアカウントである「tttt@example.com」では、まだローカルでgcloud authしてなかった。

$ gcloud auth login
Your browser has been opened to visit:
 〜省略〜
You are now logged in as [tttt@example.com].
Your current project is [【新プロジェクトID】].  You can change this setting by running:
  $ gcloud config set project PROJECT_ID

これで大丈夫だろう。

$ gcloud projects list
PROJECT_ID             NAME              PROJECT_NUMBER
【新プロジェクトID】      My First Project  999999999999

はい。これにて実践終わり。

まとめ

意外と手間だったかも。もっと簡単にできないもんか。例えば、defaultの設定をコピーして必要な部分だけ変えるとか・・・。

143
108
3

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
143
108