spcd/cd.sh

264 lines
5.9 KiB
Bash
Raw Normal View History

2024-04-26 13:38:22 +00:00
#! /usr/bin/env bash
2024-04-26 11:38:38 +00:00
CD_DNS_SERVERS=(
2024-03-25 11:26:38 +00:00
'9.9.9.9'
)
2024-04-26 23:51:54 +00:00
CD_NAME='cd'
2024-04-26 18:09:11 +00:00
CD_REPOSITORIES_PARENT='rwx'
2024-04-26 11:38:38 +00:00
CD_DNS_FILE='/etc/resolv.conf'
case "${CD_OS_NAME}" in
'debian')
2024-04-26 18:53:58 +00:00
CD_PYTHON_COMMAND='python3'
2024-04-26 18:29:26 +00:00
CD_PYTHON_PACKAGES='/usr/lib/python3/dist-packages'
2024-04-26 11:38:38 +00:00
case "${CD_OS_VERSION}" in
'bookworm')
2024-04-26 16:54:09 +00:00
echo 'TODO'
2024-04-26 11:38:38 +00:00
;;
*)
echo 'CD_OS_VERSION'
exit 2
;;
esac
;;
*)
echo 'CD_OS_NAME'
exit 1
;;
esac
2024-04-26 23:46:40 +00:00
# project / branch
[ "${CI_COMMIT_BRANCH}" ] && CD_PROJECT_BRANCH="${CI_COMMIT_BRANCH}"
[ "${GITHUB_REF_NAME}" ] && CD_PROJECT_BRANCH="${GITHUB_REF_NAME}"
if [ "${CD_PROJECT_BRANCH}" ] ; then
echo "CD_PROJECT_BRANCH = ${CD_PROJECT_BRANCH}"
else
echo 'CD_PROJECT_BRANCH'
exit 3
fi
2024-04-26 23:37:18 +00:00
# project / name
[ "${CI_PROJECT_PATH}" ] \
&& CD_PROJECT_NAME="$(basename "${CI_PROJECT_PATH}")"
2024-04-26 23:10:30 +00:00
[ "${GITHUB_REPOSITORY}" ] \
&& CD_PROJECT_NAME="$(basename "${GITHUB_REPOSITORY}")"
if [ "${CD_PROJECT_NAME}" ] ; then
2024-04-26 23:19:08 +00:00
echo "CD_PROJECT_NAME = ${CD_PROJECT_NAME}"
2024-04-26 23:10:30 +00:00
else
echo 'CD_PROJECT_NAME'
exit 3
fi
2024-04-26 23:43:14 +00:00
# projects / group
[ "${CI_PROJECT_PATH}" ] \
&& CD_PROJECTS_GROUP="$(dirname "${CI_PROJECT_PATH}")"
[ "${GITHUB_REPOSITORY}" ] \
&& CD_PROJECTS_GROUP="$(dirname "${GITHUB_REPOSITORY}")"
if [ "${CD_PROJECTS_GROUP}" ] ; then
echo "CD_PROJECTS_GROUP = ${CD_PROJECTS_GROUP}"
else
echo 'CD_PROJECTS_GROUP'
exit 3
fi
2024-04-26 23:59:16 +00:00
# projects / url
[ "${CI_SERVER_URL}" ] && CD_PROJECTS_URL="${CI_SERVER_URL}"
[ "${GITHUB_SERVER_URL}" ] && CD_PROJECTS_URL="${GITHUB_SERVER_URL}"
if [ "${CD_PROJECTS_URL}" ] ; then
CD_PROJECTS_URL+="/${CD_PROJECTS_GROUP}"
echo "CD_PROJECTS_URL = ${CD_PROJECTS_URL}"
2024-04-26 23:33:12 +00:00
else
2024-04-26 23:59:16 +00:00
echo 'CD_PROJECTS_URL'
2024-04-26 23:33:12 +00:00
exit 3
fi
2024-04-26 23:55:10 +00:00
# project / url
2024-04-26 23:59:16 +00:00
CD_PROJECT_URL="${CD_PROJECTS_URL}/${CD_PROJECT_NAME}"
2024-04-27 00:00:06 +00:00
echo "CD_PROJECT_URL = ${CD_PROJECT_URL}"
2024-03-25 11:26:38 +00:00
2024-04-26 12:18:44 +00:00
function cd_step {
2024-04-26 11:59:41 +00:00
if [ "${1}" ] ; then
2024-04-26 13:38:22 +00:00
((CD_STEP++))
2024-04-26 11:59:41 +00:00
echo "
${CD_STEP}${1}
2024-04-25 14:01:18 +00:00
"
2024-04-26 11:59:41 +00:00
fi
2024-04-26 11:44:31 +00:00
}
2024-04-26 12:18:44 +00:00
function cd_write {
2024-04-26 12:07:46 +00:00
local file="${1}"
local text="${2}"
if [ "${file}" ] ; then
echo -n "${text}" \
> "${file}" \
|| exit
fi
}
2024-04-26 13:54:37 +00:00
function cd_set_dns_resolving {
2024-04-26 12:18:44 +00:00
local server
local text=''
2024-04-26 14:17:49 +00:00
cd_step "${FUNCNAME}"
2024-04-26 12:18:44 +00:00
for server in "${CD_DNS_SERVERS[@]}" ; do
text+="nameserver ${server}
"
done
2024-04-26 14:29:40 +00:00
cd_write "${CD_DNS_FILE}" "${text}"
2024-04-26 12:18:44 +00:00
}
2024-03-25 11:26:38 +00:00
2024-04-26 14:13:35 +00:00
function cd_set_packages_repositories {
2024-04-26 14:17:49 +00:00
cd_step "${FUNCNAME}"
2024-04-26 14:13:35 +00:00
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
"
;;
*) exit 1 ;;
esac
}
2024-04-26 13:54:37 +00:00
function cd_set_packages_configuration {
2024-04-26 14:17:49 +00:00
cd_step "${FUNCNAME}"
2024-04-26 13:54:37 +00:00
case "${CD_OS_NAME}" in
'debian')
2024-04-26 15:15:00 +00:00
export DEBIAN_FRONTEND='noninteractive'
2024-04-26 14:13:35 +00:00
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-26 13:57:23 +00:00
*) exit 1 ;;
2024-04-26 13:54:37 +00:00
esac
}
2024-04-26 14:23:30 +00:00
function cd_set_https_verification_off {
cd_step "${FUNCNAME}"
case "${CD_OS_NAME}" in
'debian')
cd_write '/etc/apt/apt.conf.d/https' "\
Acquire::https::Verify-Peer False;
"
;;
*) exit 1 ;;
esac
}
2024-04-26 14:27:36 +00:00
function cd_update_packages_catalog {
cd_step "${FUNCNAME}"
case "${CD_OS_NAME}" in
2024-04-26 14:29:40 +00:00
'debian') apt-get update || exit ;;
2024-04-26 14:27:36 +00:00
*) exit 1 ;;
esac
}
2024-04-26 14:44:34 +00:00
function cd_install_package {
2024-04-26 14:50:25 +00:00
if [ "${1}" ] ; then
case "${CD_OS_NAME}" in
2024-04-26 15:15:00 +00:00
'debian') apt-get install --yes "${1}" || exit ;;
2024-04-26 14:50:25 +00:00
*) exit 1 ;;
esac
fi
}
function cd_install_ca {
2024-04-26 14:44:34 +00:00
cd_step "${FUNCNAME}"
case "${CD_OS_NAME}" in
2024-04-26 14:50:25 +00:00
'debian') cd_install_package 'ca-certificates' ;;
2024-04-26 14:44:34 +00:00
*) exit 1 ;;
esac
}
2024-04-26 14:50:25 +00:00
function cd_install_git {
cd_step "${FUNCNAME}"
case "${CD_OS_NAME}" in
'debian') cd_install_package 'git' ;;
*) exit 1 ;;
esac
}
2024-04-26 15:02:14 +00:00
function cd_install_packages_tools {
cd_step "${FUNCNAME}"
case "${CD_OS_NAME}" in
'debian') cd_install_package 'apt-utils' ;;
*) exit 1 ;;
esac
}
2024-04-26 14:50:25 +00:00
function cd_install_python {
2024-04-26 14:35:15 +00:00
cd_step "${FUNCNAME}"
case "${CD_OS_NAME}" in
2024-04-26 14:50:25 +00:00
'debian') cd_install_package 'python3' ;;
2024-04-26 14:35:15 +00:00
*) exit 1 ;;
esac
}
function cd_set_https_verification_on {
cd_step "${FUNCNAME}"
case "${CD_OS_NAME}" in
'debian') rm '/etc/apt/apt.conf.d/https' ;;
*) exit 1 ;;
esac
}
2024-04-26 14:39:24 +00:00
function cd_upgrade_packages {
cd_step "${FUNCNAME}"
case "${CD_OS_NAME}" in
'debian') apt-get upgrade --yes || exit ;;
*) exit 1 ;;
esac
}
2024-04-26 14:53:41 +00:00
function cd_clean_packages_cache {
cd_step "${FUNCNAME}"
case "${CD_OS_NAME}" in
'debian') apt-get clean || exit ;;
*) exit 1 ;;
esac
}
2024-04-26 16:54:09 +00:00
function cd_install {
2024-04-26 18:09:11 +00:00
local repository
local root
2024-04-26 16:54:09 +00:00
cd_step "${FUNCNAME}"
2024-04-26 18:09:11 +00:00
root="$(mktemp --directory)" || exit
for repository in \
"${CD_REPOSITORIES_PARENT}" \
2024-04-26 23:51:54 +00:00
"${CD_NAME}" \
2024-04-26 18:09:11 +00:00
; do
git clone \
2024-04-26 23:59:16 +00:00
"${CD_PROJECTS_URL}/${repository}" "${root}/${repository}" \
2024-04-26 18:29:26 +00:00
|| exit
2024-04-26 19:30:36 +00:00
cp --recursive \
2024-04-26 19:01:27 +00:00
"${root}/${repository}/${repository}" "${CD_PYTHON_PACKAGES}" \
2024-04-26 18:29:26 +00:00
|| exit
2024-04-26 18:09:11 +00:00
done
2024-04-26 19:01:27 +00:00
rm --force --recursive "${root}" || exit
2024-04-26 16:54:09 +00:00
}
2024-04-26 17:00:43 +00:00
function cd_bootstrap {
cd_step "${FUNCNAME}"
2024-04-26 23:51:54 +00:00
"${CD_PYTHON_COMMAND}" -m "${CD_NAME}"
2024-04-26 17:00:43 +00:00
}
2024-04-26 13:54:37 +00:00
function cd_main {
cd_set_dns_resolving
2024-04-26 14:13:35 +00:00
cd_set_packages_repositories
2024-04-26 13:54:37 +00:00
cd_set_packages_configuration
2024-04-26 14:27:36 +00:00
cd_set_https_verification_off
cd_update_packages_catalog
2024-04-26 15:02:14 +00:00
cd_install_packages_tools
2024-04-26 14:35:15 +00:00
cd_install_ca
cd_set_https_verification_on
2024-04-26 14:39:24 +00:00
cd_update_packages_catalog
cd_upgrade_packages
2024-04-26 14:50:25 +00:00
cd_install_git
cd_install_python
2024-04-26 14:53:41 +00:00
cd_clean_packages_cache
2024-04-26 16:54:09 +00:00
cd_install
2024-04-26 17:00:43 +00:00
cd_bootstrap
2024-04-26 12:18:44 +00:00
}
cd_main