diff --git a/check_process b/check_process index 0031fac..e44d632 100644 --- a/check_process +++ b/check_process @@ -10,10 +10,10 @@ setup_root=1 setup_nourl=0 setup_private=0 - setup_public=0 + setup_public=1 upgrade=1 backup_restore=1 - multi_instance=0 + multi_instance=1 incorrect_path=1 port_already_use=0 change_url=0 diff --git a/conf/mastodon-sidekiq.service b/conf/mastodon-sidekiq.service index 59f636f..920fcf4 100644 --- a/conf/mastodon-sidekiq.service +++ b/conf/mastodon-sidekiq.service @@ -7,8 +7,9 @@ User=__APP__ WorkingDirectory=__FINALPATH__/live Environment="RAILS_ENV=production" - Environment="DB_POOL=20" - ExecStart=/opt/rbenv/versions/2.6.0/bin/bundle exec sidekiq -c 20 -q default -q mailers -q pull -q push + Environment="DB_POOL=25" + Environment="MALLOC_ARENA_MAX=2" + ExecStart=/opt/rbenv/versions/2.6.0/bin/bundle exec sidekiq -c 25 TimeoutSec=15 Restart=always StandardError=syslog diff --git a/conf/mastodon-streaming.service b/conf/mastodon-streaming.service index 5f66236..2e130d5 100644 --- a/conf/mastodon-streaming.service +++ b/conf/mastodon-streaming.service @@ -8,7 +8,9 @@ WorkingDirectory=__FINALPATH__/live Environment="NODE_ENV=production" Environment="PORT=__PORT_STREAM__" - ExecStart=/opt/node_n/n/versions/node/8/bin/npm run start + Environment="STREAMING_CLUSTER_NUM=1" + Environment=PATH=__NODEJS_PATH__ + ExecStart=__NODEJS_PATH__/node ./streaming TimeoutSec=15 Restart=always StandardError=syslog diff --git a/conf/mastodon-web.service b/conf/mastodon-web.service index 32af850..c95ba7f 100644 --- a/conf/mastodon-web.service +++ b/conf/mastodon-web.service @@ -9,6 +9,7 @@ Environment="RAILS_ENV=production" Environment="PORT=__PORT_WEB__" ExecStart=/opt/rbenv/versions/2.6.0/bin/bundle exec puma -C config/puma.rb + ExecReload=/bin/kill -SIGUSR1 $MAINPID TimeoutSec=15 Restart=always StandardError=syslog diff --git a/conf/nginx.conf b/conf/nginx.conf index a183a31..190c650 100644 --- a/conf/nginx.conf +++ b/conf/nginx.conf @@ -10,31 +10,48 @@ location / { rewrite ^ https://$server_name$request_uri? permanent; } + proxy_set_header Accept-Encoding ""; try_files $uri @proxy; # Include SSOWAT user panel. include conf.d/yunohost_panel.conf.inc; } -# add to v1.4 assets -location ~ ^/(assets|system/media_attachments/files|system/accounts/avatars) { +location ~ ^/(emoji|packs|system/accounts/avatars|system/media_attachments/files) { add_header Cache-Control "public, max-age=31536000, immutable"; + add_header Strict-Transport-Security "max-age=31536000"; try_files $uri @proxy; - } +} + +location /sw.js { + add_header Cache-Control "public, max-age=0"; + add_header Strict-Transport-Security "max-age=31536000"; + try_files $uri @proxy; +} location @proxy { - proxy_set_header Host $host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Proto https; - proxy_pass_header Server; - proxy_pass http://127.0.0.1:__PORT_WEB__; - proxy_buffering off; - proxy_redirect off; - proxy_http_version 1.1; - proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection "upgrade"; - tcp_nodelay on; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto https; + proxy_set_header Proxy ""; + proxy_pass_header Server; + + proxy_pass http://127.0.0.1:3000; + proxy_buffering on; + proxy_redirect off; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + + #proxy_cache CACHE; + proxy_cache_valid 200 7d; + proxy_cache_valid 410 24h; + proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504; + add_header X-Cached $upstream_cache_status; + add_header Strict-Transport-Security "max-age=31536000"; + + tcp_nodelay on; } location /api/v1/streaming { @@ -42,11 +59,17 @@ location /api/v1/streaming { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto https; - proxy_pass http://127.0.0.1:__PORT_STREAM__; + proxy_set_header Proxy ""; + + proxy_pass http://127.0.0.1:4000; proxy_buffering off; proxy_redirect off; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; + tcp_nodelay on; } + + +error_page 500 501 502 503 504 /500.html; \ No newline at end of file diff --git a/manifest.json b/manifest.json index 1d1789e..fd70341 100644 --- a/manifest.json +++ b/manifest.json @@ -16,7 +16,7 @@ "requirements": { "yunohost": ">= 3.4" }, - "multi_instance": false, + "multi_instance": true, "services": [ "nginx" ], diff --git a/scripts/_common.sh b/scripts/_common.sh index d0c5b58..79dc04c 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -20,21 +20,6 @@ pkg_dependencies="imagemagick libpq-dev libxml2-dev libxslt1-dev file curl apt-t # FUTURE OFFICIAL HELPERS #================================================= -# Execute a command as another user -# usage: exec_as USER COMMAND [ARG ...] -exec_as() { - local user=$1 - shift 1 - - if [[ $user = $(whoami) ]]; then - eval "$@" - else - sudo --login --user="$user" "$@" - fi -} - - - # Send an email to inform the administrator # # usage: ynh_send_readme_to_admin app_message [recipients] diff --git a/scripts/backup b/scripts/backup index c5a4702..58b29a7 100644 --- a/scripts/backup +++ b/scripts/backup @@ -8,12 +8,16 @@ #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_systemd_action source /usr/share/yunohost/helpers #================================================= # MANAGE SCRIPT FAILURE #================================================= +ynh_clean_setup () { + ynh_clean_check_starting +} # Exit if an error occurs during the execution of the script ynh_abort_if_errors @@ -31,12 +35,11 @@ db_name=$(ynh_app_setting_get $app db_name) #================================================= # STOP MASTODON SERVICES #================================================= -ynh_print_info "Stopping Mastodon Services..." - -yunohost service stop "$app-web" -yunohost service stop "$app-sidekiq" -yunohost service stop "$app-streaming" +ynh_print_info "Stopping Mastodon services..." +ynh_systemd_action --action=stop --service_name=${app}-web --line_match="Stopped" --log_path=systemd +ynh_systemd_action --action=stop --service_name=${app}-sidekiq --line_match="Stopped" --log_path=systemd +ynh_systemd_action --action=stop --service_name=${app}-streaming --line_match="Stopped" --log_path=systemd #================================================= # STANDARD BACKUP STEPS @@ -81,14 +84,11 @@ ynh_backup "/etc/cron.d/$app" #================================================= # START MASTODON SERVICES #================================================= -ynh_print_info "Starting Mastodon Services..." +ynh_print_info "Starting Mastodon services..." -yunohost service start "$app-web" -yunohost service start "$app-sidekiq" -yunohost service start "$app-streaming" - -# Waiting start all services -sleep 30 +ynh_systemd_action --action=start --service_name=${app}-web --line_match="Listening on tcp" --log_path=systemd +ynh_systemd_action --action=start --service_name=${app}-sidekiq --line_match="Starting processing" --log_path=systemd +ynh_systemd_action --action=start --service_name=${app}-streaming --line_match="Worker 1 now listening" --log_path=systemd #================================================= # END OF SCRIPT diff --git a/scripts/change_url b/scripts/change_url index 51d37d6..525ecc9 100644 --- a/scripts/change_url +++ b/scripts/change_url @@ -7,6 +7,7 @@ #================================================= source _common.sh +source ynh_systemd_action source /usr/share/yunohost/helpers #================================================= @@ -97,27 +98,24 @@ fi #================================================= ynh_print_info "Stopping Mastodon services..." -yunohost service stop "$app-web" -yunohost service stop "$app-sidekiq" -yunohost service stop "$app-streaming" +ynh_systemd_action --action=stop --service_name=${app}-web --line_match="Stopped" --log_path=systemd +ynh_systemd_action --action=stop --service_name=${app}-sidekiq --line_match="Stopped" --log_path=systemd +ynh_systemd_action --action=stop --service_name=${app}-streaming --line_match="Stopped" --log_path=systemd #================================================= # CHANGE CONFIGURATION #================================================= -ynh_replace_string "LOCAL_DOMAIN=*" "LOCAL_DOMAIN=${domain}" "${final_path}/live/.env.production" -ynh_replace_string "SMTP_FROM_ADDRESS=*" "SMTP_FROM_ADDRESS=$admin_mail" "${final_path}/live/.env.production" +ynh_replace_string "LOCAL_DOMAIN=.*" "LOCAL_DOMAIN=${domain}" "${final_path}/live/.env.production" #================================================= # START MASTODON SERVICES #================================================= ynh_print_info "Starting Mastodon services..." -yunohost service start "$app-web" -yunohost service start "$app-sidekiq" -yunohost service start "$app-streaming" - -sleep 30 +ynh_systemd_action --action=start --service_name=${app}-web --line_match="Listening on tcp" --log_path=systemd +ynh_systemd_action --action=start --service_name=${app}-sidekiq --line_match="Starting processing" --log_path=systemd +ynh_systemd_action --action=start --service_name=${app}-streaming --line_match="Worker 1 now listening" --log_path=systemd #================================================= # GENERIC FINALISATION diff --git a/scripts/install b/scripts/install index 4814306..e62b078 100644 --- a/scripts/install +++ b/scripts/install @@ -7,14 +7,18 @@ #================================================= source _common.sh -source /usr/share/yunohost/helpers source ynh_install_ruby -source ynh_add_secure_repos__2 +source ynh_add_secure_repos__3 +source ynh_systemd_action +source /usr/share/yunohost/helpers #================================================= # MANAGE SCRIPT FAILURE #================================================= +ynh_clean_setup () { + ynh_clean_check_starting +} # Exit if an error occurs during the execution of the script ynh_abort_if_errors @@ -78,8 +82,6 @@ ynh_app_setting_set $app port_stream $port_stream #================================================= ynh_print_info "Installing dependencies..." -# TODO: add in a clean way backports and yarn - # Import debian archive pubkey, need on ARM arch arch=$(uname -m) if [[ "$arch" = arm* ]]; then @@ -96,7 +98,6 @@ ynh_install_extra_repo --repo="deb https://dl.yarnpkg.com/debian/ stable main" - # install nodejs ynh_install_nodejs 8 -# TODO: use the same mecanism with other files ynh_install_app_dependencies $pkg_dependencies #================================================= @@ -149,7 +150,6 @@ ynh_system_user_create $app $final_path #================================================= ynh_install_ruby --ruby_version=2.6.0 - /opt/rbenv/versions/2.6.0/bin/gem update --system #/opt/rbenv/versions/2.6.0/bin/gem install bundler --no-document @@ -214,7 +214,7 @@ ynh_secure_remove "$final_path/live/key.txt" #================================================= # SETUP CRON JOB FOR REMOVING CACHE #================================================= -ynh_print_info "Setuping a cron job for remiving cache..." +ynh_print_info "Setuping a cron job for rem0ving cache..." ynh_replace_string "__FINAL_PATH__" "$final_path" ../conf/cron ynh_replace_string "__USER__" "$app" ../conf/cron @@ -228,11 +228,19 @@ ynh_print_info "Configuring a systemd service..." # Create a dedicated systemd config ynh_replace_string "__PORT_WEB__" "$port_web" "../conf/mastodon-web.service" ynh_replace_string "__PORT_STREAM__" "$port_stream" "../conf/mastodon-streaming.service" +ynh_replace_string "__NODEJS_PATH__" "$nodejs_path" "../conf/mastodon-streaming.service" ynh_add_systemd_config "$app-web" "mastodon-web.service" ynh_add_systemd_config "$app-sidekiq" "mastodon-sidekiq.service" ynh_add_systemd_config "$app-streaming" "mastodon-streaming.service" -systemctl start "$app-web.service" "$app-sidekiq.service" "$app-streaming.service" +#================================================= +# START MASTODON SERVICES +#================================================= +ynh_print_info "Starting Mastodon services..." + +ynh_systemd_action --action=start --service_name=${app}-web --line_match="Listening on tcp" --log_path=systemd +ynh_systemd_action --action=start --service_name=${app}-sidekiq --line_match="Starting processing" --log_path=systemd +ynh_systemd_action --action=start --service_name=${app}-streaming --line_match="Worker 1 now listening" --log_path=systemd #================================================= # STORE THE CONFIG FILE CHECKSUM diff --git a/scripts/remove b/scripts/remove index 1690fdb..18cb655 100644 --- a/scripts/remove +++ b/scripts/remove @@ -7,9 +7,9 @@ #================================================= source _common.sh -source /usr/share/yunohost/helpers source ynh_install_ruby -source ynh_add_secure_repos__2 +source ynh_add_secure_repos__3 +source /usr/share/yunohost/helpers #================================================= # LOAD SETTINGS @@ -77,7 +77,6 @@ ynh_print_info "Removing dependencies" ynh_remove_ruby ynh_remove_app_dependencies ynh_remove_nodejs - ynh_remove_extra_repo #================================================= diff --git a/scripts/restore b/scripts/restore index bfe6fbb..6e39fb5 100644 --- a/scripts/restore +++ b/scripts/restore @@ -8,14 +8,18 @@ #Keep this path for calling _common.sh inside the execution's context of backup and restore scripts source ../settings/scripts/_common.sh -source /usr/share/yunohost/helpers source ../settings/scripts/ynh_install_ruby -source ../settings/scripts/ynh_add_secure_repos__2 +source ../settings/scripts/ynh_add_secure_repos__3 +source ../settings/scripts/ynh_systemd_action +source /usr/share/yunohost/helpers #================================================= # MANAGE SCRIPT FAILURE #================================================= +ynh_clean_setup () { + ynh_clean_check_starting +} # Exit if an error occurs during the execution of the script ynh_abort_if_errors @@ -78,8 +82,6 @@ chown -R $app: $final_path #================================================= ynh_print_info "Reinstalling dependencies..." -# TODO: add in a clean way backports and yarn - # Import debian archive pubkey, need on ARM arch arch=$(uname -m) if [[ "$arch" = arm* ]]; then @@ -96,12 +98,13 @@ ynh_install_extra_repo --repo="deb https://dl.yarnpkg.com/debian/ stable main" - # install nodejs ynh_install_nodejs 8 -# TODO: use the same mecanism with other files ynh_install_app_dependencies $pkg_dependencies +#================================================= +# INSTALLING RUBY AND BUNDLER +#================================================= ynh_install_ruby --ruby_version=2.6.0 - /opt/rbenv/versions/2.6.0/bin/gem update --system #================================================= @@ -144,13 +147,19 @@ ynh_restore_file "/etc/cron.d/$app" #================================================= # GENERIC FINALIZATION #================================================= -# RELOAD NGINX AND SERVICES +# START MASTODON SERVICES #================================================= -ynh_print_info "Reloading nginx web server and services..." +ynh_print_info "Starting Mastodon services..." + +ynh_systemd_action --action=start --service_name=${app}-web --line_match="Listening on tcp" --log_path=systemd +ynh_systemd_action --action=start --service_name=${app}-sidekiq --line_match="Starting processing" --log_path=systemd +ynh_systemd_action --action=start --service_name=${app}-streaming --line_match="Worker 1 now listening" --log_path=systemd + +#================================================= +# RELOAD NGINX +#================================================= +ynh_print_info "Reloading nginx web server..." -systemctl restart "$app-web" "$app-sidekiq" "$app-streaming" -# Waiting start all services -sleep 30 systemctl reload nginx #================================================= diff --git a/scripts/upgrade b/scripts/upgrade index 266eae8..b90dddd 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -7,9 +7,10 @@ #================================================= source _common.sh -source /usr/share/yunohost/helpers source ynh_install_ruby -source ynh_add_secure_repos__2 +source ynh_add_secure_repos__3 +source ynh_systemd_action +source /usr/share/yunohost/helpers #================================================= # LOAD SETTINGS @@ -104,6 +105,7 @@ ynh_print_info "Backing up the app before upgrading (may take a while)..." # Backup the current version of the app ynh_backup_before_upgrade ynh_clean_setup () { + ynh_clean_check_starting # restore it if the upgrade fails ynh_restore_upgradebackup } @@ -124,9 +126,9 @@ path_url=$(ynh_normalize_url_path $path_url) #================================================= ynh_print_info "Stopping Mastodon services..." -yunohost service stop "$app-web" -yunohost service stop "$app-sidekiq" -yunohost service stop "$app-streaming" +ynh_systemd_action --action=stop --service_name=${app}-web --line_match="Stopped" --log_path=systemd +ynh_systemd_action --action=stop --service_name=${app}-sidekiq --line_match="Stopped" --log_path=systemd +ynh_systemd_action --action=stop --service_name=${app}-streaming --line_match="Stopped" --log_path=systemd #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE @@ -259,16 +261,19 @@ ynh_print_info "Upgrading systemd configuration..." # Create a dedicated systemd config ynh_replace_string "__PORT_WEB__" "$port_web" "../conf/mastodon-web.service" ynh_replace_string "__PORT_STREAM__" "$port_stream" "../conf/mastodon-streaming.service" +ynh_replace_string "__NODEJS_PATH__" "$nodejs_path" "../conf/mastodon-streaming.service" ynh_add_systemd_config "$app-web" "mastodon-web.service" ynh_add_systemd_config "$app-sidekiq" "mastodon-sidekiq.service" ynh_add_systemd_config "$app-streaming" "mastodon-streaming.service" -yunohost service start "$app-web" -yunohost service start "$app-sidekiq" -yunohost service start "$app-streaming" +#================================================= +# START MASTODON SERVICES +#================================================= +ynh_print_info "Starting Mastodon services..." -# Waiting start all services -sleep 30 +ynh_systemd_action --action=start --service_name=${app}-web --line_match="Listening on tcp" --log_path=systemd +ynh_systemd_action --action=start --service_name=${app}-sidekiq --line_match="Starting processing" --log_path=systemd +ynh_systemd_action --action=start --service_name=${app}-streaming --line_match="Worker 1 now listening" --log_path=systemd #================================================= # GENERIC FINALIZATION diff --git a/scripts/ynh_add_secure_repos__2 b/scripts/ynh_add_secure_repos__2 deleted file mode 100644 index 1e046ea..0000000 --- a/scripts/ynh_add_secure_repos__2 +++ /dev/null @@ -1,167 +0,0 @@ -#!/bin/bash - -# Pin a repository. -# -# usage: ynh_pin_repo --package=packages --pin=pin_filter --priority=priority_value [--name=name] [--append] -# | arg: -p, --package - Packages concerned by the pin. Or all, *. -# | arg: -i, --pin - Filter for the pin. -# | arg: -p, --priority - Priority for the pin -# | arg: -n, --name - Name for the files for this repo, $app as default value. -# | arg: -a, --append - Do not overwrite existing files. -# -# See https://manpages.debian.org/stretch/apt/apt_preferences.5.en.html for information about pinning. -# -ynh_pin_repo () { - # Declare an array to define the options of this helper. - local legacy_args=pirna - declare -Ar args_array=( [p]=package= [i]=pin= [r]=priority= [n]=name= [a]=append ) - local package - local pin - local priority - local name - local append - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - package="${package:-*}" - priority=${priority:-50} - name="${name:-$app}" - append=${append:-0} - - if [ $append -eq 1 ] - then - append="tee -a" - else - append="tee" - fi - - mkdir -p "/etc/apt/preferences.d" - echo "Package: $package -Pin: $pin -Pin-Priority: $priority" \ - | $append "/etc/apt/preferences.d/$name" -} - -# Add a repository. -# -# usage: ynh_add_repo --uri=uri --suite=suite --component=component [--name=name] [--append] -# | arg: -u, --uri - Uri of the repository. -# | arg: -s, --suite - Suite of the repository. -# | arg: -c, --component - Component of the repository. -# | arg: -n, --name - Name for the files for this repo, $app as default value. -# | arg: -a, --append - Do not overwrite existing files. -# -# Example for a repo like deb http://forge.yunohost.org/debian/ stretch stable -# uri suite component -# ynh_add_repo --uri=http://forge.yunohost.org/debian/ --suite=stretch --component=stable -# -ynh_add_repo () { - # Declare an array to define the options of this helper. - local legacy_args=uscna - declare -Ar args_array=( [u]=uri= [s]=suite= [c]=component= [n]=name= [a]=append ) - local uri - local suite - local component - local name - local append - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - name="${name:-$app}" - append=${append:-0} - - if [ $append -eq 1 ] - then - append="tee -a" - else - append="tee" - fi - - mkdir -p "/etc/apt/sources.list.d" - # Add the new repo in sources.list.d - echo "deb $uri $suite $component" \ - | $append "/etc/apt/sources.list.d/$name.list" -} - -# Add an extra repository correctly, pin it and get the key. -# -# usage: ynh_install_extra_repo --repo="repo" [--key=key_url] [--name=name] [--append] -# | arg: -r, --repo - Complete url of the extra repository. -# | arg: -k, --key - url to get the public key. -# | arg: -n, --name - Name for the files for this repo, $app as default value. -# | arg: -a, --append - Do not overwrite existing files. -ynh_install_extra_repo () { - # Declare an array to define the options of this helper. - local legacy_args=rkna - declare -Ar args_array=( [r]=repo= [k]=key= [n]=name= [a]=append ) - local repo - local key - local name - local append - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - name="${name:-$app}" - append=${append:-0} - key=${key:-0} - - if [ $append -eq 1 ] - then - append="--append" - wget_append="tee -a" - else - append="" - wget_append="tee" - fi - - # Split the repository into uri, suite and components. - # Remove "deb " at the beginning of the repo. - repo="${repo#deb }" - - # Get the uri - local uri="$(echo "$repo" | awk '{ print $1 }')" - - # Get the suite - local suite="$(echo "$repo" | awk '{ print $2 }')" - - # Get the components - local component="${repo##$uri $suite }" - - # Add the repository into sources.list.d - ynh_add_repo --uri="$uri" --suite="$suite" --component="$component" --name="$name" $append - - # Pin the new repo with the default priority, so it won't be used for upgrades. - # Build $pin from the uri without http and any sub path - local pin="${uri#*://}" - pin="${pin%%/*}" - ynh_pin_repo --package="*" --pin="origin \"$pin\"" --name="$name" $append - - # Get the public key for the repo - if [ -n "$key" ] - then - mkdir -p "/etc/apt/trusted.gpg.d" - wget -q "$key" -O - | gpg --dearmor | $wget_append /etc/apt/trusted.gpg.d/$name.gpg > /dev/null - fi - - # Update the list of package with the new repo - ynh_package_update -} - -# Remove an extra repository and the assiociated configuration. -# -# usage: ynh_remove_extra_repo [--name=name] -# | arg: -n, --name - Name for the files for this repo, $app as default value. -ynh_remove_extra_repo () { - # Declare an array to define the options of this helper. - local legacy_args=n - declare -Ar args_array=( [n]=name= ) - local name - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - name="${name:-$app}" - - ynh_secure_remove "/etc/apt/sources.list.d/$name.list" - ynh_secure_remove "/etc/apt/preferences.d/$name" - ynh_secure_remove "/etc/apt/trusted.gpg.d/$name.gpg" - ynh_secure_remove "/etc/apt/trusted.gpg.d/$name.asc" - - # Update the list of package to exclude the old repo - ynh_package_update -} diff --git a/scripts/ynh_add_secure_repos__3 b/scripts/ynh_add_secure_repos__3 new file mode 100644 index 0000000..3276f00 --- /dev/null +++ b/scripts/ynh_add_secure_repos__3 @@ -0,0 +1,294 @@ +#!/bin/bash + +# Pin a repository. +# +# usage: ynh_pin_repo --package=packages --pin=pin_filter [--priority=priority_value] [--name=name] [--append] +# | arg: -p, --package - Packages concerned by the pin. Or all, *. +# | arg: -i, --pin - Filter for the pin. +# | arg: -p, --priority - Priority for the pin +# | arg: -n, --name - Name for the files for this repo, $app as default value. +# | arg: -a, --append - Do not overwrite existing files. +# +# See https://manpages.debian.org/stretch/apt/apt_preferences.5.en.html for information about pinning. +# +ynh_pin_repo () { + # Declare an array to define the options of this helper. + local legacy_args=pirna + declare -Ar args_array=( [p]=package= [i]=pin= [r]=priority= [n]=name= [a]=append ) + local package + local pin + local priority + local name + local append + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + package="${package:-*}" + priority=${priority:-50} + name="${name:-$app}" + append=${append:-0} + + if [ $append -eq 1 ] + then + append="tee -a" + else + append="tee" + fi + + mkdir -p "/etc/apt/preferences.d" + echo "Package: $package +Pin: $pin +Pin-Priority: $priority" \ + | $append "/etc/apt/preferences.d/$name" +} + +# Add a repository. +# +# usage: ynh_add_repo --uri=uri --suite=suite --component=component [--name=name] [--append] +# | arg: -u, --uri - Uri of the repository. +# | arg: -s, --suite - Suite of the repository. +# | arg: -c, --component - Component of the repository. +# | arg: -n, --name - Name for the files for this repo, $app as default value. +# | arg: -a, --append - Do not overwrite existing files. +# +# Example for a repo like deb http://forge.yunohost.org/debian/ stretch stable +# uri suite component +# ynh_add_repo --uri=http://forge.yunohost.org/debian/ --suite=stretch --component=stable +# +ynh_add_repo () { + # Declare an array to define the options of this helper. + local legacy_args=uscna + declare -Ar args_array=( [u]=uri= [s]=suite= [c]=component= [n]=name= [a]=append ) + local uri + local suite + local component + local name + local append + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + name="${name:-$app}" + append=${append:-0} + + if [ $append -eq 1 ] + then + append="tee -a" + else + append="tee" + fi + + mkdir -p "/etc/apt/sources.list.d" + # Add the new repo in sources.list.d + echo "deb $uri $suite $component" \ + | $append "/etc/apt/sources.list.d/$name.list" +} + +# Add an extra repository correctly, pin it and get the key. +# +# usage: ynh_install_extra_repo --repo="repo" [--key=key_url] [--priority=priority_value] [--name=name] [--append] +# | arg: -r, --repo - Complete url of the extra repository. +# | arg: -k, --key - url to get the public key. +# | arg: -p, --priority - Priority for the pin +# | arg: -n, --name - Name for the files for this repo, $app as default value. +# | arg: -a, --append - Do not overwrite existing files. +ynh_install_extra_repo () { + # Declare an array to define the options of this helper. + local legacy_args=rkpna + declare -Ar args_array=( [r]=repo= [k]=key= [p]=priority= [n]=name= [a]=append ) + local repo + local key + local priority + local name + local append + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + name="${name:-$app}" + append=${append:-0} + key=${key:-0} + priority=${priority:-} + + if [ $append -eq 1 ] + then + append="--append" + wget_append="tee -a" + else + append="" + wget_append="tee" + fi + + # Split the repository into uri, suite and components. + # Remove "deb " at the beginning of the repo. + repo="${repo#deb }" + + # Get the uri + local uri="$(echo "$repo" | awk '{ print $1 }')" + + # Get the suite + local suite="$(echo "$repo" | awk '{ print $2 }')" + + # Get the components + local component="${repo##$uri $suite }" + + # Add the repository into sources.list.d + ynh_add_repo --uri="$uri" --suite="$suite" --component="$component" --name="$name" $append + + # Pin the new repo with the default priority, so it won't be used for upgrades. + # Build $pin from the uri without http and any sub path + local pin="${uri#*://}" + pin="${pin%%/*}" + # Set a priority only if asked + if [ -n "$priority" ] + then + priority="--priority=$priority" + fi + ynh_pin_repo --package="*" --pin="origin \"$pin\"" $priority --name="$name" $append + + # Get the public key for the repo + if [ -n "$key" ] + then + mkdir -p "/etc/apt/trusted.gpg.d" + wget -q "$key" -O - | gpg --dearmor | $wget_append /etc/apt/trusted.gpg.d/$name.gpg > /dev/null + fi + + # Update the list of package with the new repo + ynh_package_update +} + +# Remove an extra repository and the assiociated configuration. +# +# usage: ynh_remove_extra_repo [--name=name] +# | arg: -n, --name - Name for the files for this repo, $app as default value. +ynh_remove_extra_repo () { + # Declare an array to define the options of this helper. + local legacy_args=n + declare -Ar args_array=( [n]=name= ) + local name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + name="${name:-$app}" + + ynh_secure_remove "/etc/apt/sources.list.d/$name.list" + ynh_secure_remove "/etc/apt/preferences.d/$name" + ynh_secure_remove "/etc/apt/trusted.gpg.d/$name.gpg" + ynh_secure_remove "/etc/apt/trusted.gpg.d/$name.asc" + + # Update the list of package to exclude the old repo + ynh_package_update +} + +# Install packages from an extra repository properly. +# +# usage: ynh_install_extra_app_dependencies --repo="repo" --package="dep1 dep2" [--key=key_url] [--name=name] +# | arg: -r, --repo - Complete url of the extra repository. +# | arg: -p, --package - The packages to install from this extra repository +# | arg: -k, --key - url to get the public key. +# | arg: -n, --name - Name for the files for this repo, $app as default value. +ynh_install_extra_app_dependencies () { + # Declare an array to define the options of this helper. + local legacy_args=rpkn + declare -Ar args_array=( [r]=repo= [p]=package= [k]=key= [n]=name= ) + local repo + local package + local key + local name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + name="${name:-$app}" + key=${key:-0} + + # Set a key only if asked + if [ -n "$key" ] + then + key="--key=$key" + fi + # Add an extra repository for those packages + ynh_install_extra_repo --repo="$repo" $key --priority=995 --name=$name + + # Install requested dependencies from this extra repository. + ynh_add_app_dependencies --package="$package" + + # Remove this extra repository after packages are installed + ynh_remove_extra_repo --name=$app +} + +#================================================= + +# patched version of ynh_install_app_dependencies to be used with ynh_add_app_dependencies + +# Define and install dependencies with a equivs control file +# This helper can/should only be called once per app +# +# usage: ynh_install_app_dependencies dep [dep [...]] +# | arg: dep - the package name to install in dependence +# You can give a choice between some package with this syntax : "dep1|dep2" +# Example : ynh_install_app_dependencies dep1 dep2 "dep3|dep4|dep5" +# This mean in the dependence tree : dep1 & dep2 & (dep3 | dep4 | dep5) +# +# Requires YunoHost version 2.6.4 or higher. +ynh_install_app_dependencies () { + local dependencies=$@ + dependencies="$(echo "$dependencies" | sed 's/\([^\<=\>]\)\ \([^(]\)/\1, \2/g')" + dependencies=${dependencies//|/ | } + local manifest_path="../manifest.json" + if [ ! -e "$manifest_path" ]; then + manifest_path="../settings/manifest.json" # Into the restore script, the manifest is not at the same place + fi + + local version=$(grep '\"version\": ' "$manifest_path" | cut -d '"' -f 4) # Retrieve the version number in the manifest file. + if [ ${#version} -eq 0 ]; then + version="1.0" + fi + local dep_app=${app//_/-} # Replace all '_' by '-' + + # Handle specific versions + if [[ "$dependencies" =~ [\<=\>] ]] + then + # Replace version specifications by relationships syntax + # https://www.debian.org/doc/debian-policy/ch-relationships.html + # Sed clarification + # [^(\<=\>] ignore if it begins by ( or < = >. To not apply twice. + # [\<=\>] matches < = or > + # \+ matches one or more occurence of the previous characters, for >= or >>. + # [^,]\+ matches all characters except ',' + # Ex: package>=1.0 will be replaced by package (>= 1.0) + dependencies="$(echo "$dependencies" | sed 's/\([^(\<=\>]\)\([\<=\>]\+\)\([^,]\+\)/\1 (\2 \3)/g')" + fi + + cat > /tmp/${dep_app}-ynh-deps.control << EOF # Make a control file for equivs-build +Section: misc +Priority: optional +Package: ${dep_app}-ynh-deps +Version: ${version} +Depends: ${dependencies} +Architecture: all +Description: Fake package for $app (YunoHost app) dependencies + This meta-package is only responsible of installing its dependencies. +EOF + ynh_package_install_from_equivs /tmp/${dep_app}-ynh-deps.control \ + || ynh_die --message="Unable to install dependencies" # Install the fake package and its dependencies + rm /tmp/${dep_app}-ynh-deps.control + ynh_app_setting_set --app=$app --key=apt_dependencies --value="$dependencies" +} + +ynh_add_app_dependencies () { + # Declare an array to define the options of this helper. + local legacy_args=pr + declare -Ar args_array=( [p]=package= [r]=replace) + local package + local replace + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + replace=${replace:-0} + + local current_dependencies="" + if [ $replace -eq 0 ] + then + local dep_app=${app//_/-} # Replace all '_' by '-' + if ynh_package_is_installed --package="${dep_app}-ynh-deps" + then + current_dependencies="$(dpkg-query --show --showformat='${Depends}' ${dep_app}-ynh-deps) " + fi + + current_dependencies=${current_dependencies// | /|} + fi + + ynh_install_app_dependencies "${current_dependencies}${package}" +} diff --git a/scripts/ynh_systemd_action b/scripts/ynh_systemd_action new file mode 100644 index 0000000..6bed6be --- /dev/null +++ b/scripts/ynh_systemd_action @@ -0,0 +1,89 @@ +#!/bin/bash + +# Start (or other actions) a service, print a log in case of failure and optionnaly wait until the service is completely started +# +# usage: ynh_systemd_action [-n service_name] [-a action] [ [-l "line to match"] [-p log_path] [-t timeout] [-e length] ] +# | arg: -n, --service_name= - Name of the service to reload. Default : $app +# | arg: -a, --action= - Action to perform with systemctl. Default: start +# | arg: -l, --line_match= - Line to match - The line to find in the log to attest the service have finished to boot. +# If not defined it don't wait until the service is completely started. +# | arg: -p, --log_path= - Log file - Path to the log file. Default : /var/log/$app/$app.log +# | arg: -t, --timeout= - Timeout - The maximum time to wait before ending the watching. Default : 300 seconds. +# | arg: -e, --length= - Length of the error log : Default : 20 +ynh_systemd_action() { + # Declare an array to define the options of this helper. + declare -Ar args_array=( [n]=service_name= [a]=action= [l]=line_match= [p]=log_path= [t]=timeout= [e]=length= ) + local service_name + local action + local line_match + local length + local log_path + local timeout + + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + local service_name="${service_name:-$app}" + local action=${action:-start} + local log_path="${log_path:-/var/log/$service_name/$service_name.log}" + local length=${length:-20} + local timeout=${timeout:-300} + + # Start to read the log + if [[ -n "${line_match:-}" ]] + then + local templog="$(mktemp)" + # Following the starting of the app in its log + if [ "$log_path" == "systemd" ] ; then + # Read the systemd journal + journalctl -u $service_name -f --since=-45 > "$templog" & + else + # Read the specified log file + tail -F -n0 "$log_path" > "$templog" & + fi + # Get the PID of the tail command + local pid_tail=$! + fi + + echo "${action^} the service $service_name" >&2 + systemctl $action $service_name \ + || ( journalctl --lines=$length -u $service_name >&2 \ + ; test -n "$log_path" && echo "--" && tail --lines=$length "$log_path" >&2 \ + ; false ) + + # Start the timeout and try to find line_match + if [[ -n "${line_match:-}" ]] + then + local i=0 + for i in $(seq 1 $timeout) + do + # Read the log until the sentence is found, that means the app finished to start. Or run until the timeout + if grep --quiet "$line_match" "$templog" + then + echo "The service $service_name has correctly started." >&2 + break + fi + echo -n "." >&2 + sleep 1 + done + if [ $i -eq $timeout ] + then + echo "The service $service_name didn't fully started before the timeout." >&2 + journalctl --lines=$length -u $service_name >&2 + test -n "$log_path" && echo "--" && tail --lines=$length "$log_path" >&2 + fi + + echo "" + ynh_clean_check_starting + fi +} + +# Clean temporary process and file used by ynh_check_starting +# (usually used in ynh_clean_setup scripts) +# +# usage: ynh_clean_check_starting +ynh_clean_check_starting () { + # Stop the execution of tail. + kill -s 15 $pid_tail 2>&1 + ynh_secure_remove "$templog" 2>&1 +}