From e890b056635e8b905cce4697523a49beedd438d3 Mon Sep 17 00:00:00 2001 From: Fabian Wilkens Date: Fri, 27 May 2022 17:03:57 +0200 Subject: [PATCH 1/6] Fix: install and upgrade script --- scripts/install | 2 ++ scripts/upgrade | 2 ++ 2 files changed, 4 insertions(+) diff --git a/scripts/install b/scripts/install index 3aef3df..a468a3b 100755 --- a/scripts/install +++ b/scripts/install @@ -100,6 +100,8 @@ ynh_add_nginx_config #================================================= ynh_script_progression --message="Installing..." --weight=5 +export GO111MODULE=on + pushd "$app_path" ynh_exec_warn_less go mod download ynh_exec_warn_less go build diff --git a/scripts/upgrade b/scripts/upgrade index d290ef2..448ba8e 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -106,6 +106,8 @@ ynh_install_app_dependencies $pkg_dependencies #================================================= ynh_script_progression --message="Upgrading..." --weight=5 +export GO111MODULE=on + pushd "$app_path" ynh_exec_warn_less go mod download ynh_exec_warn_less go build From 601a44ef932600f0bcffe7b142b8c6afa8af0fb1 Mon Sep 17 00:00:00 2001 From: Fabian Wilkens Date: Fri, 27 May 2022 17:07:46 +0200 Subject: [PATCH 2/6] Update: check_process --- check_process | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/check_process b/check_process index 6d333a9..7b947e1 100644 --- a/check_process +++ b/check_process @@ -7,7 +7,6 @@ ; Manifest domain="domain.tld" path="/path" - is_public=1 ; Checks pkg_linter=1 setup_sub_dir=1 @@ -16,8 +15,8 @@ setup_private=0 setup_public=1 upgrade=1 - # 2021.02.16~ynh1 - upgrade=0 from_commit=68a38f6e9b2e357db46d2827a9781f58ea2422cc + # 2021.02.16~ynh2 + upgrade=1 from_commit=708eaeab27f4ce47036a712c7452d916c5b741a8 backup_restore=1 multi_instance=1 port_already_use=0 @@ -26,6 +25,6 @@ Email= Notification=none ;;; Upgrade options - ; commit=68a38f6e9b2e357db46d2827a9781f58ea2422cc - name=2021.02.16~ynh1 - manifest_arg=domain=DOMAIN&path=PATH&&is_public=1 + ; commit=708eaeab27f4ce47036a712c7452d916c5b741a8 + name=2021.02.16~ynh2 + manifest_arg=domain=DOMAIN&path=PATH From 803a97193dbd838257eab3134aa066c462c56a5c Mon Sep 17 00:00:00 2001 From: Fabian Wilkens <46000361+FabianWilkens@users.noreply.github.com> Date: Fri, 27 May 2022 19:50:11 +0200 Subject: [PATCH 3/6] Update manifest.json --- manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manifest.json b/manifest.json index 9cf7d75..c0720c7 100644 --- a/manifest.json +++ b/manifest.json @@ -5,7 +5,7 @@ "description": { "en": "IP API service to get their public IP address." }, - "version": "2022.01.05~ynh2", + "version": "2022.01.05~ynh3", "url": "https://github.com/georgyo/ifconfig.io/", "upstream": { "license": "MIT", From fc547b2227a27fcba45be155541b60b32cfb17c0 Mon Sep 17 00:00:00 2001 From: yunohost-bot Date: Fri, 27 May 2022 17:50:14 +0000 Subject: [PATCH 4/6] Auto-update README --- README.md | 2 +- README_fr.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 11626ee..ded5562 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ I used the gin framework as it does several things to ensure that there are no m Tested to handle 15,000 requests persecond on modest hardware with an average response time of 130ms. -**Shipped version:** 2022.01.05~ynh2 +**Shipped version:** 2022.01.05~ynh3 **Demo:** https://ifconfig.io ## Documentation and resources diff --git a/README_fr.md b/README_fr.md index cda12c9..61b44a5 100644 --- a/README_fr.md +++ b/README_fr.md @@ -22,7 +22,7 @@ I used the gin framework as it does several things to ensure that there are no m Tested to handle 15,000 requests persecond on modest hardware with an average response time of 130ms. -**Version incluse :** 2022.01.05~ynh2 +**Version incluse :** 2022.01.05~ynh3 **Démo :** https://ifconfig.io ## Documentations et ressources From 456a9f2c7ecfd01d3adfdcc2e77d433d13e485e3 Mon Sep 17 00:00:00 2001 From: Fabian Wilkens Date: Fri, 27 May 2022 21:53:35 +0200 Subject: [PATCH 5/6] Update: add ynh_install_go script --- scripts/ynh_install_go | 248 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 248 insertions(+) create mode 100644 scripts/ynh_install_go diff --git a/scripts/ynh_install_go b/scripts/ynh_install_go new file mode 100644 index 0000000..53e1033 --- /dev/null +++ b/scripts/ynh_install_go @@ -0,0 +1,248 @@ +#!/bin/bash + +ynh_go_try_bash_extension() { + if [ -x src/configure ]; then + src/configure && make -C src || { + ynh_print_info --message="Optional bash extension failed to build, but things will still work normally." + } + fi +} + +goenv_install_dir="/opt/goenv" +go_version_path="$goenv_install_dir/versions" +# goenv_ROOT is the directory of goenv, it needs to be loaded as a environment variable. +export GOENV_ROOT="$goenv_install_dir" + +# Load the version of Go for an app, and set variables. +# +# ynh_use_go has to be used in any app scripts before using Go for the first time. +# This helper will provide alias and variables to use in your scripts. +# +# To use gem or Go, use the alias `ynh_gem` and `ynh_go` +# Those alias will use the correct version installed for the app +# For example: use `ynh_gem install` instead of `gem install` +# +# With `sudo` or `ynh_exec_as`, use instead the fallback variables `$ynh_gem` and `$ynh_go` +# And propagate $PATH to sudo with $ynh_go_load_path +# Exemple: `ynh_exec_as $app $ynh_go_load_path $ynh_gem install` +# +# $PATH contains the path of the requested version of Go. +# However, $PATH is duplicated into $go_path to outlast any manipulation of $PATH +# You can use the variable `$ynh_go_load_path` to quickly load your Go version +# in $PATH for an usage into a separate script. +# Exemple: $ynh_go_load_path $final_path/script_that_use_gem.sh` +# +# +# Finally, to start a Go service with the correct version, 2 solutions +# Either the app is dependent of Go or gem, but does not called it directly. +# In such situation, you need to load PATH +# `Environment="__YNH_GO_LOAD_PATH__"` +# `ExecStart=__FINALPATH__/my_app` +# You will replace __YNH_GO_LOAD_PATH__ with $ynh_go_load_path +# +# Or Go start the app directly, then you don't need to load the PATH variable +# `ExecStart=__YNH_GO__ my_app run` +# You will replace __YNH_GO__ with $ynh_go +# +# +# one other variable is also available +# - $go_path: The absolute path to Go binaries for the chosen version. +# +# usage: ynh_use_go +# +# Requires YunoHost version 3.2.2 or higher. +ynh_use_go () { + go_version=$(ynh_app_setting_get --app=$app --key=go_version) + + # Get the absolute path of this version of Go + go_path="$go_version_path/$go_version/bin" + + # Allow alias to be used into bash script + shopt -s expand_aliases + + # Create an alias for the specific version of Go and a variable as fallback + ynh_go="$go_path/go" + alias ynh_go="$ynh_go" + + # Load the path of this version of Go in $PATH + if [[ :$PATH: != *":$go_path"* ]]; then + PATH="$go_path:$PATH" + fi + # Create an alias to easily load the PATH + ynh_go_load_path="PATH=$PATH" + + # Sets the local application-specific Go version + pushd $final_path + $goenv_install_dir/bin/goenv local $go_version + popd +} + +# Install a specific version of Go +# +# ynh_install_go will install the version of Go provided as argument by using goenv. +# +# This helper creates a /etc/profile.d/goenv.sh that configures PATH environment for goenv +# for every LOGIN user, hence your user must have a defined shell (as opposed to /usr/sbin/nologin) +# +# Don't forget to execute go-dependent command in a login environment +# (e.g. sudo --login option) +# When not possible (e.g. in systemd service definition), please use direct path +# to goenv shims (e.g. $goenv_ROOT/shims/bundle) +# +# usage: ynh_install_go --go_version=go_version +# | arg: -v, --go_version= - Version of go to install. +# +# Requires YunoHost version 3.2.2 or higher. +ynh_install_go () { + # Declare an array to define the options of this helper. + local legacy_args=v + local -A args_array=( [v]=go_version= ) + local go_version + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + # Load goenv path in PATH + local CLEAR_PATH="$goenv_install_dir/bin:$PATH" + + # Remove /usr/local/bin in PATH in case of Go prior installation + PATH=$(echo $CLEAR_PATH | sed 's@/usr/local/bin:@@') + + # Move an existing Go binary, to avoid to block goenv + test -x /usr/bin/go && mv /usr/bin/go /usr/bin/go_goenv + + # Install or update goenv + goenv="$(command -v goenv $goenv_install_dir/bin/goenv | head -1)" + if [ -n "$goenv" ]; then + ynh_print_info --message="goenv already seems installed in \`$goenv'." + pushd "${goenv%/*/*}" + if git remote -v 2>/dev/null | grep "https://github.com/syndbg/goenv.git"; then + echo "Trying to update with git..." + git pull -q --tags origin master + cd .. + ynh_go_try_bash_extension + fi + popd + else + ynh_print_info --message="Installing goenv with git..." + mkdir -p $goenv_install_dir + pushd $goenv_install_dir + git init -q + git remote add -f -t master origin https://github.com/syndbg/goenv.git > /dev/null 2>&1 + git checkout -q -b master origin/master + ynh_go_try_bash_extension + goenv=$goenv_install_dir/bin/goenv + popd + fi + + goenv_latest="$(command -v "$goenv_install_dir"/plugins/*/bin/goenv-latest goenv-latest | head -1)" + if [ -n "$goenv_latest" ]; then + ynh_print_info --message="\`goenv latest' command already available in \`$goenv_latest'." + pushd "${goenv_latest%/*/*}" + if git remote -v 2>/dev/null | grep "https://github.com/momo-lab/xxenv-latest.git"; then + ynh_print_info --message="Trying to update xxenv-latest with git..." + git pull -q origin master + fi + popd + else + ynh_print_info --message="Installing xxenv-latest with git..." + mkdir -p "${goenv_install_dir}/plugins" + git clone -q https://github.com/momo-lab/xxenv-latest.git "${goenv_install_dir}/plugins/xxenv-latest" + fi + + # Enable caching + mkdir -p "${goenv_install_dir}/cache" + + # Create shims directory if needed + mkdir -p "${goenv_install_dir}/shims" + + # Restore /usr/local/bin in PATH + PATH=$CLEAR_PATH + + # And replace the old Go binary + test -x /usr/bin/go_goenv && mv /usr/bin/go_goenv /usr/bin/go + + # Install the requested version of Go + local final_go_version=$(goenv latest --print $go_version) + ynh_print_info --message="Installation of Go-$final_go_version" + goenv install --skip-existing $final_go_version + + # Store go_version into the config of this app + ynh_app_setting_set --app=$YNH_APP_INSTANCE_NAME --key=go_version --value=$final_go_version + + # Cleanup Go versions + ynh_cleanup_go + + # Set environment for Go users + echo "#goenv +export GOENV_ROOT=$goenv_install_dir +export PATH=\"$goenv_install_dir/bin:$PATH\" +eval \"\$(goenv init -)\" +#goenv" > /etc/profile.d/goenv.sh + + # Load the environment + eval "$(goenv init -)" +} + +# Remove the version of Go used by the app. +# +# This helper will also cleanup Go versions +# +# usage: ynh_remove_go +ynh_remove_go () { + local go_version=$(ynh_app_setting_get --app=$YNH_APP_INSTANCE_NAME --key=go_version) + + # Load goenv path in PATH + local CLEAR_PATH="$goenv_install_dir/bin:$PATH" + + # Remove /usr/local/bin in PATH in case of Go prior installation + PATH=$(echo $CLEAR_PATH | sed 's@/usr/local/bin:@@') + + # Remove the line for this app + ynh_app_setting_delete --app=$YNH_APP_INSTANCE_NAME --key=go_version + + # Cleanup Go versions + ynh_cleanup_go +} + +# Remove no more needed versions of Go used by the app. +# +# This helper will check what Go version are no more required, +# and uninstall them +# If no app uses Go, goenv will be also removed. +# +# usage: ynh_cleanup_go +ynh_cleanup_go () { + + # List required Go versions + local installed_apps=$(yunohost app list --output-as json --quiet | jq -r .apps[].id) + local required_go_versions="" + for installed_app in $installed_apps + do + local installed_app_go_version=$(ynh_app_setting_get --app=$installed_app --key="go_version") + if [[ $installed_app_go_version ]] + then + required_go_versions="${installed_app_go_version}\n${required_go_versions}" + fi + done + + # Remove no more needed Go versions + local installed_go_versions=$(goenv versions --bare --skip-aliases | grep -Ev '/') + for installed_go_version in $installed_go_versions + do + if ! `echo ${required_go_versions} | grep "${installed_go_version}" 1>/dev/null 2>&1` + then + ynh_print_info --message="Removing of Go-$installed_go_version" + $goenv_install_dir/bin/goenv uninstall --force $installed_go_version + fi + done + + # If none Go version is required + if [[ ! $required_go_versions ]] + then + # Remove goenv environment configuration + ynh_print_info --message="Removing of goenv" + ynh_secure_remove --file="$goenv_install_dir" + ynh_secure_remove --file="/etc/profile.d/goenv.sh" + fi +} + From cdd94a8b0a1b376a010966c28f649891ca26cda9 Mon Sep 17 00:00:00 2001 From: Fabian Wilkens Date: Fri, 27 May 2022 23:24:23 +0200 Subject: [PATCH 6/6] Update: Integrate ynh_install_go --- conf/systemd.service | 1 + scripts/_common.sh | 4 +++- scripts/install | 10 ++++++++-- scripts/remove | 2 ++ scripts/restore | 2 ++ scripts/upgrade | 10 ++++++++-- 6 files changed, 24 insertions(+), 5 deletions(-) diff --git a/conf/systemd.service b/conf/systemd.service index 75e39f5..2048f92 100644 --- a/conf/systemd.service +++ b/conf/systemd.service @@ -10,6 +10,7 @@ Environment=PORT=__PORT__ Environment=HOSTNAME=__DOMAIN__ Environment=GIN_MODE=release Environment=FORWARD_IP_HEADER=X-Forwarded-For +Environment="__YNH_GO_LOAD_PATH__" WorkingDirectory=__APP_PATH__/ ExecStart=__APP_PATH__/ifconfig.io StandardOutput=append:/var/log/__APP__/__APP__.log diff --git a/scripts/_common.sh b/scripts/_common.sh index c59394e..76cbbab 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -5,7 +5,9 @@ #================================================= # dependencies used by the app -pkg_dependencies="golang" +pkg_dependencies="" + +GO_VERSION="1.18" #================================================= # PERSONAL HELPERS diff --git a/scripts/install b/scripts/install index a468a3b..1eeb3cb 100755 --- a/scripts/install +++ b/scripts/install @@ -7,6 +7,7 @@ #================================================= source _common.sh +source ynh_install_go source /usr/share/yunohost/helpers #================================================= @@ -66,6 +67,7 @@ ynh_app_setting_set --app=$app --key=port --value=$port ynh_script_progression --message="Installing dependencies..." --weight=5 ynh_install_app_dependencies $pkg_dependencies +ynh_exec_warn_less ynh_install_go --go_version=$GO_VERSION #================================================= # CREATE DEDICATED USER @@ -100,11 +102,15 @@ ynh_add_nginx_config #================================================= ynh_script_progression --message="Installing..." --weight=5 +# Setup go environnement +ynh_use_go +export GOPATH="$final_path/go" +export GOCACHE="$final_path/go/.cache" export GO111MODULE=on pushd "$app_path" - ynh_exec_warn_less go mod download - ynh_exec_warn_less go build + ynh_exec_warn_less $ynh_go mod download + ynh_exec_warn_less $ynh_go build -tags=jsoniter popd #================================================= diff --git a/scripts/remove b/scripts/remove index 8184c37..bc90bba 100755 --- a/scripts/remove +++ b/scripts/remove @@ -7,6 +7,7 @@ #================================================= source _common.sh +source ynh_install_go source /usr/share/yunohost/helpers #================================================= @@ -72,6 +73,7 @@ ynh_script_progression --message="Removing dependencies..." --weight=5 # Remove metapackage and its dependencies ynh_remove_app_dependencies +ynh_remove_go #================================================= # CLOSE A PORT diff --git a/scripts/restore b/scripts/restore index 24de3d0..11d2772 100755 --- a/scripts/restore +++ b/scripts/restore @@ -8,6 +8,7 @@ # Keep this path for calling _common.sh inside the execution's context of backup and restore scripts source ../settings/scripts/_common.sh +source ../settings/scripts/ynh_install_go source /usr/share/yunohost/helpers #================================================= @@ -79,6 +80,7 @@ ynh_script_progression --message="Reinstalling dependencies..." --weight=5 # Define and install dependencies ynh_install_app_dependencies $pkg_dependencies +ynh_exec_warn_less ynh_install_go --go_version=$GO_VERSION #================================================= # RESTORE SYSTEMD diff --git a/scripts/upgrade b/scripts/upgrade index 448ba8e..6b38e8e 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -7,6 +7,7 @@ #================================================= source _common.sh +source ynh_install_go source /usr/share/yunohost/helpers #================================================= @@ -100,17 +101,22 @@ ynh_add_nginx_config ynh_script_progression --message="Upgrading dependencies..." --weight=5 ynh_install_app_dependencies $pkg_dependencies +ynh_exec_warn_less ynh_install_go --go_version=$GO_VERSION #================================================= # SPECIFIC UPGRADE #================================================= ynh_script_progression --message="Upgrading..." --weight=5 +# Setup go environnement +ynh_use_go +export GOPATH="$final_path/go" +export GOCACHE="$final_path/go/.cache" export GO111MODULE=on pushd "$app_path" - ynh_exec_warn_less go mod download - ynh_exec_warn_less go build + ynh_exec_warn_less $ynh_go mod download + ynh_exec_warn_less $ynh_go build -tags=jsoniter popd #=================================================