github.com/jiasir/deis@v1.12.2/builder/rootfs/usr/local/src/slugbuilder/builder/build.sh (about)

     1  #!/usr/bin/env bash
     2  set -eo pipefail
     3  
     4  if [[ -f /etc/environment_proxy ]]; then
     5  	source /etc/environment_proxy
     6  fi
     7  
     8  if [[ "$1" == "-" ]]; then
     9      slug_file="$1"
    10  else
    11      slug_file=/tmp/slug.tgz
    12      if [[ "$1" ]]; then
    13          put_url="$1"
    14      fi
    15  fi
    16  
    17  
    18  app_dir=/app
    19  build_root=/tmp/build
    20  cache_root=/tmp/cache
    21  buildpack_root=/tmp/buildpacks
    22  
    23  mkdir -p $app_dir
    24  mkdir -p $cache_root
    25  mkdir -p $buildpack_root
    26  mkdir -p $build_root/.profile.d
    27  
    28  function output_redirect() {
    29      if [[ "$slug_file" == "-" ]]; then
    30          cat - 1>&2
    31      else
    32          cat -
    33      fi
    34  }
    35  
    36  function echo_title() {
    37      echo $'\e[1G----->' $* | output_redirect
    38  }
    39  
    40  function echo_normal() {
    41      echo $'\e[1G      ' $* | output_redirect
    42  }
    43  
    44  function ensure_indent() {
    45      while read line; do
    46          if [[ "$line" == --* ]]; then
    47              echo $'\e[1G'$line | output_redirect
    48          else
    49              echo $'\e[1G      ' "$line" | output_redirect
    50          fi
    51      done
    52  }
    53  
    54  ## Copy application code over
    55  if [ -d "/tmp/app" ]; then
    56      cp -rf /tmp/app/. $app_dir
    57      chown -R slug:slug $app_dir
    58  else
    59      cat | tar -xmC $app_dir
    60  fi
    61  
    62  # In heroku, there are two separate directories, and some
    63  # buildpacks expect that.
    64  cp -r $app_dir/. $build_root
    65  
    66  ## Buildpack fixes
    67  
    68  export APP_DIR="$app_dir"
    69  export HOME="$app_dir"
    70  export REQUEST_ID=$(openssl rand -base64 32)
    71  export STACK=cedar-14
    72  
    73  ## SSH key configuration
    74  
    75  if [[ -n "$SSH_KEY" ]]; then
    76      mkdir -p ~/.ssh/
    77      chmod 700 ~/.ssh/
    78  
    79      echo $SSH_KEY | base64 -d > ~/.ssh/id_rsa
    80      chmod 400 ~/.ssh/id_rsa
    81  
    82      echo 'StrictHostKeyChecking=no' > ~/.ssh/config
    83      chmod 600 ~/.ssh/config
    84  fi
    85  
    86  ## Buildpack detection
    87  
    88  buildpacks=($buildpack_root/*)
    89  selected_buildpack=
    90  
    91  if [[ -n "$BUILDPACK_URL" ]]; then
    92      echo_title "Fetching custom buildpack"
    93  
    94      buildpack="$buildpack_root/custom"
    95      rm -fr "$buildpack"
    96  
    97      url=${BUILDPACK_URL%#*}
    98      committish=${BUILDPACK_URL#*#}
    99  
   100      if [ "$committish" == "$url" ]; then
   101          committish="master"
   102      fi
   103  
   104      set +e
   105      git clone --branch "$committish" --depth=1 "$url" "$buildpack" &> /dev/null
   106      SHALLOW_CLONED=$?
   107      set -e
   108      if [ $SHALLOW_CLONED -ne 0 ]; then
   109          # if the shallow clone failed partway through, clean up and try a full clone
   110          rm -rf "$buildpack"
   111          git clone --quiet "$url" "$buildpack"
   112          pushd "$buildpack" &>/dev/null
   113              git checkout --quiet "$committish"
   114              git submodule init --quiet
   115              git submodule update --quiet --recursive
   116          popd &>/dev/null
   117      fi
   118  
   119      selected_buildpack="$buildpack"
   120      buildpack_name=$($buildpack/bin/detect "$build_root") && selected_buildpack=$buildpack
   121  else
   122      for buildpack in "${buildpacks[@]}"; do
   123          buildpack_name=$($buildpack/bin/detect "$build_root") && selected_buildpack=$buildpack && break
   124      done
   125  fi
   126  
   127  if [[ -n "$selected_buildpack" ]]; then
   128      echo_title "$buildpack_name app detected"
   129  else
   130      echo_title "Unable to select a buildpack"
   131      exit 1
   132  fi
   133  
   134  export CURL_CONNECT_TIMEOUT="30"
   135  export CURL_TIMEOUT="180"
   136  
   137  ## Buildpack compile
   138  
   139  $selected_buildpack/bin/compile "$build_root" "$cache_root" | ensure_indent
   140  
   141  $selected_buildpack/bin/release "$build_root" "$cache_root" > $build_root/.release
   142  
   143  ## Display process types
   144  
   145  echo_title "Discovering process types"
   146  if [[ -f "$build_root/Procfile" ]]; then
   147      types=$(ruby -e "require 'yaml';puts YAML.load_file('$build_root/Procfile').keys().join(', ')")
   148      echo_normal "Procfile declares types -> $types"
   149  fi
   150  default_types=""
   151  if [[ -s "$build_root/.release" ]]; then
   152      default_types=$(ruby -e "require 'yaml';puts (YAML.load_file('$build_root/.release')['default_process_types'] || {}).keys().join(', ')")
   153      [[ $default_types ]] && echo_normal "Default process types for $buildpack_name -> $default_types"
   154  fi
   155  
   156  # Fix any wayward permissions. We want everything in app to be owned
   157  # by slug.
   158  chown -R slug:slug $build_root/*
   159  
   160  
   161  ## Produce slug
   162  
   163  if [[ -f "$build_root/.slugignore" ]]; then
   164      tar -z --exclude='.git' -X "$build_root/.slugignore" -C $build_root -cf $slug_file . | cat
   165  else
   166      tar -z --exclude='.git' -C $build_root -cf $slug_file . | cat
   167  fi
   168  
   169  if [[ "$slug_file" != "-" ]]; then
   170      slug_size=$(du -Sh "$slug_file" | cut -f1)
   171      echo_title "Compiled slug size is $slug_size"
   172  
   173      if [[ $put_url ]]; then
   174          curl -0 -s -o /dev/null -X PUT -T $slug_file "$put_url"
   175      fi
   176  fi