2

I'm using GAE to deploy my app and I have some environment variables that I would like to pass to my GAE instance. For example, every time I use the DB, the unix socket assignment is currently like this:

unix_socket='<my_path_to_my_sockets>/{instance}'
.format(instance=properties.get_property('data.instance'))

That is great but the problem is that it's a shared code and everytime someone makes a local test it changes the path and push the changes to the repository. When someone pull the new changes then it needs to change the in order to make the db requests because everyone have a different path to the sockets changes too. So I've created the following statement:

unix_socket= (os.environ['CLOUDSQL_INSTANCES_DIR'] 
if 'CLOUDSQL_INSTANCES_DIR' in os.environ 
else '/home/cpinam/cloudsql/') 
   + properties.get_property('cloud.instance')

So, if someone has an environment variable in its system, then it takes the variable and avoid the absolute path. The problem is that this environment variable is not referring to my computer but GAE instance.

The question is, how can I take my environment variable instead of any environment variable of server instance? Is it possible?

PS: I've know that I can pass environment variables through the app.yaml file but that implies to modify the file.

Thank you

cpinamtz
  • 773
  • 8
  • 13
  • Referring to the environment variable in gae instance is the correct behavior, otherwise how would you expect it to work in production server? Does people custom install the cloudsql dir? Isn't the default installation will put it in the same location? – marcadian Apr 18 '18 at 19:15
  • @marcadian that environment variable will be neccesary only under local development. The problem is that the code is referring to the gae environment variables instead my local variables and I want to refer both – cpinamtz Apr 19 '18 at 07:14

2 Answers2

2

App Engine does not support what you want in the way that you want it.

There are a few alternative approaches that you might want to consider. It sounds like your primary constraint is wanting to enable individual developers to store alternative configuration to what they might find in production. You might want to consider allowing developers to store said configuration in their local Datastore instances.

Your code would look something like:

if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'):
  unix_socket = os.environ['CLOUDSQL_INSTANCES_DIR'] 
  ...
else:
  unix_socket = your_configuration_type.get("CLOUDSQL_INSTANCES_DIR")
  ...

Another alternative would be the approach outlined here where you'd store the relevant env vars in your own version of client_secrets.json and made sure that file was listed in .gitignore.

Venantius
  • 2,471
  • 2
  • 28
  • 36
0

This can be currently done using the original App Engine SDK's appcfg.py command for deployment. This is not possible using the gcloud App Engine deployment as far as I know.

You can define a default environment variable in your app.yaml file:

env_variables:
  HOST_NAME: ''

Pass your environment variable using -E option of the appcfg.py command -

   -E NAME:VALUE. Ex : -E HOST_NAME:WOAH

-E description : Set an environment variable, potentially overriding an env_variable value from app.yaml file (flag may be repeated to set multiple variables).