Compare commits

..

31 commits

Author SHA1 Message Date
76070daa43
*)
Some checks failed
/ job (push) Failing after 13m17s
2024-06-08 19:44:32 +02:00
e6f9e2d0dd
*) 2024-06-08 19:33:46 +02:00
9450e7cb8a
cd_list_working_directory 2024-06-08 19:21:59 +02:00
a9c8aa6b54
cd_write_python_module 2024-06-08 19:15:47 +02:00
640d651a78
" 2024-06-08 18:29:10 +02:00
c5852824a3
-n 2024-06-08 18:11:41 +02:00
d73dd66251
cd_project_url 2024-06-08 17:41:57 +02:00
3c16e68d91
cd_set_packages_repositories 2024-06-08 17:28:38 +02:00
d2f060b6a1
cd_set_dns_resolving 2024-06-08 17:20:13 +02:00
68c4444367
cd_install_python_modules 2024-06-08 17:15:53 +02:00
5fdf986cde
cd_write_ca_certificates 2024-06-08 16:39:44 +02:00
2a19db986c
cd_path 2024-06-08 16:21:46 +02:00
17d22283f8
cd_cat 2024-06-08 16:10:45 +02:00
a8be6fdf64
cd_echo 2024-06-08 16:09:18 +02:00
d786c87a78
cd_error_ 2024-06-08 16:08:06 +02:00
5e8dd6fec2
cd_grep_os 2024-06-08 13:24:43 +02:00
58262405bf
cd_install_package 2024-06-08 13:21:17 +02:00
5217951380
cd_ln_python 2024-06-08 12:45:43 +02:00
ff58a3b397
cd_ls 2024-06-08 12:42:10 +02:00
953fb81032
cd_mkdir 2024-06-08 12:37:06 +02:00
571497c5cf
cd_rm 2024-06-08 12:35:51 +02:00
1100cf4234
cd_sed 2024-06-08 12:32:21 +02:00
99af49e1a8
cd_openssl 2024-06-08 12:18:39 +02:00
891d3a4067
${@} → ${*} 2024-06-08 12:14:52 +02:00
be0f461304
cd_write 2024-06-08 12:05:37 +02:00
6a93b10cf8
echo 2024-06-08 11:56:38 +02:00
1a6241033f
echo -n → printf 2024-06-07 15:45:18 +02:00
013d3bb8f3
|| 2024-06-07 15:25:37 +02:00
1813a03803
unused 2024-06-07 15:20:51 +02:00
9421afcedb
"" 2024-06-07 10:36:24 +02:00
04d9661def
" 2024-06-07 10:26:18 +02:00
2 changed files with 209 additions and 182 deletions

315
cd.sh
View file

@ -1,11 +1,11 @@
#! /usr/bin/env sh #! /usr/bin/env sh
# defaults # defaults
[ "${CD_DNS}" ] || CD_DNS="\ [ -n "${CD_DNS}" ] || CD_DNS="\
9.9.9.9 \ 9.9.9.9 \
" "
[ "${CD_GIT_CHILD}" ] || CD_GIT_CHILD="cd" [ -n "${CD_GIT_CHILD}" ] || CD_GIT_CHILD="cd"
[ "${CD_GIT_PARENT}" ] || CD_GIT_PARENT="rwx" [ -n "${CD_GIT_PARENT}" ] || CD_GIT_PARENT="rwx"
# main # main
cd_main () { cd_main () {
@ -40,6 +40,9 @@ cd_main () {
cd_set_environment_variables () { cd_set_environment_variables () {
cd_step "Set environment variables" cd_step "Set environment variables"
# set path
CD_PATH="$(realpath "${0}")"
cd_echo "CD_PATH"
# set operating system id # set operating system id
CD_OS_ID="$(cd_grep_os ID)" CD_OS_ID="$(cd_grep_os ID)"
case "${CD_OS_ID}" in case "${CD_OS_ID}" in
@ -65,6 +68,7 @@ cd_set_environment_variables () {
"${CD_OS_DEBIAN}"|"${CD_OS_UBUNTU}") "${CD_OS_DEBIAN}"|"${CD_OS_UBUNTU}")
CD_OS_VERSION="$(cd_grep_os VERSION_CODENAME)" CD_OS_VERSION="$(cd_grep_os VERSION_CODENAME)"
;; ;;
*)
esac esac
# check operating system version # check operating system version
case "${CD_OS_ID}" in case "${CD_OS_ID}" in
@ -104,7 +108,9 @@ cd_set_environment_variables () {
*) cd_error_os "CD_OS_VERSION" ;; *) cd_error_os "CD_OS_VERSION" ;;
esac esac
;; ;;
*)
esac esac
cd_split
cd_echo "CD_OS_ID" "CD_OS_VERSION" cd_echo "CD_OS_ID" "CD_OS_VERSION"
# universal # universal
CD_DNS_FILE="/etc/resolv.conf" CD_DNS_FILE="/etc/resolv.conf"
@ -133,6 +139,7 @@ cd_set_environment_variables () {
CD_CA_ROOT="/usr/local/share/ca-certificates" CD_CA_ROOT="/usr/local/share/ca-certificates"
CD_CMD_CA="update-ca-certificates" CD_CMD_CA="update-ca-certificates"
;; ;;
*)
esac esac
cd_split cd_split
cd_echo "CD_CA_ROOT" "CD_CMD_CA" cd_echo "CD_CA_ROOT" "CD_CMD_CA"
@ -150,6 +157,7 @@ cd_set_environment_variables () {
"${CD_OS_ARCH}") "${CD_OS_ARCH}")
CD_PM="${CD_PM_PACMAN}" CD_PM="${CD_PM_PACMAN}"
;; ;;
*)
esac esac
cd_split cd_split
cd_echo "CD_PM" cd_echo "CD_PM"
@ -223,6 +231,7 @@ Dir::Etc::SourceParts \"\";
Acquire::https::Verify-Peer False; Acquire::https::Verify-Peer False;
" "
;; ;;
*)
esac esac
cd_split cd_split
cd_echo "CD_PM_CLEAN" \ cd_echo "CD_PM_CLEAN" \
@ -252,34 +261,36 @@ Acquire::https::Verify-Peer False;
"${CD_OS_UBUNTU}") "${CD_OS_UBUNTU}")
CD_URL_DEFAULT="http://archive.ubuntu.com/ubuntu" CD_URL_DEFAULT="http://archive.ubuntu.com/ubuntu"
;; ;;
*)
esac esac
CD_URL_CHOSEN="${CD_URL_DEFAULT}" CD_URL_CHOSEN="${CD_URL_DEFAULT}"
case "${CD_OS_ID}" in case "${CD_OS_ID}" in
"${CD_OS_ALMA}") "${CD_OS_ALMA}")
[ "${CD_URL_ALMA}" ] && CD_URL_CHOSEN="${CD_URL_ALMA}" [ -n "${CD_URL_ALMA}" ] && CD_URL_CHOSEN="${CD_URL_ALMA}"
;; ;;
"${CD_OS_ALPINE}") "${CD_OS_ALPINE}")
[ "${CD_URL_ALPINE}" ] && CD_URL_CHOSEN="${CD_URL_ALPINE}" [ -n "${CD_URL_ALPINE}" ] && CD_URL_CHOSEN="${CD_URL_ALPINE}"
;; ;;
"${CD_OS_ARCH}") "${CD_OS_ARCH}")
[ "${CD_URL_ARCH}" ] && CD_URL_CHOSEN="${CD_URL_ARCH}" [ -n "${CD_URL_ARCH}" ] && CD_URL_CHOSEN="${CD_URL_ARCH}"
;; ;;
"${CD_OS_DEBIAN}") "${CD_OS_DEBIAN}")
[ "${CD_URL_DEBIAN}" ] && CD_URL_CHOSEN="${CD_URL_DEBIAN}" \ [ -n "${CD_URL_DEBIAN}" ] && CD_URL_CHOSEN="${CD_URL_DEBIAN}" \
|| CD_URL_CHOSEN="https://deb.debian.org/debian" || CD_URL_CHOSEN="https://deb.debian.org/debian"
;; ;;
"${CD_OS_FEDORA}") "${CD_OS_FEDORA}")
[ "${CD_URL_FEDORA}" ] && CD_URL_CHOSEN="${CD_URL_FEDORA}" \ [ -n "${CD_URL_FEDORA}" ] && CD_URL_CHOSEN="${CD_URL_FEDORA}" \
|| CD_URL_CHOSEN="https://rpmfind.net/linux/fedora/linux/releases" || CD_URL_CHOSEN="https://rpmfind.net/linux/fedora/linux/releases"
;; ;;
"${CD_OS_ROCKY}") "${CD_OS_ROCKY}")
[ "${CD_URL_ROCKY}" ] && CD_URL_CHOSEN="${CD_URL_ROCKY}" \ [ -n "${CD_URL_ROCKY}" ] && CD_URL_CHOSEN="${CD_URL_ROCKY}" \
|| CD_URL_CHOSEN="https://dl.rockylinux.org/\$contentdir" || CD_URL_CHOSEN="https://dl.rockylinux.org/\$contentdir"
;; ;;
"${CD_OS_UBUNTU}") "${CD_OS_UBUNTU}")
[ "${CD_URL_UBUNTU}" ] && CD_URL_CHOSEN="${CD_URL_UBUNTU}" \ [ -n "${CD_URL_UBUNTU}" ] && CD_URL_CHOSEN="${CD_URL_UBUNTU}" \
|| CD_URL_CHOSEN="https://ubuntu.mirrors.ovh.net/ubuntu" || CD_URL_CHOSEN="https://ubuntu.mirrors.ovh.net/ubuntu"
;; ;;
*)
esac esac
cd_split cd_split
cd_echo "CD_URL_DEFAULT" "CD_URL_CHOSEN" cd_echo "CD_URL_DEFAULT" "CD_URL_CHOSEN"
@ -301,6 +312,7 @@ Acquire::https::Verify-Peer False;
case "${CD_OS_VERSION}" in case "${CD_OS_VERSION}" in
"bookworm") CD_PYTHON_COMMAND="python3.11" ;; "bookworm") CD_PYTHON_COMMAND="python3.11" ;;
"bullseye") CD_PYTHON_COMMAND="python3.9" ;; "bullseye") CD_PYTHON_COMMAND="python3.9" ;;
*)
esac esac
CD_PYTHON_PACKAGE="python3" CD_PYTHON_PACKAGE="python3"
;; ;;
@ -312,9 +324,11 @@ Acquire::https::Verify-Peer False;
case "${CD_OS_VERSION}" in case "${CD_OS_VERSION}" in
"noble") CD_PYTHON_COMMAND="python3.12" ;; "noble") CD_PYTHON_COMMAND="python3.12" ;;
"jammy") CD_PYTHON_COMMAND="python3.10" ;; "jammy") CD_PYTHON_COMMAND="python3.10" ;;
*)
esac esac
CD_PYTHON_PACKAGE="python3" CD_PYTHON_PACKAGE="python3"
;; ;;
*)
esac esac
# set python packages # set python packages
case "${CD_OS_ID}" in case "${CD_OS_ID}" in
@ -327,19 +341,20 @@ Acquire::https::Verify-Peer False;
"${CD_OS_DEBIAN}"|"${CD_OS_UBUNTU}") "${CD_OS_DEBIAN}"|"${CD_OS_UBUNTU}")
CD_PYTHON_PACKAGES="/usr/lib/${CD_PYTHON_ALIAS}/dist-packages" CD_PYTHON_PACKAGES="/usr/lib/${CD_PYTHON_ALIAS}/dist-packages"
;; ;;
*)
esac esac
cd_split cd_split
cd_echo "CD_PYTHON_COMMAND" "CD_PYTHON_PACKAGE" "CD_PYTHON_PACKAGES" cd_echo "CD_PYTHON_COMMAND" "CD_PYTHON_PACKAGE" "CD_PYTHON_PACKAGES"
# variables # variables
[ "${CD_CA_1}" ] && CD_CA=true [ -n "${CD_CA_1}" ] && CD_CA=true
# continuous integration platform # continuous integration platform
if [ "${GITHUB_ACTIONS}" ] ; then if [ -n "${GITHUB_ACTIONS}" ] ; then
# github → gitea → forgejo # github → gitea → forgejo
CD_SERVER_URL="${GITHUB_SERVER_URL}" CD_SERVER_URL="${GITHUB_SERVER_URL}"
CD_PROJECTS_GROUP="$(dirname "${GITHUB_REPOSITORY}")" CD_PROJECTS_GROUP="$(dirname "${GITHUB_REPOSITORY}")"
CD_PROJECT_NAME="$(basename "${GITHUB_REPOSITORY}")" CD_PROJECT_NAME="$(basename "${GITHUB_REPOSITORY}")"
CD_PROJECT_BRANCH="${GITHUB_REF_NAME}" CD_PROJECT_BRANCH="${GITHUB_REF_NAME}"
elif [ "${GITLAB_CI}" ] ; then elif [ -n "${GITLAB_CI}" ] ; then
# gitlab # gitlab
CD_SERVER_URL="${CI_SERVER_URL}" CD_SERVER_URL="${CI_SERVER_URL}"
CD_PROJECTS_GROUP="$(dirname "${CI_PROJECT_PATH}")" CD_PROJECTS_GROUP="$(dirname "${CI_PROJECT_PATH}")"
@ -349,13 +364,12 @@ Acquire::https::Verify-Peer False;
# unsupported # unsupported
cd_error_ci "ø" cd_error_ci "ø"
fi fi
[ "${CD_SERVER_URL}" ] || cd_error_ci "CD_SERVER_URL" [ -n "${CD_SERVER_URL}" ] || cd_error_ci "CD_SERVER_URL"
[ "${CD_PROJECTS_GROUP}" ] || cd_error_ci "CD_PROJECTS_GROUP" [ -n "${CD_PROJECTS_GROUP}" ] || cd_error_ci "CD_PROJECTS_GROUP"
[ "${CD_PROJECT_NAME}" ] || cd_error_ci "CD_PROJECT_NAME" [ -n "${CD_PROJECT_NAME}" ] || cd_error_ci "CD_PROJECT_NAME"
[ "${CD_PROJECT_BRANCH}" ] || cd_error_ci "CD_PROJECT_BRANCH" [ -n "${CD_PROJECT_BRANCH}" ] || cd_error_ci "CD_PROJECT_BRANCH"
# #
CD_PROJECTS_URL="${CD_SERVER_URL}/${CD_PROJECTS_GROUP}" CD_PROJECTS_URL="${CD_SERVER_URL}/${CD_PROJECTS_GROUP}"
CD_PROJECT_URL="${CD_PROJECTS_URL}/${CD_PROJECT_NAME}"
# #
cd_split cd_split
cd_echo "CD_CA" cd_echo "CD_CA"
@ -363,40 +377,42 @@ Acquire::https::Verify-Peer False;
cd_echo "CD_SERVER_URL" \ cd_echo "CD_SERVER_URL" \
"CD_PROJECTS_GROUP" "CD_PROJECT_NAME" "CD_PROJECT_BRANCH" "CD_PROJECTS_GROUP" "CD_PROJECT_NAME" "CD_PROJECT_BRANCH"
cd_split cd_split
cd_echo "CD_PROJECTS_URL" "CD_PROJECT_URL" cd_echo "CD_PROJECTS_URL"
# TODO move to Python # TODO move to Python
case "${CD_PM}" in case "${CD_PM}" in
"${CD_PM_APK}"|"${CD_PM_APT}") CD_PKG_SSH="openssh-client" ;; "${CD_PM_APK}"|"${CD_PM_APT}") CD_PKG_SSH="openssh-client" ;;
"${CD_PM_DNF}") CD_PKG_SSH="openssh-clients" ;; "${CD_PM_DNF}") CD_PKG_SSH="openssh-clients" ;;
"${CD_PM_PACMAN}") CD_PKG_SSH="openssh" ;; "${CD_PM_PACMAN}") CD_PKG_SSH="openssh" ;;
*)
esac esac
} }
cd_set_packages_repositories () { cd_set_packages_repositories () {
local expression
local file
cd_step "Set packages repositories" cd_step "Set packages repositories"
case "${CD_OS_ID}" in case "${CD_OS_ID}" in
"${CD_OS_ALMA}") "${CD_OS_ALMA}")
case "${CD_OS_VERSION}" in case "${CD_OS_VERSION}" in
"8") file="/etc/yum.repos.d/almalinux.repo" ;; "8") \
"9") file="/etc/yum.repos.d/almalinux-baseos.repo" ;; cd_set_packages_repositories__file="/etc/yum.repos.d/almalinux.repo" ;;
"9") \
cd_set_packages_repositories__file="/etc/yum.repos.d/almalinux-baseos.repo" ;;
*)
esac esac
cd_sed "${file}" \ cd_sed "${cd_set_packages_repositories__file}" \
"|^mirrorlist|# mirrorlist|" \ "|^mirrorlist|# mirrorlist|" \
"|${CD_URL_DEFAULT}|${CD_URL_CHOSEN}|" \ "|${CD_URL_DEFAULT}|${CD_URL_CHOSEN}|" \
"|^# baseurl|baseurl|" "|^# baseurl|baseurl|"
;; ;;
"${CD_OS_ALPINE}") "${CD_OS_ALPINE}")
file="/etc/apk/repositories" cd_set_packages_repositories__file="/etc/apk/repositories"
cd_write "${file}" "\ cd_write "${cd_set_packages_repositories__file}" "\
${CD_URL_CHOSEN}/v${CD_OS_VERSION}/main ${CD_URL_CHOSEN}/v${CD_OS_VERSION}/main
${CD_URL_CHOSEN}/v${CD_OS_VERSION}/community ${CD_URL_CHOSEN}/v${CD_OS_VERSION}/community
" "
;; ;;
"${CD_OS_DEBIAN}") "${CD_OS_DEBIAN}")
file="/etc/apt/sources.list" cd_set_packages_repositories__file="/etc/apt/sources.list"
cd_write "${file}" "\ cd_write "${cd_set_packages_repositories__file}" "\
deb ${CD_URL_CHOSEN} ${CD_OS_VERSION} main deb ${CD_URL_CHOSEN} ${CD_OS_VERSION} main
deb ${CD_URL_CHOSEN} ${CD_OS_VERSION}-backports main deb ${CD_URL_CHOSEN} ${CD_OS_VERSION}-backports main
deb ${CD_URL_CHOSEN} ${CD_OS_VERSION}-updates main deb ${CD_URL_CHOSEN} ${CD_OS_VERSION}-updates main
@ -405,23 +421,27 @@ deb ${CD_URL_CHOSEN}-security ${CD_OS_VERSION}-security main
;; ;;
"${CD_OS_ROCKY}") "${CD_OS_ROCKY}")
case "${CD_OS_VERSION}" in case "${CD_OS_VERSION}" in
"8") file="/etc/yum.repos.d/Rocky-BaseOS.repo" ;; "8") \
"9") file="/etc/yum.repos.d/rocky.repo" ;; cd_set_packages_repositories__file="/etc/yum.repos.d/Rocky-BaseOS.repo" ;;
"9") \
cd_set_packages_repositories__file="/etc/yum.repos.d/rocky.repo" ;;
*)
esac esac
cd_sed "${file}" \ cd_sed "${cd_set_packages_repositories__file}" \
"|^mirrorlist|# mirrorlist|" \ "|^mirrorlist|# mirrorlist|" \
"|${CD_URL_DEFAULT}|${CD_URL_CHOSEN}|" \ "|${CD_URL_DEFAULT}|${CD_URL_CHOSEN}|" \
"|^#baseurl|baseurl|" "|^#baseurl|baseurl|"
;; ;;
"${CD_OS_UBUNTU}") "${CD_OS_UBUNTU}")
file="/etc/apt/sources.list" cd_set_packages_repositories__file="/etc/apt/sources.list"
cd_write "${file}" "\ cd_write "${cd_set_packages_repositories__file}" "\
deb ${CD_URL_CHOSEN} ${CD_OS_VERSION} main deb ${CD_URL_CHOSEN} ${CD_OS_VERSION} main
deb ${CD_URL_CHOSEN} ${CD_OS_VERSION}-backports main deb ${CD_URL_CHOSEN} ${CD_OS_VERSION}-backports main
deb ${CD_URL_CHOSEN} ${CD_OS_VERSION}-updates main deb ${CD_URL_CHOSEN} ${CD_OS_VERSION}-updates main
deb ${CD_URL_CHOSEN} ${CD_OS_VERSION}-security main deb ${CD_URL_CHOSEN} ${CD_OS_VERSION}-security main
" "
;; ;;
*)
esac esac
} }
@ -432,6 +452,7 @@ cd_set_packages_configuration () {
"${CD_OS_DEBIAN}"|"${CD_OS_UBUNTU}") "${CD_OS_DEBIAN}"|"${CD_OS_UBUNTU}")
export DEBIAN_FRONTEND="noninteractive" export DEBIAN_FRONTEND="noninteractive"
;; ;;
*)
esac esac
} }
@ -439,11 +460,12 @@ cd_set_packages_configuration () {
cd_list_working_directory () { cd_list_working_directory () {
cd_step "List working directory" cd_step "List working directory"
cd_ls "$(realpath .)" cd_list_working_directory__path="$(realpath .)"
cd_ls "${cd_list_working_directory__path}"
} }
cd_set_https_verification_off () { cd_set_https_verification_off () {
if [ "${CD_CA}" -o "${CD_PM}" = "${CD_PM_APT}" ] ; then if [ -n "${CD_CA}" ] || [ "${CD_PM}" = "${CD_PM_APT}" ] ; then
cd_step "Set HTTPS verification off" cd_step "Set HTTPS verification off"
cd_mkdir "$(dirname "${CD_PM_HTTPS_PATH}")" cd_mkdir "$(dirname "${CD_PM_HTTPS_PATH}")"
cd_write "${CD_PM_HTTPS_PATH}" "${CD_PM_HTTPS_TEXT}" cd_write "${CD_PM_HTTPS_PATH}" "${CD_PM_HTTPS_TEXT}"
@ -451,14 +473,13 @@ cd_set_https_verification_off () {
} }
cd_set_dns_resolving () { cd_set_dns_resolving () {
local server
local text=""
cd_step "Set DNS resolving" cd_step "Set DNS resolving"
for server in ${CD_DNS} ; do for cd_set_dns_resolving__server in ${CD_DNS} ; do
text="${text}nameserver ${server} cd_set_dns_resolving__text="${cd_set_dns_resolving__text}\
nameserver ${cd_set_dns_resolving__server}
" "
done done
cd_write "${CD_DNS_FILE}" "${text}" cd_write "${CD_DNS_FILE}" "${cd_set_dns_resolving__text}"
} }
cd_update_packages_catalog () { cd_update_packages_catalog () {
@ -477,20 +498,22 @@ cd_install_ca_certificates () {
} }
cd_write_ca_certificates () { cd_write_ca_certificates () {
local index
local path
local text
cd_step "Write CA certificates" cd_step "Write CA certificates"
cd_mkdir "${CD_CA_ROOT}" cd_mkdir "${CD_CA_ROOT}"
index=1 cd_write_ca_certificates__index=1
eval "text=\"\${CD_CA_${index}}\"" eval "cd_write_ca_certificates__text=\
while [ "${text}" ] ; do \"\${CD_CA_${cd_write_ca_certificates__index}}\""
path="${CD_CA_ROOT}/${index}.crt" while [ -n "${cd_write_ca_certificates__text}" ] ; do
cd_write_ca_certificates__path="\
${CD_CA_ROOT}/${cd_write_ca_certificates__index}.crt"
cd_split cd_split
cd_write "${path}" "${text}" cd_write \
cd_openssl "${path}" "${cd_write_ca_certificates__path}" \
index=$((index+1)) "${cd_write_ca_certificates__text}"
eval "text=\"\${CD_CA_${index}}\"" cd_openssl "${cd_write_ca_certificates__path}"
cd_write_ca_certificates__index=$((cd_write_ca_certificates__index+1))
eval "cd_write_ca_certificates__text=\
\"\${CD_CA_${cd_write_ca_certificates__index}}\""
done done
} }
@ -539,169 +562,174 @@ cd_clean_packages_cache () {
} }
cd_install_python_modules () { cd_install_python_modules () {
local path
local repository
local root
local url
cd_step "Install Python modules" cd_step "Install Python modules"
root="$(mktemp --directory)" || exit cd_install_python_modules__root="$(mktemp --directory)" || exit
echo "${root}" echo "${cd_install_python_modules__root}"
for repository in "${CD_GIT_CHILD}" "${CD_GIT_PARENT}" ; do for cd_install_python_modules__repository \
in "${CD_GIT_CHILD}" "${CD_GIT_PARENT}" ; do
cd_split cd_split
url="${CD_PROJECTS_URL}/${repository}" cd_install_python_modules__url="\
echo -n "\ ${CD_PROJECTS_URL}/${cd_install_python_modules__repository}"
${url} echo "\
${cd_install_python_modules__url}
" "
git clone \ git clone \
"${url}" "${root}/${repository}" \ "${cd_install_python_modules__url}" \
"${cd_install_python_modules__root}\
/${cd_install_python_modules__repository}" \
|| exit || exit
path="${root}/${repository}/${repository}" cd_install_python_modules__path="\
echo -n "\ ${cd_install_python_modules__root}\
${path} /${cd_install_python_modules__repository}\
/${cd_install_python_modules__repository}"
echo "\
${cd_install_python_modules__path}
${CD_PYTHON_PACKAGES} ${CD_PYTHON_PACKAGES}"
"
cp --recursive \ cp --recursive \
"${path}" "${CD_PYTHON_PACKAGES}" \ "${cd_install_python_modules__path}" "${CD_PYTHON_PACKAGES}" \
|| exit || exit
done done
cd_split cd_split
cd_ls "${CD_PYTHON_PACKAGES}" cd_ls "${CD_PYTHON_PACKAGES}"
cd_split cd_split
cd_rm "${root}" cd_rm "${cd_install_python_modules__root}"
} }
cd_write_python_module () { cd_write_python_module () {
cd_step "Write Python module" cd_step "Write Python module"
cd_write "${CD_PYTHON_PACKAGES}/env.py" "\ for cd_write_python_module__variable \
$(cd_echo CD_OPEN CD_DOWN) in OPEN DOWN VERT SPLT __UP SHUT OS_ID OS_VERSION ; do
$(cd_echo CD_VERT CD_SPLT) cd_write_python_module__value="\
$(cd_echo CD___UP CD_SHUT) $(cd_echo "CD_${cd_write_python_module__variable}")"
cd_write_python_module__text="${cd_write_python_module__text}\
$(cd_echo CD_OS_ID CD_OS_VERSION) ${cd_write_python_module__value}
"
done
cd_write "${CD_PYTHON_PACKAGES}/env.py" "${cd_write_python_module__text}\
CD_STEP = $((CD_STEP+1)) CD_STEP = $((CD_STEP+1))
" "
} }
cd_switch_to_python () { cd_switch_to_python () {
local path
cd_step "Switch to Python" cd_step "Switch to Python"
# echo "\
path="$(realpath "${0}")" ${CD_PATH}
echo -n "\
${path}
${CD_PYTHON_PACKAGES}/${CD_GIT_CHILD} ${CD_PYTHON_PACKAGES}/${CD_GIT_CHILD}"
"
"${CD_PYTHON_ALIAS}" -m "${CD_GIT_CHILD}" "${@}" "${CD_PYTHON_ALIAS}" -m "${CD_GIT_CHILD}" "${@}"
} }
# functions # functions
cd_cat () { cd_cat () {
if [ -f "${1}" ] ; then cd_cat__file="${1}"
cd_open "${1}" if [ -n "${cd_cat__file}" ] ; then
cat "${1}" || exit cd_open "${cd_cat__file}"
cd_shut "${1}" cat "${cd_cat__file}" || exit
cd_shut "${cd_cat__file}"
fi fi
} }
cd_echo () { cd_echo () {
local name if [ -n "${1}" ] ; then
if [ "${1}" ] ; then for cd_echo__name in "${@}" ; do
for name in "${@}" ; do eval "echo ${cd_echo__name} = \\\"\${${cd_echo__name}}\\\""
eval "echo ${name} = \\\"\${${name}}\\\""
done done
fi fi
} }
cd_error_ci () { cd_error_ci () {
echo "× CI: ${1}" echo "× CI: ${*}"
exit ${CD_ERROR_CI} exit "${CD_ERROR_CI}"
} }
cd_error_os () { cd_error_os () {
local variable="${1}" cd_error_os__variable="${1}"
echo -n "× OS: " printf "× OS: "
cd_echo "${variable}" cd_echo "${cd_error_os__variable}"
exit ${CD_ERROR_OS} exit "${CD_ERROR_OS}"
} }
cd_grep_os () { cd_grep_os () {
local variable="${1}" cd_grep_os__variable="${1}"
if [ "${variable}" ] ; then if [ -n "${cd_grep_os__variable}" ] ; then
grep "^${variable}=" "/etc/os-release" \ grep "^${cd_grep_os__variable}=" "/etc/os-release" \
| sed "s|^${variable}=||" \ | sed "s|^${cd_grep_os__variable}=||" \
| sed "s|^\"\(.*\)\"$|\1|" | sed "s|^\"\(.*\)\"$|\1|"
fi fi
} }
cd_install_package () { cd_install_package () {
if [ "${1}" ] ; then cd_install_package__name="${1}"
${CD_PM_INSTALL} "${1}" || exit if [ -n "${cd_install_package__name}" ] ; then
${CD_PM_INSTALL} "${cd_install_package__name}" || exit
fi fi
} }
cd_ln_python () { cd_ln_python () {
local command="${1}" cd_ln_python__command="${1}"
if [ "${command}" ] ; then if [ -n "${cd_ln_python__command}" ] ; then
echo "${CD_PYTHON_ALIAS}${command}" echo "${CD_PYTHON_ALIAS}${cd_ln_python__command}"
ln -f -s "${command}" "/usr/bin/${CD_PYTHON_ALIAS}" || exit ln -f -s "${cd_ln_python__command}" "/usr/bin/${CD_PYTHON_ALIAS}" \
|| exit
fi fi
} }
cd_ls () { cd_ls () {
if [ -d "${1}" ] ; then cd_ls__path="${1}"
cd_open "${1}" if [ -n "${cd_ls__path}" ] ; then
ls -a -l "${1}" || exit cd_open "${cd_ls__path}"
cd_shut "${1}" ls -a -l "${cd_ls__path}" || exit
cd_shut "${cd_ls__path}"
fi fi
} }
cd_mkdir () { cd_mkdir () {
if [ "${1}" ] ; then cd_mkdir__path="${1}"
echo "${1}" if [ -n "${cd_mkdir__path}" ] ; then
mkdir --parents "${1}" || exit echo "${cd_mkdir__path}"
mkdir --parents "${cd_mkdir__path}" || exit
fi fi
} }
cd_open () { cd_open () {
echo "${CD_OPEN}${@}" echo "${CD_OPEN}${*}"
} }
cd_openssl () { cd_openssl () {
local file="${1}" cd_openssl__file="${1}"
if [ -f "${file}" ] ; then if [ -f "${cd_openssl__file}" ] ; then
openssl x509 -noout -text -in "${file}" || exit openssl x509 \
-in "${cd_openssl__file}" \
-noout -text \
|| exit
fi fi
} }
cd_rm () { cd_rm () {
local path="${1}" cd_rm__path="${1}"
if [ -e "${path}" ] ; then if [ -e "${cd_rm__path}" ] ; then
echo "${path}" echo "${cd_rm__path}"
rm -r "${path}" || exit rm -r "${cd_rm__path}" || exit
fi fi
} }
cd_sed () { cd_sed () {
local expression cd_sed__file="${1}"
local file="${1}" shift
shift if [ -f "${cd_sed__file}" ] ; then
if [ -f "${file}" ] ; then cd_cat "${cd_sed__file}"
cd_cat "${file}" for cd_sed__regex in "${@}" ; do
for expression in "${@}" ; do sed --in-place "s${cd_sed__regex}g" "${cd_sed__file}" \
sed --in-place "s${expression}g" "${file}" \ && cd_cat "${cd_sed__file}" \
&& cd_cat "${file}" \
|| exit || exit
done done
fi fi
} }
cd_shut () { cd_shut () {
echo "${CD_SHUT}${@}" echo "${CD_SHUT}${*}"
} }
cd_split () { cd_split () {
@ -710,21 +738,20 @@ cd_split () {
cd_step () { cd_step () {
CD_STEP=$((CD_STEP+1)) CD_STEP=$((CD_STEP+1))
echo -n "\ echo "\
${CD_DOWN} ${CD_DOWN}
${CD_VERT} ${CD_STEP} ${@} ${CD_VERT} ${CD_STEP} ${*}
${CD___UP} ${CD___UP}"
"
} }
cd_write () { cd_write () {
local file="${1}" cd_write__file="${1}"
local text="${2}" cd_write__text="${2}"
if [ "${file}" ] ; then if [ -n "${cd_write__file}" ] ; then
[ -f "${file}" ] && cd_cat "${file}" [ -f "${cd_write__file}" ] && cd_cat "${cd_write__file}"
echo "${1}" echo "${cd_write__file}"
echo -n "${text}" > "${file}" || exit printf "%s" "${cd_write__text}" > "${cd_write__file}" || exit
cd_cat "${file}" cd_cat "${cd_write__file}"
fi fi
} }

View file

@ -6,7 +6,7 @@ import env
from rwx import fs from rwx import fs
from rwx import ps from rwx import ps
COMMANDS_PREFIX = 'cd-' COMMANDS_PREFIX = "cd-"
projects = Projects(os.environ) projects = Projects(os.environ)
project = Project(projects) project = Project(projects)
@ -17,8 +17,8 @@ def cd_browse_workspace():
def cd_build_project(): def cd_build_project():
for extension in ['py', 'sh']: for extension in ["py", "sh"]:
path = os.path.join(project.root, f'build.{extension}') path = os.path.join(project.root, f"build.{extension}")
if os.path.exists(path): if os.path.exists(path):
ps.run(path) ps.run(path)
break break
@ -27,14 +27,14 @@ def cd_build_project():
def cd_clone_branch(): def cd_clone_branch():
print(f'''\ print(f"""\
{project.url} {project.url}
''', end=str(), flush=True) """, end="", flush=True)
ps.run('git', ps.run("git",
'clone', "clone",
'--branch', project.branch, "--branch", project.branch,
'--', "--",
project.url, project.url,
project.root, project.root,
) )
@ -42,25 +42,25 @@ def cd_clone_branch():
def cd_list_environment(): def cd_list_environment():
for variable, value in sorted(projects.environment.items()): for variable, value in sorted(projects.environment.items()):
print(variable, '=', value) print(variable, "=", value)
def cd_synchronize(): def cd_synchronize():
host = 'rwx.work' host = "rwx.work"
source = 'out' source = "out"
user = 'cd' user = "cd"
# #
root = os.sep.join([str(), root = os.sep.join(["",
user, project.branch, projects.group, project.name]) user, project.branch, projects.group, project.name])
# #
target = f'{user}@{host}:{root}' target = f"{user}@{host}:{root}"
ps.run('rsync', ps.run("rsync",
'--archive', "--archive",
'--delete-before', "--delete-before",
'--verbose', "--verbose",
f'{source}/', f"{source}/",
f'{target}/', f"{target}/",
'--dry-run', "--dry-run",
) )
@ -84,38 +84,38 @@ def cat(file: str):
def install_commands(path): def install_commands(path):
step('Install commands') step("Install commands")
user = '/usr/local/bin' user = "/usr/local/bin"
for command in [ for command in [
'browse-workspace', "browse-workspace",
'build-project', "build-project",
'clone-branch', "clone-branch",
'list-environment', "list-environment",
'synchronize', "synchronize",
]: ]:
print(command) print(command)
os.symlink(path, os.path.join(user, f'{COMMANDS_PREFIX}{command}')) os.symlink(path, os.path.join(user, f"{COMMANDS_PREFIX}{command}"))
def set_ssh(*arguments): def set_ssh(*arguments):
step('Set SSH') step("Set SSH")
# #
ssh_key, ssh_hosts = arguments ssh_key, ssh_hosts = arguments
# #
ssh_type = 'ed25519' ssh_type = "ed25519"
# #
home = os.path.expanduser('~') home = os.path.expanduser("~")
# #
ssh = os.path.join(home, '.ssh') ssh = os.path.join(home, ".ssh")
os.makedirs(ssh, exist_ok=True) os.makedirs(ssh, exist_ok=True)
os.chmod(ssh, 0o700) os.chmod(ssh, 0o700)
# #
key = os.path.join(ssh, f'id_{ssh_type}') key = os.path.join(ssh, f"id_{ssh_type}")
if ssh_key: if ssh_key:
fs.write(key, ssh_key) fs.write(key, ssh_key)
os.chmod(key, 0o400) os.chmod(key, 0o400)
# #
known = os.path.join(ssh, 'known_hosts') known = os.path.join(ssh, "known_hosts")
if ssh_hosts: if ssh_hosts:
fs.write(known, ssh_hosts) fs.write(known, ssh_hosts)
os.chmod(known, 0o400) os.chmod(known, 0o400)
@ -125,12 +125,12 @@ def set_ssh(*arguments):
def open(*arguments): def open(*arguments):
print(env.CD_OPEN, end=str()) print(env.CD_OPEN, end="")
print(*arguments, flush=True) print(*arguments, flush=True)
def shut(*arguments): def shut(*arguments):
print(env.CD_SHUT, end=str()) print(env.CD_SHUT, end="")
print(*arguments, flush=True) print(*arguments, flush=True)