Run gcloud commands from PHP

257 views
Skip to first unread message

its J

unread,
Mar 30, 2020, 9:02:28 AM3/30/20
to Google App Engine

We can run shell commands from PHP using shell_exec() command. When i try to runshell_exec('ls')

it gives the list of files and folders.

I want to use shell_exec to get list of instances in the Google Cloud.

I am usingshell_exec('gcloud compute instances list')


it does not produce any result. When i run the same command 'gcloud compute instances list' on terminal, it shows all the instances under the project.

Can anyone please tell me how to achieve this?

George (Cloud Platform Support)

unread,
Mar 30, 2020, 1:48:29 PM3/30/20
to Google App Engine
What would you like to accomplish by running this command? Does your use-case ask specifically for the use of a gcloud command? 

In this particular case, the easiest and straightforward way to get the desired information is an API call to method: instances.list of the REST API, from your PHP script. To simplify calling the API, you should use the Client Library

its J

unread,
Mar 31, 2020, 6:45:51 AM3/31/20
to Google App Engine
I have created a shell script which has get of gcloud commands I want to execute that shell script on gcloud shell

George (Cloud Platform Support)

unread,
Mar 31, 2020, 11:03:47 AM3/31/20
to Google App Engine
In your local development environment, if you want to run a script including gcloud commands, you should refer to the "Scripting gcloud CLI commands" documentation page. In this local environment, it is unclear why you chose to use PHP and the shell_exec() command; you can run the script directly at the command prompt. 

If you mean to run shell_exec() in the code of your deployed PHP app, this is not supported. App Engine Standard Environment does not allow system calls. In the Flexible Environment, the following functions are disabled: exec, passthru, proc_open, proc_close, shell_exec, show_source, symlink, system. 

Mua Rachmann

unread,
Mar 31, 2020, 11:15:43 AM3/31/20
to google-a...@googlegroups.com
Hi Vinit,

George is right. These functions are disabled, but I could enable them from my app.yaml setting when ever
I need them like so...

env_variables:
# Put production environment variables here.
  APP_NAME: Test project
  WHITELIST_FUNCTIONS: phpinfo, exec, phpversion, proc_open, symlink, proc_close ,shell_exec ,passthru 

Let me know if that works for you. PS this works for the the flex environment as George clearly pointed out just that
they are disabled.

Cheers,
Mua

On Tue, Mar 31, 2020 at 4:04 PM 'George (Cloud Platform Support)' via Google App Engine <google-a...@googlegroups.com> wrote:
In your local development environment, if you want to run a script including gcloud commands, you should refer to the "Scripting gcloud CLI commands" documentation page. In this local environment, it is unclear why you chose to use PHP and the shell_exec() command; you can run the script directly at the command prompt. 

If you mean to run shell_exec() in the code of your deployed PHP app, this is not supported. App Engine Standard Environment does not allow system calls. In the Flexible Environment, the following functions are disabled: exec, passthru, proc_open, proc_close, shell_exec, show_source, symlink, system. 

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-appengi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-appengine/243ffd40-7baf-4821-9954-9dd86ac9f904%40googlegroups.com.


--

MUA N. LAURENT
: Lead Software Engineer

Akwa, Douala,CM 00237 | 174 Royal Rd, Cape Town, WC 7405, SA
Phone: (237) 670-518-086

 
Reply all
Reply to author
Forward
0 new messages