diff --git a/cd.sh b/cd.sh index eb54620..8d3a52c 100644 --- a/cd.sh +++ b/cd.sh @@ -1,24 +1,26 @@ #! /usr/bin/env sh -[ "${CD_DNS}" ] || CD_DNS="\ +# defaults +CD_DEFAULT_DNS="\ 9.9.9.9 \ " + CD_PYTHON_MODULES="\ cd \ rwx \ " -# steps +# main cd_main () { cd_set_environment - cd_set_dns_resolving ${CD_DNS} + cd_set_dns_resolving cd_set_packages_repositories cd_set_packages_configuration + cd_write_ca_certificates cd_set_https_verification_off 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 @@ -30,9 +32,10 @@ cd_main () { cd_execute_python_module ${CD_PYTHON_MODULES} } -# functions +# steps cd_set_environment () { +local variable cd_step "Set environment" # CD_CA_PACKAGE="ca-certificates" @@ -42,8 +45,13 @@ cd_set_environment () { # case "${CD_OS_NAME}" in "debian") + case "${CD_OS_VERSION}" in + "bookworm"|"bullseye") cd_nop ;; + *) cd_error_os "CD_OS_VERSION" ;; + esac CD_PYTHON_PACKAGE="python3" CD_PYTHON_PACKAGES="/usr/lib/python3/dist-packages" + CD_CA_ROOT="/usr/local/share/ca-certificates" ;; "alma") case "${CD_OS_VERSION}" in @@ -53,6 +61,7 @@ cd_set_environment () { ;; *) cd_error_os "CD_OS_VERSION" ;; esac + CD_CA_ROOT="/etc/pki/ca-trust/source/anchors" ;; *) cd_error_os "CD_OS_NAME" ;; esac @@ -81,30 +90,25 @@ cd_set_environment () { CD_PROJECTS_URL="${CD_SERVER_URL}/${CD_PROJECTS_GROUP}" CD_PROJECT_URL="${CD_PROJECTS_URL}/${CD_PROJECT_NAME}" # - echo -n "\ -CD_OS_NAME=${CD_OS_NAME} -CD_OS_VERSION=${CD_OS_VERSION} -# -CD_DNS_FILE=${CD_DNS_FILE} -# -CD_PYTHON_COMMAND=${CD_PYTHON_COMMAND} -CD_PYTHON_PACKAGES=${CD_PYTHON_PACKAGES} -# -CD_SERVER_URL=${CD_SERVER_URL} -CD_PROJECTS_GROUP=${CD_PROJECTS_GROUP} -CD_PROJECT_NAME=${CD_PROJECT_NAME} -CD_PROJECT_BRANCH=${CD_PROJECT_BRANCH} -# -CD_PROJECTS_URL=${CD_PROJECTS_URL} -CD_PROJECT_URL=${CD_PROJECT_URL} -" + cd_echo "CD_OS_NAME" "CD_OS_VERSION" + cd_split + cd_echo "CD_DNS_FILE" + cd_split + cd_echo "CD_PYTHON_PACKAGE" "CD_PYTHON_PACKAGES" "CD_CA_ROOT" + 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_dns_resolving () { local server +local servers local text="" cd_step "Set DNS resolving" - for server in "${@}" ; do + [ "${CD_DNS}" ] && servers="${CD_DNS}" || servers="${CD_DEFAULT_DNS}" + for server in ${servers} ; do text="${text}nameserver ${server} " done @@ -118,10 +122,10 @@ local file case "${CD_OS_NAME}" in "debian") cd_write "/etc/apt/sources.list" "\ -deb https://deb.debian.org/debian bookworm main -deb https://deb.debian.org/debian bookworm-backports main -deb https://deb.debian.org/debian bookworm-updates main -deb https://deb.debian.org/debian-security bookworm-security main +deb https://deb.debian.org/debian ${CD_OS_VERSION} main +deb https://deb.debian.org/debian ${CD_OS_VERSION}-backports main +deb https://deb.debian.org/debian ${CD_OS_VERSION}-updates main +deb https://deb.debian.org/debian-security ${CD_OS_VERSION}-security main " ;; "alma") @@ -129,7 +133,6 @@ deb https://deb.debian.org/debian-security bookworm-security main cd_sed "${file}" "|^m|# m|" "|^# b|b|" cd_cat "${file}" ;; - *) cd_error_os "cd_set_packages_repositories" ;; esac } @@ -147,25 +150,40 @@ Dir::Etc::SourceParts \"\"; " ;; "alma") cd_mkdir "/etc/dnf/dnf.conf.d" ;; - *) cd_error_os "cd_set_packages_configuration" ;; esac } +cd_write_ca_certificates () { +local index +local text + cd_step "Copy CA" + cd_mkdir "${CD_CA_ROOT}" + index=1 + eval "text=\${CD_CA_${index}}" + while [ "${text}" ] ; do + cd_write "${CD_CA_ROOT}/${index}.crt" "${text}" + CD_CA=$((CD_CA+1)) + index=$((index+1)) + eval "text=\${CD_CA_${index}}" + done +} + cd_set_https_verification_off () { - cd_step "Set HTTPS verification off" - case "${CD_OS_NAME}" in - "debian") - cd_write "/etc/apt/apt.conf.d/https" "\ + if [ "${CD_CA}" ] ; then + cd_step "Set HTTPS verification off" + case "${CD_OS_NAME}" in + "debian") + cd_write "/etc/apt/apt.conf.d/https" "\ Acquire::https::Verify-Peer False; " - ;; - "alma") - cd_write "/etc/dnf/dnf.conf.d/https.conf" "\ + ;; + "alma") + cd_write "/etc/dnf/dnf.conf.d/https.conf" "\ sslverify=False " - ;; - *) cd_error_os "cd_set_https_verification_off" ;; - esac + ;; + esac + fi } cd_update_packages_catalog () { @@ -173,7 +191,6 @@ cd_update_packages_catalog () { case "${CD_OS_NAME}" in "debian") apt-get update || exit ;; "alma") dnf makecache || exit ;; - *) cd_error_os "cd_update_packages_catalog" ;; esac } @@ -182,7 +199,6 @@ cd_install_packages_tools () { case "${CD_OS_NAME}" in "debian") cd_install_package "apt-utils" ;; "alma") echo "TODO" ;; - *) cd_error_os "cd_install_packages_tools" ;; esac } @@ -191,41 +207,22 @@ cd_install_ca_certificates () { cd_install_package "${CD_CA_PACKAGE}" } -cd_write_ca_certificates () { -local index -local target -local text - cd_step "Copy CA" - case "${CD_OS_NAME}" in - "debian") target="/usr/local/share/ca-certificates" ;; - "alma") target="/etc/pki/ca-trust/source/anchors" ;; - *) cd_error_os "cd_copy_ca" ;; - esac - index=1 - eval "text=\${CD_CA_${index}}" - while [ "${text}" ] ; do - cd_write "${target}/${index}.crt" "${text}" - index=$((index+1)) - eval "text=\${CD_CA_${index}}" - done -} - cd_update_ca_certificates () { cd_step "Update CA" case "${CD_OS_NAME}" in "debian") update-ca-certificates || exit ;; "alma") update-ca-trust || exit ;; - *) cd_error_os "cd_update_ca" ;; esac } cd_set_https_verification_on () { - cd_step "Set HTTPS verification on" - case "${CD_OS_NAME}" in - "debian") cd_rm "/etc/apt/apt.conf.d/https" ;; - "alma") cd_rm "/etc/dnf/dnf.conf.d/https.conf" ;; - *) cd_error_os "cd_set_https_verification_on" ;; - esac + if [ "${CD_CA}" ] ; then + cd_step "Set HTTPS verification on" + case "${CD_OS_NAME}" in + "debian") cd_rm "/etc/apt/apt.conf.d/https" ;; + "alma") cd_rm "/etc/dnf/dnf.conf.d/https.conf" ;; + esac + fi } cd_upgrade_packages () { @@ -233,7 +230,6 @@ cd_upgrade_packages () { case "${CD_OS_NAME}" in "debian") apt-get upgrade --assume-yes || exit ;; "alma") dnf upgrade --assumeyes || exit ;; - *) cd_error_os "cd_upgrade_packages" ;; esac } @@ -252,7 +248,6 @@ cd_clean_packages_cache () { case "${CD_OS_NAME}" in "debian") apt-get clean || exit ;; "alma") dnf clean all || exit ;; - *) cd_error_os "cd_clean_packages_cache" ;; esac } @@ -265,8 +260,9 @@ local url root="$(mktemp --directory)" || exit echo "${root}" for repository in "${@}" ; do + cd_split url="${CD_PROJECTS_URL}/${repository}" - echo -n " + echo -n "\ ${url} ↓ " @@ -283,6 +279,7 @@ ${CD_PYTHON_PACKAGES} "${path}" "${CD_PYTHON_PACKAGES}" \ || exit done + cd_split cd_rm "${root}" } @@ -298,21 +295,33 @@ ${1} "${CD_PYTHON_COMMAND}" -m "${1}" "${CD_STEP}" "${self}" } -# tools +# functions cd_cat () { if [ -f "${1}" ] ; then - echo " ↙ $(realpath "${1}")" + 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 1 } + cd_error_os () { - echo "× OS: ${1}" +local variable="${1}" + echo -n "× OS: " + cd_echo "${variable}" exit 2 } @@ -321,7 +330,6 @@ cd_install_package () { case "${CD_OS_NAME}" in "debian") apt-get install --assume-yes "${1}" || exit ;; "alma") dnf install --assumeyes "${1}" || exit ;; - *) exit 1 ;; esac fi } @@ -333,6 +341,8 @@ cd_mkdir () { fi } +cd_nop () { true ; } + cd_rm () { if [ -e "${1}" ] ; then echo "← $(realpath "${1}")" @@ -373,12 +383,10 @@ cd_write () { local file="${1}" local text="${2}" if [ "${file}" ] ; then - echo -n "${text}" \ - > "${file}" \ - || exit + echo -n "${text}" > "${file}" || exit cd_cat "${file}" fi } -# main +# run cd_main diff --git a/readme.md b/readme.md index 95f9c4b..f2dcd3e 100644 --- a/readme.md +++ b/readme.md @@ -9,6 +9,12 @@ * [X] ForgeJo / Gitea / GitHub * [X] GitLab +## How + +### Variables + +| CD_DNS | Space separated servers | + ## Tasks * use constants for OS names