Automate gcloud components update - gcloud

How can I update the gcloud components programmatically within a shell script?
Calling gcloud components update requires an user entry, e.g.:
$ gcloud components update
The following components will be installed:
--------------------------------------------
| kubectl (Linux, x86_64) | 1.0.1 | 4.5 MB |
--------------------------------------------
For the latest release notes, please visit:
https://dl.google.com/dl/cloudsdk/release/RELEASE_NOTES
Do you want to continue (Y/n)?
I can't find an argument for gcloud to enforce the update.

You're looking for the --quiet flag.
From gcloud --help:
--quiet, -q
Disable all interactive prompts when running gcloud commands. If input
is required, defaults will be used, or an error will be raised.
This is generally a flag you'll want for non-interactive contexts.
You may also set the CLOUDSDK_CORE_DISABLE_PROMPTS environment variable to a non-empty value:
export CLOUDSDK_CORE_DISABLE_PROMPTS=1
gcloud components update # This works for all gcloud commands

If you encounter this problem while running a gcloud command on a Continuous Integration (CI) server, one thing you can try is run with a Docker image that already contains the components you need. Thus you can avoid having to update gcloud components.
For example, if you're trying to run gcloud beta firebase test android run, you could use the image: google/cloud-sdk:latest because at https://github.com/GoogleCloudPlatform/cloud-sdk-docker it shows :latest contains gcloud Beta Commands.
I tested this on Gitlab hosted CI (.gitlab-ci.yml) and it worked.

Related

flyctl launch: Error name argument or flag must be specified when not running interactively

I am trying to deploy a flask app in fly.io, but when execute flyctl launch in the terminal I get an error:
Error name argument or flag must be specified when not running interactively.
I don't see any other way to make a deployment in fly.io other than the console. I tried with a Dockerfile but flyctl launch continue throwing the same error.
Apparently flyctl believes you're not running its command-line tool interactively. That may or may not be a bug of flyctl itself, you can ask about that in the fly.io community.
The solution to this problem is to add the required information as parameters instead of being prompted for data entry. To my knowledge, you only need the name of the app you want to launch and the region code of the server location. The syntax for that can be found using the fly help launch command:
λ flyctl help launch
Create and configure a new app from source code or a Docker image.
Usage:
flyctl launch [flags]
Flags:
--auto-confirm Will automatically confirm changes when running non-interactively.
--build-arg strings Set of build time variables in the form of NAME=VALUE pairs. Can be specified multiple times.
--build-only Build but do not deploy
--build-secret strings Set of build secrets of NAME=VALUE pairs. Can be specified multiple times. See https://docs.docker.com/develop/develop-images/build_enhancements/#new-docker-build-secret-information
--build-target string Set the target build stage to build if the Dockerfile has more than one stage
--copy-config Use the configuration file if present without prompting
--detach Return immediately instead of monitoring deployment progress
--dockerfile string Path to a Dockerfile. Defaults to the Dockerfile in the working directory.
--dockerignore-from-gitignore If a .dockerignore does not exist, create one from .gitignore files
-e, --env strings Set of environment variables in the form of NAME=VALUE pairs. Can be specified multiple times.
--generate-name Always generate a name for the app, without prompting
-i, --image string The Docker image to deploy
--image-label string Image label to use when tagging and pushing to the fly registry. Defaults to "deployment-{timestamp}".
--local-only Only perform builds locally using the local docker daemon
--name string Name of the new app
--nixpacks Deploy using nixpacks to generate the image
--no-cache Do not use the build cache when building the image
--no-deploy Do not prompt for deployment
--now Deploy now without confirmation
-o, --org string The target Fly organization
--path string Path to the app source root, where fly.toml file will be saved (default ".")
--push Push image to registry after build is complete
-r, --region string The target region (see 'flyctl platform regions')
--remote-only Perform builds on a remote builder instance instead of using the local docker daemon
--strategy string The strategy for replacing running instances. Options are canary, rolling, bluegreen, or immediate. Default is canary, or rolling when max-per-region is set.
Global Flags:
-t, --access-token string Fly API Access Token
-j, --json json output
--verbose verbose output
In summary, the following command, executed in the directory of the app you want to launch on fly.io, should create an app called your-app-name in the Toronto, Canada location.
flyctl launch --name your-app-name --region yyz

gcloud SDK: Unable to write file

I installed gcloud SDK with brew cask install google-cloud-sdk
$ gcloud container clusters get-credentials my-gke-cluster --region europe-west4-c
Fetching cluster endpoint and auth data.
ERROR: (gcloud.container.clusters.get-credentials)
Unable to write file [/Users/xxxxx/my-repo]: [Errno 21] Is a directory: '/Users/xxxxx/my-repo'
Now all permissions of the folder and recursive files are restricted to 600 (drw-------). Tried to reinstall gcloud but with no effect on its behavior.
I assume you're using macOS and I'm unfamiliar with it.
The gcloud container clusters get-credentials command should write to a file called ${HOME}/.kube/config.
The error suggests that it's trying to write the credentials to /Users/xxxxx/my-repo and this is determined by the value of ${KUBECONFIG}. Have you changed either ${KUBECONFIG} or your ${HOME} environment variable? You should be able to printf "HOME=${HOME}\nKUBECONFIG=${KUBECONFIG}" to inspect these.
You may be able to choose a different destination by adjust the value of KUBECONFIG. Perhaps set this to /Users/xxxxx and try the command again.
Ultimately, this is some sugar to update the local configuration file. It should be possible to create this manually if needs be. If the above don't work, I can update this answer with more details.

How can we run gcloud/gsutil/bq command for different accounts in parallel in one server?

I have installed gcloud/bq/gsutil command line tool in one linux server.
And we have several accounts configured in this server.
**gcloud config configurations list**
NAME IS_ACTIVE ACCOUNT PROJECT DEFAULT_ZONE DEFAULT_REGION
gaa True a#xxx.com a
gab False b#xxx.com b
Now I have problem to both run gaa/gab in this server at same time. Because they have different access control on BigQuery and Cloud Stroage.
I will use below commands (bq and gsutil commands):
Set up account
Gcloud config set account a#xxx.com
Copy data from bigquery to Cloud
bq extract --compression=GZIP --destination_format=NEWLINE_DELIMITED_JSON 'nl:82421.ga_sessions_20161219' gs://ga-data-export/82421/82421_ga_sessions_20161219_*.json.gz
Download data from Cloud to local system
gsutil -m cp gs://ga-data-export/82421/82421_ga_sessions_20161219*gz
If only run one account, it is not a problem.
But there are several accounts need to run on one server at same time, I have no idea how to deal with this case.
Per the gcloud documentation on configurations, you can switch your active configuration via the --configuration flag for any gcloud command. However, gsutil does not have such a flag; you must set the environment variable CLOUDSDK_ACTIVE_CONFIG_NAME:
$ # Shell 1
$ export CLOUDSDK_ACTIVE_CONFIG_NAME=gaa
$ gcloud # ...
$ # Shell 2
$ export CLOUDSDK_ACTIVE_CONFIG_NAME=gab
$ gsutil # ...

gcloud app deploy, flag to automate prompting?

Is it possible to do silent deployment when using gcloud app deploy
When I run the command gcloud app deploy ./deployment/app.yaml --version v1 its always prompting for
Do you want to continue (Y/n)? Y
how to automate this?
is there any flag that we can pass in to mute this?
You're looking for the --quiet flag, available across all gcloud commands:
$ gcloud --help
--quiet, -q
Disable all interactive prompts when running gcloud commands. If input
is required, defaults will be used, or an error will be raised.
$ gcloud app deploy --quiet
Or also:
$ gcloud app deploy -q

How do I list images and tags in google container registry

Looking for a way to use the gcloud commandline to get the tags of container engine registry images.
this command
cloud docker search gcr.io/PROJECT/myimage
returns
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
PROJECT/myimage 0
but i want to see the tags used so far like the gcloud web console shows.
the machine I run this command on pulls images from docker hub, tags them, then pushes them to google container engine with gcloud docker push...
I suspect I may be asking how to make the local docker client do commands against both google repo and docker hub repo so I can get docker images listings
****** UPDATE: see https://stackoverflow.com/a/38061253/201911 for this capability in the latest release.
gcloud container images list-tags gcr.io/PROJECT/myimage
https://cloud.google.com/sdk/gcloud/reference/container/images/list-tags
Let me tell you hack to relieve your pain:
ZONE=eu
PROJECT_ID=testproject
PROJECT_URL=gs://${ZONE}.artifacts.${PROJECT_ID}.appspot.com/containers/repositories/library
# this generates images list
gsutil ls $PROJECT_URL | sed -e 's#/$##' -e 's#.*/##'
IMAGE_URL=gs://${ZONE}.artifacts.${PROJECT_ID}.appspot.com/containers/repositories/library/${IMAGE_NAME}/
# this generates tags list for specified image
gsutil ls $IMAGE_URL | sed -n '/\/tag_/ { s#.*/tag_##p }'
This will serve you in most of the cases.
You will need read permissions to list files in that specific storage bucket in GCE.
This is not currently possible with gcloud. gcloud docker is just a wrapper around the docker command line tool, which doesn't easily expose the information you'd like for remote repositories.
At some point in the future, gcloud may support this feature.
First install the gcloud CLI (brew install google-cloud-sdk on MacOS). Then log in (gcloud auth login). Then use a command from below to list images/tags.
Listing images
CLOUDSDK_CORE_PROJECT=[PROJECT] gcloud container images list (e.g. CLOUDSDK_CORE_PROJECT=spiffe-io gcloud container images list)
Listing image tags
gcloud container images list-tags gcr.io/[PROJECT]/[IMAGE] (e.g. gcloud container images list-tags gcr.io/spiffe-io/spire-server)
There is currently no way to search for images across all projects.
See
https://cloud.google.com/sdk/gcloud/reference/container/images/list-tags
https://cloud.google.com/sdk/gcloud/reference/container/images/list