client: lib_gen: rewrite/fix global env gen
- Fixes resetting of globals after sourcing custom env file (location) - Reorganizes flow logic - Related refactoring - Clarifies comments - Adds debug output
This commit is contained in:
@@ -53,87 +53,69 @@ fi
|
|||||||
#
|
#
|
||||||
# "Constructor" for environment generation
|
# "Constructor" for environment generation
|
||||||
#
|
#
|
||||||
# Sets client-side environment with defaults or use existing environment
|
# 1. Sets client-side environment with defaults or use existing environment
|
||||||
|
# 2. If configured, resets to alternative environment configuration after bootstrap
|
||||||
#
|
#
|
||||||
# NOTE: some bootstrapped defaults are ignored by environment file (as seen below)
|
# NOTE: some bootstrapped defaults are ignored by environment file (as seen below)
|
||||||
#
|
#
|
||||||
|
|
||||||
# TODO: further refactoring; make clearer transient env vars versus persistent env vars
|
|
||||||
function lib_gen::gen()
|
function lib_gen::gen()
|
||||||
{
|
{
|
||||||
lib_utils::print_debug "Constructing environment"
|
[ -z "$DOCKER_FINANCE_CLIENT_REPO" ] && lib_utils::die_fatal
|
||||||
|
|
||||||
|
# NOTE: global_* *MUST* be reset after sourcing new env file
|
||||||
|
|
||||||
|
#
|
||||||
# Generate `docker-finance` version
|
# Generate `docker-finance` version
|
||||||
|
#
|
||||||
|
|
||||||
global_repo_manifest="${DOCKER_FINANCE_CLIENT_REPO}/client/docker-finance.yaml"
|
global_repo_manifest="${DOCKER_FINANCE_CLIENT_REPO}/client/docker-finance.yaml"
|
||||||
declare -gr global_repo_manifest
|
declare -g global_repo_manifest
|
||||||
|
|
||||||
global_client_version="$(grep '^version: ' $global_repo_manifest | sed -e 's/version: "//' -e 's/"//g')"
|
global_client_version="$(grep '^version: ' $global_repo_manifest | sed -e 's/version: "//' -e 's/"//g')"
|
||||||
# shellcheck disable=SC2034 # used during env gen
|
# shellcheck disable=SC2034 # used during env gen
|
||||||
declare -gr global_client_version
|
declare -g global_client_version
|
||||||
|
|
||||||
# Repository-provided (not user-defined) default environment
|
|
||||||
global_repo_conf_dir="${DOCKER_FINANCE_CLIENT_REPO}/client/docker-finance.d"
|
|
||||||
declare -gr global_repo_conf_dir
|
|
||||||
|
|
||||||
# Environment
|
|
||||||
declare -gr global_repo_env_file="${global_repo_conf_dir}/client/env/gen.bash"
|
|
||||||
[ ! -f "$global_repo_env_file" ] \
|
|
||||||
&& lib_utils::die_fatal "Missing environment defaults! ($global_repo_env_file)"
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Dockerfiles
|
# If empty environment:
|
||||||
|
#
|
||||||
|
# 1. Poke at possible (default) location of end-user environment file
|
||||||
|
# a. If found, point to new location and read/reset from there
|
||||||
|
#
|
||||||
|
# 2. If file not found, bootstrap with defaults
|
||||||
#
|
#
|
||||||
|
|
||||||
# Set image type
|
# Environment (end-user)
|
||||||
[ -z "$global_platform" ] && lib_utils::die_fatal
|
global_conf_filename="${USER}@$(uname -n)"
|
||||||
case "$global_platform" in
|
declare -g global_conf_filename
|
||||||
archlinux | ubuntu)
|
|
||||||
global_platform_image="finance"
|
|
||||||
;;
|
|
||||||
dev-tools)
|
|
||||||
global_platform_image="dev-tools"
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
lib_utils::die_fatal "unsupported platform"
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
declare -gr global_platform_image
|
|
||||||
|
|
||||||
# Base location of Docker-related files (.in and final out files)
|
# Environment (end-user) tag dir (not full path)
|
||||||
global_repo_dockerfiles="${DOCKER_FINANCE_CLIENT_REPO}/client/Dockerfiles/${global_platform_image}"
|
[ -z "$global_tag" ] && lib_utils::die_fatal
|
||||||
# shellcheck disable=SC2034 # used in lib_docker
|
global_tag_dir="client/$(uname -s)-$(uname -m)/${global_platform}/${global_tag}"
|
||||||
declare -gr global_repo_dockerfiles
|
declare -g global_tag_dir
|
||||||
|
|
||||||
# Base custom end-user .in Dockerfile (to be appended to final Dockerfile after installation)
|
local _env_dir="/home/${USER}/.config/docker-finance.d/${global_tag_dir}/env" # NOTE: keep aligned with gen.bash
|
||||||
declare -gr global_repo_custom_dockerfile="${global_repo_conf_dir}/client/Dockerfiles/${global_platform_image}/Dockerfile.${global_platform}.in"
|
local _env_file="${_env_dir}/${global_conf_filename}"
|
||||||
[ ! -f "$global_repo_custom_dockerfile" ] \
|
|
||||||
&& lib_utils::die_fatal "Missing default custom Dockerfile '${global_repo_custom_dockerfile}'"
|
|
||||||
|
|
||||||
#
|
# shellcheck source=/dev/null
|
||||||
# If empty environment, bootstrap with defaults
|
[ -f "$_env_file" ] && source "$_env_file"
|
||||||
#
|
|
||||||
|
|
||||||
if [ -z "$DOCKER_FINANCE_CLIENT_CONF" ]; then
|
if [ -z "$DOCKER_FINANCE_CLIENT_CONF" ]; then
|
||||||
# shellcheck source=/dev/null
|
# shellcheck source=/dev/null
|
||||||
source "$global_repo_env_file"
|
source "${DOCKER_FINANCE_CLIENT_REPO}/client/docker-finance.d/client/env/gen.bash"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
[ -z "$DOCKER_FINANCE_CLIENT_CONF" ] \
|
[ -z "$DOCKER_FINANCE_CLIENT_CONF" ] \
|
||||||
&& lib_utils::die_fatal "Defaults not generated! (${global_repo_env_file})"
|
&& lib_utils::die_fatal "Defaults not generated! (${global_repo_env_file})"
|
||||||
|
|
||||||
[ -z "$DOCKER_FINANCE_USER" ] && lib_utils::die_fatal
|
|
||||||
global_conf_filename="${DOCKER_FINANCE_USER}@$(uname -n)"
|
|
||||||
declare -gr global_conf_filename
|
|
||||||
|
|
||||||
lib_gen::__set_client_globals
|
lib_gen::__set_client_globals
|
||||||
|
|
||||||
#
|
#
|
||||||
# Reset environment with user-provided (user-defined) existing file (if available)
|
# Reset environment with user-provided (user-defined) existing file (if available)
|
||||||
#
|
#
|
||||||
|
|
||||||
[ -z "$global_tag" ] && lib_utils::die_fatal
|
_env_dir="${DOCKER_FINANCE_CLIENT_CONF}/${global_tag_dir}/env"
|
||||||
local -r _env_dir="${DOCKER_FINANCE_CLIENT_CONF}/client/$(uname -s)-$(uname -m)/${global_platform}/${global_tag}/env"
|
_env_file="${_env_dir}/${global_conf_filename}"
|
||||||
local -r _env_file="${_env_dir}/${global_conf_filename}"
|
|
||||||
|
|
||||||
if [ -f "$_env_file" ]; then
|
if [ -f "$_env_file" ]; then
|
||||||
if [ -s "$_env_file" ]; then
|
if [ -s "$_env_file" ]; then
|
||||||
@@ -167,22 +149,93 @@ function lib_gen::gen()
|
|||||||
|
|
||||||
function lib_gen::__set_client_globals()
|
function lib_gen::__set_client_globals()
|
||||||
{
|
{
|
||||||
lib_utils::print_debug "Setting client globals"
|
lib_utils::print_debug "Setting (or resetting) client globals"
|
||||||
|
|
||||||
# Appending all client-side conf paths
|
#
|
||||||
local _client_tag_path
|
# Repository env
|
||||||
_client_tag_path="${DOCKER_FINANCE_CLIENT_CONF}/client/$(uname -s)-$(uname -m)/${global_platform}/${global_tag}"
|
#
|
||||||
|
|
||||||
# Client-side environment file (if available)
|
[ -z "$DOCKER_FINANCE_CLIENT_REPO" ] && lib_utils::die_fatal
|
||||||
local _client_env_dir="${_client_tag_path}/env"
|
|
||||||
|
# Generate `docker-finance` version
|
||||||
|
global_repo_manifest="${DOCKER_FINANCE_CLIENT_REPO}/client/docker-finance.yaml"
|
||||||
|
declare -g global_repo_manifest
|
||||||
|
lib_utils::print_debug "global_repo_manifest=${global_repo_manifest}"
|
||||||
|
|
||||||
|
global_client_version="$(grep '^version: ' $global_repo_manifest | sed -e 's/version: "//' -e 's/"//g')"
|
||||||
|
# shellcheck disable=SC2034 # used during env gen
|
||||||
|
declare -g global_client_version
|
||||||
|
lib_utils::print_debug "global_client_version=${global_client_version}"
|
||||||
|
|
||||||
|
# This does not reset when reading env; export again
|
||||||
|
export DOCKER_FINANCE_VERSION="$global_client_version"
|
||||||
|
lib_utils::print_debug "DOCKER_FINANCE_VERSION=${DOCKER_FINANCE_VERSION}"
|
||||||
|
|
||||||
|
# Repository-provided (not user-defined) default environment
|
||||||
|
global_repo_conf_dir="${DOCKER_FINANCE_CLIENT_REPO}/client/docker-finance.d"
|
||||||
|
declare -g global_repo_conf_dir
|
||||||
|
lib_utils::print_debug "global_repo_conf_dir=${global_repo_conf_dir}"
|
||||||
|
|
||||||
|
# Environment (repo)
|
||||||
|
declare -g global_repo_env_file="${global_repo_conf_dir}/client/env/gen.bash"
|
||||||
|
[ ! -f "$global_repo_env_file" ] \
|
||||||
|
&& lib_utils::die_fatal "Missing environment defaults! ($global_repo_env_file)"
|
||||||
|
lib_utils::print_debug "global_repo_env_file=${global_repo_env_file}"
|
||||||
|
|
||||||
|
#
|
||||||
|
# Repository env (Dockerfiles)
|
||||||
|
#
|
||||||
|
|
||||||
|
# Set image type
|
||||||
|
[ -z "$global_platform" ] && lib_utils::die_fatal
|
||||||
|
case "$global_platform" in
|
||||||
|
archlinux | ubuntu)
|
||||||
|
global_platform_image="finance"
|
||||||
|
;;
|
||||||
|
dev-tools)
|
||||||
|
global_platform_image="dev-tools"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
lib_utils::die_fatal "unsupported platform"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
declare -g global_platform_image
|
||||||
|
lib_utils::print_debug "global_platform_image=${global_platform_image}"
|
||||||
|
|
||||||
|
# Base location of Docker-related files (.in and final out files)
|
||||||
|
global_repo_dockerfiles="${DOCKER_FINANCE_CLIENT_REPO}/client/Dockerfiles/${global_platform_image}"
|
||||||
|
# shellcheck disable=SC2034 # used in lib_docker
|
||||||
|
declare -g global_repo_dockerfiles
|
||||||
|
lib_utils::print_debug "global_repo_dockerfiles=${global_repo_dockerfiles}"
|
||||||
|
|
||||||
|
# Base custom end-user .in Dockerfile (to be appended to final Dockerfile after installation)
|
||||||
|
declare -g global_repo_custom_dockerfile="${global_repo_conf_dir}/client/Dockerfiles/${global_platform_image}/Dockerfile.${global_platform}.in"
|
||||||
|
[ ! -f "$global_repo_custom_dockerfile" ] \
|
||||||
|
&& lib_utils::die_fatal "Missing default custom Dockerfile '${global_repo_custom_dockerfile}'"
|
||||||
|
lib_utils::print_debug "global_repo_custom_dockerfile=${global_repo_custom_dockerfile}"
|
||||||
|
|
||||||
|
#
|
||||||
|
# Client-side env
|
||||||
|
#
|
||||||
|
|
||||||
|
[ -z "$global_tag_dir" ] && lib_utils::die_fatal
|
||||||
|
|
||||||
|
# Environment (end-user) format
|
||||||
|
[ -z "$DOCKER_FINANCE_USER" ] && lib_utils::die_fatal
|
||||||
|
global_conf_filename="${DOCKER_FINANCE_USER}@$(uname -n)"
|
||||||
|
declare -g global_conf_filename
|
||||||
|
lib_utils::print_debug "global_conf_filename=${global_conf_filename}"
|
||||||
|
|
||||||
|
# Environment file (if available)
|
||||||
|
local _client_env_dir="${DOCKER_FINANCE_CLIENT_CONF}/${global_tag_dir}/env"
|
||||||
[ ! -d "$_client_env_dir" ] && mkdir -p "$_client_env_dir"
|
[ ! -d "$_client_env_dir" ] && mkdir -p "$_client_env_dir"
|
||||||
global_env_file="${_client_env_dir}/${global_conf_filename}"
|
global_env_file="${_client_env_dir}/${global_conf_filename}"
|
||||||
lib_utils::print_debug "global_env_file=${global_env_file}"
|
lib_utils::print_debug "global_env_file=${global_env_file}"
|
||||||
|
|
||||||
# Client-side custom Dockerfile (if available)
|
# Custom Dockerfile (if available)
|
||||||
local _client_dockerfile_dir="${_client_tag_path}/Dockerfiles"
|
local _client_dockerfile_dir="${global_tag_dir}/Dockerfiles"
|
||||||
[ ! -d "$_client_dockerfile_dir" ] && mkdir -p "$_client_dockerfile_dir"
|
[ ! -d "$_client_dockerfile_dir" ] && mkdir -p "$_client_dockerfile_dir"
|
||||||
global_custom_dockerfile="${_client_dockerfile_dir}/${global_conf_filename}"
|
global_custom_dockerfile="${DOCKER_FINANCE_CLIENT_CONF}/${_client_dockerfile_dir}/${global_conf_filename}"
|
||||||
lib_utils::print_debug "global_custom_dockerfile=${global_custom_dockerfile}"
|
lib_utils::print_debug "global_custom_dockerfile=${global_custom_dockerfile}"
|
||||||
|
|
||||||
# NOTE:
|
# NOTE:
|
||||||
@@ -200,7 +253,6 @@ function lib_gen::__set_client_globals()
|
|||||||
lib_utils::print_debug "global_shell_file=${global_shell_file}"
|
lib_utils::print_debug "global_shell_file=${global_shell_file}"
|
||||||
|
|
||||||
# Client view of client portion of repository
|
# Client view of client portion of repository
|
||||||
[ -z "$DOCKER_FINANCE_CLIENT_REPO" ] && lib_utils::die_fatal
|
|
||||||
global_repo_client="${DOCKER_FINANCE_CLIENT_REPO}/client"
|
global_repo_client="${DOCKER_FINANCE_CLIENT_REPO}/client"
|
||||||
[ ! -d "$global_repo_client" ] && lib_utils::die_fatal "Repository '${global_repo_client}' not found!"
|
[ ! -d "$global_repo_client" ] && lib_utils::die_fatal "Repository '${global_repo_client}' not found!"
|
||||||
lib_utils::print_debug "global_repo_client=${global_repo_client}"
|
lib_utils::print_debug "global_repo_client=${global_repo_client}"
|
||||||
|
|||||||
Reference in New Issue
Block a user