Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor namespaced variable names, add log and loader modules #32

Merged
49 changes: 24 additions & 25 deletions src/bootstrap.sh
Original file line number Diff line number Diff line change
@@ -1,44 +1,43 @@
#!/usr/bin/env bash

CICD_TOOLS_REPO_ORG="${CICD_TOOLS_REPO_ORG:-RedHatInsights}"
CICD_TOOLS_REPO_BRANCH="${CICD_TOOLS_REPO_BRANCH:-main}"
CICD_TOOLS_ROOTDIR="${CICD_TOOLS_ROOTDIR:-.cicd_tools}"
CICD_TOOLS_SCRIPTS_DIR="${CICD_TOOLS_ROOTDIR}/src"
CICD_TOOLS_SKIP_CLEANUP=${CICD_TOOLS_SKIP_CLEANUP:-}
CICD_BOOTSTRAP_REPO_ORG="${CICD_BOOTSTRAP_REPO_ORG:-RedHatInsights}"
CICD_BOOTSTRAP_REPO_BRANCH="${CICD_BOOTSTRAP_REPO_BRANCH:-main}"
CICD_BOOTSTRAP_ROOTDIR="${CICD_BOOTSTRAP_ROOTDIR:-.cicd_tools}"
CICD_BOOTSTRAP_SKIP_CLEANUP=${CICD_BOOTSTRAP_SKIP_CLEANUP:-}
CICD_BOOTSTRAP_SKIP_GIT_CLONE=${CICD_BOOTSTRAP_SKIP_GIT_CLONE:-}

clone_cicd_tools_repo() {
cicd::bootstrap::clone_cicd_tools_repo() {

if [ -d "${CICD_TOOLS_ROOTDIR}" ]; then
_delete_cicd_tools_rootdir
if [ -d "${CICD_BOOTSTRAP_ROOTDIR}" ]; then
cicd::bootstrap::_delete_rootdir
fi

git clone -q \
--branch "$CICD_TOOLS_REPO_BRANCH" \
"https://github.com/${CICD_TOOLS_REPO_ORG}/cicd-tools.git" "$CICD_TOOLS_ROOTDIR"
--branch "$CICD_BOOTSTRAP_REPO_BRANCH" \
"https://github.com/${CICD_BOOTSTRAP_REPO_ORG}/cicd-tools.git" "$CICD_BOOTSTRAP_ROOTDIR"
}

_delete_cicd_tools_rootdir() {
cicd::debug "Removing existing CICD tools directory: '${CICD_TOOLS_ROOTDIR}'"
rm -rf "${CICD_TOOLS_ROOTDIR}"
cicd::bootstrap::_delete_rootdir() {
cicd::log::debug "Removing existing CICD tools directory: '${CICD_BOOTSTRAP_ROOTDIR}'"
rm -rf "${CICD_BOOTSTRAP_ROOTDIR}"
}

cleanup() {
_delete_cicd_tools_rootdir
unset clone_cicd_tools_repo _delete_cicd_tools_rootdir cleanup
cicd::bootstrap::cleanup() {
cicd::bootstrap::_delete_rootdir
unset cicd::bootstrap::clone_cicd_tools_repo cicd::bootstrap::_delete_rootdir cicd::bootstrap::cleanup
unset CICD_BOOTSTRAP_REPO_ORG CICD_BOOTSTRAP_REPO_BRANCH CICD_BOOTSTRAP_ROOTDIR CICD_BOOTSTRAP_SKIP_CLEANUP CICD_BOOTSTRAP_SKIP_GIT_CLONE
}

if [ -z "$CICD_TOOLS_SKIP_GIT_CLONE" ]; then
if ! clone_cicd_tools_repo; then
if [ -z "$CICD_BOOTSTRAP_SKIP_GIT_CLONE" ]; then
if ! cicd::bootstrap::clone_cicd_tools_repo; then
echo "couldn't clone cicd-tools repository!"
exit 1
fi
fi

# shellcheck source=src/main.sh
source "$CICD_TOOLS_SCRIPTS_DIR/main.sh" "$@" || exit 1
if [ -z "$CICD_TOOLS_SKIP_CLEANUP" ]; then
if ! cleanup; then
echo "couldn't perform cicd tools cleanup!"
exit 1
fi
# shellcheck source=src/load_module.sh
source "$CICD_BOOTSTRAP_ROOTDIR/src/load_module.sh" "$@" || exit 1
if [[ -z "$CICD_BOOTSTRAP_SKIP_CLEANUP" ]] && ! cicd::bootstrap::cleanup; then
echo "couldn't perform cicd tools cleanup!"
exit 1
fi
16 changes: 16 additions & 0 deletions src/load_module.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

# https://stackoverflow.com/a/246128
if [ -z "$CICD_LOADER_SCRIPTS_DIR" ]; then
CICD_LOADER_SCRIPTS_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
fi

readonly CICD_LOADER_SCRIPTS_DIR

# shellcheck source=src/shared/loader.sh
if ! source "${CICD_LOADER_SCRIPTS_DIR}/shared/loader.sh"; then
echo "Error loading 'loader' module!"
exit 1
fi

cicd::loader::load_module "$1"
64 changes: 0 additions & 64 deletions src/main.sh

This file was deleted.

35 changes: 35 additions & 0 deletions src/shared/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash

# Common functions that are shared across the different modules

if [[ -n "$CICD_COMMON_MODULE_LOADED" ]]; then
cicd::log::debug "common module already loaded, skipping"
return 0
fi

if [[ -z "$CICD_LOADER_MODULE_LOADED" ]]; then
echo "loader module not found, please use 'load_module.sh' to load modules."
return 1
fi

cicd::log::debug "loading common module"

cicd::common::command_is_present() {
command -v "$1" >/dev/null 2>&1
}

cicd::common::get_7_chars_commit_hash() {
cicd::common::_get_n_chars_commit_hash 7
}

cicd::common::_get_n_chars_commit_hash() {
git rev-parse --short="$1" HEAD
}

cicd::common::is_ci_context() {
[[ "$CI" = "true" ]]
}

cicd::log::debug "common module loaded"

CICD_COMMON_MODULE_LOADED='true'
42 changes: 0 additions & 42 deletions src/shared/common_lib.sh

This file was deleted.

118 changes: 118 additions & 0 deletions src/shared/container.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#!/bin/bash

# container engine helper functions to handle both podman and docker commands

if [[ -n "$CICD_CONTAINER_MODULE_LOADED" ]]; then
cicd::log::debug "container engine module already loaded, skipping"
return 0
fi

if [[ -z "$CICD_LOADER_MODULE_LOADED" ]]; then
echo "loader module not found, please use 'load_module.sh' to load modules."
return 1
fi

cicd::log::debug "loading container module"

CICD_CONTAINER_ENGINE=''
CICD_CONTAINER_PREFER_ENGINE=${CICD_CONTAINER_PREFER_ENGINE:-}

cicd::container::cmd() {
"$CICD_CONTAINER_ENGINE" "$@"
}

cicd::container::_set_container_engine_cmd() {

if cicd::container::_preferred_container_engine_available; then
CICD_CONTAINER_ENGINE="$CICD_CONTAINER_PREFER_ENGINE"
else
if cicd::container::_container_engine_available 'podman'; then
CICD_CONTAINER_ENGINE='podman'
elif cicd::container::_container_engine_available 'docker'; then
CICD_CONTAINER_ENGINE='docker'
else
cicd::log::err "ERROR, no container engine found, please install either podman or docker first"
return 1
fi
fi

readonly CICD_CONTAINER_ENGINE

cicd::log::debug "Container engine selected: $CICD_CONTAINER_ENGINE"
}

cicd::container::_preferred_container_engine_available() {

local engine_available=1

if [ -n "$CICD_CONTAINER_PREFER_ENGINE" ]; then
if cicd::container::_container_engine_available "$CICD_CONTAINER_PREFER_ENGINE"; then
engine_available=0
else
cicd::log::info "WARNING: preferred container engine '${CICD_CONTAINER_PREFER_ENGINE}' not present, or isn't supported, finding alternative..."
fi
fi

return "$engine_available"
}

cicd::container::_container_engine_available() {

local cmd="$1"
local available=1

if cicd::container::_cmd_exists_and_is_supported "$cmd"; then
available=0
fi

return "$available"
}

cicd::container::_cmd_exists_and_is_supported() {

local cmd="$1"
local result=0

if cicd::container::_supported_container_engine "$cmd" && cicd::common::command_is_present "$cmd"; then
if [[ "$cmd" == 'docker' ]] && cicd::container::_docker_seems_emulated; then
cicd::log::info "WARNING: docker seems emulated, skipping."
result=1
fi
else
result=1
fi

return "$result"
}

cicd::container::_supported_container_engine() {

local engine_to_check="$1"

[ "$engine_to_check" = 'docker' ] ||
[ "$engine_to_check" = 'podman' ]
}

cicd::container::_docker_seems_emulated() {
[[ "$(docker 2>/dev/null --version)" =~ podman\ +version ]]
}

cicd::container::_podman_version_under_4_5_0() {
[ "$(echo -en "4.5.0\n$(_podman_version)" | sort -V | head -1)" != "4.5.0" ]
}

cicd::container::_module_setup() {

if ! cicd::container::_set_container_engine_cmd; then
cicd::log::err "Error configuring a container engine!"
return 1
fi
}

if ! cicd::container::_module_setup; then
cicd::log::err "container module setup failed!"
return 1
fi

cicd::log::debug "container module loaded"
CICD_CONTAINER_MODULE_LOADED='true'
Loading