#!/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 .
#
# "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}
\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}
\e[32mArguments:\e[0m
Configuration type:
type${global_arg_delim_2}
\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[@]}"
}
function lib_docker::__version()
{
[ -z "${DOCKER_FINANCE_VERSION}" ] && lib_utils::die_fatal
[ -z "${DOCKER_FINANCE_CLIENT_REPO}" ] && lib_utils::die_fatal
#
# `docker-finance` version
#
local _hash
if pushd "$DOCKER_FINANCE_CLIENT_REPO" 1>/dev/null; then
_hash="$(git log --pretty='format:%h' -n 1)"
popd 1>/dev/null || lib_utils::die_fatal
else
lib_utils::die_fatal
fi
echo
echo -e "docker-finance v${DOCKER_FINANCE_VERSION} | commit: $_hash"
echo -e "───────────────────────────────────────"
#
# Client dependencies
#
echo
echo "client.system:"
echo -e \\t"$(uname -rmo)"
echo -e \\t"$(bash --version | head -n1)"
echo -e \\t"$(sed --version | head -n1)"
echo
echo "client.docker:"
echo -e \\t"$(docker compose version)"
docker version \
| while read _line; do
echo -e \\t"${_line}"
done
#
# Container dependencies
#
[ -z "$global_platform" ] && lib_utils::die_fatal
# Feed dependency manifest (client is not bind-mounted for security reasons)
local _manifest
_manifest="$(<${DOCKER_FINANCE_CLIENT_REPO}/client/docker-finance.yaml)"
case "$global_platform" in
# NOTE: uses variable "callback"
archlinux)
_platform="$global_platform"
_image="finance"
_pkg="pacman -Q \$_package"
;;
ubuntu | dev-tools)
if [[ "$global_platform" =~ ^ubuntu$|^dev-tools$ ]]; then
_platform="ubuntu"
_image="finance"
[[ "$global_platform" == "dev-tools" ]] && _image="dev-tools"
fi
_pkg="dpkg -s \$_package | grep -E \"(^Package:|^Version:)\" | cut -d' ' -f2 | paste -s | awk '{print \$1 \" \" \$2}'"
;;
*)
lib_utils::die_fatal "unsupported platform"
;;
esac
lib_docker::__run "
echo '${_manifest}' \\
| shyaml keys container.${_platform}.${_image} \\
| while read _key; do
echo -e \\\ncontainer.${_platform}.${_image}.\${_key}:
echo '${_manifest}' | shyaml get-values container.${_platform}.${_image}.\${_key}.packages 2>/dev/null \\
| while read _package; do
echo -e \\\t\$($_pkg)
done
echo '${_manifest}' | shyaml get-values container.${_platform}.${_image}.\${_key}.commands 2>/dev/null \\
| while read _command; do
echo -e \\\t\$(\$_command)
done
done
echo"
return $?
}
# vim: sw=2 sts=2 si ai et