Skip to content

Commit f13dcb3

Browse files
Ryan Chenkurtisvg
Ryan Chen
authored andcommitted
Update cloud-sql/postgres sample (GoogleCloudPlatform#2235)
1 parent a9f269b commit f13dcb3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+317
-13
lines changed

cloud-sql/mysql/sqlalchemy/README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Download a JSON key to use to authenticate your connection.
2020
1. Use the information noted in the previous steps:
2121
```bash
2222
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json
23-
export CLOUD_SQL_CONNECTION_NAME='<MY-PROJECT>:<INSTANCE-REGION>:<MY-DATABASE>'
23+
export CLOUD_SQL_CONNECTION_NAME='<MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>'
2424
export DB_USER='my-db-user'
2525
export DB_PASS='my-db-pass'
2626
export DB_NAME='my_db'
@@ -31,9 +31,16 @@ secure solution such as [Cloud KMS](https://cloud.google.com/kms/) to help keep
3131
## Running locally
3232

3333
To run this application locally, download and install the `cloud_sql_proxy` by
34-
following the instructions [here](https://cloud.google.com/sql/docs/mysql/sql-proxy#install).
34+
following the instructions
35+
[here](https://cloud.google.com/sql/docs/mysql/sql-proxy#install). Once the
36+
proxy has been downloaded, use the following commands to create the `/cloudsql`
37+
directory and give the user running the proxy the appropriate permissions:
38+
```bash
39+
sudo mkdir /cloudsql
40+
sudo chown -R $USER /cloudsql
41+
```
3542

36-
Once the proxy is ready, use the following command to start the proxy in the
43+
Once the `/cloudsql` directory is ready, use the following command to start the proxy in the
3744
background:
3845
```bash
3946
./cloud_sql_proxy -dir=/cloudsql --instances=$CLOUD_SQL_CONNECTION_NAME --credential_file=$GOOGLE_APPLICATION_CREDENTIALS

cloud-sql/mysql/sqlalchemy/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@
3636
# managing a pool of connections to your database
3737
db = sqlalchemy.create_engine(
3838
# Equivalent URL:
39-
# mysql+pymysql://<db_user>:<db_pass>@/<db_name>?unix_socket=/cloudsql/<cloud_sql_instance_name>
39+
# mysql+pymysql://<db_user>:<db_pass>@/<db_name>?unix_sock=/cloudsql/<cloud_sql_instance_name>
4040
sqlalchemy.engine.url.URL(
4141
drivername='mysql+pymysql',
4242
username=db_user,
4343
password=db_pass,
4444
database=db_name,
4545
query={
46-
'unix_socket': '/cloudsql/{}'.format(cloud_sql_connection_name)
46+
'unix_socket': '/cloudsql/{}/'.format(cloud_sql_connection_name)
4747
}
4848
),
4949
# ... Specify additional properties here.

cloud-sql/postgres/sqlalchemy/README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Download a JSON key to use to authenticate your connection.
2020
1. Use the information noted in the previous steps:
2121
```bash
2222
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json
23-
export CLOUD_SQL_CONNECTION_NAME='<MY-PROJECT>:<INSTANCE-REGION>:<MY-DATABASE>'
23+
export CLOUD_SQL_CONNECTION_NAME='<MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>'
2424
export DB_USER='my-db-user'
2525
export DB_PASS='my-db-pass'
2626
export DB_NAME='my_db'
@@ -31,16 +31,18 @@ secure solution such as [Cloud KMS](https://cloud.google.com/kms/) to help keep
3131
## Running locally
3232

3333
To run this application locally, download and install the `cloud_sql_proxy` by
34-
following the instructions [here](https://cloud.google.com/sql/docs/mysql/sql-proxy#install).
34+
following the instructions [here](https://cloud.google.com/sql/docs/mysql/sql-proxy#install). Once the proxy has been downloaded, use the following commands to create the `/cloudsql`
35+
directory and give the user running the proxy the appropriate permissions:
36+
```bash
37+
sudo mkdir /cloudsql
38+
sudo chown -R $USER /cloudsql
39+
```
3540

36-
Once the proxy is ready, use the following command to start the proxy in the
41+
Once the `/cloudsql` directory is ready, use the following command to start the proxy in the
3742
background:
3843
```bash
3944
./cloud_sql_proxy -dir=/cloudsql --instances=$CLOUD_SQL_CONNECTION_NAME --credential_file=$GOOGLE_APPLICATION_CREDENTIALS
4045
```
41-
Note: Make sure to run the command under a user with write access in the
42-
`/cloudsql` directory. This proxy will use this folder to create a unix socket
43-
the application will use to connect to Cloud SQL.
4446

4547
Next, setup install the requirements into a virtual enviroment:
4648
```bash
@@ -67,4 +69,5 @@ variables into the runtime.
6769
Next, the following command will deploy the application to your Google Cloud project:
6870
```bash
6971
gcloud app deploy
72+
7073
```

cloud-sql/postgres/sqlalchemy/main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@
3636
# managing a pool of connections to your database
3737
db = sqlalchemy.create_engine(
3838
# Equivalent URL:
39-
# postgres+pg8000://<db_user>:<db_pass>@/<db_name>?unix_socket=/cloudsql/<cloud_sql_instance_name>
39+
# postgres+pg8000://<db_user>:<db_pass>@/<db_name>?unix_socket=/cloudsql/<cloud_sql_instance_name>/.s.PGSQL.5432
4040
sqlalchemy.engine.url.URL(
4141
drivername='postgres+pg8000',
4242
username=db_user,
4343
password=db_pass,
4444
database=db_name,
4545
query={
46-
'unix_sock': '/cloudsql/{}'.format(cloud_sql_connection_name)
46+
'unix_sock': '/cloudsql/{}/.s.PGSQL.5432'.format(
47+
cloud_sql_connection_name)
4748
}
4849
),
4950
# ... Specify additional properties here.

venv/bin/activate

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# This file must be used with "source bin/activate" *from bash*
2+
# you cannot run it directly
3+
4+
deactivate () {
5+
# reset old environment variables
6+
if [ -n "${_OLD_VIRTUAL_PATH:-}" ] ; then
7+
PATH="${_OLD_VIRTUAL_PATH:-}"
8+
export PATH
9+
unset _OLD_VIRTUAL_PATH
10+
fi
11+
if [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ] ; then
12+
PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME:-}"
13+
export PYTHONHOME
14+
unset _OLD_VIRTUAL_PYTHONHOME
15+
fi
16+
17+
# This should detect bash and zsh, which have a hash command that must
18+
# be called to get it to forget past commands. Without forgetting
19+
# past commands the $PATH changes we made may not be respected
20+
if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then
21+
hash -r
22+
fi
23+
24+
if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then
25+
PS1="${_OLD_VIRTUAL_PS1:-}"
26+
export PS1
27+
unset _OLD_VIRTUAL_PS1
28+
fi
29+
30+
unset VIRTUAL_ENV
31+
if [ ! "$1" = "nondestructive" ] ; then
32+
# Self destruct!
33+
unset -f deactivate
34+
fi
35+
}
36+
37+
# unset irrelevant variables
38+
deactivate nondestructive
39+
40+
VIRTUAL_ENV="/usr/local/google/home/ryachen/Documents/FORK-python-docs/venv"
41+
export VIRTUAL_ENV
42+
43+
_OLD_VIRTUAL_PATH="$PATH"
44+
PATH="$VIRTUAL_ENV/bin:$PATH"
45+
export PATH
46+
47+
# unset PYTHONHOME if set
48+
# this will fail if PYTHONHOME is set to the empty string (which is bad anyway)
49+
# could use `if (set -u; : $PYTHONHOME) ;` in bash
50+
if [ -n "${PYTHONHOME:-}" ] ; then
51+
_OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME:-}"
52+
unset PYTHONHOME
53+
fi
54+
55+
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then
56+
_OLD_VIRTUAL_PS1="${PS1:-}"
57+
if [ "x(venv) " != x ] ; then
58+
PS1="(venv) ${PS1:-}"
59+
else
60+
if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then
61+
# special case for Aspen magic directories
62+
# see http://www.zetadev.com/software/aspen/
63+
PS1="[`basename \`dirname \"$VIRTUAL_ENV\"\``] $PS1"
64+
else
65+
PS1="(`basename \"$VIRTUAL_ENV\"`)$PS1"
66+
fi
67+
fi
68+
export PS1
69+
fi
70+
71+
# This should detect bash and zsh, which have a hash command that must
72+
# be called to get it to forget past commands. Without forgetting
73+
# past commands the $PATH changes we made may not be respected
74+
if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then
75+
hash -r
76+
fi

venv/bin/activate.csh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# This file must be used with "source bin/activate.csh" *from csh*.
2+
# You cannot run it directly.
3+
# Created by Davide Di Blasi <davidedb@gmail.com>.
4+
# Ported to Python 3.3 venv by Andrew Svetlov <andrew.svetlov@gmail.com>
5+
6+
alias deactivate 'test $?_OLD_VIRTUAL_PATH != 0 && setenv PATH "$_OLD_VIRTUAL_PATH" && unset _OLD_VIRTUAL_PATH; rehash; test $?_OLD_VIRTUAL_PROMPT != 0 && set prompt="$_OLD_VIRTUAL_PROMPT" && unset _OLD_VIRTUAL_PROMPT; unsetenv VIRTUAL_ENV; test "\!:*" != "nondestructive" && unalias deactivate'
7+
8+
# Unset irrelevant variables.
9+
deactivate nondestructive
10+
11+
setenv VIRTUAL_ENV "/usr/local/google/home/ryachen/Documents/FORK-python-docs/venv"
12+
13+
set _OLD_VIRTUAL_PATH="$PATH"
14+
setenv PATH "$VIRTUAL_ENV/bin:$PATH"
15+
16+
17+
set _OLD_VIRTUAL_PROMPT="$prompt"
18+
19+
if (! "$?VIRTUAL_ENV_DISABLE_PROMPT") then
20+
if ("venv" != "") then
21+
set env_name = "venv"
22+
else
23+
if (`basename "VIRTUAL_ENV"` == "__") then
24+
# special case for Aspen magic directories
25+
# see http://www.zetadev.com/software/aspen/
26+
set env_name = `basename \`dirname "$VIRTUAL_ENV"\``
27+
else
28+
set env_name = `basename "$VIRTUAL_ENV"`
29+
endif
30+
endif
31+
set prompt = "[$env_name] $prompt"
32+
unset env_name
33+
endif
34+
35+
alias pydoc python -m pydoc
36+
37+
rehash

venv/bin/activate.fish

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# This file must be used with ". bin/activate.fish" *from fish* (http://fishshell.org)
2+
# you cannot run it directly
3+
4+
function deactivate -d "Exit virtualenv and return to normal shell environment"
5+
# reset old environment variables
6+
if test -n "$_OLD_VIRTUAL_PATH"
7+
set -gx PATH $_OLD_VIRTUAL_PATH
8+
set -e _OLD_VIRTUAL_PATH
9+
end
10+
if test -n "$_OLD_VIRTUAL_PYTHONHOME"
11+
set -gx PYTHONHOME $_OLD_VIRTUAL_PYTHONHOME
12+
set -e _OLD_VIRTUAL_PYTHONHOME
13+
end
14+
15+
if test -n "$_OLD_FISH_PROMPT_OVERRIDE"
16+
functions -e fish_prompt
17+
set -e _OLD_FISH_PROMPT_OVERRIDE
18+
functions -c _old_fish_prompt fish_prompt
19+
functions -e _old_fish_prompt
20+
end
21+
22+
set -e VIRTUAL_ENV
23+
if test "$argv[1]" != "nondestructive"
24+
# Self destruct!
25+
functions -e deactivate
26+
end
27+
end
28+
29+
# unset irrelevant variables
30+
deactivate nondestructive
31+
32+
set -gx VIRTUAL_ENV "/usr/local/google/home/ryachen/Documents/FORK-python-docs/venv"
33+
34+
set -gx _OLD_VIRTUAL_PATH $PATH
35+
set -gx PATH "$VIRTUAL_ENV/bin" $PATH
36+
37+
# unset PYTHONHOME if set
38+
if set -q PYTHONHOME
39+
set -gx _OLD_VIRTUAL_PYTHONHOME $PYTHONHOME
40+
set -e PYTHONHOME
41+
end
42+
43+
if test -z "$VIRTUAL_ENV_DISABLE_PROMPT"
44+
# fish uses a function instead of an env var to generate the prompt.
45+
46+
# save the current fish_prompt function as the function _old_fish_prompt
47+
functions -c fish_prompt _old_fish_prompt
48+
49+
# with the original prompt function renamed, we can override with our own.
50+
function fish_prompt
51+
# Save the return status of the last command
52+
set -l old_status $status
53+
54+
# Prompt override?
55+
if test -n "(venv) "
56+
printf "%s%s" "(venv) " (set_color normal)
57+
else
58+
# ...Otherwise, prepend env
59+
set -l _checkbase (basename "$VIRTUAL_ENV")
60+
if test $_checkbase = "__"
61+
# special case for Aspen magic directories
62+
# see http://www.zetadev.com/software/aspen/
63+
printf "%s[%s]%s " (set_color -b blue white) (basename (dirname "$VIRTUAL_ENV")) (set_color normal)
64+
else
65+
printf "%s(%s)%s" (set_color -b blue white) (basename "$VIRTUAL_ENV") (set_color normal)
66+
end
67+
end
68+
69+
# Restore the return status of the previous command.
70+
echo "exit $old_status" | .
71+
_old_fish_prompt
72+
end
73+
74+
set -gx _OLD_FISH_PROMPT_OVERRIDE "$VIRTUAL_ENV"
75+
end

venv/bin/easy_install

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/local/google/home/ryachen/Documents/FORK-python-docs/venv/bin/python3
2+
3+
# -*- coding: utf-8 -*-
4+
import re
5+
import sys
6+
7+
from setuptools.command.easy_install import main
8+
9+
if __name__ == '__main__':
10+
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
11+
sys.exit(main())

venv/bin/easy_install-3.6

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/local/google/home/ryachen/Documents/FORK-python-docs/venv/bin/python3
2+
3+
# -*- coding: utf-8 -*-
4+
import re
5+
import sys
6+
7+
from setuptools.command.easy_install import main
8+
9+
if __name__ == '__main__':
10+
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
11+
sys.exit(main())

venv/bin/flake8

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/local/google/home/ryachen/Documents/FORK-python-docs/venv/bin/python3
2+
3+
# -*- coding: utf-8 -*-
4+
import re
5+
import sys
6+
7+
from flake8.main.cli import main
8+
9+
if __name__ == '__main__':
10+
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
11+
sys.exit(main())

venv/bin/flask

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/local/google/home/ryachen/Documents/FORK-python-docs/venv/bin/python3
2+
3+
# -*- coding: utf-8 -*-
4+
import re
5+
import sys
6+
7+
from flask.cli import main
8+
9+
if __name__ == '__main__':
10+
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
11+
sys.exit(main())

venv/bin/pip

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/local/google/home/ryachen/Documents/FORK-python-docs/venv/bin/python3
2+
3+
# -*- coding: utf-8 -*-
4+
import re
5+
import sys
6+
7+
from pip import main
8+
9+
if __name__ == '__main__':
10+
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
11+
sys.exit(main())

venv/bin/pip3

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/local/google/home/ryachen/Documents/FORK-python-docs/venv/bin/python3
2+
3+
# -*- coding: utf-8 -*-
4+
import re
5+
import sys
6+
7+
from pip import main
8+
9+
if __name__ == '__main__':
10+
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
11+
sys.exit(main())

venv/bin/pip3.6

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/local/google/home/ryachen/Documents/FORK-python-docs/venv/bin/python3
2+
3+
# -*- coding: utf-8 -*-
4+
import re
5+
import sys
6+
7+
from pip import main
8+
9+
if __name__ == '__main__':
10+
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
11+
sys.exit(main())

venv/bin/pycodestyle

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/local/google/home/ryachen/Documents/FORK-python-docs/venv/bin/python3
2+
3+
# -*- coding: utf-8 -*-
4+
import re
5+
import sys
6+
7+
from pycodestyle import _main
8+
9+
if __name__ == '__main__':
10+
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
11+
sys.exit(_main())

0 commit comments

Comments
 (0)