Skip to content

Instantly share code, notes, and snippets.

@autumnjolitz
Last active June 15, 2025 07:40
Show Gist options
  • Save autumnjolitz/ffe4591dad74ff017e8dc444924dd311 to your computer and use it in GitHub Desktop.
Save autumnjolitz/ffe4591dad74ff017e8dc444924dd311 to your computer and use it in GitHub Desktop.
Used to make a graphite install suitable for nullfs mounting
#!/usr/bin/env sh
set -eu
set -o pipefail || true
# requires:
# pkg install py311-sqlite3
perror () {
>&2 printf "[$(date -Is)] %s\n" "$@"
}
GRAPHITE_USER=www
PYTHON=python3.11
OPWD="$PWD"
DEFAULT_PREFIX=graphite.$(date +%Y-%m-%d)
while [ $# -gt 0 ]; do
case "$1" in
--help|-h)
perror '--prefix? lol'
exit 0
;;
--prefix)
shift
if [ "x${1}" = x ]; then
perror 'empty prefix?!'
exit 94
fi
PREFIX="$1"
shift
;;
-*)
perror 'unknown argument '"$1"
exit 9
;;
*)
perror 'idk'
exit 4
;;
esac
done
PREFIX="${PREFIX:-${DEFAULT_PREFIX}}"
perror "PREFIX=$PREFIX"
_method=mv
if [ -d $PREFIX ]; then
perror 'WARNING: '"$PREFIX"' already exists! using cpdup instead...'
_method=cpdup
fi
install_sqlite3() {
if ! $PYTHON -c 'import sqlite3'; then
perror 'run this first!
pkg install py311-sqlite3'
exit 2
fi
}
graphite_pip_install() {
>pip.log 2>&1 eval "$PIP" install "${GRAPHITE_INSTALL_EXTRA}" "$@"
local rc="$?"
if [ $rc -ne 0 ]; then
perror 'error in installing '"$@"'!'
tail -200 pip.log
fi
rm -f pip.log
return $rc
}
cleanup () {
cd "$OPWD"
local temp_home="${TEMP_HOME:-}"
if [ x != "x${temp_home:-}" -a -e "${temp_home:-}" ]; then
perror "removing temp dir at ${temp_home}"
rm -rf "$temp_home"
fi
}
fixup_scripts () {
# fix up interpreter references
find . -type f \( -not -name "*.pyc" \) -perm +111 -exec grep -qFil "${TEMP_HOME}" {} ';' -print0 \
| xargs -0 ${PYTHON} -c '
import sys
from pathlib import Path
for filename in sys.argv[1:]:
with open(filename, "r") as fh:
content = list(fh)
with open(filename, "w") as fh:
fh.write("""#!/usr/bin/env sh
"exec" "$(dirname $0)/python" "$0" "$@"
# The above is necessary for a relative shebang
# original shebang as follows:
""")
for line in content:
fh.write(line)
'
}
trap cleanup EXIT
TEMP_HOME="$(mktemp -d)"
cd "$TEMP_HOME"
install_sqlite3
perror 'creating venv'
eval ${PYTHON} -m venv --copies --clear --upgrade-deps .
perror 'verifying venv'
PYTHON=./bin/python
PIP="${PYTHON} -m pip"
if ! "$PYTHON" -c "import os" ; then
perror 'error in venv!'
exit 2
fi
GRAPHITE_LIBDIR="$($PYTHON -c 'import sysconfig; print(sysconfig.get_paths()["purelib"].split("'$TEMP_HOME'/", 1)[1])')"
GRAPHITE_BINDIR=bin
GRAPHITE_ETCDIR=etc
GRAPHITE_INSTALL_EXTRA='--install-option="--install-scripts='"${GRAPHITE_BINDIR}"'" --install-option="--install-lib='"${GRAPHITE_LIBDIR}"'" --install-option="--install-data='"${GRAPHITE_ETCDIR}"'"'
GRAPHITE_INSTALL_EXTRA='--config-settings="'"${GRAPHITE_INSTALL_EXTRA}"'"'
perror "GRAPHITE_LIBDIR=$GRAPHITE_LIBDIR"
perror "GRAPHITE_BINDIR=$GRAPHITE_BINDIR"
perror "GRAPHITE_ETCDIR=$GRAPHITE_ETCDIR"
mkdir -p \
"$GRAPHITE_LIBDIR" \
"$GRAPHITE_BINDIR" \
"$GRAPHITE_ETCDIR" \
storage/log/webapp \
share
perror 'installing whisper'
graphite_pip_install 'https://github.com/graphite-project/whisper/tarball/master'
# perror 'installing ceres'
# graphite_pip_install 'https://github.com/graphite-project/ceres/tarball/master'
perror 'installing carbon'
graphite_pip_install 'https://github.com/graphite-project/carbon/tarball/master'
perror 'installing graphite-web'
graphite_pip_install 'https://github.com/graphite-project/graphite-web/tarball/master'
# for some reason it puts part of the app in $GRAPHITE_LIBDIR/opt/graphite/webapp !
if [ ! -d webapp/graphite ]; then
perror 'moving $GRAPHITE_LIBDIR/opt/graphite/webapp/graphite to ./webapp/graphite'
mv "${GRAPHITE_LIBDIR}/opt/graphite/webapp/graphite" ./webapp/graphite
fi
perror 'fixing up scripts to use relative venv'
fixup_scripts
perror 'freezing requirements.txt'
>requirements.txt $PIP freeze
perror 'adding default configurations'
for filename in $(echo conf/*.example)
do
cfile="conf/$(basename "$filename" .example)"
if [ ! -e "$cfile" ]; then
if ! >"$cfile" grep -vE '^$|^#' "$filename"; then
cp "$filename" "$cfile"
fi
fi
mv "$filename" "share/$(basename "$filename")"
done
perror 'creating webapp database'
PYTHONPATH=./webapp GRAPHITE_ROOT=. ./bin/django-admin migrate --settings=graphite.settings
chmod -R g+rx,o+rx "$TEMP_HOME"
chmod -R g+r,o+r "$TEMP_HOME"
(cd conf && ln -s graphite.wsgi wsgi.py)
>uwsgi.ini echo '
[uwsgi]
processes = 2
http-socket = 0.0.0.0:8081
gid = www
uid = www
virtualenv = /opt/graphite
chdir = /opt/graphite/conf
module = wsgi:application
static-map = /static=/opt/graphite/webapp/content
'
cd "$OPWD"
perror "Installing to $PREFIX"
eval "$_method" "$TEMP_HOME" "$PREFIX"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment