#! /usr/bin/env sh # defaults CD_DEFAULT_DNS="\ 9.9.9.9 \ " CD_PYTHON_MODULES="\ cd \ rwx \ " # main cd_main () { cd_set_environment_variables cd_set_packages_repositories cd_set_packages_configuration # cd_list_working_directory cd_set_https_verification_off cd_set_dns_resolving cd_update_packages_catalog cd_install_packages_tools cd_install_ca_certificates cd_write_ca_certificates cd_update_ca_certificates cd_set_https_verification_on cd_update_packages_catalog cd_upgrade_packages cd_install_git cd_install_python cd_clean_packages_cache cd_install_python_modules ${CD_PYTHON_MODULES} cd_execute_python_module ${CD_PYTHON_MODULES} } # steps cd_set_environment_variables () { cd_step "Set environment variables" # operating system CD_OS_ID="$(cd_grep_os ID)" case "${CD_OS_ID}" in "almalinux") CD_OS_ID="${CD_OS_ALMA}" ;; "alpine") CD_OS_ID="${CD_OS_ALPINE}" ;; "arch") CD_OS_ID="${CD_OS_ARCH}" ;; "debian") CD_OS_ID="${CD_OS_DEBIAN}" ;; "rocky") CD_OS_ID="${CD_OS_ROCKY}" ;; "ubuntu") CD_OS_ID="${CD_OS_UBUNTU}" ;; *) cd_error_os "CD_OS_ID" ;; esac case "${CD_OS_ID}" in "${CD_OS_ALMA}"|"${CD_OS_ARCH}"|"${CD_OS_ROCKY}") CD_OS_VERSION=$(cd_grep_os VERSION_ID \ | sed "s|^\([0-9]\+\)\..*|\1|") ;; "${CD_OS_ALPINE}") CD_OS_VERSION=$(cd_grep_os VERSION_ID \ | sed "s|^\([0-9]\+\.[0-9]\+\)\..*|\1|") ;; "${CD_OS_DEBIAN}"|"${CD_OS_UBUNTU}") CD_OS_VERSION="$(cd_grep_os VERSION_CODENAME)" ;; esac case "${CD_OS_ID}" in "${CD_OS_ALMA}"|"${CD_OS_ROCKY}") case "${CD_OS_VERSION}" in "8"|"9") ;; *) cd_error_os "CD_OS_VERSION" ;; esac ;; "${CD_OS_ALPINE}") case "${CD_OS_VERSION}" in "3.18"|"3.19") ;; *) cd_error_os "CD_OS_VERSION" ;; esac ;; "${CD_OS_ARCH}") case "${CD_OS_VERSION}" in "20231112"|"20240101") ;; *) cd_error_os "CD_OS_VERSION" ;; esac ;; "${CD_OS_DEBIAN}") case "${CD_OS_VERSION}" in "bookworm"|"bullseye") ;; *) cd_error_os "CD_OS_VERSION" ;; esac ;; "${CD_OS_UBUNTU}") case "${CD_OS_VERSION}" in "mantic"|"noble") ;; *) cd_error_os "CD_OS_VERSION" ;; esac ;; esac cd_echo "CD_OS_ID" "CD_OS_VERSION" # universal CD_DNS_FILE="/etc/resolv.conf" CD_PKG_GIT="git" cd_split cd_echo "CD_DNS_FILE" "CD_PKG_CA" "CD_PKG_GIT" # shared case "${CD_OS_ID}" in "${CD_OS_ALMA}"|"${CD_OS_ROCKY}") CD_CA_ROOT="/etc/pki/ca-trust/source/anchors" CD_CMD_CA="update-ca-trust" ;; "${CD_OS_ALPINE}"|"${CD_OS_DEBIAN}"|"${CD_OS_UBUNTU}") CD_CA_ROOT="/usr/local/share/ca-certificates" CD_CMD_CA="update-ca-certificates" ;; esac cd_split cd_echo "CD_CA_ROOT" "CD_CMD_CA" # common case "${CD_OS_ID}" in "${CD_OS_ALMA}"|"${CD_OS_ROCKY}") CD_CMD_CLEAN="dnf clean all" CD_CMD_INSTALL="dnf install --assumeyes" CD_CMD_QUERY="rpm --query" CD_CMD_UPDATE="dnf makecache" CD_CMD_UPGRADE="dnf upgrade --assumeyes" CD_PKG_CA="ca-certificates" CD_PKG_PKG="" CD_PM_CONF_PATH="/etc/dnf/dnf.conf" CD_PM_CONF_TEXT="\ [main] best=True clean_requirements_on_remove=True gpgcheck=1 installonly_limit=3 skip_if_unavailable=False " CD_PM_HTTPS_PATH="/etc/dnf/dnf.conf.d/https.conf" CD_PM_HTTPS_TEXT="sslverify=False" ;; "${CD_OS_ALPINE}") CD_CMD_CLEAN="apk cache purge" CD_CMD_INSTALL="apk add" CD_CMD_QUERY="apk info" CD_CMD_UPDATE="apk update" CD_CMD_UPGRADE="apk upgrade" CD_PKG_CA="ca-certificates-bundle" CD_PKG_PKG="" CD_PM_CONF_PATH="" CD_PM_CONF_TEXT="" CD_PM_HTTPS_PATH="/etc/apk/repositories.d/https" CD_PM_HTTPS_TEXT="--no-verify" ;; "${CD_OS_DEBIAN}"|"${CD_OS_UBUNTU}") CD_CMD_CLEAN="apt-get clean" CD_CMD_INSTALL="apt-get install --assume-yes" CD_CMD_QUERY="dpkg-query --show" CD_CMD_UPDATE="apt-get update" CD_CMD_UPGRADE="apt-get upgrade --assume-yes" CD_PKG_CA="ca-certificates" CD_PKG_PKG="apt-utils" CD_PM_CONF_PATH="/etc/apt/apt.conf.d/apt.conf" CD_PM_CONF_TEXT="\ Acquire::Check-Valid-Until True; APT::Get::Show-Versions True; APT::Install-Recommends False; APT::Install-Suggests False; Dir::Etc::SourceParts \"\"; " CD_PM_HTTPS_PATH="/etc/apt/apt.conf.d/https" CD_PM_HTTPS_TEXT="Acquire::https::Verify-Peer False;" ;; esac cd_split cd_echo "CD_CMD_CLEAN" \ "CD_CMD_INSTALL" "CD_CMD_QUERY" "CD_CMD_UPDATE" "CD_CMD_UPGRADE" cd_split cd_echo "CD_PKG_PKG" "CD_PM_CONF_PATH" "CD_PM_HTTPS_PATH" # specific case "${CD_OS_ID}" in "${CD_OS_ALMA}") CD_URL_DEFAULT="https://repo.almalinux.org/almalinux" ;; "${CD_OS_ALPINE}") CD_URL_DEFAULT="https://dl-cdn.alpinelinux.org/alpine" ;; "${CD_OS_DEBIAN}") CD_URL_DEFAULT="http://deb.debian.org/debian" ;; "${CD_OS_ROCKY}") CD_URL_DEFAULT="http://dl.rockylinux.org/\$contentdir" ;; "${CD_OS_UBUNTU}") CD_URL_DEFAULT="http://archive.ubuntu.com/ubuntu" ;; esac CD_URL_CHOSEN="${CD_URL_DEFAULT}" case "${CD_OS_ID}" in "${CD_OS_ALMA}") [ "${CD_URL_ALMA}" ] && CD_URL_CHOSEN="${CD_URL_ALMA}" ;; "${CD_OS_ALPINE}") [ "${CD_URL_ALPINE}" ] && CD_URL_CHOSEN="${CD_URL_ALPINE}" ;; "${CD_OS_DEBIAN}") [ "${CD_URL_DEBIAN}" ] && CD_URL_CHOSEN="${CD_URL_DEBIAN}" \ || CD_URL_CHOSEN="https://deb.debian.org/debian" ;; "${CD_OS_ROCKY}") [ "${CD_URL_ROCKY}" ] && CD_URL_CHOSEN="${CD_URL_ROCKY}" \ || CD_URL_CHOSEN="https://dl.rockylinux.org/\$contentdir" ;; "${CD_OS_UBUNTU}") [ "${CD_URL_UBUNTU}" ] && CD_URL_CHOSEN="${CD_URL_UBUNTU}" \ || CD_URL_CHOSEN="https://ubuntu.mirrors.ovh.net/ubuntu" ;; esac cd_split cd_echo "CD_URL_DEFAULT" "CD_URL_CHOSEN" # python case "${CD_OS_ID}" in "${CD_OS_ALMA}"|"${CD_OS_ROCKY}") CD_PYTHON_COMMAND="python3.11" CD_PYTHON_PACKAGE="python3.11" CD_PYTHON_PACKAGES="/usr/lib64/python3.11/site-packages" ;; "${CD_OS_ALPINE}") CD_PYTHON_COMMAND="python3.11" CD_PYTHON_PACKAGE="python3" CD_PYTHON_PACKAGES="/usr/lib/python3.11/site-packages" ;; "${CD_OS_DEBIAN}") case "${CD_OS_VERSION}" in "bookworm") CD_PYTHON_COMMAND="python3.11" ;; "bullseye") CD_PYTHON_COMMAND="python3.9" ;; esac CD_PYTHON_PACKAGE="python3" CD_PYTHON_PACKAGES="/usr/lib/python3/dist-packages" ;; "${CD_OS_UBUNTU}") case "${CD_OS_VERSION}" in "noble") CD_PYTHON_COMMAND="python3.12" ;; "mantic") CD_PYTHON_COMMAND="python3.11" ;; esac CD_PYTHON_PACKAGE="python3" CD_PYTHON_PACKAGES="/usr/lib/python3/dist-packages" ;; esac cd_split cd_echo "CD_PYTHON_COMMAND" "CD_PYTHON_PACKAGE" "CD_PYTHON_PACKAGES" # variables [ "${CD_CA_1}" ] && CD_CA=true # continuous integration platform if [ "${GITHUB_ACTIONS}" ] ; then # github → gitea → forgejo CD_SERVER_URL="${GITHUB_SERVER_URL}" CD_PROJECTS_GROUP="$(dirname "${GITHUB_REPOSITORY}")" CD_PROJECT_NAME="$(basename "${GITHUB_REPOSITORY}")" CD_PROJECT_BRANCH="${GITHUB_REF_NAME}" elif [ "${GITLAB_CI}" ] ; then # gitlab CD_SERVER_URL="${CI_SERVER_URL}" CD_PROJECTS_GROUP="$(dirname "${CI_PROJECT_PATH}")" CD_PROJECT_NAME="$(basename "${CI_PROJECT_PATH}")" CD_PROJECT_BRANCH="${CI_COMMIT_BRANCH}" else # unsupported cd_error_ci "ø" fi [ "${CD_SERVER_URL}" ] || cd_error_ci "CD_SERVER_URL" [ "${CD_PROJECTS_GROUP}" ] || cd_error_ci "CD_PROJECTS_GROUP" [ "${CD_PROJECT_NAME}" ] || cd_error_ci "CD_PROJECT_NAME" [ "${CD_PROJECT_BRANCH}" ] || cd_error_ci "CD_PROJECT_BRANCH" # CD_PROJECTS_URL="${CD_SERVER_URL}/${CD_PROJECTS_GROUP}" CD_PROJECT_URL="${CD_PROJECTS_URL}/${CD_PROJECT_NAME}" # cd_split cd_echo "CD_CA" cd_split cd_echo "CD_SERVER_URL" \ "CD_PROJECTS_GROUP" "CD_PROJECT_NAME" "CD_PROJECT_BRANCH" cd_split cd_echo "CD_PROJECTS_URL" "CD_PROJECT_URL" } cd_set_packages_repositories () { local expression local file cd_step "Set packages repositories" case "${CD_OS_ID}" in "${CD_OS_ALMA}") case "${CD_OS_VERSION}" in "8") file="/etc/yum.repos.d/almalinux.repo" ;; "9") file="/etc/yum.repos.d/almalinux-baseos.repo" ;; esac cd_sed "${file}" \ "|^mirrorlist|# mirrorlist|" \ "|${CD_URL_DEFAULT}|${CD_URL_CHOSEN}|" \ "|^# baseurl|baseurl|" ;; "${CD_OS_ALPINE}") file="/etc/apk/repositories" cd_write "${file}" "\ ${CD_URL_CHOSEN}/v${CD_OS_VERSION}/main ${CD_URL_CHOSEN}/v${CD_OS_VERSION}/community " ;; "${CD_OS_DEBIAN}") file="/etc/apt/sources.list" cd_write "${file}" "\ deb ${CD_URL_CHOSEN} ${CD_OS_VERSION} main deb ${CD_URL_CHOSEN} ${CD_OS_VERSION}-backports main deb ${CD_URL_CHOSEN} ${CD_OS_VERSION}-updates main deb ${CD_URL_CHOSEN}-security ${CD_OS_VERSION}-security main " ;; "${CD_OS_ROCKY}") case "${CD_OS_VERSION}" in "8") file="/etc/yum.repos.d/Rocky-BaseOS.repo" ;; "9") file="/etc/yum.repos.d/rocky.repo" ;; esac cd_sed "${file}" \ "|^mirrorlist|# mirrorlist|" \ "|${CD_URL_DEFAULT}|${CD_URL_CHOSEN}|" \ "|^#baseurl|baseurl|" ;; "${CD_OS_UBUNTU}") file="/etc/apt/sources.list" cd_write "${file}" "\ deb ${CD_URL_CHOSEN} ${CD_OS_VERSION} main deb ${CD_URL_CHOSEN} ${CD_OS_VERSION}-backports main deb ${CD_URL_CHOSEN} ${CD_OS_VERSION}-updates main deb ${CD_URL_CHOSEN} ${CD_OS_VERSION}-security main " ;; esac } cd_set_packages_configuration () { cd_step "Set packages configuration" cd_write "${CD_PM_CONF_PATH}" "${CD_PM_CONF_TEXT}" case "${CD_OS_ID}" in "${CD_OS_DEBIAN}"|"${CD_OS_UBUNTU}") export DEBIAN_FRONTEND="noninteractive" ;; esac } # agnostic steps cd_list_working_directory () { cd_step "List working directory" cd_ls "." } cd_set_https_verification_off () { # TODO if [ "${CD_CA}" -o "${CD_OS_ID}" = "${CD_OS_DEBIAN}" ] ; then cd_step "Set HTTPS verification off" cd_mkdir "$(dirname "${CD_PM_HTTPS_PATH}")" cd_write "${CD_PM_HTTPS_PATH}" "${CD_PM_HTTPS_TEXT}" fi } cd_set_dns_resolving () { local server local servers local text="" cd_step "Set DNS resolving" [ "${CD_DNS}" ] && servers="${CD_DNS}" || servers="${CD_DEFAULT_DNS}" for server in ${servers} ; do text="${text}nameserver ${server} " done cd_write "${CD_DNS_FILE}" "${text}" } cd_update_packages_catalog () { cd_step "Update packages catalog" ${CD_CMD_UPDATE} || exit } cd_install_packages_tools () { cd_step "Install packages tools" cd_install_package "${CD_PKG_PKG}" } cd_install_ca_certificates () { cd_step "Install CA" cd_install_package "${CD_PKG_CA}" } cd_write_ca_certificates () { local index local path local text cd_step "Write CA certificates" cd_mkdir "${CD_CA_ROOT}" index=1 eval "text=\"\${CD_CA_${index}}\"" while [ "${text}" ] ; do path="${CD_CA_ROOT}/${index}.crt" cd_split cd_write "${path}" "${text}" cd_openssl "${path}" index=$((index+1)) eval "text=\"\${CD_CA_${index}}\"" done } cd_update_ca_certificates () { cd_step "Update CA certificates" ${CD_CMD_CA} || exit } cd_set_https_verification_on () { cd_step "Set HTTPS verification on" cd_rm "${CD_PM_HTTPS_PATH}" } cd_upgrade_packages () { cd_step "Upgrade packages" ${CD_CMD_UPGRADE} || exit } cd_install_git () { cd_step "Install Git" cd_install_package "${CD_PKG_GIT}" } cd_install_python () { cd_step "Install Python" cd_install_package "${CD_PYTHON_PACKAGE}" cd_split cd_ln_python "${CD_PYTHON_COMMAND}" } cd_clean_packages_cache () { cd_step "Clean packages cache" ${CD_CMD_CLEAN} || exit } cd_install_python_modules () { local path local repository local root local url cd_step "Install Python modules" root="$(mktemp --directory)" || exit echo "${root}" for repository in "${@}" ; do cd_split url="${CD_PROJECTS_URL}/${repository}" echo -n "\ ${url} ↓ " git clone \ "${url}" "${root}/${repository}" \ || exit path="${root}/${repository}/${repository}" echo -n "\ ${path} ↓ ${CD_PYTHON_PACKAGES} " cp --recursive \ "${path}" "${CD_PYTHON_PACKAGES}" \ || exit done cd_split cd_rm "${root}" } cd_execute_python_module () { local self cd_step "Execute Python module" self="$(realpath "${0}")" echo -n "\ ${self} ↓ ${1} " "${CD_PYTHON_COMMAND}" -m "${1}" "${CD_STEP}" "${self}" } # functions cd_cat () { if [ "${1}" ] ; then echo "╭╴$(realpath "${1}")" cat "${1}" || exit fi } cd_echo () { local name if [ "${1}" ] ; then for name in "${@}" ; do eval "echo ${name}=\${${name}}" done fi } cd_error_ci () { echo "× CI: ${1}" exit ${CD_ERROR_CI} } cd_error_os () { local variable="${1}" echo -n "× OS: " cd_echo "${variable}" exit ${CD_ERROR_OS} } cd_grep_os () { local variable="${1}" if [ "${variable}" ] ; then grep "^${variable}=" "/etc/os-release" \ | sed "s|^${variable}=||" \ | sed "s|^\"\(.*\)\"$|\1|" fi } cd_install_package () { if [ "${1}" ] ; then ${CD_CMD_INSTALL} "${1}" || exit fi } cd_ln_python () { if [ "${1}" ] ; then echo "python3 → ${1}" ln -f -s "${1}" "/usr/bin/python3" || exit fi } cd_ls () { if [ "${1}" ] ; then echo "╭╴$(realpath "${1}")" ls -a -l "${1}" || exit fi } cd_mkdir () { if [ "${1}" ] ; then echo "→ ${1}" mkdir --parents "${1}" || exit fi } cd_openssl () { local file="${1}" if [ "${file}" ] ; then openssl x509 -noout -text -in "${file}" || exit fi } cd_rm () { if [ -e "${1}" ] ; then echo "← ${1}" rm -r "${1}" || exit fi } cd_sed () { local expression local file if [ -f "${1}" ] ; then file="${1}" shift for expression in "${@}" ; do sed --in-place "s${expression}g" "${file}" || exit done cd_cat "${file}" fi } cd_split () { echo -n "\ ╶─╌╌┄┄┈┈ " } cd_step () { if [ "${1}" ] ; then CD_STEP=$((CD_STEP+1)) echo -n "\ ╭─╌╌┄┄┈┈ │ ${CD_STEP} ${1} ╰─╌╌┄┄┈┈ " fi } cd_write () { local file="${1}" local text="${2}" if [ "${file}" ] ; then [ -f "${file}" ] && cd_cat "${file}" echo "→ ${1}" echo -n "${text}" > "${file}" || exit cd_cat "${file}" fi } # constants CD_ERROR_CI=2 CD_ERROR_OS=1 CD_OS_ALMA="alma" CD_OS_ALPINE="alpine" CD_OS_ARCH="arch" CD_OS_DEBIAN="debian" CD_OS_ROCKY="rocky" CD_OS_UBUNTU="ubuntu" # run cd_main