commit 3dba401c755db0882ecaf7e83cf7f97bb2b4d4e7 Author: garrettmills Date: Wed Feb 26 00:39:57 2020 -0600 Add libraries, deploys diff --git a/bootstrap.bash b/bootstrap.bash new file mode 100755 index 0000000..573410c --- /dev/null +++ b/bootstrap.bash @@ -0,0 +1,26 @@ +#!/bin/bash + +# The path to the libglm root. +LIBGLM_ROOT=${1:-~/.libglm} + +# Get the path of the source file for some libglm mod by name. +# $1 - the name of the mod +function libglm_mod { + source $LIBGLM_ROOT/lib/env.bash + source $LIBGLM_ROOT/lib/fs.bash + case $1 in + deploy|args|env|string|notify|fs) + echo $(fs_lib $1) + ;; + *) + echo "Error: no such mod name found: $1"; + ;; + esac + +} + +# Source the specified mod into the current shell. +# $1 - the name of the mod +function libglm_load { + . $(libglm_mod $1) +} diff --git a/deploys/edge/garrettmills.bash b/deploys/edge/garrettmills.bash new file mode 100755 index 0000000..8eb4635 --- /dev/null +++ b/deploys/edge/garrettmills.bash @@ -0,0 +1,12 @@ +#!/bin/bash -xe + +systemctl stop garrettmills + +cd /etc/glmdev/app/garrettmills-dev + +git pull + +yarn install + +systemctl start garrettmills + diff --git a/deploys/localhost.localdomain/test.bash b/deploys/localhost.localdomain/test.bash new file mode 100755 index 0000000..d293697 --- /dev/null +++ b/deploys/localhost.localdomain/test.bash @@ -0,0 +1,2 @@ +#!/bin/bash +echo "Test deployment worked!" diff --git a/lib/args.bash b/lib/args.bash new file mode 100755 index 0000000..7203dc7 --- /dev/null +++ b/lib/args.bash @@ -0,0 +1,69 @@ +#!/bin/bash + +# Search the given arguments for the value of the specified key +# $1 - the name of the argument - (e.g. "verbose") +# $2+ - arguments to parse for value - (e.g. some args --verbose=3 more args) +function args_get_long { + local want_key=$1; shift; + while [[ $# -gt 0 ]] + do + local key="$1" + case $key in + "--$want_key") + echo $2 + return + ;; + --$want_key=*) + echo "${key#*=}" + return + ;; + esac + shift + done +} + +# Search the given arguments for the value of the specified key +# $1 - the name of the argument's short form - (e.g. "v") +# $2+ - arguments to parse for value - (e.g. some args -v=3 more args) +function args_get_short { + local want_key=$1; shift; + while [[ $# -gt 0 ]] + do + local key="$1" + case $key in + "-$want_key") + echo $2 + return + ;; + -$want_key=*) + echo "${key#*=}" + return + ;; + esac + shift + done +} + +# Search the given arguments for the value of the specified key or short +# $1 - the long name of the argument - (e.g. "verbose") +# $2 - the short name of the argument - (e.g. "v") +# $3+ - arguments to parse for value - (e.g. some args -w shorts --and longs more args) +function args_get { + local want_long=$1; shift; + local want_short=$1; shift; + while [[ $# -gt 0 ]] + do + local key="$1" + case $key in + "--$want_long"|"-$want_short") + echo $2 + return + ;; + --$want_long=*|-$want_short=*) + echo "${key#*=}" + return + ;; + esac + shift + done +} diff --git a/lib/deploy.bash b/lib/deploy.bash new file mode 100755 index 0000000..3377cab --- /dev/null +++ b/lib/deploy.bash @@ -0,0 +1,15 @@ +#!/bin/bash + +# Get the path to the deploy script for this host by name. +# $1 - the name of the deploy script +function deploy_get { + source $(libglm_mod fs) + source $(libglm_mod env) + echo "$(fs_glm 'deploys' $LIBGLM_HOSTNAME $1).bash" +} + +# Execute the deploy script for this host by name. +# $1 - the name of the deploy script +function deploy_run { + bash -xe $(deploy_get $1) +} diff --git a/lib/env.bash b/lib/env.bash new file mode 100755 index 0000000..651a602 --- /dev/null +++ b/lib/env.bash @@ -0,0 +1,10 @@ +#!/bin/bash + +# Fully qualified URL to the Gotify server. Should not end with a /. +NOTIFY_URL="https://notify.garrettmills.dev" + +# App token to use for the Gotify server. +NOTIFY_TOKEN="A1dyzPhND7Bq7v_" + +# The current hostname +LIBGLM_HOSTNAME=$(hostname) diff --git a/lib/fs.bash b/lib/fs.bash new file mode 100755 index 0000000..d232395 --- /dev/null +++ b/lib/fs.bash @@ -0,0 +1,23 @@ +#!/bin/bash + +# Resolve the arguments into a path, joined by / chars. +# $@ - paths to join +function fs_resolve { + source "$LIBGLM_ROOT/lib/string.bash" + local SPLIT_PATH=$(string_join_array / $@) + echo $SPLIT_PATH +} + +# Resolve the path to a libglm BASH library. +# $@ - relative name of the lib - not ending in .bash +function fs_lib { + local SPLIT_PATH=$(fs_resolve $LIBGLM_ROOT/lib $@) + echo $SPLIT_PATH.bash +} + +# Resolve the path to a resource in the libglm root. +# $@ - relative paths to join +function fs_glm { + local SPLIT_PATH=$(fs_resolve $LIBGLM_ROOT $@) + echo $SPLIT_PATH +} diff --git a/lib/notify.bash b/lib/notify.bash new file mode 100755 index 0000000..65afd28 --- /dev/null +++ b/lib/notify.bash @@ -0,0 +1,15 @@ +#!/bin/bash + +# Send a notification through the Gotify gateway. +# $1 - the title of the message +# $2 - the body of the message +# [$3 = 5] - the message priority +# [$4 = $NOTIFY_TOKEN] - app token to use +function notify_push { + source $(libglm_mod env) + MSG_TITLE=$1 + MSG_BODY=$2 + MSG_TOKEN=${4:-$NOTIFY_TOKEN} + MSG_PRIORITY=${3:-5} + curl -X POST --data "title=$MSG_TITLE" --data "message=$MSG_BODY" --data "priority=$MSG_PRIORITY" "$NOTIFY_URL/message?token=$MSG_TOKEN" +} diff --git a/lib/string.bash b/lib/string.bash new file mode 100755 index 0000000..baa42fe --- /dev/null +++ b/lib/string.bash @@ -0,0 +1,8 @@ +#!/bin/bash + +# Joins all arguments by the delimiter. +# $1 - delimiter (e.g. ,) +# $2+ - strings to be joined by the delimeter +function string_join_array { + local IFS="$1"; shift; echo "$*"; +}