diff --git a/appengine/standard/cloudsql/README.md b/appengine/standard/cloudsql/README.md index a87d4f1e013..e4d839cb2ac 100644 --- a/appengine/standard/cloudsql/README.md +++ b/appengine/standard/cloudsql/README.md @@ -8,6 +8,4 @@ Refer to the [App Engine Samples README](../README.md) for information on how to 1. You will need to create a [Cloud SQL instance](https://cloud.google.com/sql/docs/create-instance). -2. Edit the `CLOUDSQL_INSTANCE` and `CLOUDSQL_PROJECT` values in `main.py`. - -3. To run locally, you will need to be running a local instance of MySQL. You may need to update the connection code in `main.py` with the appropriate local username and password. +2. Edit the update the `env_variables` section in `app.yaml` with your Cloud SQL configuration. diff --git a/appengine/standard/cloudsql/app.yaml b/appengine/standard/cloudsql/app.yaml index 58843564e0e..22df6a2dce0 100644 --- a/appengine/standard/cloudsql/app.yaml +++ b/appengine/standard/cloudsql/app.yaml @@ -9,3 +9,10 @@ handlers: libraries: - name: MySQLdb version: "latest" + +# [START env_variables] +env_variables: + CLOUDSQL_CONNECTION_NAME: your-connection-name + CLOUDSQL_USER: root + CLOUDSQL_PASSWORD: your-cloudsql-user-password +# [END env_variables] diff --git a/appengine/standard/cloudsql/main.py b/appengine/standard/cloudsql/main.py index 372bf22528e..a0d5f109ec6 100644 --- a/appengine/standard/cloudsql/main.py +++ b/appengine/standard/cloudsql/main.py @@ -14,40 +14,57 @@ """ Sample App Engine application demonstrating how to connect to Google Cloud SQL -using App Engine's native unix socket. +using App Engine's native unix socket or using TCP when running locally. For more information, see the README.md. """ # [START all] - import os import MySQLdb import webapp2 -CLOUDSQL_PROJECT = '' -CLOUDSQL_INSTANCE = '' +# These environment variables are configured in app.yaml. +CLOUDSQL_CONNECTION_NAME = os.environ.get('CLOUDSQL_CONNECTION_NAME') +CLOUDSQL_USER = os.environ.get('CLOUDSQL_USER') +CLOUDSQL_PASSWORD = os.environ.get('CLOUDSQL_PASSWORD') + + +def connect_to_cloudsql(): + # When deployed to App Engine, the `SERVER_SOFTWARE` environment variable + # will be set to 'Google App Engine/version'. + if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'): + # Connect using the unix socket located at + # /cloudsql/cloudsql-connection-name. + cloudsql_unix_socket = os.path.join( + '/cloudsql', CLOUDSQL_CONNECTION_NAME) + + db = MySQLdb.connect( + unix_socket=cloudsql_unix_socket, + user=CLOUDSQL_USER, + passwd=CLOUDSQL_PASSWORD) + + # If the unix socket is unavailable, then try to connect using TCP. This + # will work if you're running a local MySQL server or using the Cloud SQL + # proxy, for example: + # + # $ cloud_sql_proxy -instances=your-connection-name=tcp:3306 + # + else: + db = MySQLdb.connect( + host='127.0.0.1', user=CLOUDSQL_USER, passwd=CLOUDSQL_PASSWORD) + + return db class MainPage(webapp2.RequestHandler): def get(self): + """Simple request handler that shows all of the MySQL variables.""" self.response.headers['Content-Type'] = 'text/plain' - # When running on Google App Engine, use the special unix socket - # to connect to Cloud SQL. - if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'): - db = MySQLdb.connect( - unix_socket='/cloudsql/{}:{}'.format( - CLOUDSQL_PROJECT, - CLOUDSQL_INSTANCE), - user='root') - # When running locally, you can either connect to a local running - # MySQL instance, or connect to your Cloud SQL instance over TCP. - else: - db = MySQLdb.connect(host='localhost', user='root') - + db = connect_to_cloudsql() cursor = db.cursor() cursor.execute('SHOW VARIABLES') diff --git a/appengine/standard/cloudsql/main_test.py b/appengine/standard/cloudsql/main_test.py index b08fddac57a..d20847d02c4 100644 --- a/appengine/standard/cloudsql/main_test.py +++ b/appengine/standard/cloudsql/main_test.py @@ -18,13 +18,19 @@ import pytest import webtest -import main + +@pytest.fixture +def main(monkeypatch): + monkeypatch.setenv('CLOUDSQL_USER', 'root') + monkeypatch.setenv('CLOUDSQL_PASSWORD', '') + import main + return main @pytest.mark.skipif( not os.path.exists('/var/run/mysqld/mysqld.sock'), - reason='MySQL server not available.') -def test_app(): + reason='Local MySQL server not available.') +def test_app(main): app = webtest.TestApp(main.app) response = app.get('/')