spcd/cd.sh

329 lines
7.1 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-04-27 21:36:26 +00:00
set "cd" "rwx"
2024-04-26 11:38:38 +00:00
2024-04-27 17:56:19 +00:00
cd_main () {
2024-04-27 15:36:14 +00:00
cd_set_environment
cd_set_dns_resolving \
2024-04-27 21:36:26 +00:00
"9.9.9.9"
2024-04-27 15:36:14 +00:00
cd_set_packages_repositories
cd_set_packages_configuration
cd_set_https_verification_off
cd_update_packages_catalog
cd_install_packages_tools
cd_install_ca
2024-04-27 22:12:11 +00:00
cd_copy_ca
2024-04-27 21:58:48 +00:00
cd_update_ca
2024-04-27 15:36:14 +00:00
cd_set_https_verification_on
cd_update_packages_catalog
cd_upgrade_packages
cd_install_git
cd_install_python
cd_clean_packages_cache
2024-04-28 21:29:31 +00:00
cd_install_python_modules "${@}"
2024-04-28 13:42:36 +00:00
cd_execute_python_module "${@}"
2024-04-27 15:36:14 +00:00
}
2024-04-27 19:47:02 +00:00
#
2024-04-27 17:56:19 +00:00
cd_set_environment () {
2024-04-27 21:36:26 +00:00
cd_step "Set environment"
CD_DNS_FILE="/etc/resolv.conf"
2024-04-28 10:44:28 +00:00
#
2024-04-27 09:59:30 +00:00
case "${CD_OS_NAME}" in
2024-04-27 21:36:26 +00:00
"debian")
CD_PYTHON_COMMAND="python3"
CD_PYTHON_PACKAGES="/usr/lib/python3/dist-packages"
2024-04-27 09:59:30 +00:00
case "${CD_OS_VERSION}" in
2024-04-27 21:36:26 +00:00
"bookworm")
echo "TODO"
2024-04-27 09:59:30 +00:00
;;
*)
2024-04-27 21:36:26 +00:00
echo "CD_OS_VERSION"
2024-04-27 09:59:30 +00:00
exit 2
;;
esac
;;
*)
2024-04-27 21:36:26 +00:00
echo "CD_OS_NAME"
2024-04-27 09:59:30 +00:00
exit 1
;;
esac
2024-04-29 08:58:37 +00:00
# ci / github
if [ "${GITHUB_ACTIONS}" ] ; then
CD_SERVER_URL="${GITHUB_SERVER_URL}"
CD_PROJECT_BRANCH="${GITHUB_REF_NAME}"
CD_PROJECT_NAME="$(basename "${GITHUB_REPOSITORY}")"
CD_PROJECTS_GROUP="$(dirname "${GITHUB_REPOSITORY}")"
# ci / gitlab
elif [ "${GITLAB_CI}" ] ; then
CD_SERVER_URL="${CI_SERVER_URL}"
CD_PROJECT_BRANCH="${CI_COMMIT_BRANCH}"
CD_PROJECT_NAME="$(basename "${CI_PROJECT_PATH}")"
CD_PROJECTS_GROUP="$(dirname "${CI_PROJECT_PATH}")"
# ci / none
2024-04-27 09:59:30 +00:00
else
2024-04-29 08:58:37 +00:00
echo "CI"
2024-04-27 09:59:30 +00:00
exit 3
fi
2024-04-29 08:58:37 +00:00
#
[ "${CD_SERVER_URL}" ] || echo "CD_SERVER_URL" && exit 3
[ "${CD_PROJECT_BRANCH}" ] || echo "CD_PROJECT_BRANCH" && exit 3
[ "${CD_PROJECT_NAME}" ] || echo "CD_PROJECT_NAME" && exit 3
[ "${CD_PROJECTS_GROUP}" ] || echo "CD_PROJECTS_GROUP" && exit 3
#
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
#
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}
#
2024-04-29 08:58:37 +00:00
CD_SERVER_URL=${CD_SERVER_URL}
2024-04-28 10:44:28 +00:00
CD_PROJECT_BRANCH=${CD_PROJECT_BRANCH}
CD_PROJECT_NAME=${CD_PROJECT_NAME}
CD_PROJECTS_GROUP=${CD_PROJECTS_GROUP}
2024-04-29 08:58:37 +00:00
#
2024-04-28 10:44:28 +00:00
CD_PROJECTS_URL=${CD_PROJECTS_URL}
2024-04-29 08:58:37 +00:00
CD_PROJECT_URL=${CD_PROJECT_URL}
2024-04-28 10:44:28 +00:00
"
2024-04-27 09:56:16 +00:00
}
2024-04-27 17:56:19 +00:00
cd_set_dns_resolving () {
2024-04-26 12:18:44 +00:00
local server
2024-04-27 21:36:26 +00:00
local text=""
cd_step "Set DNS resolving"
2024-04-27 15:36:14 +00:00
for server in "${@}" ; do
2024-04-27 15:40:52 +00:00
text="${text}nameserver ${server}
2024-04-26 12:18:44 +00:00
"
done
2024-04-26 14:29:40 +00:00
cd_write "${CD_DNS_FILE}" "${text}"
2024-04-28 10:35:43 +00:00
cd_cat "${CD_DNS_FILE}"
2024-04-26 12:18:44 +00:00
}
2024-03-25 11:26:38 +00:00
2024-04-27 17:56:19 +00:00
cd_set_packages_repositories () {
2024-04-27 21:36:26 +00:00
cd_step "Set packages repositories"
2024-04-26 14:13:35 +00:00
case "${CD_OS_NAME}" in
2024-04-27 21:36:26 +00:00
"debian")
cd_write "/etc/apt/sources.list" "\
2024-04-26 14:13:35 +00:00
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
"
2024-04-28 11:00:18 +00:00
cd_cat "/etc/apt/sources.list"
2024-04-26 14:13:35 +00:00
;;
*) exit 1 ;;
esac
}
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-04-26 13:54:37 +00:00
case "${CD_OS_NAME}" in
2024-04-27 21:36:26 +00:00
"debian")
export DEBIAN_FRONTEND="noninteractive"
cd_write "/etc/apt/apt.conf.d/apt.conf" "\
2024-04-25 13:33:58 +00:00
Acquire::Check-Valid-Until True;
2024-04-05 15:49:25 +00:00
APT::Get::Show-Versions True;
2024-04-08 08:07:37 +00:00
APT::Install-Recommends False;
APT::Install-Suggests False;
2024-04-26 14:03:17 +00:00
Dir::Etc::SourceParts \"\";
2024-04-26 13:54:37 +00:00
"
2024-04-28 11:00:18 +00:00
cd_cat "/etc/apt/apt.conf.d/apt.conf"
2024-04-26 13:54:37 +00:00
;;
2024-04-26 13:57:23 +00:00
*) exit 1 ;;
2024-04-26 13:54:37 +00:00
esac
}
2024-04-27 17:56:19 +00:00
cd_set_https_verification_off () {
2024-04-27 21:36:26 +00:00
cd_step "Set HTTPS verification off"
2024-04-26 14:23:30 +00:00
case "${CD_OS_NAME}" in
2024-04-27 21:36:26 +00:00
"debian")
cd_write "/etc/apt/apt.conf.d/https" "\
2024-04-26 14:23:30 +00:00
Acquire::https::Verify-Peer False;
"
2024-04-28 11:00:18 +00:00
cd_cat "/etc/apt/apt.conf.d/https"
2024-04-26 14:23:30 +00:00
;;
*) exit 1 ;;
esac
}
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-04-26 14:27:36 +00:00
case "${CD_OS_NAME}" in
2024-04-27 21:36:26 +00:00
"debian") apt-get update || exit ;;
2024-04-26 14:27:36 +00:00
*) exit 1 ;;
esac
}
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-04-27 19:36:51 +00:00
case "${CD_OS_NAME}" in
2024-04-27 21:36:26 +00:00
"debian") cd_install_package "apt-utils" ;;
2024-04-27 19:36:51 +00:00
*) exit 1 ;;
esac
}
2024-04-27 17:56:19 +00:00
cd_install_ca () {
2024-04-27 21:36:26 +00:00
cd_step "Install CA"
2024-04-26 14:44:34 +00:00
case "${CD_OS_NAME}" in
2024-04-27 21:36:26 +00:00
"debian") cd_install_package "ca-certificates" ;;
2024-04-26 14:44:34 +00:00
*) exit 1 ;;
esac
}
2024-04-27 22:12:11 +00:00
cd_copy_ca () {
local target
cd_step "Copy CA"
case "${CD_OS_NAME}" in
"debian") target="/usr/local/share/ca-certificates" ;;
*) exit 1 ;;
esac
# TODO copy
}
2024-04-27 21:58:48 +00:00
cd_update_ca () {
cd_step "Update CA"
case "${CD_OS_NAME}" in
2024-04-28 11:00:18 +00:00
"debian") update-ca-certificates || exit ;;
2024-04-27 21:58:48 +00:00
*) exit 1 ;;
esac
}
2024-04-27 19:40:50 +00:00
cd_set_https_verification_on () {
2024-04-27 21:36:26 +00:00
cd_step "Set HTTPS verification on"
2024-04-26 14:50:25 +00:00
case "${CD_OS_NAME}" in
2024-04-28 11:00:18 +00:00
"debian") cd_rm "/etc/apt/apt.conf.d/https" ;;
2024-04-26 14:50:25 +00:00
*) exit 1 ;;
esac
}
2024-04-27 19:42:29 +00:00
cd_upgrade_packages () {
2024-04-27 21:36:26 +00:00
cd_step "Upgrade packages"
2024-04-26 14:35:15 +00:00
case "${CD_OS_NAME}" in
2024-04-27 21:36:26 +00:00
"debian") apt-get upgrade --yes || exit ;;
2024-04-26 14:35:15 +00:00
*) exit 1 ;;
esac
}
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-26 14:35:15 +00:00
case "${CD_OS_NAME}" in
2024-04-27 21:36:26 +00:00
"debian") cd_install_package "git" ;;
2024-04-26 14:35:15 +00:00
*) exit 1 ;;
esac
}
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-26 14:39:24 +00:00
case "${CD_OS_NAME}" in
2024-04-27 21:36:26 +00:00
"debian") cd_install_package "python3" ;;
2024-04-26 14:39:24 +00:00
*) exit 1 ;;
esac
}
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-04-26 14:53:41 +00:00
case "${CD_OS_NAME}" in
2024-04-27 21:36:26 +00:00
"debian") apt-get clean || exit ;;
2024-04-26 14:53:41 +00:00
*) exit 1 ;;
esac
}
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-28 21:29:31 +00:00
url="${CD_PROJECTS_URL}/${repository}"
echo -n "
${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-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-04-27 19:38:43 +00:00
#
2024-04-28 10:35:43 +00:00
cd_cat () {
if [ -f "${1}" ] ; then
2024-04-28 13:42:36 +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-04-27 19:38:43 +00:00
cd_install_package () {
if [ "${1}" ] ; then
case "${CD_OS_NAME}" in
2024-04-27 21:36:26 +00:00
"debian") apt-get install --yes "${1}" || exit ;;
2024-04-27 19:38:43 +00:00
*) exit 1 ;;
esac
fi
}
2024-04-28 11:00:18 +00:00
cd_rm () {
2024-04-28 21:29:31 +00:00
if [ -e "${1}" ] ; then
2024-04-28 13:42:36 +00:00
echo "× $(realpath "${1}")"
2024-04-28 21:29:31 +00:00
rm --recursive "${1}" || exit
2024-04-28 11:00:18 +00:00
fi
}
2024-04-27 19:24:59 +00:00
cd_step () {
if [ "${1}" ] ; then
CD_STEP=$((CD_STEP+1))
2024-04-28 21:41:12 +00:00
echo "\
\\
2024-04-27 19:24:59 +00:00
${CD_STEP} ${1}
2024-04-28 21:41:12 +00:00
/
2024-04-27 19:24:59 +00:00
"
fi
}
cd_write () {
local file="${1}"
local text="${2}"
if [ "${file}" ] ; then
echo -n "${text}" \
> "${file}" \
|| exit
fi
}
2024-04-27 19:38:43 +00:00
#
2024-04-27 15:57:12 +00:00
cd_main "${@}"