diff --git a/.github/workflows/autoblack.yml b/.github/workflows/autoblack.yml index 369f88824..561cad656 100644 --- a/.github/workflows/autoblack.yml +++ b/.github/workflows/autoblack.yml @@ -1,29 +1,24 @@ name: Check / auto apply Black + on: push: - branches: - - dev + branches: [ "dev" ] + jobs: black: name: Check / auto apply black runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - name: Check files using the black formatter uses: psf/black@stable id: black with: options: "." continue-on-error: true - - shell: pwsh - id: check_files_changed - run: | - # Diff HEAD with the previous commit - $diff = git diff - $HasDiff = $diff.Length -gt 0 - Write-Host "::set-output name=files_changed::$HasDiff" + - name: Create Pull Request - if: steps.check_files_changed.outputs.files_changed == 'true' uses: peter-evans/create-pull-request@v6 with: token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/n_updater.yml b/.github/workflows/n_updater.yml index 7d4dee940..340a5893f 100644 --- a/.github/workflows/n_updater.yml +++ b/.github/workflows/n_updater.yml @@ -12,28 +12,18 @@ jobs: steps: - name: Fetch the source code uses: actions/checkout@v4 - with: - token: ${{ secrets.GITHUB_TOKEN }} + - name: Run the updater script id: run_updater run: | - # Setting up Git user - git config --global user.name 'yunohost-bot' - git config --global user.email 'yunohost-bot@users.noreply.github.com' # Download n wget https://raw.githubusercontent.com/tj/n/master/bin/n --output-document=helpers/vendor/n/n - # Proceed only if there is a change - [[ -z "$(git diff helpers/vendor/n/n)" ]] || echo "PROCEED=true" >> $GITHUB_ENV - echo "VERSION=$(sed -n 's/^VERSION=\"\(.*\)\"/\1/p' < n)" >> $GITHUB_ENV - - name: Commit changes - id: commit - if: ${{ env.PROCEED == 'true' }} - run: | - git commit -am "Upgrade n to v$VERSION" + + echo "VERSION=$(sed -n 's/^VERSION=\"\(.*\)\"/\1/p' < helpers/vendor/n/n)" >> $GITHUB_ENV + - name: Create Pull Request - id: cpr - if: ${{ env.PROCEED == 'true' }} uses: peter-evans/create-pull-request@v6 + id: cpr with: token: ${{ secrets.GITHUB_TOKEN }} commit-message: Update n to ${{ env.VERSION }} @@ -41,7 +31,7 @@ jobs: author: 'yunohost-bot ' signoff: false base: dev - branch: ci-auto-update-n-v${{ env.VERSION }} + branch: ci-auto-update-n-${{ env.VERSION }} delete-branch: true title: 'Upgrade n to ${{ env.VERSION }}' body: | diff --git a/debian/control b/debian/control index ca44e6b75..9ee977c15 100644 --- a/debian/control +++ b/debian/control @@ -28,7 +28,7 @@ Depends: ${python3:Depends}, ${misc:Depends} , opendkim-tools, opendkim, postsrsd, procmail, mailutils , redis-server , acl - , git, curl, wget, cron, unzip, jq, bc, at, procps + , git, curl, wget, cron, unzip, jq, bc, at, procps, j2cli , lsb-release, haveged, fake-hwclock, equivs, lsof, whois Recommends: yunohost-admin, yunohost-portal (>= 12.0) , ntp, inetutils-ping | iputils-ping diff --git a/debian/install b/debian/install index 5169d0b62..86636fa93 100644 --- a/debian/install +++ b/debian/install @@ -1,7 +1,7 @@ bin/* /usr/bin/ share/* /usr/share/yunohost/ hooks/* /usr/share/yunohost/hooks/ -helpers/* /usr/share/yunohost/helpers.d/ +helpers/* /usr/share/yunohost/ conf/* /usr/share/yunohost/conf/ locales/* /usr/share/yunohost/locales/ doc/yunohost.8.gz /usr/share/man/man8/ diff --git a/helpers/helpers b/helpers/helpers new file mode 100644 index 000000000..01b6fa7e3 --- /dev/null +++ b/helpers/helpers @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +# Entrypoint for the helpers scripts +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) + +# Helpers version can be specified via an environment variable or default to 1. +YNH_HELPERS_VERSION=${YNH_HELPERS_VERSION:-1} + +# This is a trick to later only restore set -x if it was set when calling this script +readonly XTRACE_ENABLE=$(set +o | grep xtrace) +set +x + +YNH_HELPERS_DIR="$SCRIPT_DIR/helpers.v${YNH_HELPERS_VERSION}.d" +case "$YNH_HELPERS_VERSION" in + "1" | "2") + readarray -t HELPERS < <(find -L "$YNH_HELPERS_DIR" -mindepth 1 -maxdepth 1 -type f) + for helper in "${HELPERS[@]}"; do + [ -r "$helper" ] && source "$helper" + done + ;; + *) + echo "Helpers are not available in version '$YNH_HELPERS_VERSION'." >&2 + exit 1 +esac + +eval "$XTRACE_ENABLE" diff --git a/helpers/apps b/helpers/helpers.v1.d/apps similarity index 100% rename from helpers/apps rename to helpers/helpers.v1.d/apps diff --git a/helpers/apt b/helpers/helpers.v1.d/apt similarity index 100% rename from helpers/apt rename to helpers/helpers.v1.d/apt diff --git a/helpers/backup b/helpers/helpers.v1.d/backup similarity index 100% rename from helpers/backup rename to helpers/helpers.v1.d/backup diff --git a/helpers/config b/helpers/helpers.v1.d/config similarity index 100% rename from helpers/config rename to helpers/helpers.v1.d/config diff --git a/helpers/fail2ban b/helpers/helpers.v1.d/fail2ban similarity index 100% rename from helpers/fail2ban rename to helpers/helpers.v1.d/fail2ban diff --git a/helpers/getopts b/helpers/helpers.v1.d/getopts similarity index 100% rename from helpers/getopts rename to helpers/helpers.v1.d/getopts diff --git a/helpers/go b/helpers/helpers.v1.d/go similarity index 76% rename from helpers/go rename to helpers/helpers.v1.d/go index 0e18301f7..63ff0548d 100644 --- a/helpers/go +++ b/helpers/helpers.v1.d/go @@ -111,43 +111,35 @@ ynh_install_go () { 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 + mkdir -p $goenv_install_dir + pushd "$goenv_install_dir" + if ! [ -x "$goenv_install_dir/bin/goenv" ]; then + ynh_print_info --message="Downloading goenv..." 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 + git remote add origin https://github.com/syndbg/goenv.git + else + ynh_print_info --message="Updating goenv..." + fi + git fetch -q --tags --prune origin + local git_latest_tag=$(git describe --tags "$(git rev-list --tags --max-count=1)") + git checkout -q "$git_latest_tag" + ynh_go_try_bash_extension + goenv=$goenv_install_dir/bin/goenv + popd - 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 + # Install or update xxenv-latest + mkdir -p "$goenv_install_dir/plugins/xxenv-latest" + pushd "$goenv_install_dir/plugins/xxenv-latest" + if ! [ -x "$goenv_install_dir/plugins/xxenv-latest/bin/goenv-latest" ]; then + ynh_print_info --message="Downloading xxenv-latest..." + git init -q + git remote add origin https://github.com/momo-lab/xxenv-latest.git + else + ynh_print_info --message="Updating xxenv-latest..." + fi + local git_latest_tag=$(git describe --tags "$(git rev-list --tags --max-count=1)") + git checkout -q "$git_latest_tag" + popd # Enable caching mkdir -p "${goenv_install_dir}/cache" @@ -162,12 +154,12 @@ ynh_install_go () { 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) + 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 + 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 + ynh_app_setting_set --app="$app" --key="go_version" --value="$final_go_version" # Cleanup Go versions ynh_cleanup_go @@ -189,7 +181,7 @@ eval \"\$(goenv init -)\" # # usage: ynh_remove_go ynh_remove_go () { - local go_version=$(ynh_app_setting_get --app=$YNH_APP_INSTANCE_NAME --key=go_version) + local go_version=$(ynh_app_setting_get --app="$app" --key="go_version") # Load goenv path in PATH local CLEAR_PATH="$goenv_install_dir/bin:$PATH" @@ -198,7 +190,7 @@ ynh_remove_go () { 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 + ynh_app_setting_delete --app="$app" --key="go_version" # Cleanup Go versions ynh_cleanup_go @@ -224,7 +216,7 @@ ynh_cleanup_go () { 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 @@ -232,7 +224,7 @@ ynh_cleanup_go () { 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 + $goenv_install_dir/bin/goenv uninstall --force "$installed_go_version" fi done diff --git a/helpers/hardware b/helpers/helpers.v1.d/hardware similarity index 100% rename from helpers/hardware rename to helpers/helpers.v1.d/hardware diff --git a/helpers/logging b/helpers/helpers.v1.d/logging similarity index 100% rename from helpers/logging rename to helpers/helpers.v1.d/logging diff --git a/helpers/logrotate b/helpers/helpers.v1.d/logrotate similarity index 100% rename from helpers/logrotate rename to helpers/helpers.v1.d/logrotate diff --git a/helpers/mongodb b/helpers/helpers.v1.d/mongodb similarity index 100% rename from helpers/mongodb rename to helpers/helpers.v1.d/mongodb diff --git a/helpers/multimedia b/helpers/helpers.v1.d/multimedia similarity index 100% rename from helpers/multimedia rename to helpers/helpers.v1.d/multimedia diff --git a/helpers/mysql b/helpers/helpers.v1.d/mysql similarity index 100% rename from helpers/mysql rename to helpers/helpers.v1.d/mysql diff --git a/helpers/network b/helpers/helpers.v1.d/network similarity index 100% rename from helpers/network rename to helpers/helpers.v1.d/network diff --git a/helpers/nginx b/helpers/helpers.v1.d/nginx similarity index 100% rename from helpers/nginx rename to helpers/helpers.v1.d/nginx diff --git a/helpers/nodejs b/helpers/helpers.v1.d/nodejs similarity index 97% rename from helpers/nodejs rename to helpers/helpers.v1.d/nodejs index 9401fc875..be79cef66 100644 --- a/helpers/nodejs +++ b/helpers/helpers.v1.d/nodejs @@ -115,7 +115,7 @@ ynh_install_nodejs() { # Install (or update if YunoHost vendor/ folder updated since last install) n mkdir -p $n_install_dir/bin/ - cp /usr/share/yunohost/helpers.d/vendor/n/n $n_install_dir/bin/n + cp "$YNH_HELPERS_DIR/vendor/n/n" $n_install_dir/bin/n # Tweak for n to understand it's installed in $N_PREFIX ynh_replace_string --match_string="^N_PREFIX=\${N_PREFIX-.*}$" --replace_string="N_PREFIX=\${N_PREFIX-$N_PREFIX}" --target_file="$n_install_dir/bin/n" @@ -144,7 +144,7 @@ ynh_install_nodejs() { fi # Store the ID of this app and the version of node requested for it - echo "$YNH_APP_INSTANCE_NAME:$nodejs_version" | tee --append "$n_install_dir/ynh_app_version" + echo "$app:$nodejs_version" | tee --append "$n_install_dir/ynh_app_version" # Store nodejs_version into the config of this app ynh_app_setting_set --app=$app --key=nodejs_version --value=$nodejs_version @@ -168,7 +168,7 @@ ynh_remove_nodejs() { nodejs_version=$(ynh_app_setting_get --app=$app --key=nodejs_version) # Remove the line for this app - sed --in-place "/$YNH_APP_INSTANCE_NAME:$nodejs_version/d" "$n_install_dir/ynh_app_version" + sed --in-place "/$app:$nodejs_version/d" "$n_install_dir/ynh_app_version" # If no other app uses this version of nodejs, remove it. if ! grep --quiet "$nodejs_version" "$n_install_dir/ynh_app_version"; then diff --git a/helpers/permission b/helpers/helpers.v1.d/permission similarity index 100% rename from helpers/permission rename to helpers/helpers.v1.d/permission diff --git a/helpers/php b/helpers/helpers.v1.d/php similarity index 100% rename from helpers/php rename to helpers/helpers.v1.d/php diff --git a/helpers/postgresql b/helpers/helpers.v1.d/postgresql similarity index 100% rename from helpers/postgresql rename to helpers/helpers.v1.d/postgresql diff --git a/helpers/redis b/helpers/helpers.v1.d/redis similarity index 100% rename from helpers/redis rename to helpers/helpers.v1.d/redis diff --git a/helpers/ruby b/helpers/helpers.v1.d/ruby similarity index 95% rename from helpers/ruby rename to helpers/helpers.v1.d/ruby index 82a946935..24e4b218b 100644 --- a/helpers/ruby +++ b/helpers/helpers.v1.d/ruby @@ -54,7 +54,7 @@ ynh_use_ruby () { ruby_version=$(ynh_app_setting_get --app=$app --key=ruby_version) # Get the absolute path of this version of Ruby - ruby_path="$ruby_version_path/$YNH_APP_INSTANCE_NAME/bin" + ruby_path="$ruby_version_path/$app/bin" # Allow alias to be used into bash script shopt -s expand_aliases @@ -207,16 +207,16 @@ ynh_install_ruby () { RUBY_CONFIGURE_OPTS="--disable-install-doc --with-jemalloc" MAKE_OPTS="-j2" rbenv install --skip-existing $final_ruby_version > /dev/null 2>&1 # Store ruby_version into the config of this app - ynh_app_setting_set --app=$YNH_APP_INSTANCE_NAME --key=ruby_version --value=$final_ruby_version + ynh_app_setting_set --app=$app --key=ruby_version --value=$final_ruby_version # Remove app virtualenv - if rbenv alias --list | grep --quiet "$YNH_APP_INSTANCE_NAME " + if rbenv alias --list | grep --quiet "$app " then - rbenv alias $YNH_APP_INSTANCE_NAME --remove + rbenv alias $app --remove fi # Create app virtualenv - rbenv alias $YNH_APP_INSTANCE_NAME $final_ruby_version + rbenv alias $app $final_ruby_version # Cleanup Ruby versions ynh_cleanup_ruby @@ -238,7 +238,7 @@ eval \"\$(rbenv init -)\" # # usage: ynh_remove_ruby ynh_remove_ruby () { - local ruby_version=$(ynh_app_setting_get --app=$YNH_APP_INSTANCE_NAME --key=ruby_version) + local ruby_version=$(ynh_app_setting_get --app=$app --key=ruby_version) # Load rbenv path in PATH local CLEAR_PATH="$rbenv_install_dir/bin:$PATH" @@ -246,10 +246,10 @@ ynh_remove_ruby () { # Remove /usr/local/bin in PATH in case of Ruby prior installation PATH=$(echo $CLEAR_PATH | sed 's@/usr/local/bin:@@') - rbenv alias $YNH_APP_INSTANCE_NAME --remove + rbenv alias $app --remove # Remove the line for this app - ynh_app_setting_delete --app=$YNH_APP_INSTANCE_NAME --key=ruby_version + ynh_app_setting_delete --app=$app --key=ruby_version # Cleanup Ruby versions ynh_cleanup_ruby diff --git a/helpers/setting b/helpers/helpers.v1.d/setting similarity index 100% rename from helpers/setting rename to helpers/helpers.v1.d/setting diff --git a/helpers/string b/helpers/helpers.v1.d/string similarity index 100% rename from helpers/string rename to helpers/helpers.v1.d/string diff --git a/helpers/systemd b/helpers/helpers.v1.d/systemd similarity index 100% rename from helpers/systemd rename to helpers/helpers.v1.d/systemd diff --git a/helpers/user b/helpers/helpers.v1.d/user similarity index 100% rename from helpers/user rename to helpers/helpers.v1.d/user diff --git a/helpers/utils b/helpers/helpers.v1.d/utils similarity index 96% rename from helpers/utils rename to helpers/helpers.v1.d/utils index 3837e8deb..73cb0cc62 100644 --- a/helpers/utils +++ b/helpers/helpers.v1.d/utils @@ -318,7 +318,7 @@ ynh_setup_source() { mv $src_filename $dest_dir/$src_rename fi elif [[ "$src_format" == "docker" ]]; then - /usr/share/yunohost/helpers.d/vendor/docker-image-extract/docker-image-extract -p $src_platform -o $dest_dir $src_url 2>&1 + "$YNH_HELPERS_DIR/vendor/docker-image-extract/docker-image-extract" -p $src_platform -o $dest_dir $src_url 2>&1 elif [[ "$src_format" == "zip" ]]; then # Zip format # Using of a temp directory, because unzip doesn't manage --strip-components @@ -451,12 +451,17 @@ ynh_local_curl() { # # usage: ynh_add_config --template="template" --destination="destination" # | arg: -t, --template= - Template config file to use -# | arg: -d, --destination= - Destination of the config file +# | arg: -d, --destination= - Destination of the config file +# | arg: -j, --jinja - Use jinja template instead of legacy __MY_VAR__ # # examples: # ynh_add_config --template=".env" --destination="$install_dir/.env" use the template file "../conf/.env" +# ynh_add_config --jinja --template="config.j2" --destination="$install_dir/config" use the template file "../conf/config.j2" # ynh_add_config --template="/etc/nginx/sites-available/default" --destination="etc/nginx/sites-available/mydomain.conf" # +## +## How it works in "legacy" mode +## # The template can be by default the name of a file in the conf directory # of a YunoHost Package, a relative path or an absolute path. # @@ -480,6 +485,37 @@ ynh_local_curl() { # __VAR_2__ by $var_2 # ``` # +## +## When --jinja is enabled +## +# For a full documentation of the template you can refer to: https://jinja.palletsprojects.com/en/3.1.x/templates/ +# In Yunohost context there are no really some specificity except that all variable passed are of type string. +# So here are some example of recommended usage: +# +# If you need a conditional block +# +# {% if should_my_block_be_shown == 'true' %} +# ... +# {% endif %} +# +# or +# +# {% if should_my_block_be_shown == '1' %} +# ... +# {% endif %} +# +# If you need to iterate with loop: +# +# {% for yolo in var_with_multiline_value.splitlines() %} +# ... +# {% endfor %} +# +# or +# +# {% for jail in my_var_with_coma.split(',') %} +# ... +# {% endfor %} +# # The helper will verify the checksum and backup the destination file # if it's different before applying the new template. # @@ -489,13 +525,15 @@ ynh_local_curl() { # Requires YunoHost version 4.1.0 or higher. ynh_add_config() { # Declare an array to define the options of this helper. - local legacy_args=tdv - local -A args_array=([t]=template= [d]=destination=) + local legacy_args=tdj + local -A args_array=([t]=template= [d]=destination= [j]=jinja) local template local destination + local jinja # Manage arguments with getopts ynh_handle_getopts_args "$@" local template_path + jinja="${jinja:-0}" if [ -f "$YNH_APP_BASEDIR/conf/$template" ]; then template_path="$YNH_APP_BASEDIR/conf/$template" @@ -512,14 +550,20 @@ ynh_add_config() { # created a file beforehand to have control over it # (cp won't overwrite ownership / modes by default...) touch $destination - chown root:root $destination chmod 640 $destination - - cp -f "$template_path" "$destination" - _ynh_apply_default_permissions $destination - ynh_replace_vars --file="$destination" + if [[ "$jinja" == 1 ]] + then + # This is ran in a subshell such that the "export" does not "contaminate" the main process + ( + export $(compgen -v) + j2 "$template_path" -f env -o $destination + ) + else + cp -f "$template_path" "$destination" + ynh_replace_vars --file="$destination" + fi ynh_store_file_checksum --file="$destination" } @@ -1084,7 +1128,7 @@ _ynh_apply_default_permissions() { # Crons should be owned by root # Also we don't want systemd conf, nginx conf or others stuff to be owned by the app, # otherwise they could self-edit their own systemd conf and escalate privilege - if echo "$target" | grep -q '^/etc/cron\|/etc/php\|/etc/nginx/conf.d\|/etc/fail2ban\|/etc/systemd/system' + if grep -qE '^(/etc/cron|/etc/php|/etc/nginx/conf.d|/etc/fail2ban|/etc/systemd/system)' <<< "$target" then chmod 400 $target chown root:root $target diff --git a/helpers/helpers.v1.d/vendor b/helpers/helpers.v1.d/vendor new file mode 120000 index 000000000..9c39cc9f8 --- /dev/null +++ b/helpers/helpers.v1.d/vendor @@ -0,0 +1 @@ +../vendor \ No newline at end of file diff --git a/helpers/helpers.v2.d b/helpers/helpers.v2.d new file mode 120000 index 000000000..e2614c897 --- /dev/null +++ b/helpers/helpers.v2.d @@ -0,0 +1 @@ +helpers.v1.d \ No newline at end of file diff --git a/share/helpers b/share/helpers deleted file mode 100644 index 04f7b538c..000000000 --- a/share/helpers +++ /dev/null @@ -1,8 +0,0 @@ -# -*- shell-script -*- - -readonly XTRACE_ENABLE=$(set +o | grep xtrace) # This is a trick to later only restore set -x if it was set when calling this script -set +x -for helper in $(run-parts --list /usr/share/yunohost/helpers.d 2>/dev/null) ; do - [ -r $helper ] && . $helper || true -done -eval "$XTRACE_ENABLE" diff --git a/src/app.py b/src/app.py index b38812f1c..e4a07ddcd 100644 --- a/src/app.py +++ b/src/app.py @@ -2870,6 +2870,10 @@ def _make_environment_for_app_script( "YNH_APP_INSTANCE_NUMBER": str(app_instance_nb), "YNH_APP_MANIFEST_VERSION": manifest.get("version", "?"), "YNH_APP_PACKAGING_FORMAT": str(manifest["packaging_format"]), + "YNH_HELPERS_VERSION": str( + manifest.get("integration", {}).get("helpers_version") + or manifest["packaging_format"] + ).replace(".0", ""), "YNH_ARCH": system_arch(), "YNH_DEBIAN_VERSION": debian_version(), } diff --git a/tests/test_helpers.d/ynhtest_templating.sh b/tests/test_helpers.d/ynhtest_templating.sh new file mode 100644 index 000000000..118e643ee --- /dev/null +++ b/tests/test_helpers.d/ynhtest_templating.sh @@ -0,0 +1,62 @@ +ynhtest_simple_template_app_config() { + + mkdir -p /etc/yunohost/apps/$app/ + echo "id: $app" > /etc/yunohost/apps/$app/settings.yml + + template="$(mktemp -d -p $VAR_WWW)/template.txt" + cat << EOF > $template +app=__APP__ +foo=__FOO__ +EOF + + foo="bar" + + ynh_add_config --template="$template" --destination="$VAR_WWW/config.txt" + + test "$(cat $VAR_WWW/config.txt)" == "$(echo -ne 'app=ynhtest\nfoo=bar')" + test "$(ls -l $VAR_WWW/config.txt | cut -d' ' -f1-4)" == "-rw-r----- 1 ynhtest ynhtest" +} + +ynhtest_simple_template_system_config() { + + mkdir -p /etc/yunohost/apps/$app/ + echo "id: $app" > /etc/yunohost/apps/$app/settings.yml + + rm -f /etc/cron.d/ynhtest_config + + template="$(mktemp -d -p $VAR_WWW)/template.txt" + cat << EOF > $template +app=__APP__ +foo=__FOO__ +EOF + + foo="bar" + + ynh_add_config --template="$template" --destination="/etc/cron.d/ynhtest_config" + + test "$(cat $VAR_WWW/config.txt)" == "$(echo -ne 'app=ynhtest\nfoo=bar')" + test "$(ls -l /etc/cron.d/ynhtest_config | cut -d' ' -f1-4)" == "-r-------- 1 root root" + + rm -f /etc/cron.d/ynhtest_config +} + +ynhtest_jinja_template_app_config() { + + mkdir -p /etc/yunohost/apps/$app/ + echo "id: $app" > /etc/yunohost/apps/$app/settings.yml + + template="$(mktemp -d -p $VAR_WWW)/template.txt" + cat << EOF > $template +app={{ app }} +{% if foo == "bar" %}foo=true{% endif %} +EOF + + foo="bar" + + ynh_add_config --template="$template" --destination="$VAR_WWW/config.txt" --jinja + + test "$(cat $VAR_WWW/config.txt)" == "$(echo -ne 'app=ynhtest\nfoo=true')" + test "$(ls -l $VAR_WWW/config.txt | cut -d' ' -f1-4)" == "-rw-r----- 1 ynhtest ynhtest" +} + + diff --git a/tests/test_helpers.sh b/tests/test_helpers.sh index 153ce1386..a2ccb75c4 100644 --- a/tests/test_helpers.sh +++ b/tests/test_helpers.sh @@ -41,6 +41,10 @@ popd >/dev/null VAR_WWW=$(mktemp -d)/var/www mkdir -p $VAR_WWW + +# Needed to check the permission behavior in ynh_add_config x_x +getent passwd ynhtest &>/dev/null || useradd --system ynhtest + # ========================================================= for TEST_SUITE in $(ls test_helpers.d/*)