github.com/dahs81/otto@v0.2.1-0.20160126165905-6400716cf085/builtin/app/ruby/data/aws-simple/build/build-ruby.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:chris-lea/node.js
    26  oe sudo apt-add-repository -y ppa:brightbox/ruby-ng
    27  oe sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7
    28  echo 'deb https://oss-binaries.phusionpassenger.com/apt/passenger trusty main' | sudo tee /etc/apt/sources.list.d/passenger.list > /dev/null
    29  oe sudo apt-get update
    30  
    31  export RUBY_VERSION="{{ ruby_version }}"
    32  
    33  ol "Installing Ruby, Passenger, Nginx, and other packages..."
    34  export DEBIAN_FRONTEND=noninteractive
    35  oe sudo apt-get install -y bzr git mercurial build-essential \
    36    libpq-dev zlib1g-dev software-properties-common \
    37    nodejs \
    38    libsqlite3-dev \
    39    ruby$RUBY_VERSION ruby$RUBY_VERSION-dev \
    40    nginx-extras passenger
    41  
    42  ol "Installing Bundler..."
    43  oe sudo gem install bundler --no-ri --no-rdoc
    44  
    45  ol "Extracting app..."
    46  sudo mkdir -p /srv/otto-app
    47  sudo tar zxf /tmp/otto-app.tgz -C /srv/otto-app
    48  
    49  ol "Adding application user..."
    50  oe sudo adduser --disabled-password --gecos "" otto-app
    51  
    52  ol "Setting permissions..."
    53  oe sudo chown -R otto-app: /srv/otto-app
    54  
    55  ol "Configuring nginx..."
    56  
    57  # This is required for passenger to get a reasonable environment where it can
    58  # find executables like /usr/bin/env, /usr/bin/curl, etc. It also apparently
    59  # needs to occur high in the config. Appending it is insufficient. :-|
    60  sudo sed -i '1s/^/env PATH;\n/' /etc/nginx/nginx.conf
    61  sudo sed -i '1s/^/# Otto: set PATH so passenger can see binaries\n/' /etc/nginx/nginx.conf
    62  
    63  # Need to remove this so nginx reads our site
    64  sudo rm /etc/nginx/sites-enabled/default
    65  
    66  # These lines are present as comments in the passenger-packaged nginx.conf, but
    67  # it's easier to drop a separate config than to sed out an uncomment.
    68  cat <<NGINXCONF | sudo tee /etc/nginx/conf.d/passenger.conf > /dev/null
    69  # Generated by Otto
    70  passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
    71  passenger_ruby /usr/bin/passenger_free_ruby;
    72  NGINXCONF
    73  
    74  cat <<NGINXCONF | sudo tee /etc/nginx/sites-enabled/otto-app.conf > /dev/null
    75  # Generated by Otto
    76  server {
    77      listen 80;
    78      root /srv/otto-app/public;
    79      passenger_enabled on;
    80  }
    81  NGINXCONF
    82  
    83  ol "Bundle installing the app..."
    84  sudo -u otto-app -i /bin/bash -lc "cd /srv/otto-app && bundle install --deployment --without development test"
    85  
    86  ol "...done!"