Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a better way to set a gcloud project in a directory?

I work on multiple appengine projects in any given week. i.e. assume multiple clients. Earlier I could set application in app.yaml. So whenever I did appcfg.py update.... it would ensure deployment to the right project.

When deploying, the application variable throws an error with gcloud deploy. I had to use gcloud app deploy --project [YOUR_PROJECT_ID]. So what used to be a directory level setting for a project, is now going into our build tooling. And missing out that simple detail can push a project code to the wrong customer. i.e. if I did gcloud config set project proj1 and then somehow did a gcloud app deploy in proj2, it would deploy to proj1. Production deployments are done after detailed verification on the build tools and hence it is less of an issue there because we still use the --project flag.

But its hard to do similar stuff on the development environment. dev_appserver.py doesn't have a --project flag. When starting dev_appserver.py I've to do gcloud config set project <project-id> before I start the server. This is important when I using stuff like PubSub or GCS (in dev topics or dev buckets).

Unfortunately, missing out a simple configuration like setting a project ID in a dev environment can result into uploading blobs/messages/etc into the wrong dev gcs bucket or wrong dev pubsub topic (not using emulators). And this has happened quite a few times especially when starting new projects.

I find the above solutions as hackish-workarounds. Is there a good way to ensure that we do not deploy or develop in a wrong project when working from a certain directory?

like image 377
anups Avatar asked Jul 07 '17 14:07

anups


People also ask

What is folder and project in GCP?

Folders are nodes in the Cloud Platform Resource Hierarchy. A folder can contain projects, other folders, or a combination of both. Organizations can use folders to group projects under the organization node in a hierarchy.

Where is gcloud configuration stored?

Configurations are stored in your user config directory (typically ~/. config/gcloud on MacOS and Linux, or %APPDATA%\gcloud on Windows); you can find the location of your config directory by running gcloud info --format='value(config.

What is gcloud config set?

A gcloud configuration is a set of properties that govern the behavior of gcloud and other Google Cloud SDK tools. When you first install gcloud on your desktop a configuration named default is created. The creation of a configuration can be accomplished with gcloud or manually.

How do I set up a Google Cloud project?

Set the Google Cloud project id to the active gcloud configuration. Setup a project in Google Cloud if you havenā€™t already to get the project id. Authorize the Google account that has some type of access/ownership of this project. Login to that account if you havenā€™t already.

What is cloud Directory sync (GCDs)?

With Google&nbsp;Cloud Directory Sync (GCDS), you can synchronize the data in your Google Account with your Microsoft&nbsp;Active Directory or LDAP server. GCDS doesn't migrate any content (such as em Google Workspace Admin Help Sign in Google Help Help Center Community Google Workspace Admin Privacy Policy Terms of Service Submit feedback

How do I switch between Google Cloud accounts?

Create a new gcloud configuration for your project on the machine you will use to access it. It will automatically set it as the active account unless you pass a flag to not make this active in the command. Replace the [ NAME] placeholder with a name to use when switching between Google Cloud projects.

How do I create a new project in the cloud console?

You can create a new project using the Cloud Console, the gcloud command-line tool, or the projects.create () method. Console gcloud API Python To create a new project, do the following: Go to the Manage resources page in the Cloud Console.


3 Answers

TL;DR - Not supported based on the current working directory, but there are workarounds.

Available workarounds

gcloud does not directly let you set up a configuration per working directory. Instead, you could use one of these 3 options to achieve something similar:

  1. Specify --project, --region, --zone or the config of interest per command. This is painful but gets the job done.

  2. Specify a different gcloud configuration directory per command (gcloud uses ~/.config/gcloud on *nix by default):

    CLOUDSDK_CONFIG=/path/to/config/dir1 gcloud COMMAND
    CLOUDSDK_CONFIG=/path/to/config/dir2 gcloud COMMAND
    
  3. Create multiple configurations and switch between them as needed.

    gcloud config configurations activate config-1 && gcloud COMMAND
    

Shell helpers

As all of the above options are ways to customize on the command line, aliases and/or functions in your favorite shell will also help make things easier.

For example in bash, option 2 can be implemented as follows:

function gcloud_proj1() {
  CLOUDSDK_CONFIG=CLOUDSDK_CONFIG=/path/to/config/dir1 $@
}

function gcloud_proj2() {
  CLOUDSDK_CONFIG=CLOUDSDK_CONFIG=/path/to/config/dir2 $@
}

gcloud_proj1 COMMAND
gcloud_proj2 COMMAND
like image 77
Tuxdude Avatar answered Oct 30 '22 00:10

Tuxdude


I've had this problem for years and I believe I found a decent compromise.

  1. Create a simple script called contextual-gcloud. Note the \gcloud, fundamental for future aliasing.
šŸ§$ cat > contextual-gcloud 
#!/bin/bash

if [ -d .gcloudconfig/ ]; then
    echo "[$0] .gcloudconfig/ directory detected: using that dir for configs instead of default."
    CLOUDSDK_CONFIG=./.gcloudconfig/ \gcloud "$@"
else
    \gcloud "$@"
fi
  1. Add to your .bashrc and reload / start new bash. This will fix autocompletion.

    alias gcloud=contextual-gcloud

  2. That's it! If you have a directory called that way the system will use that instead, which means you can load your configuration into source control etc.. only remember to git ignore stuff like logs, and private stuff (keys, certificates, ..).

Note: auto-completion is fixed by the alias ;)

Code: https://github.com/palladius/sakura/blob/master/bin/contextual-gcloud

like image 27
Riccardo Avatar answered Oct 29 '22 23:10

Riccardo


There's a very nice way I've been using with PyCharm, I suspect you can do so with other IDEs.

You can declare the default env variables for the IDE Terminal, so when you open a new terminal gcloud recognises these env variables and sets the project and account.

pycharm preferences config terminal env variables

No need to switch configurations between projects manually (gcloud config configurations activate ). Terminals open in other projects will inherit it's own GCP project and config from the ENV variables.

like image 23
dinigo Avatar answered Oct 29 '22 23:10

dinigo