#! /usr/bin/env sh # defaults [ "${CD_DNS}" ] || CD_DNS="\ 9.9.9.9 \ " [ "${CD_GIT_CHILD}" ] || CD_GIT_CHILD="cd" [ "${CD_GIT_PARENT}" ] || CD_GIT_PARENT="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 # TODO move to Python cd_install_rsync # TODO move to Python cd_install_ssh cd_clean_packages_cache cd_install_python_modules cd_execute_python_module "${@}" } # steps cd_set_environment_variables () { cd_step "Set environment variables" # set operating system id 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}" ;; "fedora") CD_OS_ID="${CD_OS_FEDORA}" ;; "rocky") CD_OS_ID="${CD_OS_ROCKY}" ;; "ubuntu") CD_OS_ID="${CD_OS_UBUNTU}" ;; *) cd_error_os "CD_OS_ID" ;; esac # set operating system version case "${CD_OS_ID}" in "${CD_OS_ALMA}"|"${CD_OS_ARCH}"|"${CD_OS_FEDORA}"|"${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 # check operating system version 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_FEDORA}") case "${CD_OS_VERSION}" in "39"|"40") ;; *) cd_error_os "CD_OS_VERSION" ;; esac ;; "${CD_OS_UBUNTU}") case "${CD_OS_VERSION}" in "jammy"|"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_CA="ca-certificates" CD_PKG_GIT="git" # TODO move to Python CD_PKG_RSYNC="rsync" CD_PYTHON_ALIAS="python3" cd_split cd_echo "CD_DNS_FILE" "CD_PKG_CA" "CD_PKG_GIT" "CD_PYTHON_ALIAS" # set ca command & root case "${CD_OS_ID}" in "${CD_OS_ALMA}"|"${CD_OS_FEDORA}"|"${CD_OS_ROCKY}") CD_CA_ROOT="/etc/pki/ca-trust/source/anchors" CD_CMD_CA="update-ca-trust" ;; "${CD_OS_ALPINE}") CD_CA_ROOT="/usr/local/share/ca-certificates" CD_CMD_CA="update-ca-certificates" ;; "${CD_OS_ARCH}") CD_CA_ROOT="/etc/ca-certificates/trust-source/anchors" CD_CMD_CA="update-ca-trust" ;; "${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" # set package manager case "${CD_OS_ID}" in "${CD_OS_ALPINE}") CD_PM="${CD_PM_APK}" ;; "${CD_OS_DEBIAN}"|"${CD_OS_UBUNTU}") CD_PM="${CD_PM_APT}" ;; "${CD_OS_ALMA}"|"${CD_OS_FEDORA}"|"${CD_OS_ROCKY}") CD_PM="${CD_PM_DNF}" ;; "${CD_OS_ARCH}") CD_PM="${CD_PM_PACMAN}" ;; esac cd_split cd_echo "CD_PM" case "${CD_PM}" in "${CD_PM_DNF}") CD_PM_CLEAN="dnf clean all" CD_PM_INSTALL="dnf install --assumeyes" CD_PM_QUERY="rpm --query" CD_PM_UPDATE="dnf makecache" CD_PM_UPGRADE="dnf upgrade --assumeyes" 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_PM_APK}") CD_PM_CLEAN="apk cache purge" CD_PM_INSTALL="apk add" CD_PM_QUERY="apk info" CD_PM_UPDATE="apk update" CD_PM_UPGRADE="apk upgrade" 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_PM_PACMAN}") CD_PM_CLEAN="pacman --sync --clean --noconfirm" CD_PM_INSTALL="pacman --sync --noconfirm" CD_PM_QUERY="pacman --query" CD_PM_UPDATE="pacman --sync --refresh" CD_PM_UPGRADE="pacman --sync --sysupgrade --noconfirm" CD_PKG_PKG="" CD_PM_CONF_PATH="" CD_PM_CONF_TEXT="" CD_PM_HTTPS_PATH="/etc/pacman.d/https.conf" CD_PM_HTTPS_TEXT="SSLVerify = No" ;; "${CD_PM_APT}") CD_PM_CLEAN="apt-get clean" CD_PM_INSTALL="apt-get install --assume-yes" CD_PM_QUERY="dpkg-query --show" CD_PM_UPDATE="apt-get update" CD_PM_UPGRADE="apt-get upgrade --assume-yes" 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_PM_CLEAN" \ "CD_PM_INSTALL" "CD_PM_QUERY" "CD_PM_UPDATE" "CD_PM_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_ARCH}") CD_URL_DEFAULT="https://geo.mirror.pkgbuild.com" ;; "${CD_OS_DEBIAN}") CD_URL_DEFAULT="http://deb.debian.org/debian" ;; "${CD_OS_FEDORA}") CD_URL_DEFAULT="http://download.example/pub/fedora/linux/releases" ;; "${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_ARCH}") [ "${CD_URL_ARCH}" ] && CD_URL_CHOSEN="${CD_URL_ARCH}" ;; "${CD_OS_DEBIAN}") [ "${CD_URL_DEBIAN}" ] && CD_URL_CHOSEN="${CD_URL_DEBIAN}" \ || CD_URL_CHOSEN="https://deb.debian.org/debian" ;; "${CD_OS_FEDORA}") [ "${CD_URL_FEDORA}" ] && CD_URL_CHOSEN="${CD_URL_FEDORA}" \ || CD_URL_CHOSEN="https://rpmfind.net/linux/fedora/linux/releases" ;; "${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" # set python command & package case "${CD_OS_ID}" in "${CD_OS_ALMA}"|"${CD_OS_ROCKY}") CD_PYTHON_COMMAND="python3.11" CD_PYTHON_PACKAGE="python3.11" ;; "${CD_OS_ALPINE}") CD_PYTHON_COMMAND="python3.11" CD_PYTHON_PACKAGE="python3" ;; "${CD_OS_ARCH}") CD_PYTHON_COMMAND="python3.12" CD_PYTHON_PACKAGE="python" ;; "${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_OS_FEDORA}") CD_PYTHON_COMMAND="python3.12" CD_PYTHON_PACKAGE="python3" ;; "${CD_OS_UBUNTU}") case "${CD_OS_VERSION}" in "noble") CD_PYTHON_COMMAND="python3.12" ;; "jammy") CD_PYTHON_COMMAND="python3.10" ;; esac CD_PYTHON_PACKAGE="python3" ;; esac # set python packages case "${CD_OS_ID}" in "${CD_OS_ALMA}"|"${CD_OS_FEDORA}"|"${CD_OS_ROCKY}") CD_PYTHON_PACKAGES="/usr/lib64/${CD_PYTHON_COMMAND}/site-packages" ;; "${CD_OS_ALPINE}"|"${CD_OS_ARCH}") CD_PYTHON_PACKAGES="/usr/lib/${CD_PYTHON_COMMAND}/site-packages" ;; "${CD_OS_DEBIAN}"|"${CD_OS_UBUNTU}") CD_PYTHON_PACKAGES="/usr/lib/${CD_PYTHON_ALIAS}/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" # TODO move to Python case "${CD_PM}" in "${CD_PM_APK}"|"${CD_PM_APT}") CD_PKG_SSH="openssh-client" ;; "${CD_PM_DNF}") CD_PKG_SSH="openssh-clients" ;; "${CD_PM_PACMAN}") CD_PKG_SSH="openssh" ;; esac } 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 () { if [ "${CD_CA}" -o "${CD_PM}" = "${CD_PM_APT}" ] ; 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 text="" cd_step "Set DNS resolving" for server in ${CD_DNS} ; do text="${text}nameserver ${server} " done cd_write "${CD_DNS_FILE}" "${text}" } cd_update_packages_catalog () { cd_step "Update packages catalog" ${CD_PM_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_PM_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}" } # TODO move to Python cd_install_rsync () { cd_step "Install Rsync" cd_install_package "${CD_PKG_RSYNC}" } # TODO move to Python cd_install_ssh () { cd_step "Install SSH" cd_install_package "${CD_PKG_SSH}" } cd_clean_packages_cache () { cd_step "Clean packages cache" ${CD_PM_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 "${CD_GIT_CHILD}" "${CD_GIT_PARENT}" ; 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" # cd_echo \ "CD_OS_ID" "CD_OS_VERSION" \ "CD_SPLIT" \ > "${CD_PYTHON_PACKAGES}/env.py" # self="$(realpath "${0}")" echo -n "\ ${self} ↓ ${CD_GIT_CHILD} " "${CD_PYTHON_ALIAS}" -m "${CD_GIT_CHILD}" "${@}" } # 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_PM_INSTALL} "${1}" || exit fi } cd_ln_python () { local command="${1}" if [ "${command}" ] ; then echo "${CD_PYTHON_ALIAS} → ${command}" ln -f -s "${command}" "/usr/bin/${CD_PYTHON_ALIAS}" || 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 [ -f "${file}" ] ; then openssl x509 -noout -text -in "${file}" || exit fi } cd_rm () { local path="${1}" if [ -e "${path}" ] ; then echo "← ${path}" rm -r "${path}" || exit fi } cd_sed () { local expression local file="${1}" shift if [ -f "${file}" ] ; then cd_cat "${file}" for expression in "${@}" ; do sed --in-place "s${expression}g" "${file}" \ && cd_cat "${file}" \ || exit done fi } cd_split () { echo -n "\ ╶${CD_SPLIT} " } cd_step () { CD_STEP=$((CD_STEP+1)) echo -n "\ ╭${CD_SPLIT} │ ${CD_STEP} ${@} ╰${CD_SPLIT} " } 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_FEDORA="fedora" CD_OS_ROCKY="rocky" CD_OS_UBUNTU="ubuntu" CD_PM_APK="apk" CD_PM_APT="apt" CD_PM_DNF="dnf" CD_PM_PACMAN="pacman" CD_SPLIT="─╌╌┄┄┈┈" # run cd_main "${@}"