spcd/cd.sh

462 lines
11 KiB
Bash
Raw Normal View History

2024-04-27 15:47:23 +00:00
#! /usr/bin/env sh
2024-04-26 13:38:22 +00:00
2024-05-02 08:27:18 +00:00
# defaults
CD_DEFAULT_DNS="\
2024-04-29 13:10:51 +00:00
9.9.9.9 \
"
2024-05-03 17:02:51 +00:00
CD_DEFAULT_REPO_ALMA="https://repo.almalinux.org/almalinux"
CD_DEFAULT_REPO_DEBIAN="https://deb.debian.org/debian"
2024-05-02 08:27:18 +00:00
2024-04-29 13:14:49 +00:00
CD_PYTHON_MODULES="\
cd \
rwx \
"
2024-04-26 11:38:38 +00:00
2024-05-02 08:19:18 +00:00
# main
2024-04-27 17:56:19 +00:00
cd_main () {
2024-05-04 08:39:18 +00:00
cd_list_working_directory
2024-05-03 10:55:28 +00:00
cd_set_environment_variables
2024-04-27 15:36:14 +00:00
cd_set_packages_repositories
cd_set_packages_configuration
2024-05-02 06:38:49 +00:00
cd_set_https_verification_off
2024-05-04 10:14:30 +00:00
cd_set_dns_resolving
2024-04-27 15:36:14 +00:00
cd_update_packages_catalog
cd_install_packages_tools
2024-04-30 10:31:56 +00:00
cd_install_ca_certificates
2024-05-03 15:20:44 +00:00
cd_write_ca_certificates
2024-04-30 10:31:56 +00:00
cd_update_ca_certificates
2024-05-02 06:38:49 +00:00
cd_set_https_verification_on
2024-04-27 15:36:14 +00:00
cd_update_packages_catalog
cd_upgrade_packages
cd_install_git
cd_install_python
cd_clean_packages_cache
2024-04-29 13:14:49 +00:00
cd_install_python_modules ${CD_PYTHON_MODULES}
cd_execute_python_module ${CD_PYTHON_MODULES}
2024-04-27 15:36:14 +00:00
}
2024-05-02 08:19:18 +00:00
# steps
2024-04-27 19:47:02 +00:00
2024-05-04 08:39:18 +00:00
cd_list_working_directory () {
cd_step "List working directory"
cd_ls "."
}
2024-05-03 10:55:28 +00:00
cd_set_environment_variables () {
cd_step "Set environment variables"
#
2024-05-03 17:31:25 +00:00
[ "${CD_CA_1}" ] && CD_CA=true
2024-05-03 15:19:35 +00:00
#
2024-05-03 10:55:28 +00:00
case "$(cd_grep_os ID)" in
2024-05-03 11:03:47 +00:00
"almalinux")
CD_OS_ID="${CD_OS_ALMA}"
CD_OS_VERSION="$(cd_grep_os VERSION_ID)"
;;
2024-05-03 19:03:07 +00:00
"debian")
CD_OS_ID="${CD_OS_DEBIAN}"
CD_OS_VERSION="$(cd_grep_os VERSION_CODENAME)"
;;
2024-05-03 10:55:28 +00:00
esac
2024-04-29 15:37:15 +00:00
#
2024-04-29 15:53:51 +00:00
CD_CA_PACKAGE="ca-certificates"
2024-04-27 21:36:26 +00:00
CD_DNS_FILE="/etc/resolv.conf"
2024-04-29 15:25:30 +00:00
CD_GIT_PACKAGE="git"
2024-04-28 10:44:28 +00:00
#
2024-05-03 10:45:25 +00:00
case "${CD_OS_ID}" in
2024-05-02 09:10:54 +00:00
"${CD_OS_ALMA}")
2024-05-03 17:02:51 +00:00
CD_OS_REPO="${CD_DEFAULT_REPO_ALMA}"
[ "${CD_REPO_ALMA}" ] && CD_OS_REPO="${CD_REPO_ALMA}"
2024-04-29 15:23:10 +00:00
case "${CD_OS_VERSION}" in
2024-05-04 09:06:25 +00:00
"8"|"9") ;;
2024-05-02 08:33:34 +00:00
*) cd_error_os "CD_OS_VERSION" ;;
2024-04-29 15:23:10 +00:00
esac
2024-05-02 07:59:17 +00:00
CD_CA_ROOT="/etc/pki/ca-trust/source/anchors"
2024-05-03 19:28:37 +00:00
CD_CMD_CA="update-ca-trust"
2024-05-03 19:16:03 +00:00
CD_CMD_CLEAN="dnf clean all"
2024-05-03 22:03:04 +00:00
CD_CMD_INSTALL="dnf install --assumeyes"
2024-05-03 19:30:28 +00:00
CD_CMD_UPDATE="dnf makecache"
2024-05-03 19:18:20 +00:00
CD_CMD_UPGRADE="dnf upgrade --assumeyes"
2024-05-03 19:43:25 +00:00
CD_PKG_PKG=""
2024-05-03 20:52:24 +00:00
CD_PM_CONF_PATH="/etc/dnf/dnf.conf"
2024-05-03 21:19:30 +00:00
CD_PM_CONF_TEXT="\
[main]
best=True
clean_requirements_on_remove=True
gpgcheck=1
installonly_limit=3
skip_if_unavailable=False
"
2024-05-03 20:04:05 +00:00
CD_PM_HTTPS_PATH="/etc/dnf/dnf.conf.d/https.conf"
CD_PM_HTTPS_TEXT="sslverify=False"
2024-05-03 15:39:49 +00:00
CD_PYTHON_COMMAND="python3.11"
2024-05-02 09:12:09 +00:00
CD_PYTHON_PACKAGE="python3.11"
CD_PYTHON_PACKAGES="/usr/lib64/python3.11/site-packages"
2024-04-29 15:23:10 +00:00
;;
2024-05-03 19:03:07 +00:00
"${CD_OS_DEBIAN}")
2024-05-03 19:56:30 +00:00
export DEBIAN_FRONTEND="noninteractive"
#
2024-05-03 19:03:07 +00:00
CD_OS_REPO="${CD_DEFAULT_REPO_DEBIAN}"
[ "${CD_REPO_DEBIAN}" ] && CD_OS_REPO="${CD_REPO_DEBIAN}"
case "${CD_OS_VERSION}" in
2024-05-03 22:28:49 +00:00
"bookworm") CD_PYTHON_COMMAND="python3.11" ;;
"bullseye") CD_PYTHON_COMMAND="python3.9" ;;
2024-05-03 19:03:07 +00:00
*) cd_error_os "CD_OS_VERSION" ;;
esac
CD_CA_ROOT="/usr/local/share/ca-certificates"
2024-05-03 19:28:37 +00:00
CD_CMD_CA="update-ca-certificates"
2024-05-03 19:16:03 +00:00
CD_CMD_CLEAN="apt-get clean"
2024-05-03 22:03:04 +00:00
CD_CMD_INSTALL="apt-get install --assume-yes"
2024-05-03 19:30:28 +00:00
CD_CMD_UPDATE="apt-get update"
2024-05-03 19:18:20 +00:00
CD_CMD_UPGRADE="apt-get upgrade --assume-yes"
2024-05-03 19:43:25 +00:00
CD_PKG_PKG="apt-utils"
2024-05-03 20:52:24 +00:00
CD_PM_CONF_PATH="/etc/apt/apt.conf.d/apt.conf"
2024-05-03 21:19:30 +00:00
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 \"\";
"
2024-05-03 20:04:05 +00:00
CD_PM_HTTPS_PATH="/etc/apt/apt.conf.d/https"
CD_PM_HTTPS_TEXT="Acquire::https::Verify-Peer False;"
2024-05-03 19:03:07 +00:00
CD_PYTHON_PACKAGE="python3"
CD_PYTHON_PACKAGES="/usr/lib/python3/dist-packages"
;;
2024-05-03 10:45:25 +00:00
*) cd_error_os "CD_OS_ID" ;;
2024-04-27 09:59:30 +00:00
esac
2024-05-03 11:06:52 +00:00
# continuous integration platform
2024-04-29 08:58:37 +00:00
if [ "${GITHUB_ACTIONS}" ] ; then
2024-05-03 11:06:52 +00:00
# forgejo / gitea / github
2024-04-29 08:58:37 +00:00
CD_SERVER_URL="${GITHUB_SERVER_URL}"
CD_PROJECTS_GROUP="$(dirname "${GITHUB_REPOSITORY}")"
2024-04-29 10:15:49 +00:00
CD_PROJECT_NAME="$(basename "${GITHUB_REPOSITORY}")"
CD_PROJECT_BRANCH="${GITHUB_REF_NAME}"
2024-04-29 08:58:37 +00:00
elif [ "${GITLAB_CI}" ] ; then
2024-05-03 11:06:52 +00:00
# gitlab
2024-04-29 08:58:37 +00:00
CD_SERVER_URL="${CI_SERVER_URL}"
CD_PROJECTS_GROUP="$(dirname "${CI_PROJECT_PATH}")"
2024-04-29 10:15:49 +00:00
CD_PROJECT_NAME="$(basename "${CI_PROJECT_PATH}")"
CD_PROJECT_BRANCH="${CI_COMMIT_BRANCH}"
2024-04-27 09:59:30 +00:00
else
2024-05-03 11:06:52 +00:00
# unsupported
2024-04-29 09:08:29 +00:00
cd_error_ci "ø"
2024-04-27 09:59:30 +00:00
fi
2024-04-29 08:58:37 +00:00
#
2024-04-29 09:08:29 +00:00
[ "${CD_SERVER_URL}" ] || cd_error_ci "CD_SERVER_URL"
[ "${CD_PROJECTS_GROUP}" ] || cd_error_ci "CD_PROJECTS_GROUP"
2024-04-29 10:15:49 +00:00
[ "${CD_PROJECT_NAME}" ] || cd_error_ci "CD_PROJECT_NAME"
[ "${CD_PROJECT_BRANCH}" ] || cd_error_ci "CD_PROJECT_BRANCH"
2024-04-29 08:58:37 +00:00
#
CD_PROJECTS_URL="${CD_SERVER_URL}/${CD_PROJECTS_GROUP}"
2024-04-27 09:56:16 +00:00
CD_PROJECT_URL="${CD_PROJECTS_URL}/${CD_PROJECT_NAME}"
2024-04-28 10:44:28 +00:00
#
2024-05-03 15:19:35 +00:00
cd_echo "CD_CA"
cd_split
2024-05-03 10:45:25 +00:00
cd_echo "CD_OS_ID" "CD_OS_VERSION"
2024-05-02 07:59:17 +00:00
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"
2024-04-27 09:56:16 +00:00
}
2024-04-27 17:56:19 +00:00
cd_set_packages_repositories () {
2024-04-29 20:41:28 +00:00
local expression
local file
2024-04-27 21:36:26 +00:00
cd_step "Set packages repositories"
2024-05-03 10:45:25 +00:00
case "${CD_OS_ID}" in
2024-05-02 09:10:54 +00:00
"${CD_OS_ALMA}")
2024-04-29 20:41:28 +00:00
file="/etc/yum.repos.d/almalinux.repo"
2024-05-03 17:02:51 +00:00
cd_sed "${file}" \
"|^mirrorlist|# mirrorlist|" \
"|${CD_DEFAULT_REPO_ALMA}|${CD_OS_REPO}|" \
"|^# baseurl|baseurl|"
2024-04-29 20:43:51 +00:00
cd_cat "${file}"
2024-04-29 20:41:28 +00:00
;;
2024-05-03 19:03:07 +00:00
"${CD_OS_DEBIAN}")
2024-05-04 08:50:26 +00:00
file="/etc/apt/sources.list"
cd_write "${file}" "\
2024-05-03 19:03:07 +00:00
deb ${CD_OS_REPO} ${CD_OS_VERSION} main
deb ${CD_OS_REPO} ${CD_OS_VERSION}-backports main
deb ${CD_OS_REPO} ${CD_OS_VERSION}-updates main
deb ${CD_OS_REPO}-security ${CD_OS_VERSION}-security main
"
;;
2024-04-26 14:13:35 +00:00
esac
}
2024-05-03 21:19:30 +00:00
# agnostic steps
2024-04-27 17:56:19 +00:00
cd_set_packages_configuration () {
2024-04-27 21:36:26 +00:00
cd_step "Set packages configuration"
2024-05-03 21:19:30 +00:00
cd_write "${CD_PM_CONF_PATH}" "${CD_PM_CONF_TEXT}"
2024-04-26 13:54:37 +00:00
}
2024-04-27 17:56:19 +00:00
cd_set_https_verification_off () {
2024-05-03 18:20:26 +00:00
if [ "${CD_CA}" -o "${CD_OS_ID}" = "${CD_OS_DEBIAN}" ] ; then
cd_step "Set HTTPS verification off"
2024-05-03 20:04:05 +00:00
cd_mkdir "$(dirname "${CD_PM_HTTPS_PATH}")"
cd_write "${CD_PM_HTTPS_PATH}" "${CD_PM_HTTPS_TEXT}"
2024-05-03 18:20:26 +00:00
fi
2024-04-26 14:23:30 +00:00
}
2024-05-04 10:14:30 +00:00
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}"
}
2024-04-27 17:56:19 +00:00
cd_update_packages_catalog () {
2024-04-27 21:36:26 +00:00
cd_step "Update packages catalog"
2024-05-03 19:30:28 +00:00
${CD_CMD_UPDATE} || exit
2024-04-26 14:27:36 +00:00
}
2024-04-27 19:36:51 +00:00
cd_install_packages_tools () {
2024-04-27 21:36:26 +00:00
cd_step "Install packages tools"
2024-05-03 19:43:25 +00:00
cd_install_package "${CD_PKG_PKG}"
2024-04-27 19:36:51 +00:00
}
2024-04-30 10:31:56 +00:00
cd_install_ca_certificates () {
2024-04-27 21:36:26 +00:00
cd_step "Install CA"
2024-04-29 15:53:51 +00:00
cd_install_package "${CD_CA_PACKAGE}"
2024-04-26 14:44:34 +00:00
}
2024-05-03 15:20:44 +00:00
cd_write_ca_certificates () {
local index
2024-05-04 07:50:13 +00:00
local path
2024-05-03 15:20:44 +00:00
local text
2024-05-03 19:28:37 +00:00
cd_step "Write CA certificates"
2024-05-03 15:20:44 +00:00
cd_mkdir "${CD_CA_ROOT}"
index=1
eval "text=\"\${CD_CA_${index}}\""
while [ "${text}" ] ; do
2024-05-04 07:50:13 +00:00
path="${CD_CA_ROOT}/${index}.crt"
cd_split
cd_write "${path}" "${text}"
cd_openssl "${path}"
2024-05-03 15:20:44 +00:00
index=$((index+1))
eval "text=\"\${CD_CA_${index}}\""
done
}
2024-04-30 10:31:56 +00:00
cd_update_ca_certificates () {
2024-05-03 19:28:37 +00:00
cd_step "Update CA certificates"
${CD_CMD_CA} || exit
2024-04-27 21:58:48 +00:00
}
2024-04-27 19:40:50 +00:00
cd_set_https_verification_on () {
2024-05-03 18:20:26 +00:00
if [ "${CD_CA}" -o "${CD_OS_ID}" = "${CD_OS_DEBIAN}" ] ; then
cd_step "Set HTTPS verification on"
2024-05-03 20:04:05 +00:00
cd_rm "${CD_PM_HTTPS_PATH}"
2024-05-03 18:20:26 +00:00
fi
2024-04-26 14:50:25 +00:00
}
2024-04-27 19:42:29 +00:00
cd_upgrade_packages () {
2024-04-27 21:36:26 +00:00
cd_step "Upgrade packages"
2024-05-03 19:18:20 +00:00
${CD_CMD_UPGRADE} || exit
2024-04-26 14:35:15 +00:00
}
2024-04-27 19:42:29 +00:00
cd_install_git () {
2024-04-27 21:36:26 +00:00
cd_step "Install Git"
2024-04-29 15:25:30 +00:00
cd_install_package "${CD_GIT_PACKAGE}"
2024-04-26 14:35:15 +00:00
}
2024-04-27 19:42:29 +00:00
cd_install_python () {
2024-04-27 21:36:26 +00:00
cd_step "Install Python"
2024-04-29 15:58:57 +00:00
cd_install_package "${CD_PYTHON_PACKAGE}"
2024-05-03 22:24:14 +00:00
cd_split
cd_ln_python "${CD_PYTHON_COMMAND}"
2024-04-26 14:39:24 +00:00
}
2024-04-27 17:56:19 +00:00
cd_clean_packages_cache () {
2024-04-27 21:36:26 +00:00
cd_step "Clean packages cache"
2024-05-03 19:16:03 +00:00
${CD_CMD_CLEAN} || exit
2024-04-26 14:53:41 +00:00
}
2024-04-28 21:29:31 +00:00
cd_install_python_modules () {
local path
2024-04-26 18:09:11 +00:00
local repository
local root
2024-04-28 21:29:31 +00:00
local url
cd_step "Install Python modules"
2024-04-26 18:09:11 +00:00
root="$(mktemp --directory)" || exit
2024-04-28 21:41:12 +00:00
echo "${root}"
2024-04-27 15:47:23 +00:00
for repository in "${@}" ; do
2024-04-30 11:45:05 +00:00
cd_split
2024-04-28 21:29:31 +00:00
url="${CD_PROJECTS_URL}/${repository}"
2024-05-01 18:33:24 +00:00
echo -n "\
2024-04-28 21:29:31 +00:00
${url}
"
2024-04-26 18:09:11 +00:00
git clone \
2024-04-28 21:29:31 +00:00
"${url}" "${root}/${repository}" \
2024-04-26 18:29:26 +00:00
|| exit
2024-04-28 21:29:31 +00:00
path="${root}/${repository}/${repository}"
echo -n "\
${path}
${CD_PYTHON_PACKAGES}
"
2024-04-26 19:30:36 +00:00
cp --recursive \
2024-04-28 21:29:31 +00:00
"${path}" "${CD_PYTHON_PACKAGES}" \
2024-04-26 18:29:26 +00:00
|| exit
2024-04-26 18:09:11 +00:00
done
2024-05-01 18:33:24 +00:00
cd_split
2024-04-28 21:29:31 +00:00
cd_rm "${root}"
2024-04-26 16:54:09 +00:00
}
2024-04-28 13:42:36 +00:00
cd_execute_python_module () {
local self
2024-04-28 15:49:06 +00:00
cd_step "Execute Python module"
2024-04-28 13:42:36 +00:00
self="$(realpath "${0}")"
echo -n "\
${self}
${1}
"
2024-04-28 15:49:06 +00:00
"${CD_PYTHON_COMMAND}" -m "${1}" "${CD_STEP}" "${self}"
2024-04-26 17:00:43 +00:00
}
2024-05-02 08:19:18 +00:00
# functions
2024-04-27 19:38:43 +00:00
2024-04-28 10:35:43 +00:00
cd_cat () {
2024-05-04 07:50:13 +00:00
if [ "${1}" ] ; then
2024-04-30 11:44:12 +00:00
echo "╭╴$(realpath "${1}")"
2024-04-28 11:00:18 +00:00
cat "${1}" || exit
2024-04-28 10:35:43 +00:00
fi
}
2024-05-02 07:59:17 +00:00
cd_echo () {
local name
if [ "${1}" ] ; then
for name in "${@}" ; do
eval "echo ${name}=\${${name}}"
done
fi
}
2024-04-29 09:01:48 +00:00
cd_error_ci () {
echo "× CI: ${1}"
2024-05-03 12:11:52 +00:00
exit ${CD_ERROR_CI}
2024-04-29 09:01:48 +00:00
}
2024-05-02 08:33:34 +00:00
2024-04-29 09:01:48 +00:00
cd_error_os () {
2024-05-02 08:33:34 +00:00
local variable="${1}"
echo -n "× OS: "
cd_echo "${variable}"
2024-05-03 12:11:52 +00:00
exit ${CD_ERROR_OS}
2024-04-29 09:01:48 +00:00
}
2024-05-03 10:42:35 +00:00
cd_grep_os () {
local variable="${1}"
if [ "${variable}" ] ; then
grep "^${variable}=" "/etc/os-release" \
2024-05-03 11:14:52 +00:00
| sed "s|^${variable}=||" \
2024-05-04 07:26:45 +00:00
| sed "s|^\"\(.*\)\"$|\1|" \
| sed "s|^\([0-9]\+\)\..*|\1|"
2024-05-03 10:42:35 +00:00
fi
2024-05-02 14:46:08 +00:00
}
2024-04-27 19:38:43 +00:00
cd_install_package () {
if [ "${1}" ] ; then
2024-05-03 22:03:04 +00:00
${CD_CMD_INSTALL} "${1}" || exit
2024-04-27 19:38:43 +00:00
fi
}
2024-05-03 22:24:14 +00:00
cd_ln_python () {
if [ "${1}" ] ; then
echo "python3 → ${1}"
ln --force --symbolic "${1}" "/usr/bin/python3" || exit
fi
}
2024-05-04 08:30:33 +00:00
cd_ls () {
if [ "${1}" ] ; then
echo "╭╴$(realpath "${1}")"
ls --all -l "${1}" || exit
fi
}
2024-04-29 18:16:14 +00:00
cd_mkdir () {
if [ "${1}" ] ; then
echo "${1}"
mkdir --parents "${1}" || exit
fi
}
2024-05-04 07:50:13 +00:00
cd_openssl () {
local file="${1}"
if [ "${file}" ] ; then
openssl x509 -noout -text -in "${file}" || exit
fi
}
2024-04-28 11:00:18 +00:00
cd_rm () {
2024-04-28 21:29:31 +00:00
if [ -e "${1}" ] ; then
2024-05-04 08:45:31 +00:00
echo "${1}"
2024-04-28 21:29:31 +00:00
rm --recursive "${1}" || exit
2024-04-28 11:00:18 +00:00
fi
}
2024-04-29 21:07:52 +00:00
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
fi
}
2024-04-30 10:36:33 +00:00
cd_split () {
echo -n "\
╶─╌╌┄┄┈┈
"
}
2024-04-27 19:24:59 +00:00
cd_step () {
if [ "${1}" ] ; then
CD_STEP=$((CD_STEP+1))
2024-04-29 10:51:48 +00:00
echo -n "\
2024-04-30 10:36:33 +00:00
╭─╌╌┄┄┈┈
2024-04-29 10:23:22 +00:00
${CD_STEP} ${1}
2024-04-30 10:36:33 +00:00
╰─╌╌┄┄┈┈
2024-04-27 19:24:59 +00:00
"
fi
}
cd_write () {
local file="${1}"
local text="${2}"
if [ "${file}" ] ; then
2024-05-04 08:50:26 +00:00
[ -f "${file}" ] && cd_cat "${file}"
2024-05-04 08:45:31 +00:00
echo "${1}"
2024-05-02 08:29:43 +00:00
echo -n "${text}" > "${file}" || exit
2024-04-29 18:07:17 +00:00
cd_cat "${file}"
2024-04-27 19:24:59 +00:00
fi
}
2024-05-02 09:10:54 +00:00
# constants
2024-05-03 12:11:52 +00:00
CD_ERROR_CI=2
CD_ERROR_OS=1
2024-05-02 09:10:54 +00:00
CD_OS_ALMA="alma"
CD_OS_DEBIAN="debian"
2024-05-02 08:19:18 +00:00
# run
2024-04-29 13:14:49 +00:00
cd_main