Skip to content

Instantly share code, notes, and snippets.

@allex
Last active June 28, 2023 15:44
Show Gist options
  • Save allex/7dd3f33104c2c0c01e7a666ef412e0fa to your computer and use it in GitHub Desktop.
Save allex/7dd3f33104c2c0c01e7a666ef412e0fa to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -eu
# buildbox-cli agent
# by @allex <iallex.com> | MIT
#
# Install
# curl -sSLf https://git.io/fpWaM -o /usr/sbin/buildbox-cli-agent && chmod +x /usr/sbin/buildbox-cli-agent
#
# GistID: 7dd3f33104c2c0c01e7a666ef412e0fa
# GistURL: https://gist.github.com/7dd3f33104c2c0c01e7a666ef412e0fa
PROG=$(basename "$0")
SH_DIR="$(cd -P -- "$(dirname -- "$(readlink -f "$0")")" && pwd -P)"
command=
tput () { command tput "$@" 2>/dev/null || echo ''; }
setaf () { tput setaf "$1"; }
c_cls="$(tput sgr0)"
info () { printf "$(setaf 6)> %b${c_cls}\n" "$*"; }
error() { printf "$(setaf 1)x %b${c_cls}\n" "$*" >&2; }
success() { printf "$(setaf 2)✓ %b${c_cls}\n" "$*"; }
die() { [ "${1-}" ] && error "fatal: ${1}"; exit "${2-1}"; }
usage () {
cat <<-HELP
USAGE:
${PROG} command [options] [arguments...]
COMMAND
build-cli [--with-privilege-ssh-agent] [--privilege-ssh-key] -- <cmd> [opts...] # run buildbox cli with sub-command
HELP
exit 1
}
get_curr_docker_host () {
docker context inspect "$(docker context show)" -f '{{.Endpoints.docker.Host}}' 2>/dev/null
}
run_docker () {
BUILD_BOX_IMAGE=${BUILD_BOX_IMAGE:-harbor.tidu.io/tdio/buildbox:1.3.0}
## build docker parameters: env, volume, tty, etc,.
CLI_ARGS="--rm"
# > add DOCKER_HOST
DOCKER_HOST="${DOCKER_HOST:-$(get_curr_docker_host)}"
if [ -z "$DOCKER_HOST" ]; then
die "get docker host \$DOCKER_HOST faild."
fi
CLI_ARGS="${CLI_ARGS} -e DOCKER_HOST=${DOCKER_HOST}"
if [ -z "${DOCKER_HOST##unix://*}" ]; then
# binding volume with docker sock additionally
sockfile=${DOCKER_HOST#unix://}
if [ -S "$sockfile" ]; then
CLI_ARGS="${CLI_ARGS} -v ${sockfile}:${sockfile}"
else
warn "$sockfile not a valid docker sock binding"
fi
fi
# > add SSH_AUTH_SOCK
if [ -S "${SSH_AUTH_SOCK-}" ]; then
CLI_ARGS="${CLI_ARGS} -e SSH_AUTH_SOCK=${SSH_AUTH_SOCK} -v ${SSH_AUTH_SOCK}:${SSH_AUTH_SOCK}"
fi
# > add SSH_KEY_FILE
if [ -r "${SSH_KEY_FILE-}" ]; then
CLI_ARGS="${CLI_ARGS} -e SSH_KEY_FILE=${SSH_KEY_FILE} -v ${SSH_KEY_FILE}:${SSH_KEY_FILE}"
fi
if [ -t 0 ]; then
CLI_ARGS="${CLI_ARGS} -t"
fi
# shellcheck disable=SC2086
docker run --name $$ \
-e CI=true \
-v "${PWD}":/builder \
--mount type=bind,source="$HOME"/.docker,target=/root/.docker \
-i \
${CLI_ARGS} \
"${BUILD_BOX_IMAGE}" "$@"
}
ssh_agent_add () {
for k; do
ssh-add "$k" >/dev/null 2>&1
done
}
trap on_exit 0 1 2 3 6 15
on_exit () {
if [ -n "${SSH_AGENT_PID-}" ] ; then
info "trying to cleanup ssh-agent sock ..."
eval "$(ssh-agent -k)"
fi
}
# Init privilege ssh-agent, export envs: ${SSH_AUTH_SOCK}, ${SSH_AGENT_PID}
#
# NOTE: DO NOT use `exec <command>` when ssh-agent is running. (may causing the process hang up)
ssh_agent_init () {
ssh_key=${1?invalid ssh key}
sockfile="${SSH_AUTH_SOCK-}"
excode=0
ssh-add -l >/dev/null >/dev/null 2>&1 || { excode=$?; }
if [ $excode -eq 2 ]; then
[ -f "$sockfile" ] && rm -f "$sockfile"
sockfile=/tmp/ssh-agent.$$
eval "$(umask 066; ssh-agent -t 24h -a $sockfile -s)" >/dev/null
info "init ssh-agent: ${SSH_AGENT_PID:-${SSH_AUTH_SOCK}}"
fi
if [ -r "$ssh_key" ]; then
if ! ssh-add -t 4h "$ssh_key" &>/dev/null; then
die "adding the ssh key failed, please check if ssh-agent is alive"
fi
success "add ssh key done."
else
die "invalid ssh key file"
fi
}
cmd_build_cli () {
with_ssh_agent=
ssh_key=
while [ $# != 0 ]
do
case "$1" in
--with-privilege-ssh-agent)
with_ssh_agent=1
;;
--privilege-ssh-key | --privilege-ssh-key=*)
if [ "${1##*=}" = "$1" ]; then
shift
ssh_key=${1-}
else
ssh_key=${1##*=}
fi
;;
-h | --help)
usage
;;
# use -- as builtin `build-cli` parameters indicator
--)
shift
break
;;
esac
shift
done
if [ -n "$with_ssh_agent" ]; then
ssh_agent_init "$ssh_key"
fi
# ensure run the CLI within a buildbox container
if [ -n "${BUILDBOX_VERSION-}" ] && command -v build-cli >/dev/null 2>&1; then
build-cli "$@"
fi
run_docker "$@"
}
while [ $# != 0 ] && [ -z "$command" ]
do
case "$1" in
--) break ;;
-*) usage ;;
*) command="$1" ;;
esac
shift
done
if [ -z "$command" ]; then
usage
else
"cmd_${command//-/_}" "$@"
fi
# vim: set ft=sh fdm=marker ts=2 sw=2 sts=2 tw=85 et:
# nginx configure (by @allex wang)
# Last Modified: Tue Nov 20, 2018 09:37
# GistID: 7dd3f33104c2c0c01e7a666ef412e0fa
# https://git.io/fpWaM
rm -rf $(dirname $0)/../openssl-1.0.2p/.openssl/
# tell nginx's build system where to find LuaJIT 2.1:
export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.1
configure --prefix=/usr/local/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/tmp/nginx/body \
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid \
--with-ld-opt=-L/usr/local/pcre/lib --with-cc-opt=-I/usr/local/pcre/include \
--with-http_ssl_module --with-openssl=$PWD/../openssl-1.0.2p/.openssl/ \
--with-http_stub_status_module \
--with-http_v2_module \
--add-module=../nginx-http-concat \
--add-dynamic-module=../echo-nginx-module \
--add-dynamic-module=../ngx_devel_kit-0.3.0 \
--add-dynamic-module=../lua-nginx-module-0.10.13 \
--with-stream=dynamic
# patchs for openssl
sed -i"" -e "s#/\.openssl//\.openssl#/.openssl#g" -e "s#nginx-1\.14\.1/\.\./openssl-1\.0\.2p#openssl-1.0.2p#g" objs/Makefile
# cat objs/Makefile
#/Users/allex/local/src/openssl-1.0.2p/.openssl/include/openssl/ssl.h: objs/Makefile
# cd /Users/allex/local/src/openssl-1.0.2p \
# && if [ -f Makefile ]; then $(MAKE) clean; fi \
# && ./Configure darwin64-x86_64-cc --prefix=/Users/allex/local/src/openssl-1.0.2p/.openssl no-shared no-threads \
# && $(MAKE) \
# && $(MAKE) install_sw LIBDIR=lib
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment