From abd5eee3922b18a25ba59bb5a5d59a969ab5deb9 Mon Sep 17 00:00:00 2001 From: Markus Heiser Date: Tue, 16 Nov 2021 18:00:47 +0100 Subject: [PATCH 1/2] [mod] Tools to install and maintain golang [1] binaries & packages. [1] https://golang.org/doc/devel/release#policy A simple *helloworld* test with user 'my_user' : sudo -H adduser my_user ./manage go.golang go1.17.3 my_user ./manage go.install github.com/go-training/helloworld@latest my_user ./manage go.bash my_user $ helloword Hello World!! Don't forget to remove 'my_user': sudo -H deluser --remove-home my_user Signed-off-by: Markus Heiser --- Makefile | 1 + manage | 4 + utils/lib_go.sh | 214 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 219 insertions(+) create mode 100755 utils/lib_go.sh diff --git a/Makefile b/Makefile index 801323361..eb0fa3f79 100644 --- a/Makefile +++ b/Makefile @@ -65,6 +65,7 @@ test.shell: utils/lib.sh \ utils/lib_install.sh \ utils/lib_static.sh \ + utils/lib_go.sh \ utils/filtron.sh \ utils/searx.sh \ utils/morty.sh \ diff --git a/manage b/manage index 0c7b04fb8..b61b4a19b 100755 --- a/manage +++ b/manage @@ -9,6 +9,9 @@ source "$(dirname "${BASH_SOURCE[0]}")/utils/lib.sh" # shellcheck source=utils/lib_static.sh source "$(dirname "${BASH_SOURCE[0]}")/utils/lib_static.sh" +# shellcheck source=utils/lib_go.sh +source "$(dirname "${BASH_SOURCE[0]}")/utils/lib_go.sh" + # config PYOBJECTS="searx" @@ -91,6 +94,7 @@ themes.: pygments.: less : build LESS files for pygments EOF + go.help static_help } diff --git a/utils/lib_go.sh b/utils/lib_go.sh new file mode 100755 index 000000000..314204e1a --- /dev/null +++ b/utils/lib_go.sh @@ -0,0 +1,214 @@ +#!/usr/bin/env bash +# -*- coding: utf-8; mode: sh indent-tabs-mode: nil -*- +# SPDX-License-Identifier: AGPL-3.0-or-later +# +# Tools to install and maintain golang [1] binaries & packages. +# +# [1] https://golang.org/doc/devel/release#policy +# +# A simple *helloworld* test with user 'my_user' : +# +# sudo -H adduser my_user +# ./manage go.golang go1.17.3 my_user +# ./manage go.install github.com/go-training/helloworld@latest my_user +# ./manage go.bash my_user +# $ helloword +# Hello World!! +# +# Don't forget to remove 'my_user': sudo -H deluser --remove-home my_user + +# shellcheck source=utils/lib.sh +. /dev/null + +# shellcheck disable=SC2034 +declare main_cmd + +# configure golang environment +# ---------------------------- + +[[ -z "${GO_VERSION}" ]] && GO_VERSION="go1.17.3" + +GO_DL_URL="https://golang.org/dl" + +# implement go functions +# ----------------------- + +go.help(){ + cat < [filename|sha256|size] + # + # kind: [archive|source|installer] + # os: [darwin|freebsd|linux|windows] + # arch: [amd64|arm64|386|armv6l|ppc64le|s390x] + + python3 - "$@" < [] + + local version fname sha size user userpr + local buf=() + + version="${1:-${GO_VERSION}}" + user="${2:-${USERNAME}}" + userpr=" ${_Yellow}|${user}|${_creset} " + + rst_title "Install Go in ${user}'s HOME" section + + mapfile -t buf < <( + go.ver_info "${version}" archive "$(go.os)" "$(go.arch)" filename sha256 size + ) + + if [ ${#buf[@]} -eq 0 ]; then + die 42 "can't find info of golang version: ${version}" + fi + fname="${buf[0]}" + sha="${buf[1]}" + size="$(numfmt --to=iec "${buf[2]}")" + + info_msg "Download go binary ${fname} (${size}B)" + cache_download "${GO_DL_URL}/${fname}" "${fname}" + + pushd "${CACHE}" &> /dev/null + echo "${sha} ${fname}" > "${fname}.sha256" + if ! sha256sum -c "${fname}.sha256" >/dev/null; then + die 42 "downloaded file ${fname} checksum does not match" + else + info_msg "${fname} checksum OK" + fi + popd &> /dev/null + + info_msg "install golang" + tee_stderr 0.1 < \$HOME/.go_env +echo "export PATH=\$HOME/local/go/bin:\\\$GOPATH/bin:\\\$PATH" >> \$HOME/.go_env +EOF + info_msg "test golang installation" + sudo -i -u "${user}" < [] + + local package user userpr + + package="${1}" + user="${2:-${USERNAME}}" + userpr=" ${_Yellow}|${user}|${_creset} " + + if [ -z "${package}" ]; then + die 42 "${FUNCNAME[0]}() - missing argument: " + fi + tee_stderr 0.1 <] + + local user + user="${1:-${USERNAME}}" + sudo -i -u "${user}" bash --init-file "~${user}/.go_env" +} + +go.version(){ + local user + user="${1:-${USERNAME}}" + sudo -i -u "${user}" < Date: Tue, 16 Nov 2021 18:03:34 +0100 Subject: [PATCH 2/2] [fix] filtron & morty - install golang binary that fits to arch & os Closes: https://github.com/searxng/searxng/issues/507 Signed-off-by: Markus Heiser --- docs/build-templates/filtron.rst | 9 ++++---- docs/build-templates/morty.rst | 9 ++++---- utils/filtron.sh | 26 ++++++------------------ utils/lib.sh | 35 -------------------------------- utils/morty.sh | 26 ++++++------------------ 5 files changed, 22 insertions(+), 83 deletions(-) diff --git a/docs/build-templates/filtron.rst b/docs/build-templates/filtron.rst index 79b254399..6a39d1f58 100644 --- a/docs/build-templates/filtron.rst +++ b/docs/build-templates/filtron.rst @@ -19,7 +19,8 @@ .. tabs:: - .. group-tab:: bash + .. group-tab:: os: linux / arch: amd64 + .. code-block:: bash @@ -30,9 +31,9 @@ $ sudo -i -u \"${SERVICE_USER}\" (${SERVICE_USER}) $ echo 'source $GO_ENV' >> ~/.profile (${SERVICE_USER}) $ mkdir ${SERVICE_HOME}/local - (${SERVICE_USER}) $ wget --progress=bar -O \"${GO_TAR}\" \\ - \"${GO_PKG_URL}\" - (${SERVICE_USER}) $ tar -C ${SERVICE_HOME}/local -xzf \"${GO_TAR}\" + (${SERVICE_USER}) $ wget --progress=bar -O \"${GO_VERSION}.linux-amd64.tar.gz\" \\ + \"${GO_DL_URL}/${GO_VERSION}.linux-amd64.tar.gz\" + (${SERVICE_USER}) $ tar -C ${SERVICE_HOME}/local -xzf \"${GO_VERSION}.linux-amd64.tar.gz\" (${SERVICE_USER}) $ which go ${SERVICE_HOME}/local/go/bin/go diff --git a/docs/build-templates/morty.rst b/docs/build-templates/morty.rst index 2be24da55..b03a54f96 100644 --- a/docs/build-templates/morty.rst +++ b/docs/build-templates/morty.rst @@ -19,7 +19,8 @@ .. tabs:: - .. group-tab:: bash + .. group-tab:: os: linux / arch: amd64 + .. code-block:: bash @@ -30,9 +31,9 @@ $ sudo -i -u \"${SERVICE_USER}\" (${SERVICE_USER}) $ echo 'source $GO_ENV' >> ~/.profile (${SERVICE_USER}) $ mkdir ${SERVICE_HOME}/local - (${SERVICE_USER}) $ wget --progress=bar -O \"${GO_TAR}\" \\ - \"${GO_PKG_URL}\" - (${SERVICE_USER}) $ tar -C ${SERVICE_HOME}/local -xzf \"${GO_TAR}\" + (${SERVICE_USER}) $ wget --progress=bar -O \"${GO_VERSION}.linux-amd64.tar.gz\" \\ + \"${GO_DL_URL}/${GO_VERSION}.linux-amd64.tar.gz\" + (${SERVICE_USER}) $ tar -C ${SERVICE_HOME}/local -xzf \"${GO_VERSION}.linux-amd64.tar.gz\" (${SERVICE_USER}) $ which go ${SERVICE_HOME}/local/go/bin/go diff --git a/utils/filtron.sh b/utils/filtron.sh index 4e8d6aa5d..ff380a327 100755 --- a/utils/filtron.sh +++ b/utils/filtron.sh @@ -4,6 +4,8 @@ # shellcheck source=utils/lib.sh source "$(dirname "${BASH_SOURCE[0]}")/lib.sh" +# shellcheck source=utils/lib_go.sh +source "${REPO_ROOT}/utils/lib_go.sh" # shellcheck source=utils/lib_install.sh source "${REPO_ROOT}/utils/lib_install.sh" @@ -43,8 +45,6 @@ SERVICE_GROUP="${SERVICE_USER}" GO_ENV="${SERVICE_HOME}/.go_env" GO_VERSION="go1.17.2" -GO_PKG_URL="https://golang.org/dl/${GO_VERSION}.linux-amd64.tar.gz" -GO_TAR=$(basename "$GO_PKG_URL") APACHE_FILTRON_SITE="searxng.conf" NGINX_FILTRON_SITE="searxng.conf" @@ -218,7 +218,7 @@ install_all() { rst_title "Install $SERVICE_NAME (service)" assert_user wait_key - install_go "${GO_PKG_URL}" "${GO_TAR}" "${SERVICE_USER}" + go.golang "${GO_VERSION}" "${SERVICE_USER}" wait_key install_filtron install_rules @@ -301,9 +301,7 @@ install_check() { } go_version(){ - sudo -i -u "$SERVICE_USER" < "$GO_ENV" <> ~/.profile EOF @@ -353,22 +345,16 @@ filtron_is_installed() { [[ -f $SERVICE_HOME/go-apps/bin/filtron ]] } -_svcpr=" ${_Yellow}|${SERVICE_USER}|${_creset} " - install_filtron() { rst_title "Install filtron in user's ~/go-apps" section echo - tee_stderr <&1 | prefix_stdout "$_svcpr" -go install -v github.com/asciimoo/filtron@latest -EOF + go.install github.com/asciimoo/filtron@latest "${SERVICE_USER}" } update_filtron() { rst_title "Update filtron" section echo - tee_stderr <&1 | prefix_stdout "$_svcpr" -go install -v github.com/asciimoo/filtron@latest -EOF + go.install github.com/asciimoo/filtron@latest "${SERVICE_USER}" } install_rules() { diff --git a/utils/lib.sh b/utils/lib.sh index 7f6ff58c4..4472b9d32 100755 --- a/utils/lib.sh +++ b/utils/lib.sh @@ -504,7 +504,6 @@ install_template() { done } - service_is_available() { # usage: service_is_available @@ -825,40 +824,6 @@ EOF build_msg GH-PAGES "deployed" } -# golang -# ------ - -go_is_available() { - - # usage: go_is_available $SERVICE_USER && echo "go is installed!" - - sudo -i -u "${1}" which go &>/dev/null -} - -install_go() { - - # usage: install_go "${GO_PKG_URL}" "${GO_TAR}" "${SERVICE_USER}" - - local _svcpr=" ${_Yellow}|${3}|${_creset} " - - rst_title "Install Go in user's HOME" section - - rst_para "download and install go binary .." - cache_download "${1}" "${2}" - - tee_stderr 0.1 </dev/null && echo "ERROR - Go Installation not found in PATH!?!" -which go >/dev/null && go version && echo "congratulations -- Go installation OK :)" -EOF -} - # system accounts # --------------- diff --git a/utils/morty.sh b/utils/morty.sh index c04df7a0c..c1f2bc968 100755 --- a/utils/morty.sh +++ b/utils/morty.sh @@ -3,6 +3,8 @@ # shellcheck source=utils/lib.sh source "$(dirname "${BASH_SOURCE[0]}")/lib.sh" +# shellcheck source=utils/lib_go.sh +source "${REPO_ROOT}/utils/lib_go.sh" # shellcheck source=utils/lib_install.sh source "${REPO_ROOT}/utils/lib_install.sh" @@ -29,8 +31,6 @@ SERVICE_ENV_DEBUG=false GO_ENV="${SERVICE_HOME}/.go_env" GO_VERSION="go1.17.2" -GO_PKG_URL="https://golang.org/dl/${GO_VERSION}.linux-amd64.tar.gz" -GO_TAR=$(basename "$GO_PKG_URL") # shellcheck disable=SC2034 CONFIG_FILES=() @@ -234,7 +234,7 @@ install_all() { rst_title "Install $SERVICE_NAME (service)" assert_user wait_key - install_go "${GO_PKG_URL}" "${GO_TAR}" "${SERVICE_USER}" + go.golang "${GO_VERSION}" "${SERVICE_USER}" wait_key install_morty wait_key @@ -306,9 +306,7 @@ install_check() { } go_version(){ - sudo -i -u "$SERVICE_USER" < "$GO_ENV" <> ~/.profile EOF @@ -353,22 +345,16 @@ morty_is_installed() { [[ -f $SERVICE_HOME/go-apps/bin/morty ]] } -_svcpr=" ${_Yellow}|${SERVICE_USER}|${_creset} " - install_morty() { rst_title "Install morty in user's ~/go-apps" section echo - tee_stderr <&1 | prefix_stdout "$_svcpr" -go install -v github.com/asciimoo/morty@latest -EOF + go.install github.com/asciimoo/morty@latest "${SERVICE_USER}" } update_morty() { rst_title "Update morty" section echo - tee_stderr <&1 | prefix_stdout "$_svcpr" -go install -v github.com/asciimoo/morty@latest -EOF + go.install github.com/asciimoo/morty@latest "${SERVICE_USER}" } set_service_env_debug() {