Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcloud compute execute command remotely

Currently, if I want to execute something on a VM, I copy files over like this:

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 go and run some command:

cd /some/path python example.py 

How do I combine step 2 and 3 together and execute a command remotely?

Note: I want to use gcloud or the python api. I don't want to use 3rd party packages like Fabric.

like image 444
Roman Avatar asked Aug 23 '16 10:08

Roman


People also ask

What does gcloud compute SSH do?

You use the gcloud compute ssh command to connect to your VM. Compute Engine sets a username and creates a persistent SSH key pair with the following configurations: Your username is set as the username in your local machine.

Where do I run gcloud commands?

Running gcloud CLI commands You can run gcloud CLI commands from the command line and from scripts and other automations—for example, when using Jenkins to automate Google Cloud tasks. Note: gcloud CLI reference documentation and examples use backslashes, \ , to denote long commands.


1 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 
like image 100
Zachary Newman Avatar answered Oct 06 '22 19:10

Zachary Newman