Run gcloud command run command remotely

Currently, if I want to execute something in a virtual machine, I copy the files as follows:

gcloud compute --project <project_id> copy-files --zone <zone_name> /home/roman/source/dir roman@<vm_name>:/some/path 

Then I need to SSH into it manually, like this:

 gcloud compute --project <project_id> ssh --zone <zone_name> <vm_name> 

And then run and execute some command:

 cd /some/path python example.py 

How to combine steps 2 and 3 together and execute the command remotely?

Note. I want to use gcloud or python api. I do not want to use third-party packages such as Fabric.

+8
ssh google-compute-engine gcloud
source share
3 answers

Try:

 $ gcloud compute ssh --zone ZONE INSTANCE -- 'cd /tmp && python some.py' 

From gcloud compute ssh --help :

  [-- IMPLEMENTATION-ARGS ...] Flags and positionals passed to the underlying ssh implementation. The '--' argument must be specified between gcloud specific args on the left and IMPLEMENTATION-ARGS on the right. Example: $ gcloud compute ssh example-instance --zone us-central1-a -- -vvv \ -L 80:%INSTANCE%:80 
+14
source share

The gcloud update, which appeared a week ago, blocked the transfer of ssh commands after the '-' and effectively used the --command parameter for this. In this case, use:

gcloud compute ssh --zone ZONE INSTANCE --command 'cd /tmp && python some.py' .

+8
source share

Did it with a little tangent; using a regular ssh client instead of gcloud compute :

 ssh -i ~/.ssh/google_compute_engine roman@<vm_IP> 'python /some/path/example.py' 

The gcloud ssh key is in the ~/.ssh/google_compute_engine that it uses. It also requires the external IP address of the instance instead of its name.

+2
source share

All Articles