Compare commits

..

No commits in common. "15bd46e9aad2db8cd9a63d6210a0ab4973be4dc9" and "8739a7be34a4a741087bcf495edb9c53cbf76f62" have entirely different histories.

2 changed files with 78 additions and 92 deletions

136
cd.sh
View file

@ -1,26 +1,24 @@
#! /usr/bin/env sh
# defaults
CD_DEFAULT_DNS="\
[ "${CD_DNS}" ] || CD_DNS="\
9.9.9.9 \
"
CD_PYTHON_MODULES="\
cd \
rwx \
"
# main
# steps
cd_main () {
cd_set_environment
cd_set_dns_resolving
cd_set_dns_resolving ${CD_DNS}
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
@ -32,10 +30,9 @@ cd_main () {
cd_execute_python_module ${CD_PYTHON_MODULES}
}
# steps
# functions
cd_set_environment () {
local variable
cd_step "Set environment"
#
CD_CA_PACKAGE="ca-certificates"
@ -45,13 +42,8 @@ local variable
#
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
@ -61,7 +53,6 @@ local variable
;;
*) cd_error_os "CD_OS_VERSION" ;;
esac
CD_CA_ROOT="/etc/pki/ca-trust/source/anchors"
;;
*) cd_error_os "CD_OS_NAME" ;;
esac
@ -90,25 +81,30 @@ local variable
CD_PROJECTS_URL="${CD_SERVER_URL}/${CD_PROJECTS_GROUP}"
CD_PROJECT_URL="${CD_PROJECTS_URL}/${CD_PROJECT_NAME}"
#
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"
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_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
for server in "${@}" ; do
text="${text}nameserver ${server}
"
done
@ -122,10 +118,10 @@ local file
case "${CD_OS_NAME}" in
"debian")
cd_write "/etc/apt/sources.list" "\
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
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
"
;;
"alma")
@ -133,6 +129,7 @@ deb https://deb.debian.org/debian-security ${CD_OS_VERSION}-security main
cd_sed "${file}" "|^m|# m|" "|^# b|b|"
cd_cat "${file}"
;;
*) cd_error_os "cd_set_packages_repositories" ;;
esac
}
@ -150,26 +147,11 @@ 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 () {
if [ "${CD_CA}" ] ; then
cd_step "Set HTTPS verification off"
case "${CD_OS_NAME}" in
"debian")
@ -182,8 +164,8 @@ Acquire::https::Verify-Peer False;
sslverify=False
"
;;
*) cd_error_os "cd_set_https_verification_off" ;;
esac
fi
}
cd_update_packages_catalog () {
@ -191,6 +173,7 @@ 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
}
@ -199,6 +182,7 @@ 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
}
@ -207,22 +191,41 @@ 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 () {
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" ;;
*) cd_error_os "cd_set_https_verification_on" ;;
esac
fi
}
cd_upgrade_packages () {
@ -230,6 +233,7 @@ 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
}
@ -248,6 +252,7 @@ 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
}
@ -260,9 +265,8 @@ local url
root="$(mktemp --directory)" || exit
echo "${root}"
for repository in "${@}" ; do
cd_split
url="${CD_PROJECTS_URL}/${repository}"
echo -n "\
echo -n "
${url}
"
@ -279,7 +283,6 @@ ${CD_PYTHON_PACKAGES}
"${path}" "${CD_PYTHON_PACKAGES}" \
|| exit
done
cd_split
cd_rm "${root}"
}
@ -295,33 +298,21 @@ ${1}
"${CD_PYTHON_COMMAND}" -m "${1}" "${CD_STEP}" "${self}"
}
# functions
# tools
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 () {
local variable="${1}"
echo -n "× OS: "
cd_echo "${variable}"
echo "× OS: ${1}"
exit 2
}
@ -330,6 +321,7 @@ 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
}
@ -341,8 +333,6 @@ cd_mkdir () {
fi
}
cd_nop () { true ; }
cd_rm () {
if [ -e "${1}" ] ; then
echo "$(realpath "${1}")"
@ -383,10 +373,12 @@ 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
}
# run
# main
cd_main

View file

@ -9,12 +9,6 @@
* [X] ForgeJo / Gitea / GitHub
* [X] GitLab
## How
### Variables
| CD_DNS | Space separated servers |
## Tasks
* use constants for OS names