github.com/leowmjw/otto@v0.2.1-0.20160126165905-6400716cf085/builtin/app/python/data/aws-simple/build/build-python.sh.tpl (about)

     1  #!/bin/bash
     2  
     3  set -o nounset -o errexit -o pipefail -o errtrace
     4  
     5  error() {
     6     local sourcefile=$1
     7     local lineno=$2
     8     echo "ERROR at ${sourcefile}:${lineno}; Last logs:"
     9     grep otto /var/log/syslog | tail -n 20
    10  }
    11  trap 'error "${BASH_SOURCE}" "${LINENO}"' ERR
    12  
    13  oe() { "$@" 2>&1 | logger -t otto > /dev/null; }
    14  ol() { echo "[otto] $@"; }
    15  
    16  # cloud-config can interfere with apt commands if it's still in progress
    17  ol "Waiting for cloud-config to complete..."
    18  until [[ -f /var/lib/cloud/instance/boot-finished ]]; do
    19    sleep 0.5
    20  done
    21  
    22  ol "Adding apt repositories and updating..."
    23  oe sudo apt-get update
    24  oe sudo apt-get install -y python-software-properties software-properties-common apt-transport-https
    25  oe sudo add-apt-repository -y ppa:fkrull/deadsnakes
    26  oe sudo apt-get update
    27  
    28  export PYTHON_VERSION="{{ python_version }}"
    29  export PYTHON_ENTRYPOINT="{{ python_entrypoint }}"
    30  
    31  ol "Installing Python, Nginx, and other packages..."
    32  export DEBIAN_FRONTEND=noninteractive
    33  oe sudo apt-get install -y bzr git mercurial build-essential \
    34    libpq-dev zlib1g-dev software-properties-common \
    35    nodejs \
    36    libsqlite3-dev \
    37    python$PYTHON_VERSION python$PYTHON_VERSION-dev \
    38    nginx-extras
    39  
    40  
    41  ol "Installing pip and virtualenv..."
    42  oe sudo bash -c "python$PYTHON_VERSION <(wget -q -O - https://bootstrap.pypa.io/get-pip.py)"
    43  oe sudo -H pip install virtualenv
    44  
    45  ol "Creating virtualenv..."
    46  sudo virtualenv --python=/usr/bin/python$PYTHON_VERSION /srv/otto-app/virtualenv
    47  
    48  ol "Install gunicorn..."
    49  # we install using pip to support alternate python versions
    50  oe sudo /srv/otto-app/virtualenv/bin/pip -H install gunicorn
    51  
    52  ol "Extracting app..."
    53  sudo mkdir -p /srv/otto-app/src
    54  oe sudo tar xf /tmp/otto-app.tgz -C /srv/otto-app/src
    55  
    56  ol "Adding application user..."
    57  oe sudo adduser --disabled-password --gecos "" otto-app
    58  
    59  ol "Setting permissions..."
    60  oe sudo chown -R otto-app: /srv/otto-app
    61  
    62  ol "Configuring gunicorn..."
    63  cat <<GUNICORN | sudo tee /etc/init/gunicorn.conf > /dev/null
    64  description "gunicorn"
    65  
    66  start on (filesystem)
    67  stop on runlevel [016]
    68  
    69  respawn
    70  setuid otto-app
    71  setgid otto-app
    72  chdir /srv/otto-app/src
    73  
    74  exec /srv/otto-app/virtualenv/bin/gunicorn -w 4 $PYTHON_ENTRYPOINT
    75  
    76  GUNICORN
    77  
    78  ol "Configuring nginx..."
    79  
    80  # Need to remove this so nginx reads our site
    81  sudo rm /etc/nginx/sites-enabled/default
    82  
    83  cat <<NGINXCONF | sudo tee /etc/nginx/sites-enabled/otto-app.conf > /dev/null
    84  # Generated by Otto
    85  server {
    86      listen 80;
    87      location / {
    88          proxy_pass http://127.0.0.1:8000;
    89          proxy_set_header Host \$host;
    90          proxy_set_header X-Forward-For \$proxy_add_x_forwarded_for;
    91      }
    92  }
    93  NGINXCONF
    94  
    95  ol "Installing the app..."
    96  if [[ -f /srv/otto-app/src/setup.py ]]; then
    97      (
    98          cd /src/otto-app/src
    99          oe sudo -u otto-app /srv/otto-app/virtualenv/bin/python setup.py install
   100      )
   101  fi
   102  if [[ -f /srv/otto-app/src/requirements.txt ]]; then
   103      oe sudo -H -u otto-app /srv/otto-app/virtualenv/bin/pip install -r /srv/otto-app/src/requirements.txt
   104  fi
   105  
   106  ol "...done!"