1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/photoprism_ynh.git synced 2024-09-03 19:56:41 +02:00

more docker stuff

This commit is contained in:
yalh76 2022-06-24 02:37:54 +02:00
parent 4fd77c449c
commit 2a997cece0
10 changed files with 70 additions and 484 deletions

View file

@ -4,13 +4,8 @@
# COMMON VARIABLES # COMMON VARIABLES
#================================================= #=================================================
# Tag name of the installed version
PHOTOPRISM_VERSION="220617-bullseye" PHOTOPRISM_VERSION="220617-bullseye"
MEMORY_NEEDED="3000"
NODEJS_VERSION=14
GO_VERSION="1.18"
#pkg_dependencies="libc6-dev libssl-dev libxft-dev libhdf5-serial-dev libpng-dev libheif-examples librsvg2-bin libx264-dev libx265-dev libnss3 libfreetype6 libfreetype6-dev libfontconfig1 libfontconfig1-dev libzmq3-dev" #pkg_dependencies="libc6-dev libssl-dev libxft-dev libhdf5-serial-dev libpng-dev libheif-examples librsvg2-bin libx264-dev libx265-dev libnss3 libfreetype6 libfreetype6-dev libfontconfig1 libfontconfig1-dev libzmq3-dev"
pkg_dependencies="" pkg_dependencies=""

View file

@ -29,14 +29,8 @@ app=$YNH_APP_INSTANCE_NAME
final_path=$(ynh_app_setting_get --app=$app --key=final_path) final_path=$(ynh_app_setting_get --app=$app --key=final_path)
domain=$(ynh_app_setting_get --app=$app --key=domain) domain=$(ynh_app_setting_get --app=$app --key=domain)
path_url=$(ynh_app_setting_get --app=$app --key=path)
db_name=$(ynh_app_setting_get --app=$app --key=db_name) db_name=$(ynh_app_setting_get --app=$app --key=db_name)
db_user=$db_name
db_pwd=$(ynh_app_setting_get --app=$app --key=mysqlpwd)
language_key=$(ynh_app_setting_get --app=$app --key=language)
datadir=$(ynh_app_setting_get --app=$app --key=datadir) datadir=$(ynh_app_setting_get --app=$app --key=datadir)
port=$(ynh_app_setting_get --app=$app --key=port)
password=$(ynh_app_setting_get --app=$app --key=password)
#================================================= #=================================================
# DECLARE DATA AND CONF FILES TO BACKUP # DECLARE DATA AND CONF FILES TO BACKUP

View file

@ -33,7 +33,7 @@ final_path=$(ynh_app_setting_get --app=$app --key=final_path)
db_name=$(ynh_app_setting_get --app=$app --key=db_name) db_name=$(ynh_app_setting_get --app=$app --key=db_name)
db_user=$db_name db_user=$db_name
db_pwd=$(ynh_app_setting_get --app=$app --key=mysqlpwd) db_pwd=$(ynh_app_setting_get --app=$app --key=mysqlpwd)
language_key=$(ynh_app_setting_get --app=$app --key=language) language_key=$(ynh_app_setting_get --app=$app --key=language_key)
datadir=$(ynh_app_setting_get --app=$app --key=datadir) datadir=$(ynh_app_setting_get --app=$app --key=datadir)
port=$(ynh_app_setting_get --app=$app --key=port) port=$(ynh_app_setting_get --app=$app --key=port)
password=$(ynh_app_setting_get --app=$app --key=password) password=$(ynh_app_setting_get --app=$app --key=password)
@ -113,17 +113,17 @@ fi
#================================================= #=================================================
# SPECIFIC MODIFICATIONS # SPECIFIC MODIFICATIONS
#================================================= #=================================================
ynh_script_progression --message="Updating the configuration file..." --weight=1 # UPDATE A CONFIG FILE
#=================================================
ynh_script_progression --message="Updating a configuration file..." --weight=1
domain=$new_domain domain=$new_domain
path_url=$new_path path_url=$new_path
ynh_secure_remove --file="$final_path/build/config/options.yml" ynh_add_config --template="options.yml" --destination="$final_path/live/config/options.yml"
ynh_add_config --template="options.yml" --destination="$final_path/build/config/options.yml"
chmod 400 "$final_path/build/config/options.yml"
chown $app:$app "$final_path/build/config/options.yml"
#================================================= chmod 400 "$final_path/live/config/options.yml"
chown $app:$app "$final_path/live/config/options.yml"
#================================================= #=================================================
# GENERIC FINALISATION # GENERIC FINALISATION
@ -132,7 +132,8 @@ chown $app:$app "$final_path/build/config/options.yml"
#================================================= #=================================================
ynh_script_progression --message="Starting a systemd service..." --time --weight=1 ynh_script_progression --message="Starting a systemd service..." --time --weight=1
ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log" # Start a systemd service
ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log" --line_match="http: starting web server at"
#================================================= #=================================================
# RELOAD NGINX # RELOAD NGINX

View file

@ -7,8 +7,6 @@
#================================================= #=================================================
source _common.sh source _common.sh
source ynh_install_go
source ynh_add_swap
source /usr/share/yunohost/helpers source /usr/share/yunohost/helpers
#================================================= #=================================================
@ -57,7 +55,7 @@ ynh_script_progression --message="Storing installation settings..." --time --wei
ynh_app_setting_set --app=$app --key=domain --value=$domain ynh_app_setting_set --app=$app --key=domain --value=$domain
ynh_app_setting_set --app=$app --key=path --value=$path_url ynh_app_setting_set --app=$app --key=path --value=$path_url
ynh_app_setting_set --app=$app --key=language --value=$language_key ynh_app_setting_set --app=$app --key=language_key --value=$language_key
ynh_app_setting_set --app=$app --key=password --value=$password ynh_app_setting_set --app=$app --key=password --value=$password
#================================================= #=================================================
@ -68,7 +66,7 @@ ynh_app_setting_set --app=$app --key=password --value=$password
ynh_script_progression --message="Finding an available port..." --time --weight=1 ynh_script_progression --message="Finding an available port..." --time --weight=1
# Find an available port # Find an available port
port=$(ynh_find_port --port=2342) port=$(ynh_find_port --port=8095)
ynh_app_setting_set --app=$app --key=port --value=$port ynh_app_setting_set --app=$app --key=port --value=$port
#================================================= #=================================================
@ -78,28 +76,6 @@ ynh_script_progression --message="Installing dependencies..." --time --weight=1
ynh_install_app_dependencies $pkg_dependencies ynh_install_app_dependencies $pkg_dependencies
# Install nodejs
#ynh_install_nodejs --nodejs_version=$NODEJS_VERSION 2>&1
# Install go
#ynh_exec_warn_less ynh_install_go --go_version=$GO_VERSION
#=================================================
# ADD SWAP IF NEEDED
#=================================================
ynh_script_progression --message="Adding swap is needed..."
total_memory=$(ynh_get_ram --total)
swap_needed=0
if [ $total_memory -lt $MEMORY_NEEDED ]; then
# Need a minimum of 2.5Go of memory
swap_needed=$(($MEMORY_NEEDED - $total_memory))
fi
ynh_script_progression --message="Adding $swap_needed Mo to swap..."
ynh_add_swap --size=$swap_needed
#================================================= #=================================================
# CREATE DEDICATED USER # CREATE DEDICATED USER
#================================================= #=================================================
@ -140,20 +116,7 @@ ynh_script_progression --message="Configuring NGINX web server..." --time --weig
ynh_add_nginx_config ynh_add_nginx_config
#================================================= #=================================================
# CREATE DATA DIRECTORY # SPECIFIC SETUP
#=================================================
ynh_script_progression --message="Creating a data directory..." --time --weight=1
datadir=/home/yunohost.app/$app
ynh_app_setting_set --app=$app --key=datadir --value=$datadir
mkdir -p "$datadir"
#rsync -a "$final_path/build/assets/" "$datadir/assets/"
chmod 750 "$datadir"
chmod -R o-rwx "$datadir"
chown -R $app:www-data "$datadir"
#================================================= #=================================================
# MAKE INSTALL # MAKE INSTALL
#================================================= #=================================================
@ -163,14 +126,26 @@ ynh_script_progression --message="Making install..."
pushd $final_path/build pushd $final_path/build
./docker-image-extract photoprism/photoprism:$PHOTOPRISM_VERSION ./docker-image-extract photoprism/photoprism:$PHOTOPRISM_VERSION
popd popd
mv -f "$final_path/build/output/opt/photoprism/" "$final_path/live/" mkdir -p "$final_path/live/"
mv -f "$final_path/live/assets/" "$datadir/assets/" rsync -a "$final_path/build/output/opt/photoprism/" "$final_path/live/"
ynh_secure_remove --file="$final_path/build" ynh_secure_remove --file="$final_path/build"
chmod 750 "$final_path" chmod 750 "$final_path"
chmod -R o-rwx "$final_path" chmod -R o-rwx "$final_path"
chown -R $app:$app "$final_path" chown -R $app:$app "$final_path"
#=================================================
# CREATE DATA DIRECTORY
#=================================================
ynh_script_progression --message="Creating a data directory..." --time --weight=1
datadir=/home/yunohost.app/$app
ynh_app_setting_set --app=$app --key=datadir --value=$datadir
mkdir -p $datadir
rsync -a "$final_path/live/assets/" "$datadir/assets/"
ynh_secure_remove --file="$final_path/live/assets/"
chmod 750 "$datadir" chmod 750 "$datadir"
chmod -R o-rwx "$datadir" chmod -R o-rwx "$datadir"
chown -R $app:www-data "$datadir" chown -R $app:www-data "$datadir"
@ -216,7 +191,7 @@ yunohost service add $app --description="AI-Powered Photos App for the Decentral
ynh_script_progression --message="Starting a systemd service..." --time --weight=1 ynh_script_progression --message="Starting a systemd service..." --time --weight=1
# Start a systemd service # Start a systemd service
ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log" ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log" --line_match="http: starting web server at"
#================================================= #=================================================
# SETUP SSOWAT # SETUP SSOWAT

View file

@ -7,7 +7,6 @@
#================================================= #=================================================
source _common.sh source _common.sh
source ynh_add_swap
source /usr/share/yunohost/helpers source /usr/share/yunohost/helpers
#================================================= #=================================================
@ -119,9 +118,6 @@ ynh_secure_remove --file="/etc/cron.d/$app"
# Remove the log files # Remove the log files
ynh_secure_remove --file="/var/log/$app" ynh_secure_remove --file="/var/log/$app"
# Remove swap
ynh_del_swap
#================================================= #=================================================
# GENERIC FINALIZATION # GENERIC FINALIZATION
#================================================= #=================================================

View file

@ -8,7 +8,6 @@
# Keep this path for calling _common.sh inside the execution's context of backup and restore scripts # 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/_common.sh
source ../settings/scripts/ynh_add_swap
source /usr/share/yunohost/helpers source /usr/share/yunohost/helpers
#================================================= #=================================================
@ -33,11 +32,7 @@ path_url=$(ynh_app_setting_get --app=$app --key=path)
final_path=$(ynh_app_setting_get --app=$app --key=final_path) final_path=$(ynh_app_setting_get --app=$app --key=final_path)
db_name=$(ynh_app_setting_get --app=$app --key=db_name) db_name=$(ynh_app_setting_get --app=$app --key=db_name)
db_user=$db_name db_user=$db_name
db_pwd=$(ynh_app_setting_get --app=$app --key=mysqlpwd)
language_key=$(ynh_app_setting_get --app=$app --key=language)
datadir=$(ynh_app_setting_get --app=$app --key=datadir) datadir=$(ynh_app_setting_get --app=$app --key=datadir)
port=$(ynh_app_setting_get --app=$app --key=port)
password=$(ynh_app_setting_get --app=$app --key=password)
#================================================= #=================================================
# CHECK IF THE APP CAN BE RESTORED # CHECK IF THE APP CAN BE RESTORED
@ -75,8 +70,7 @@ ynh_script_progression --message="Restoring the data directory..." --time --weig
ynh_restore_file --origin_path="$datadir" --not_mandatory ynh_restore_file --origin_path="$datadir" --not_mandatory
mkdir -p "$datadir/assets/" mkdir -p $datadir
rsync -a "$final_path/build/assets/" "$datadir/assets/"
chmod 750 "$datadir" chmod 750 "$datadir"
chmod -R o-rwx "$datadir" chmod -R o-rwx "$datadir"
@ -108,22 +102,6 @@ db_pwd=$(ynh_app_setting_get --app=$app --key=mysqlpwd)
ynh_mysql_setup_db --db_user=$db_user --db_name=$db_name --db_pwd=$db_pwd ynh_mysql_setup_db --db_user=$db_user --db_name=$db_name --db_pwd=$db_pwd
ynh_mysql_connect_as --user=$db_user --password=$db_pwd --database=$db_name < ./db.sql ynh_mysql_connect_as --user=$db_user --password=$db_pwd --database=$db_name < ./db.sql
#=================================================
# ADD SWAP IF NEEDED
#=================================================
ynh_script_progression --message="Adding swap is needed..."
total_memory=$(ynh_get_ram --total)
swap_needed=0
if [ $total_memory -lt $MEMORY_NEEDED ]; then
# Need a minimum of 2.5Go of memory
swap_needed=$(($MEMORY_NEEDED - $total_memory))
fi
ynh_script_progression --message="Adding $swap_needed Mo to swap..."
ynh_add_swap --size=$swap_needed
#================================================= #=================================================
# RESTORE SYSTEMD # RESTORE SYSTEMD
#================================================= #=================================================
@ -151,7 +129,7 @@ yunohost service add $app --description="AI-Powered Photos App for the Decentral
#================================================= #=================================================
ynh_script_progression --message="Starting a systemd service..." --time --weight=1 ynh_script_progression --message="Starting a systemd service..." --time --weight=1
ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log" ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log" --line_match="http: starting web server at"
#================================================= #=================================================
# GENERIC FINALIZATION # GENERIC FINALIZATION

View file

@ -7,8 +7,6 @@
#================================================= #=================================================
source _common.sh source _common.sh
source ynh_install_go
source ynh_add_swap
source /usr/share/yunohost/helpers source /usr/share/yunohost/helpers
#================================================= #=================================================
@ -24,7 +22,7 @@ final_path=$(ynh_app_setting_get --app=$app --key=final_path)
db_name=$(ynh_app_setting_get --app=$app --key=db_name) db_name=$(ynh_app_setting_get --app=$app --key=db_name)
db_user=$db_name db_user=$db_name
db_pwd=$(ynh_app_setting_get --app=$app --key=mysqlpwd) db_pwd=$(ynh_app_setting_get --app=$app --key=mysqlpwd)
language_key=$(ynh_app_setting_get --app=$app --key=language) language_key=$(ynh_app_setting_get --app=$app --key=language_key)
datadir=$(ynh_app_setting_get --app=$app --key=datadir) datadir=$(ynh_app_setting_get --app=$app --key=datadir)
port=$(ynh_app_setting_get --app=$app --key=port) port=$(ynh_app_setting_get --app=$app --key=port)
password=$(ynh_app_setting_get --app=$app --key=password) password=$(ynh_app_setting_get --app=$app --key=password)
@ -65,6 +63,13 @@ ynh_systemd_action --service_name=$app --action="stop" --log_path="/var/log/$app
#================================================= #=================================================
ynh_script_progression --message="Ensuring downward compatibility..." --time --weight=1 ynh_script_progression --message="Ensuring downward compatibility..." --time --weight=1
# If language_key doesn't exist, create it
if [ -z "$language_key" ]; then
language_key=$(ynh_app_setting_get --app=$app --key=language)
ynh_app_setting_set --app=$app --key=language_key --value=$language_key
ynh_app_setting_delete --app=$app --key=language
fi
#================================================= #=================================================
# CREATE DEDICATED USER # CREATE DEDICATED USER
#================================================= #=================================================
@ -81,10 +86,8 @@ if [ "$upgrade_type" == "UPGRADE_APP" ]
then then
ynh_script_progression --message="Upgrading source files..." --time --weight=1 ynh_script_progression --message="Upgrading source files..." --time --weight=1
pushd "$final_path" # Download, check integrity, uncompress and patch the source from app.src
git fetch origin ynh_setup_source --dest_dir="$final_path/build/" --source_id="docker-image-extract"
git checkout "$PHOTOPRISM_VERSION" 2>&1
popd
fi fi
chmod 750 "$final_path" chmod 750 "$final_path"
@ -109,52 +112,36 @@ ynh_add_nginx_config
#================================================= #=================================================
# SPECIFIC UPGRADE # SPECIFIC UPGRADE
#================================================= #=================================================
# ADD SWAP IF NEEDED # MAKE INSTALL
#================================================= #=================================================
ynh_script_progression --message="Adding swap is needed..." ynh_script_progression --message="Making install..." --time --weight=1
total_memory=$(ynh_get_ram --total) if [ "$upgrade_type" == "UPGRADE_APP" ]
swap_needed=0 then
# Install photoprism
if [ $total_memory -lt $MEMORY_NEEDED ]; then pushd $final_path/build
# Need a minimum of 2.5Go of memory ./docker-image-extract photoprism/photoprism:$PHOTOPRISM_VERSION
swap_needed=$(($MEMORY_NEEDED - $total_memory)) popd
mkdir -p "$final_path/live/"
rsync -a "$final_path/build/output/opt/photoprism/" "$final_path/live/"
ynh_secure_remove --file="$final_path/build"
fi fi
ynh_script_progression --message="Adding $swap_needed Mo to swap..." chmod 750 "$final_path"
ynh_add_swap --size=$swap_needed chmod -R o-rwx "$final_path"
chown -R $app:$app "$final_path"
#=================================================
# BUILD PHOTOPRISM
#=================================================
ynh_script_progression --message="Building Photoprism..." --time --weight=1
pushd "$final_path"
# Setup go exe and environnement
ynh_use_go
export GOPATH="$final_path/go"
export GOCACHE="$final_path/go/.cache"
export GOBIN=$GOPATH/bin
# Setup tensorflow library
./scripts/dist/install-tensorflow.sh
# Remove the former build file
ynh_secure_remove --file="$final_path/build"
# Setup node environment
export NODE_OPTIONS=--max_old_space_size=1024
# Build server from source
ynh_use_nodejs
env "$ynh_node_load_PATH" npm install -g npm
env "$ynh_node_load_PATH" make dep-tensorflow dep-js dep-go build-js install DESTDIR=$final_path/build 2>&1
ynh_secure_remove --file="$final_path/go"
popd
#================================================= #=================================================
# UPDATE DATA DIRECTORY # UPDATE DATA DIRECTORY
#================================================= #=================================================
ynh_script_progression --message="Updating data directory..." --time --weight=1 ynh_script_progression --message="Updating data directory..." --time --weight=1
rsync -a "$final_path/build/assets/" "$datadir/assets/" mkdir -p $datadir
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
rsync -a "$final_path/live/assets/" "$datadir/assets/"
ynh_secure_remove --file="$final_path/live/assets/"
fi
chmod 750 "$datadir" chmod 750 "$datadir"
chmod -R o-rwx "$datadir" chmod -R o-rwx "$datadir"
@ -165,10 +152,10 @@ chown -R $app:www-data "$datadir"
#================================================= #=================================================
ynh_script_progression --message="Updating a configuration file..." --time --weight=1 ynh_script_progression --message="Updating a configuration file..." --time --weight=1
ynh_add_config --template="options.yml" --destination="$final_path/build/config/options.yml" ynh_add_config --template="../conf/options.yml" --destination="$final_path/live/config/options.yml"
chmod 400 "$final_path/build/config/options.yml" chmod 400 "$final_path/live/config/options.yml"
chown $app:$app "$final_path/build/config/options.yml" chown $app:$app "$final_path/live/config/options.yml"
#================================================= #=================================================
# SETUP SYSTEMD # SETUP SYSTEMD
@ -200,7 +187,7 @@ yunohost service add $app --description="AI-Powered Photos App for the Decentral
#================================================= #=================================================
ynh_script_progression --message="Starting a systemd service..." --time --weight=1 ynh_script_progression --message="Starting a systemd service..." --time --weight=1
ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log" ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log" --line_match="http: starting web server at"
#================================================= #=================================================
# RELOAD NGINX # RELOAD NGINX

View file

@ -1,93 +0,0 @@
#!/bin/bash
# Add swap
#
# usage: ynh_add_swap --size=SWAP in Mb
# | arg: -s, --size= - Amount of SWAP to add in Mb.
ynh_add_swap () {
# Declare an array to define the options of this helper.
declare -Ar args_array=( [s]=size= )
local size
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
local swap_max_size=$(( $size * 1024 ))
local free_space=$(df --output=avail / | sed 1d)
# Because we don't want to fill the disk with a swap file, divide by 2 the available space.
local usable_space=$(( $free_space / 2 ))
SD_CARD_CAN_SWAP=${SD_CARD_CAN_SWAP:-0}
# Swap on SD card only if it's is specified
if ynh_is_main_device_a_sd_card && [ "$SD_CARD_CAN_SWAP" == "0" ]
then
ynh_print_warn --message="The main mountpoint of your system '/' is on an SD card, swap will not be added to prevent some damage of this one, but that can cause troubles for the app $app. If you still want activate the swap, you can relaunch the command preceded by 'SD_CARD_CAN_SWAP=1'"
return
fi
# Compare the available space with the size of the swap.
# And set a acceptable size from the request
if [ $usable_space -ge $swap_max_size ]
then
local swap_size=$swap_max_size
elif [ $usable_space -ge $(( $swap_max_size / 2 )) ]
then
local swap_size=$(( $swap_max_size / 2 ))
elif [ $usable_space -ge $(( $swap_max_size / 3 )) ]
then
local swap_size=$(( $swap_max_size / 3 ))
elif [ $usable_space -ge $(( $swap_max_size / 4 )) ]
then
local swap_size=$(( $swap_max_size / 4 ))
else
echo "Not enough space left for a swap file" >&2
local swap_size=0
fi
# If there's enough space for a swap, and no existing swap here
if [ $swap_size -ne 0 ] && [ ! -e /swap_$app ]
then
# Preallocate space for the swap file, fallocate may sometime not be used, use dd instead in this case
if ! fallocate -l ${swap_size}K /swap_$app
then
dd if=/dev/zero of=/swap_$app bs=1024 count=${swap_size}
fi
chmod 0600 /swap_$app
# Create the swap
mkswap /swap_$app
# And activate it
swapon /swap_$app
# Then add an entry in fstab to load this swap at each boot.
echo -e "/swap_$app swap swap defaults 0 0 #Swap added by $app" >> /etc/fstab
fi
}
ynh_del_swap () {
# If there a swap at this place
if [ -e /swap_$app ]
then
# Clean the fstab
sed -i "/#Swap added by $app/d" /etc/fstab
# Desactive the swap file
swapoff /swap_$app
# And remove it
rm /swap_$app
fi
}
# Check if the device of the main mountpoint "/" is an SD card
#
# [internal]
#
# return 0 if it's an SD card, else 1
ynh_is_main_device_a_sd_card () {
local main_device=$(lsblk --output PKNAME --noheadings $(findmnt / --nofsroot --uniq --output source --noheadings --first-only))
if echo $main_device | grep --quiet "mmc" && [ $(tail -n1 /sys/block/$main_device/queue/rotational) == "0" ]
then
return 0
else
return 1
fi
}

View file

@ -1,247 +0,0 @@
#!/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
}