|
a |
|
b/cloud/startup-script.sh |
|
|
1 |
set -v |
|
|
2 |
|
|
|
3 |
# Talk to the metadata server to get the project id |
|
|
4 |
PROJECTID=$(curl -s "http://metadata.google.internal/computeMetadata/v1/project/project-id" -H "Metadata-Flavor: Google") |
|
|
5 |
|
|
|
6 |
# Install logging monitor. The monitor will automatically pickup logs sent to |
|
|
7 |
# syslog. |
|
|
8 |
curl -s "https://storage.googleapis.com/signals-agents/logging/google-fluentd-install.sh" | bash |
|
|
9 |
service google-fluentd restart & |
|
|
10 |
|
|
|
11 |
# Install dependencies from apt |
|
|
12 |
apt-get update |
|
|
13 |
apt-get install -yq \ |
|
|
14 |
git build-essential supervisor python python-dev python-pip libffi-dev \ |
|
|
15 |
libssl-dev |
|
|
16 |
|
|
|
17 |
# Create a pythonapp user. The application will run as this user. |
|
|
18 |
useradd -m -d /home/pythonapp pythonapp |
|
|
19 |
|
|
|
20 |
# pip from apt is out of date, so make it update itself and install virtualenv. |
|
|
21 |
pip install --upgrade pip virtualenv |
|
|
22 |
|
|
|
23 |
# Get the source code from the Google Cloud Repository |
|
|
24 |
# git requires $HOME and it's not set during the startup script. |
|
|
25 |
export HOME=/root |
|
|
26 |
git config --global credential.helper gcloud.sh |
|
|
27 |
git clone https://source.developers.google.com/p/cnncancertherapy/r/cnnCancerTherapy /opt/app |
|
|
28 |
|
|
|
29 |
# Install app dependencies |
|
|
30 |
virtualenv /opt/app/7-gce/env |
|
|
31 |
/opt/app/7-gce/env/bin/pip install -r /opt/app/7-gce/requirements.txt |
|
|
32 |
|
|
|
33 |
# Make sure the pythonapp user owns the application code |
|
|
34 |
chown -R pythonapp:pythonapp /opt/app |
|
|
35 |
|
|
|
36 |
# Configure supervisor to start gunicorn inside of our virtualenv and run the |
|
|
37 |
# application. |
|
|
38 |
cat >/etc/supervisor/conf.d/python-app.conf << EOF |
|
|
39 |
[program:pythonapp] |
|
|
40 |
directory=/opt/app/7-gce |
|
|
41 |
command=/opt/app/7-gce/env/bin/gunicorn main:app --bind 0.0.0.0:8080 |
|
|
42 |
autostart=true |
|
|
43 |
autorestart=true |
|
|
44 |
user=pythonapp |
|
|
45 |
# Environment variables ensure that the application runs inside of the |
|
|
46 |
# configured virtualenv. |
|
|
47 |
environment=VIRTUAL_ENV="/opt/app/env/7-gce",PATH="/opt/app/7-gce/env/bin",\ |
|
|
48 |
HOME="/home/pythonapp",USER="pythonapp" |
|
|
49 |
stdout_logfile=syslog |
|
|
50 |
stderr_logfile=syslog |
|
|
51 |
EOF |
|
|
52 |
|
|
|
53 |
supervisorctl reread |
|
|
54 |
supervisorctl update |
|
|
55 |
|
|
|
56 |
# Application should now be running under supervisor |