#!/usr/bin/env bash # Downloads a public stemcell for CPI into /var/vcap/store/stemcells/ # # Required: # * $MICRO_BOSH_STEMCELL_NAME - public stemcell name to be downloaded # - if 'custom' the create stemcell from BOSH source # # Optional: # * $PROVIDER - required for 'custom' $MICRO_BOSH_STEMCELL_NAME; e.g. aws, openstack # * $BOSH_RELEASE_REPO - defaults to https://github.com/cloudfoundry/bosh-release.git set -e # exit immediately if a simple command exits with a non-zero status set -u # report the usage of uninitialized variables if [[ $EUID -ne 0 ]]; then echo "ERROR: This script must be run as root" 1>&2 exit 1 fi if [[ "${MICRO_BOSH_STEMCELL_NAME}X" == "X" ]]; then echo 'INTERNAL ERROR: Missing $MICRO_BOSH_STEMCELL_NAME environment variable' exit 1 fi if [[ "${MICRO_BOSH_STEMCELL_NAME}" == "custom" ]]; then BOSH_RELEASE_REPO=${BOSH_RELEASE_REPO:-'https://github.com/cloudfoundry/bosh-release.git'} BOSH_RELEASE_DIRNAME=${BOSH_RELEASE_DIRNAME:-'bosh-release'} BOSH_RELEASE_DIR=/var/vcap/store/releases/$BOSH_RELEASE_DIRNAME echo "Creating custom stemcell..." if [[ -d /var/tmp/bosh/ ]]; then echo "But first, cleaning out previous stemcell temporary files..." rm -rf /var/tmp/bosh fi cd $(dirname $BOSH_RELEASE_DIR) if [[ -d $BOSH_RELEASE_DIRNAME ]]; then echo "Updating BOSH's own bosh-release for custom stemcell" cd $BOSH_RELEASE_DIRNAME git pull origin master else echo "Fetching BOSH's own bosh-release for custom stemcell" git clone $BOSH_RELEASE_REPO cd $BOSH_RELEASE_DIRNAME fi echo -e "Host github.com\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config echo "Rewriting all git:// & git@ to https:// to allow access if port 22 is blocked..." # Snippet written by Mike Reeves on bosh-users mailing list # Date 2012-12-06 grep -rI "git[@:/]\{0,3\}github.com" * .gitmodules | awk 'BEGIN {FS=":"} { print($1) }' | while read file do echo "changing - $file" sed -i 's/git\:\/\/github.com/https:\/\/github.com/g' $file sed -i 's/git@github.com:/https:\/\/github.com\//g' $file done echo "Updating source submodules..." git submodule update --init echo "Within submodules, rewriting all git:// & git@ to https:// to allow access if port 22 is blocked..." # Snippet written by Mike Reeves on bosh-users mailing list # Date 2012-12-06 grep -rI "git[@:/]\{0,3\}github.com" * .gitmodules | awk 'BEGIN {FS=":"} { print($1) }' | while read file do echo "changing - $file" sed -i 's/git\:\/\/github.com/https:\/\/github.com/g' $file sed -i 's/git@github.com:/https:\/\/github.com\//g' $file done echo "Creating bosh release (fetching any new blobs)..." bosh -n create release --force --with-tarball # dev_releases/index.yml contains the dev_releases # and its last line contains the latest dev_release name # For example: # --- # builds: # 4800ad1226e2b673595a8646e8e11bc968ab6ab6: # version: 119.1-dev # 7e0318d711daf0a5fa2e65c0257cd3b6af2ec4e5: # version: 119.2-dev # # So to get the latest dev_release name, grab the last line's 2nd token # For example, above this would be "119.2-dev" RELEASE_NAME="bosh-release" LATEST_DEV_RELEASE_NAME=$(tail -n 1 dev_releases/index.yml | awk '{print $2}') LATEST_BOSH_RELEASE_PATH=$BOSH_RELEASE_DIR/dev_releases/$RELEASE_NAME-$LATEST_DEV_RELEASE_NAME.tgz MICRO_BOSH_MANIFEST_PATH=$BOSH_RELEASE_DIR/micro/$PROVIDER.yml BOSH_DIR=/var/vcap/store/repos/bosh cd $(dirname $BOSH_DIR) if [[ -d bosh ]]; then echo "Updating bosh source for agent/stemcell scripts" cd bosh git pull origin master else echo "Cloning bosh source for agent/stemcell scripts" git clone https://github.com/cloudfoundry/bosh.git cd bosh fi cd $BOSH_DIR echo "Creating custom stemcell..." bundle install --without test development bundle exec rake stemcell:micro[$PROVIDER,$MICRO_BOSH_MANIFEST_PATH,$LATEST_BOSH_RELEASE_PATH] echo "Copying to stemcells folder..." MICRO_BOSH_STEMCELL_PATH=$(ls -t /var/tmp/bosh/agent-*/work/work/*.tgz | sort | tail -n 1) mv $MICRO_BOSH_STEMCELL_PATH /var/vcap/store/stemcells MICRO_BOSH_STEMCELL_NAME=$(basename $MICRO_BOSH_STEMCELL_PATH) echo "Cleaning up..." rm $(dirname $LATEST_BOSH_RELEASE_PATH)/*.tgz elif [[ -f /var/vcap/store/stemcells/$MICRO_BOSH_STEMCELL_NAME ]]; then echo "Stemcell $(pwd)/$MICRO_BOSH_STEMCELL_NAME already exists." else cd /var/vcap/store/stemcells echo "Downloading public stemcell $MICRO_BOSH_STEMCELL_NAME" # set $COLUMNS manually else progressbar runs `tput cols`.to_i which causes grief COLUMNS=80 bosh -n download public stemcell $MICRO_BOSH_STEMCELL_NAME fi # print name of stemcell created/downloaded to be stored back in settings echo $MICRO_BOSH_STEMCELL_NAME