|
| 1 | +#! /bin/bash |
| 2 | + |
| 3 | +# Copyright 2018 Google Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# [START memorystore_startup_script_sh] |
| 17 | +set -v |
| 18 | + |
| 19 | +# Talk to the metadata server to get the project id and location of application binary. |
| 20 | +PROJECTID=$(curl -s "http://metadata.google.internal/computeMetadata/v1/project/project-id" -H "Metadata-Flavor: Google") |
| 21 | +GCS_APP_LOCATION=$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/app-location" -H "Metadata-Flavor: Google") |
| 22 | +REDISHOST=$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/redis-host" -H "Metadata-Flavor: Google") |
| 23 | +REDISPORT=$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/redis-port" -H "Metadata-Flavor: Google") |
| 24 | + |
| 25 | +# Install dependencies from apt |
| 26 | +apt-get update |
| 27 | +apt-get install -yq \ |
| 28 | + git build-essential supervisor python python-dev python-pip libffi-dev \ |
| 29 | + libssl-dev |
| 30 | + |
| 31 | +# Install logging monitor. The monitor will automatically pickup logs send to |
| 32 | +# syslog. |
| 33 | +curl -s "https://storage.googleapis.com/signals-agents/logging/google-fluentd-install.sh" | bash |
| 34 | +service google-fluentd restart & |
| 35 | + |
| 36 | +gsutil cp $GCS_APP_LOCATION /app.tar |
| 37 | +mkdir -p /app |
| 38 | +tar -x -f /app.tar -C /app |
| 39 | +cd /app |
| 40 | + |
| 41 | +# Install the app dependencies |
| 42 | +pip install --upgrade pip virtualenv |
| 43 | +virtualenv /app/env |
| 44 | +/app/env/bin/pip install -r /app/requirements.txt |
| 45 | + |
| 46 | +# Create a pythonapp user. The application will run as this user. |
| 47 | +getent passwd pythonapp || useradd -m -d /home/pythonapp pythonapp |
| 48 | +chown -R pythonapp:pythonapp /app |
| 49 | + |
| 50 | +# Configure supervisor to run the Go app. |
| 51 | +cat >/etc/supervisor/conf.d/pythonapp.conf << EOF |
| 52 | +[program:pythonapp] |
| 53 | +directory=/app |
| 54 | +environment=HOME="/home/pythonapp",USER="pythonapp",REDISHOST=$REDISHOST,REDISPORT=$REDISPORT |
| 55 | +command=/app/env/bin/gunicorn main:app --bind 0.0.0:8080 |
| 56 | +autostart=true |
| 57 | +autorestart=true |
| 58 | +user=pythonapp |
| 59 | +stdout_logfile=syslog |
| 60 | +stderr_logfile=syslog |
| 61 | +EOF |
| 62 | + |
| 63 | +supervisorctl reread |
| 64 | +supervisorctl update |
| 65 | +# [END memorystore_startup_script_sh] |
0 commit comments