Skip to content
This repository has been archived by the owner on Jun 12, 2018. It is now read-only.

Commit

Permalink
Squashing lots of stuff to share with the world!
Browse files Browse the repository at this point in the history
  • Loading branch information
fgrehm committed Mar 18, 2014
0 parents commit 4e653a9
Show file tree
Hide file tree
Showing 46 changed files with 2,471 additions and 0 deletions.
82 changes: 82 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
FROM progrium/cedarish
MAINTAINER Fabio Rehm "fgrehm@gmail.com"

#####################################################################
# Create a default user to avoid using the container as root, we set
# the user and group ids to 1000 as it is the most common ids for
# single user Ubuntu machines.
# The provided /usr/bin/fix-permissions script can be used at startup
# to ensure the 'developer' user id / group id are the same as the
# directory bind mounted into the container.
RUN mkdir -p /.devstep/cache && \
mkdir -p /.devstep/.profile.d && \
mkdir -p /.devstep/bin && \
mkdir -p /.devstep/log && \
mkdir -p /workspace && \
echo "developer:x:1000:1000:Developer,,,:/.devstep:/bin/bash" >> /etc/passwd && \
echo "developer:x:1000:" >> /etc/group && \
echo "developer ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/developer && \
chmod 0440 /etc/sudoers.d/developer

#####################################################################
# Init script based on phusion/baseimage-docker's

RUN DEBIAN_FRONTEND=noninteractive && \
apt-get update && \
apt-get install -y runit python && \
mkdir -p /etc/service && \
mkdir -p /etc/my_init.d && \
mkdir -p /etc/container_environment

#####################################################################
# Install and configure PostgreSQL and MySQL clients
RUN DEBIAN_FRONTEND=noninteractive && \
apt-get update && \
apt-get install -y postgresql-client mysql-client

RUN echo "[client]\nprotocol=tcp\nuser=root" >> /.devstep/.my.cnf && \
echo "export PGHOST=localhost" >> /.devstep/.profile.d/postgresql.sh && \
echo "export PGUSER=postgres" >> /.devstep/.profile.d/postgresql.sh && \
echo "localhost" > /etc/container_environment/PGHOST

#####################################################################
# Download and install jq as it is being used by a few buildpacks
# See http://stedolan.github.io/jq for more info
RUN mkdir -p /.devstep/bin && \
curl -L -s http://stedolan.github.io/jq/download/linux64/jq > /.devstep/bin/jq && \
chmod +x /.devstep/bin/jq

#####################################################################
# Fix permissions
RUN chown -R developer:developer /.devstep && \
chown -R developer:developer /workspace && \
chown -R developer:developer /etc/service && \
chown -R developer:developer /etc/my_init.d && \
chown -R developer:developer /etc/container_environment

#####################################################################
# Devstep goodies (ADDed at the end to increase image "cacheability")

ADD stack/bashrc /.devstep/.bashrc
ADD stack/fix-permissions /usr/bin/fix-permissions
ADD stack/my-init /usr/bin/my-init
ADD stack/forward-ports /usr/bin/forward-ports
ADD stack/load-devstep-env /.devstep/load-env.sh
ADD stack/hack /.devstep/bin/hack
ADD builder/build.sh /.devstep/bin/build-project
ADD buildpacks /.devstep/buildpacks
ADD https://godist.herokuapp.com/projects/ddollar/forego/releases/current/linux-amd64/forego /.devstep/bin/forego

RUN chmod +x /usr/bin/fix-permissions && \
chmod +x /usr/bin/my-init && \
chmod +x /usr/bin/forward-ports && \
chmod +x /.devstep/bin/forego && \
chmod +x /.devstep/bin/build-project && \
chmod +x /.devstep/bin/hack && \
chmod u+s /usr/bin/sudo && \
echo 'source /.devstep/load-env.sh' > /etc/my_init.d/load-devstep-env.sh && \
ln -s /usr/bin/fix-permissions /etc/my_init.d/fix-permissions.sh && \
ln -s /usr/bin/forward-ports /etc/my_init.d/forward-ports.sh

USER developer
ENV HOME /.devstep
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2014 Fabio Rehm

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Devstep

Build development environments using [buildpacks](https://devcenter.heroku.com/articles/buildpacks)
and [Docker](https://www.docker.io/).

_Documentation is being worked on, stay tunned for updates or have a look at
project sources if you are feeling adventurous_
119 changes: 119 additions & 0 deletions builder/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#!/bin/bash
set -eo pipefail

project_root=${1:-/workspace}
cache_root=${2:-/.devstep/cache}
buildpack_root=/.devstep/buildpacks

mkdir -p $project_root
mkdir -p $cache_root
mkdir -p $buildpack_root
mkdir -p /.devstep/.profile.d

function output_redirect() {
if [[ "$slug_file" == "-" ]]; then
cat - 1>&2
else
cat -
fi
}

function echo_title() {
echo $'\e[1G----->' $* | output_redirect
}

function echo_normal() {
echo $'\e[1G ' $* | output_redirect
}

function ensure_indent() {
while read line; do
if [[ "$line" == --* ]]; then
echo $'\e[1G'$line | output_redirect
else
echo $'\e[1G ' "$line" | output_redirect
fi
done
}

# Centralize cache directories on a single place to simplify
# caching on the host

if ! [ -L /var/cache/apt/archives ]; then
mkdir -p $cache_root/apt
# TODO: Use a cache warm up approach similar to vagrant-cachier's
sudo rm -rf /var/cache/apt/archives
sudo ln -s $cache_root/apt /var/cache/apt/archives
fi

if ! [ -L /var/lib/apt/lists ]; then
mkdir -p $cache_root/apt-lists
# TODO: Use a cache warm up approach similar to vagrant-cachier's
sudo rm -rf /var/lib/apt/lists
sudo ln -s $cache_root/apt-lists /var/lib/apt/lists
fi

# In heroku, there are two separate directories, and some
# buildpacks expect that.
# TODO: Figure out if this is needed
# cp -r $project_dir/. $build_root

## Buildpack fixes

export REQUEST_ID=$(openssl rand -base64 32 2>/dev/null)
export APP_DIR="$project_root"
# export HOME="$project_root"

## Fix directory permissions

(cd $project_root && /usr/bin/fix-permissions)

## Buildpack detection

if [[ -z "$BUILDPACKS" ]]; then
buildpacks=($buildpack_root/*)
declare -a selected_buildpacks
for buildpack in "${buildpacks[@]}"; do
if $($buildpack/bin/detect "${project_root}" &>/dev/null); then
selected_buildpacks=("${selected_buildpacks[@]}" $buildpack)
fi
done
else
declare -a selected_buildpacks
for buildpack in "${BUILDPACKS[@]}"; do
selected_buildpacks=("${selected_buildpacks[@]}" ${buildpack_root}/${buildpack})
done
fi

## Compile!

if [[ -n "$selected_buildpacks" ]]; then
# TODO: This output is not needed if a single buildpack was detected
echo_title "Building project at '${project_root}' with the folllowing buildpacks:"
for bp in "${selected_buildpacks[@]}"; do
echo "- $bp" | ensure_indent
done
else
echo_title "Unable to identify a buildpack for your project!"
exit 0
fi

for bp in "${selected_buildpacks[@]}"; do
echo_title "Building with $bp"
$bp/bin/compile "$project_root" "$cache_root" | ensure_indent
done

echo_title "Build finished successfully!"

## Save on disk space if wanted

if [ "${CLEANUP}" = '1' ]; then
echo_title 'Cleaning up...'

# TODO: We can't do this yet because the golang buildpack stores things on this dir
# echo_normal "Running 'rm -rf $cache_root/*'"
# rm -rf $cache_root/*

echo_normal "Running 'sudo rm -rf tmp/*'"
sudo rm -rf /tmp/*
fi
18 changes: 18 additions & 0 deletions buildpacks/bats/bin/compile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash
# usage: bin/compile <build-dir> <cache-dir> <env-dir>

set -eo pipefail

cache=$2

if $(which bats &>/dev/null); then
echo '-----> Bats already installed, skipping'
else
echo '-----> Downloading bats from GitHub...'
if ! [ -f $cache/bats.tar.gz ]; then
curl -sL --insecure http://github.com/sstephenson/bats/tarball/master > $cache/bats.tar.gz
fi
mkdir -p /.devstep/bats
tar xfz $cache/bats.tar.gz -C /.devstep/bats --strip-components=1
(cd /.devstep/bats && sudo ./install.sh /.devstep)
fi
11 changes: 11 additions & 0 deletions buildpacks/bats/bin/detect
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash
# usage: bin/detect <build-dir>

set -eo pipefail

if test -n "$(find "$1" -type f -name '*.bats' | sed 1q)"; then
echo Bats
else
echo no
exit 1
fi
101 changes: 101 additions & 0 deletions buildpacks/golang/bin/compile
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/usr/bin/env bash
# usage: bin/compile <build-dir> <cache-dir> <env-dir>

# This is a modified version of https://github.com/kr/heroku-buildpack-go/blob/master/bin/compile

set -eo pipefail

mkdir -p "$1" "$2"
build=$1
cache=$2
arch=$(uname -m|tr A-Z a-z)
if test $arch = x86_64
then arch=amd64
fi
plat=$(uname|tr A-Z a-z)-$arch

heroku_buildpack_bin='https://github.com/kr/heroku-buildpack-go/raw/master/linux-amd64/bin'
target='/.devstep/bin'
if [ -f $build/Godeps ] || [ -d $build/Godeps ]; then
if ! [ -f ${target}/godep ]; then
echo -n "-----> Installing godep to ${target}..."
if ! [ -f ${cache}/godep ]; then
curl -L -s ${heroku_buildpack_bin}/godep > ${cache}/godep
fi
cp ${cache}/godep ${target}/godep
chmod +x ${target}/godep
echo ' done'
else
echo -n "-----> Using ${target}/godep"
fi
fi
PATH=$target:$PATH

if test -f $build/Godeps; then
name=$(<$build/Godeps jq -r .ImportPath)
ver=$(<$build/Godeps jq -r .GoVersion)
elif test -d $build/Godeps; then
name=$(<$build/Godeps/Godeps.json jq -r .ImportPath)
ver=$(<$build/Godeps/Godeps.json jq -r .GoVersion)
elif test -f $build/.godir; then
name=$(cat $build/.godir)
fi
ver=${ver:-go${GOVERSION:-1.2.1}}

file=${GOFILE:-$ver.$(uname|tr A-Z a-z)-amd64.tar.gz}
url=${GOURL:-http://go.googlecode.com/files/$file}

mkdir -p $cache/$ver
cd $cache/$ver
if test -f $cache/$ver/$file; then
echo "-----> Using $ver"
else
echo "-----> Downloading ${ver}..."
curl -sO $url
echo "done"
fi
tar zxf $file -C /.devstep

GOROOT=/.devstep/go export GOROOT
PATH=$GOROOT/bin:$PATH export PATH

GOPATH=/.devstep/gocode export GOPATH
GOBIN=$GOPATH/bin export GOBIN
PATH=$GOBIN:$PATH export PATH

mkdir -p $GOBIN
mkdir -p $GOPATH

echo "-----> Configuring ${ver}..."
echo "export GOPATH=$GOPATH" > /.devstep/.profile.d/go.sh
echo "export GOROOT=$GOROOT" >> /.devstep/.profile.d/go.sh
echo "export GOBIN=$GOBIN" >> /.devstep/.profile.d/go.sh
echo 'export PATH=$PATH:$GOROOT/bin' >> /.devstep/.profile.d/go.sh
echo 'export PATH=$PATH:$GOBIN' >> /.devstep/.profile.d/go.sh
echo 'done'

if [ -z "${name}" ] && [ -n "${GO_PROJECT_NAME}" ]; then
name=$GO_PROJECT_NAME
fi

if [ -z "${name}" ]; then
echo -e "-----> Unable to identify project name\nPlease add the full import path to \`.godir\` or set the GO_PROJECT_NAME environmental variable"
p=$build
else
p=$GOPATH/src/$name
mkdir -p $(dirname $p)
if ! [ -L $p ]; then
ln -s $build $p
fi
fi

unset GIT_DIR # unset git dir or it will mess with goinstall
cd $p
if test -e $build/Godeps
then
echo "-----> Running: godep go build -o $(basename $name)"
godep go build -o $(basename $name)
else
echo "-----> Running: go get -d -v"
go get -d -v
fi
13 changes: 13 additions & 0 deletions buildpacks/golang/bin/detect
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash
# usage: bin/detect <build-dir>

# From https://github.com/kr/heroku-buildpack-go/blob/master/bin/detect

set -eo pipefail

if test -n "$(find "$1" -type f -name '*.go' | sed 1q)"; then
echo Go
else
echo no
exit 1
fi
Loading

0 comments on commit 4e653a9

Please sign in to comment.