docker-finance | modern accounting for the power-user

Dedicated to Michael Morgan: a beautiful, beautiful soul.

---

Internal signing keys:

  Aaron Fiore (sole author)

    - 518A22F85BEFD32BCC99C48603F90C4F35E0213E
    - 31ECA5C347A0CC0815EDE730A3EACCFCDA7E685E
    - C8187C585CB07A4DA81CC0F37318B50EBE9C0DA8

Internal repositories (rebased from):

  Staging:

    $ git log -n1 --pretty=format:"%H"
    c8e0cd66f6c89fa7b3c62f72fb524a4cc454b7b6

    $ git rev-list --max-parents=0 HEAD
    ac3863b8c234755855f1aea3a07a853122decdf2

  Private:

    $ git log -n1 --pretty=format:"%H"
    69bb3591eaa2990a9637832bb484690e00c4f926

    $ git rev-list --max-parents=0 HEAD
    a5c1cc9fb593c4cf09bc0adfef6cb6d2964511ae
This commit is contained in:
2024-03-04 03:12:40 -08:00
commit b621e87df2
505 changed files with 37687 additions and 0 deletions

View File

@@ -0,0 +1,323 @@
#!/usr/bin/env bash
# docker-finance | modern accounting for the power-user
#
# Copyright (C) 2021-2024 Aaron Fiore (Founder, Evergreen Crypto LLC)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# "Libraries"
#
# Utilities (a container library (but container is never exposed to client code))
source "$(dirname "$(realpath -s $0)")/../container/src/finance/lib/internal/lib_utils.bash" || exit 1
#
# Implementation
#
# Dependencies
deps=("docker" "sed")
lib_utils::deps_check "${deps[@]}"
if ! docker compose version 1>/dev/null; then
lib_utils::die_fatal "Docker compose plugin not installed"
fi
if [ -z "$EDITOR" ]; then
editors=("vim" "vi" "emacs" "nano")
for editor in "${editors[@]}"; do
hash "$editor" &>/dev/null && export EDITOR="$editor" && break
done
if [ $? -ne 0 ]; then
lib_utils::die_fatal "Shell EDITOR is not set, export EDITOR in your shell"
fi
fi
#
# `docker compose`
#
function lib_docker::__docker_compose()
{
[ -z "$global_env_file" ] && lib_utils::die_fatal
[ -z "$global_dockerfile_path" ] && lib_utils::die_fatal
[ ! -f "$global_env_file" ] \
&& lib_utils::die_fatal "$global_env_file not found! Run the gen command!"
pushd "$global_dockerfile_path" 1>/dev/null \
|| return $? \
&& docker compose -f docker-compose.yml --env-file "$global_env_file" "$@"
popd 1>/dev/null || return $?
}
function lib_docker::__build()
{
time lib_docker::__docker_compose build --pull docker-finance "$@"
}
function lib_docker::__up()
{
# NOTE: for openvpn support, input this into /etc/docker/daemon.json and restart docker:
# {
# "default-address-pools" : [
# {
# "base" : "172.31.0.0/16",
# "size" : 24
# }
# ]
# }
lib_utils::print_debug "Bringing up network and container"
docker network create "$global_network" 2>/dev/null
lib_docker::__docker_compose up -d docker-finance "$@" \
&& docker exec -it "$global_container" /bin/bash
}
function lib_docker::__down()
{
[ -z "$global_network" ] && lib_utils::die_fatal
lib_utils::print_debug "Bringing down container and network"
lib_docker::__docker_compose down "$@" \
&& docker network rm "$global_network" 1>/dev/null
}
function lib_docker::__start()
{
lib_utils::print_debug "Starting container"
lib_docker::__docker_compose start "$@"
}
function lib_docker::__stop()
{
lib_utils::print_debug "Stopping container"
lib_docker::__docker_compose stop "$@"
}
function lib_docker::__backup()
{
# NOTE: does not export to tar, simply tags
local -r _backup="${global_image}:backup_$(date +%s)"
if docker tag "${global_image}:${global_tag}" "$_backup"; then
lib_utils::print_info "Created backup: ${global_image}:${global_tag} -> $_backup"
else
lib_utils::die_fatal "Could not create backup -> $_backup"
fi
}
function lib_docker::__rm()
{
[ -z "$global_image" ] && lib_utils::die_fatal
[ -z "$global_tag" ] && lib_utils::die_fatal
lib_utils::print_debug "Removing image"
docker image rm "${global_image}:${global_tag}" "$@"
}
function lib_docker::__run()
{
[ -z "$global_network" ] && lib_utils::die_fatal
lib_utils::print_debug "Creating network"
docker network create "$global_network" &>/dev/null
# NOTE: runs bash as interactive so .bashrc doesn't `return` (sources/aliases are needed)
lib_utils::print_debug "Spawning container with command '$*'"
lib_docker::__docker_compose run -it --rm --entrypoint='/bin/bash -i -c' docker-finance "$@"
local -r _return=$?
lib_utils::print_debug "Removing network"
docker network rm "$global_network" &>/dev/null # Don't force, if in use
return $_return
}
function lib_docker::__shell()
{
[ -z "$global_usage" ] && lib_utils::die_fatal
[ -z "$global_arg_delim_2" ] && lib_utils::die_fatal
[ -z "$global_arg_delim_3" ] && lib_utils::die_fatal
[ -z "$global_user" ] && lib_utils::die_fatal
local -r _arg="$1"
local -r _usage="
\e[32mDescription:\e[0m
Open a shell as given container user
\e[32mUsage:\e[0m
$ $global_usage user${global_arg_delim_2}<user>
\e[32mArguments:\e[0m
Container user:
user${global_arg_delim_2}<${global_user}|root|...>
\e[32mExamples:\e[0m
\e[37;2m# Open shell as current user (no arg)\e[0m
$ $global_usage
\e[37;2m# Open shell as current user (with arg)\e[0m
$ $global_usage user${global_arg_delim_2}${global_user}
\e[37;2m# Open root shell\e[0m
$ $global_usage user${global_arg_delim_2}root
"
if [ $# -gt 1 ]; then
lib_utils::die_usage "$_usage"
fi
# Get/Set user
if [ ! -z "$_arg" ]; then
if [[ ! "$_arg" =~ ^user[s]?${global_arg_delim_2} ]]; then
lib_utils::die_usage "$_usage"
fi
# Parse key for value
local _key="${_arg%${global_arg_delim_2}*}"
local _len="$((${#_key} + 1))"
if [[ "$_key" =~ ^user[s]?$ ]]; then
local _arg_user="${_arg:${_len}}"
if [ -z "$_arg_user" ]; then
lib_utils::die_usage "$_usage"
fi
fi
else
# Use default user
local _arg_user="${global_user}"
fi
if [ -z "$_arg_user" ]; then
lib_utils::die_usage "$_usage"
fi
[ -z "$global_container" ] && lib_utils::die_fatal
lib_utils::print_debug "Spawning shell in container '${global_container}'"
docker exec -it --user "$_arg_user" --workdir / "$global_container" /bin/bash
}
function lib_docker::__edit()
{
[ -z "$global_usage" ] && lib_utils::die_fatal
[ -z "$global_arg_delim_2" ] && lib_utils::die_fatal
[ -z "$global_arg_delim_3" ] && lib_utils::die_fatal
local -r _usage="
\e[32mDescription:\e[0m
Edit client configuration files
\e[32mUsage:\e[0m
$ $global_usage type${global_arg_delim_2}<type>
\e[32mArguments:\e[0m
Configuration type:
type${global_arg_delim_2}<env|{shell|superscript}>
\e[32mExamples:\e[0m
\e[37;2m# Edit client/container environment variables\e[0m
$ $global_usage type${global_arg_delim_2}env
\e[37;2m# Edit client/container shell (superscript) \e[0m
$ $global_usage type${global_arg_delim_2}shell
\e[37;2m# Edit client/container shell (w/ alternate wording) and environment variables\e[0m
$ $global_usage type${global_arg_delim_2}superscript${global_arg_delim_3}env
"
if [ $# -ne 1 ]; then
lib_utils::die_usage "$_usage"
fi
# Get/Set type
local -r _arg="$1"
if [ ! -z "$_arg" ]; then
if [[ ! "$_arg" =~ ^type[s]?${global_arg_delim_2} ]]; then
lib_utils::die_usage "$_usage"
fi
# Parse key for value
local _key="${_arg%${global_arg_delim_2}*}"
local _len="$((${#_key} + 1))"
if [[ "$_key" =~ ^type[s]?$ ]]; then
local _arg_type="${_arg:${_len}}"
if [ -z "$_arg_type" ]; then
lib_utils::die_usage "$_usage"
fi
fi
fi
IFS="$global_arg_delim_3"
# Arg: type
if [ ! -z "$_arg_type" ]; then
read -ra _read <<<"$_arg_type"
for _type in "${_read[@]}"; do
if [[ ! "$_type" =~ ^env$|^shell$|^superscript$ ]]; then
lib_utils::die_usage "$_usage"
fi
done
readonly _edit_arg_type=("${_read[@]}")
fi
[ -z "$EDITOR" ] \
&& lib_utils::die_fatal "Export EDITOR to your preferred editor"
[ -z "$global_env_file" ] && lib_utils::die_fatal
[ ! -f "$global_env_file" ] \
&& lib_utils::die_fatal "Environment file now found"
[ -z "$global_shell_file" ] && lib_utils::die_fatal
[ ! -f "$global_shell_file" ] \
&& lib_utils::die_fatal "Shell (superscript) file now found"
# Run all files through one editor instance
local _paths=()
for _type in "${_edit_arg_type[@]}"; do
case "$_type" in
env)
_paths+=("$global_env_file")
;;
shell | superscript)
[ -z "$global_platform" ] && lib_utils::die_fatal
[[ "$global_platform" == "dev-tools" ]] \
&& lib_utils::die_fatal "Invalid platform, use finance image"
_paths+=("$global_shell_file")
;;
esac
done
# Execute
lib_utils::print_debug "Editing '${_paths[*]}'"
$EDITOR "${_paths[@]}"
}
# vim: sw=2 sts=2 si ai et