Skip to content

Instantly share code, notes, and snippets.

@benwilber
Created September 13, 2015 06:53
Show Gist options
  • Save benwilber/8ef62ceb3c312547cfaf to your computer and use it in GitHub Desktop.
Save benwilber/8ef62ceb3c312547cfaf to your computer and use it in GitHub Desktop.
generate self-contained, executable app archives
#!/bin/bash
trim() {
local var="$*"
var="${var#"${var%%[![:space:]]*}"}"
echo -n "${var%"${var##*[![:space:]]}"}"
}
parseprocfile() {
local IFS=":"
while read -r name cmd; do
echo "$(trim "$name")" "$(trim "$cmd")"
done <<< "$(grep -v "^\s*$" "$1")"
}
writehead() {
cat << "EOF" > "$1"
#!/bin/bash
pushd "$(dirname $0)" > /dev/null
dir="$(pwd)"
bin="$dir/$(basename $0)"
popd > /dev/null
end="$(awk '/^ENDSCRIPT/ { print NR + 1; exit 0; }' "$bin")"
EOF
}
writecmds() {
echo 'case "$1" in' >> "$1"
while read -r name cmd; do
echo " \"$name\") exec $cmd;;" >> "$1"
done <<< "$(parseprocfile Procfile)"
echo ' *) echo "unknown command: \"$1\"" >&2;;' >> "$1"
echo 'esac' >> "$1"
}
writebody() {
cat << "EOF" >> "$1"
rm -rf extract
mkdir extract
tail -n +"$end" "$bin" | tar -zx -C extract
test -d extract/.venv && source extract/.venv/bin/activate
EOF
writecmds "$1"
echo 'exit 1' >> "$1"
}
writetail() {
echo "ENDSCRIPT" >> "$2"
git archive "$1" | gzip >> "$2"
}
out="run"
ref="HEAD"
while getopts "o:r:" opt; do
case "$opt" in
o) out="$OPTARG";;
r) ref="$OPTARG";;
esac
done
shift $(( $OPTIND - 1 ))
dir="${1:-.}"
pushd "$dir" > /dev/null
writehead "$out"
writebody "$out"
writetail "$ref" "$out"
chmod +x "$out"
popd > /dev/null
$ slugger -o run .
$ PORT=5678 ./run web
[2015-09-13 02:50:32 -0400] [24411] [INFO] Starting gunicorn 19.3.0
[2015-09-13 02:50:32 -0400] [24411] [INFO] Listening at: http://127.0.0.1:5678 (24411)
[2015-09-13 02:50:32 -0400] [24411] [INFO] Using worker: sync
[2015-09-13 02:50:32 -0400] [24426] [INFO] Booting worker with pid: 24426
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment