1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/glitchsoc_ynh.git synced 2024-09-03 19:15:59 +02:00

Merge pull request #76 from Jibec/master

Refactoring
This commit is contained in:
nemsia 2018-05-03 15:42:48 +02:00 committed by GitHub
commit 56e9231be4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 688 additions and 414 deletions

View file

@ -4,7 +4,7 @@
domain="domain.tld" (DOMAIN) domain="domain.tld" (DOMAIN)
admin="john" (USER) admin="john" (USER)
path="/path" (PATH) path="/path" (PATH)
passwd="():g9!co.'G{2+f/Wd\,e" passwd="12345678"
; Checks ; Checks
pkg_linter=1 pkg_linter=1
setup_sub_dir=0 setup_sub_dir=0

6
conf/app-mastodon.src Normal file
View file

@ -0,0 +1,6 @@
SOURCE_URL=https://github.com/tootsuite/mastodon/archive/v2.3.3.tar.gz
SOURCE_SUM=b2b2e2ee7cc034e92258263500c423b900611407db67682777eef0526118f66e
SOURCE_SUM_PRG=sha256sum
SOURCE_FORMAT=tar.gz
SOURCE_IN_SUBDIR=true
SOURCE_FILENAME=

6
conf/app-rbenv.src Normal file
View file

@ -0,0 +1,6 @@
SOURCE_URL=https://github.com/rbenv/rbenv/archive/v1.1.1.tar.gz
SOURCE_SUM=41f1a60714c55eceb21d692a469aee1ec4f46bba351d0dfcb0c660ff9cf1a1c9
SOURCE_SUM_PRG=sha256sum
SOURCE_FORMAT=tar.gz
SOURCE_IN_SUBDIR=true
SOURCE_FILENAME=

6
conf/app-ruby-build.src Normal file
View file

@ -0,0 +1,6 @@
SOURCE_URL=https://github.com/rbenv/ruby-build/archive/v20180424.tar.gz
SOURCE_SUM=71dbaf87081369c1f5d27b6a94a927c1eeeb1f36bdffe7851f0a9c1ec87b9373
SOURCE_SUM_PRG=sha256sum
SOURCE_FORMAT=tar.gz
SOURCE_IN_SUBDIR=true
SOURCE_FILENAME=

View file

@ -1,14 +1,14 @@
[Unit] [Unit]
Description=mastodon-sidekiq Description=__APP__-sidekiq
After=network.target After=network.target
[Service] [Service]
Type=simple Type=simple
User=mastodon User=__APP__
WorkingDirectory=/opt/mastodon/live WorkingDirectory=__FINALPATH__/live
Environment="RAILS_ENV=production" Environment="RAILS_ENV=production"
Environment="DB_POOL=20" Environment="DB_POOL=20"
ExecStart=/opt/mastodon/.rbenv/shims/bundle exec sidekiq -c 20 -q default -q mailers -q pull -q push ExecStart=__FINALPATH__/live/bin/bundle exec sidekiq -c 20 -q default -q mailers -q pull -q push
TimeoutSec=15 TimeoutSec=15
Restart=always Restart=always
StandardError=syslog StandardError=syslog

View file

@ -1,11 +1,11 @@
[Unit] [Unit]
Description=mastodon-streaming Description=__APP__-streaming
After=network.target After=network.target
[Service] [Service]
Type=simple Type=simple
User=mastodon User=__APP__
WorkingDirectory=/opt/mastodon/live WorkingDirectory=__FINALPATH__/live
Environment="NODE_ENV=production" Environment="NODE_ENV=production"
Environment="PORT=4000" Environment="PORT=4000"
ExecStart=/usr/bin/npm run start ExecStart=/usr/bin/npm run start

View file

@ -1,14 +1,14 @@
[Unit] [Unit]
Description=mastodon-web Description=__APP__-web
After=network.target After=network.target
[Service] [Service]
Type=simple Type=simple
User=mastodon User=__APP__
WorkingDirectory=/opt/mastodon/live WorkingDirectory=__FINALPATH__/live
Environment="RAILS_ENV=production" Environment="RAILS_ENV=production"
Environment="PORT=3000" Environment="PORT=3000"
ExecStart=/opt/mastodon/.rbenv/shims/bundle exec puma -C config/puma.rb ExecStart=__FINALPATH__/live/bin/bundle exec puma -C config/puma.rb
TimeoutSec=15 TimeoutSec=15
Restart=always Restart=always
StandardError=syslog StandardError=syslog

View file

@ -9,7 +9,7 @@
"en": "Mastodon is a free, open-source social network.", "en": "Mastodon is a free, open-source social network.",
"fr": "Mastodon est un réseau social gratuit et open source." "fr": "Mastodon est un réseau social gratuit et open source."
}, },
"version": "2.2.0-1", "version": "2.3.3-1",
"url": "https://github.com/tootsuite/mastodon", "url": "https://github.com/tootsuite/mastodon",
"license": "AGPL-3.0-or-later", "license": "AGPL-3.0-or-later",
"maintainer": { "maintainer": {

View file

@ -1,73 +1,175 @@
#!/bin/bash #!/bin/bash
# Create a db without password
#
# usage: ynh_mysql_create_user user
# | arg: user - the user name to create
ynh_psql_create_db_without_password() {
db=$1
sudo su -c "psql" postgres <<< \
"CREATE USER $db CREATEDB;"
}
# Create a user # Execute a command as another user
# # usage: exec_as USER COMMAND [ARG ...]
# usage: ynh_mysql_create_user user pwd [host] exec_as() {
# | arg: user - the user name to create local user=$1
# | arg: pwd - the password to identify user by shift 1
ynh_psql_create_user() {
sudo su -c "psql" postgres <<< \
"CREATE USER ${1} WITH PASSWORD '${2}';"
}
# Create a user without password if [[ $user = $(whoami) ]]; then
# eval "$@"
# usage: ynh_mysql_create_user user pwd [host]
# | arg: user - the user name to create
ynh_psql_create_user_without_password() {
sudo su -c "psql" postgres <<< \
"CREATE USER ${1};"
}
# Create a database and grant optionnaly privilegies to a user
#
# usage: ynh_mysql_create_db db [user [pwd]]
# | arg: db - the database name to create
# | arg: user - the user to grant privilegies
# | arg: pwd - the password to identify user by
ynh_psql_create_db() {
db=$1
# grant all privilegies to user
if [[ $# -gt 1 ]]; then
ynh_psql_create_user ${2} "${3}"
sudo su -c "createdb -O ${2} $db" postgres
else else
sudo su -c "createdb $db" postgres sudo --login --user="$user" "$@"
fi
}
#=================================================
#
# POSTGRES HELPERS
#
# Point of contact : Jean-Baptiste Holcroft <jean-baptiste@holcroft.fr>
#=================================================
# Create a master password and set up global settings
# Please always call this script in install and restore scripts
#
# usage: ynh_psql_test_if_first_run
ynh_psql_test_if_first_run() {
if [ -f /etc/yunohost/psql ];
then
echo "PostgreSQL is already installed, no need to create master password"
else
pgsql=$(ynh_string_random)
pg_hba=""
echo "$pgsql" >> /etc/yunohost/psql
if [ -e /etc/postgresql/9.4/ ]
then
pg_hba=/etc/postgresql/9.4/main/pg_hba.conf
elif [ -e /etc/postgresql/9.6/ ]
then
pg_hba=/etc/postgresql/9.6/main/pg_hba.conf
else
ynh_die "postgresql shoud be 9.4 or 9.6"
fi fi
systemctl start postgresql
sudo --login --user=postgres psql -c"ALTER user postgres WITH PASSWORD '$pgsql'" postgres
# force all user to connect to local database using passwords
# https://www.postgresql.org/docs/current/static/auth-pg-hba-conf.html#EXAMPLE-PG-HBA.CONF
# Note: we can't use peer since YunoHost create users with nologin
# See: https://github.com/YunoHost/yunohost/blob/unstable/data/helpers.d/user
sed -i '/local\s*all\s*all\s*peer/i \
local all all password' "$pg_hba"
systemctl enable postgresql
systemctl reload postgresql
fi
} }
# Drop a role # Open a connection as a user
# #
# usage: ynh_mysql_drop_role db # example: ynh_psql_connect_as 'user' 'pass' <<< "UPDATE ...;"
# | arg: db - the database name to drop # example: ynh_psql_connect_as 'user' 'pass' < /path/to/file.sql
ynh_psql_drop_role() { #
sudo su -c "psql" postgres <<< \ # usage: ynh_psql_connect_as user pwd [db]
"DROP ROLE ${1};" # | arg: user - the user name to connect as
# | arg: pwd - the user password
# | arg: db - the database to connect to
ynh_psql_connect_as() {
user="$1"
pwd="$2"
db="$3"
sudo --login --user=postgres PGUSER="$user" PGPASSWORD="$pwd" psql "$db"
}
# # Execute a command as root user
#
# usage: ynh_psql_execute_as_root sql [db]
# | arg: sql - the SQL command to execute
# | arg: db - the database to connect to
ynh_psql_execute_as_root () {
sql="$1"
sudo --login --user=postgres psql <<< "$sql"
}
# Execute a command from a file as root user
#
# usage: ynh_psql_execute_file_as_root file [db]
# | arg: file - the file containing SQL commands
# | arg: db - the database to connect to
ynh_psql_execute_file_as_root() {
file="$1"
db="$2"
sudo --login --user=postgres psql "$db" < "$file"
}
# Create a database, an user and its password. Then store the password in the app's config
#
# After executing this helper, the password of the created database will be available in $db_pwd
# It will also be stored as "psqlpwd" into the app settings.
#
# usage: ynh_psql_setup_db user name [pwd]
# | arg: user - Owner of the database
# | arg: name - Name of the database
# | arg: pwd - Password of the database. If not given, a password will be generated
ynh_psql_setup_db () {
db_user="$1"
app="$1"
db_name="$2"
new_db_pwd=$(ynh_string_random) # Generate a random password
# If $3 is not given, use new_db_pwd instead for db_pwd.
db_pwd="${3:-$new_db_pwd}"
ynh_psql_create_db "$db_name" "$db_user" "$db_pwd" # Create the database
ynh_app_setting_set "$app" psqlpwd "$db_pwd" # Store the password in the app's config
}
# Create a database and grant privilegies to a user
#
# usage: ynh_psql_create_db db [user [pwd]]
# | arg: db - the database name to create
# | arg: user - the user to grant privilegies
# | arg: pwd - the user password
ynh_psql_create_db() {
db="$1"
user="$2"
pwd="$3"
ynh_psql_create_user "$user" "$pwd"
sudo --login --user=postgres createdb --owner="$user" "$db"
} }
# Drop a database # Drop a database
# #
# usage: ynh_mysql_drop_db db # usage: ynh_psql_drop_db db
# | arg: db - the database name to drop # | arg: db - the database name to drop
ynh_psql_drop_db() { # | arg: user - the user to drop
sudo su -c "dropdb ${1}" postgres ynh_psql_remove_db() {
db="$1"
user="$2"
sudo --login --user=postgres dropdb "$db"
ynh_psql_drop_user "$user"
}
# Dump a database
#
# example: ynh_psql_dump_db 'roundcube' > ./dump.sql
#
# usage: ynh_psql_dump_db db
# | arg: db - the database name to dump
# | ret: the psqldump output
ynh_psql_dump_db() {
db="$1"
sudo --login --user=postgres pg_dump "$db"
}
# Create a user
#
# usage: ynh_psql_create_user user pwd [host]
# | arg: user - the user name to create
ynh_psql_create_user() {
user="$1"
pwd="$2"
sudo --login --user=postgres psql -c"CREATE USER $user WITH PASSWORD '$pwd'" postgres
} }
# Drop a user # Drop a user
# #
# usage: ynh_mysql_drop_user user # usage: ynh_psql_drop_user user
# | arg: user - the user name to drop # | arg: user - the user name to drop
ynh_psql_drop_user() { ynh_psql_drop_user() {
sudo su -c "dropuser ${1}" postgres user="$1"
sudo --login --user=postgres dropuser "$user"
} }

127
scripts/_future.sh Normal file
View file

@ -0,0 +1,127 @@
#!/bin/bash
# needed to have "service_name" as an option
# https://github.com/YunoHost/yunohost/commit/9c4ddcca39d9d6d92bd5f9a23978337e48d0a4e1
ynh_add_systemd_config () {
local service_name="${1:-$app}"
finalsystemdconf="/etc/systemd/system/$service_name.service"
ynh_backup_if_checksum_is_different "$finalsystemdconf"
sudo cp ../conf/${2:-systemd.service} "$finalsystemdconf"
# To avoid a break by set -u, use a void substitution ${var:-}. If the variable is not set, it's simply set with an empty variable.
# Substitute in a nginx config file only if the variable is not empty
if test -n "${final_path:-}"; then
ynh_replace_string "__FINALPATH__" "$final_path" "$finalsystemdconf"
fi
if test -n "${app:-}"; then
ynh_replace_string "__APP__" "$app" "$finalsystemdconf"
fi
ynh_store_file_checksum "$finalsystemdconf"
sudo chown root: "$finalsystemdconf"
sudo systemctl enable $service_name
sudo systemctl daemon-reload
}
# needed to have "service_name" as an option
# https://github.com/YunoHost/yunohost/commit/9c4ddcca39d9d6d92bd5f9a23978337e48d0a4e1
ynh_remove_systemd_config () {
local service_name="${1:-$app}"
local finalsystemdconf="/etc/systemd/system/$service_name.service"
if [ -e "$finalsystemdconf" ]; then
sudo systemctl stop $service_name
sudo systemctl disable $service_name
ynh_secure_remove "$finalsystemdconf"
sudo systemctl daemon-reload
fi
}
# LOCAL ADDITION:
# save file locally if not in the cache
#
# Download, check integrity, uncompress and patch the source from app.src
ynh_setup_source () {
local dest_dir=$1
local src_id=${2:-app} # If the argument is not given, source_id equals "app"
# Load value from configuration file (see above for a small doc about this file
# format)
local src_url=$(grep 'SOURCE_URL=' "$YNH_CWD/../conf/${src_id}.src" | cut -d= -f2-)
local src_sum=$(grep 'SOURCE_SUM=' "$YNH_CWD/../conf/${src_id}.src" | cut -d= -f2-)
local src_sumprg=$(grep 'SOURCE_SUM_PRG=' "$YNH_CWD/../conf/${src_id}.src" | cut -d= -f2-)
local src_format=$(grep 'SOURCE_FORMAT=' "$YNH_CWD/../conf/${src_id}.src" | cut -d= -f2-)
local src_in_subdir=$(grep 'SOURCE_IN_SUBDIR=' "$YNH_CWD/../conf/${src_id}.src" | cut -d= -f2-)
local src_filename=$(grep 'SOURCE_FILENAME=' "$YNH_CWD/../conf/${src_id}.src" | cut -d= -f2-)
# Default value
src_sumprg=${src_sumprg:-sha256sum}
src_in_subdir=${src_in_subdir:-true}
src_format=${src_format:-tar.gz}
src_format=$(echo "$src_format" | tr '[:upper:]' '[:lower:]')
if [ "$src_filename" = "" ] ; then
src_filename="${src_id}.${src_format}"
fi
local local_src="/var/cache/yunohost/ynh_setup_source/${YNH_APP_ID}/${src_filename}"
# if cache file exists and the checksum isn't good, download it again
# if not, just download the file
if test -e "$local_src"
then
echo "${src_sum} ${local_src}" | ${src_sumprg} -c --status \
|| wget -nv -O $local_src $src_url
else
mkdir -p "/var/cache/yunohost/ynh_setup_source/${YNH_APP_ID}"
wget -nv -O $local_src $src_url
fi
cp $local_src $src_filename
# Check the control sum
echo "${src_sum} ${src_filename}" | ${src_sumprg} -c --status \
|| ynh_die "Corrupt source"
# Extract source into the app dir
mkdir -p "$dest_dir"
if [ "$src_format" = "zip" ]
then
# Zip format
# Using of a temp directory, because unzip doesn't manage --strip-components
if $src_in_subdir ; then
local tmp_dir=$(mktemp -d)
unzip -quo $src_filename -d "$tmp_dir"
cp -a $tmp_dir/*/. "$dest_dir"
ynh_secure_remove "$tmp_dir"
else
unzip -quo $src_filename -d "$dest_dir"
fi
else
local strip=""
if $src_in_subdir ; then
strip="--strip-components 1"
fi
if [[ "$src_format" =~ ^tar.gz|tar.bz2|tar.xz$ ]] ; then
tar -xf $src_filename -C "$dest_dir" $strip
else
ynh_die "Archive format unrecognized."
fi
fi
# Apply patches
if (( $(find $YNH_CWD/../sources/patches/ -type f -name "${src_id}-*.patch" 2> /dev/null | wc -l) > "0" )); then
local old_dir=$(pwd)
(cd "$dest_dir" \
&& for p in $YNH_CWD/../sources/patches/${src_id}-*.patch; do \
patch -p1 < $p; done) \
|| ynh_die "Unable to apply patches"
cd $old_dir
fi
# Add supplementary files
if test -e "$YNH_CWD/../sources/extra_files/${src_id}"; then
cp -a $YNH_CWD/../sources/extra_files/$src_id/. "$dest_dir"
fi
}

View file

@ -1,47 +1,80 @@
#!/bin/bash #!/bin/bash
# Exit on command errors and treat unset variables as an error #=================================================
set -eu # GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
if [ ! -e _common.sh ]; then if [ ! -e _common.sh ]; then
# Get file fonction if not been to the current directory # Get the _common.sh file if it's not in the current directory
sudo cp ../settings/scripts/_common.sh ./_common.sh cp ../settings/scripts/_common.sh ./_common.sh
sudo chmod a+rx _common.sh cp ../settings/scripts/_future.sh ./_future.sh
chmod a+rx _common.sh _future.sh
fi fi
# Loads the generic functions usually used in the script
source _common.sh source _common.sh
# Source app helpers
source /usr/share/yunohost/helpers source /usr/share/yunohost/helpers
source _future.sh
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
# LOAD SETTINGS
#=================================================
# Get multi-instances specific variables # Get multi-instances specific variables
app=$YNH_APP_INSTANCE_NAME app=$YNH_APP_INSTANCE_NAME
# Retrieve app settings # Retrieve app settings
domain=$(ynh_app_setting_get "$app" domain) domain=$(ynh_app_setting_get "$app" domain)
final_path=$(ynh_app_setting_get "$app" final_path)
db_name=$(ynh_sanitize_dbid "$app")
# Copy the app files #=================================================
final_path="/opt/${app}" # STANDARD BACKUP STEPS
ynh_backup "$final_path" "sources" 1 #=================================================
# BACKUP THE APP MAIN DIR
#=================================================
# final_path on nginx ynh_backup "$final_path"
sudo sed -i "s@$final_path@__FINALPATH__@g" /etc/nginx/conf.d/${domain}.d/${app}.conf
#=================================================
# BACKUP THE NGINX CONFIGURATION
#=================================================
ynh_backup "/etc/nginx/conf.d/$domain.d/$app.conf"
#=================================================
# BACKUP THE MYSQL DATABASE
#=================================================
ynh_psql_dump_db "$db_name" > db.sql
#=================================================
# SPECIFIC BACKUP
#=================================================
# BACKUP SYSTEMD
#=================================================
ynh_backup "/etc/systemd/system/$app-web.service"
ynh_backup "/etc/systemd/system/$app-sidekiq.service"
ynh_backup "/etc/systemd/system/$app-streaming.service"
#=================================================
# BACKUP THE CRON FILE
#=================================================
# Copy the nginx conf files # Copy the nginx conf files
ynh_backup "/etc/nginx/conf.d/${domain}.d/${app}.conf" "nginx.conf" ynh_backup "/etc/cron.d/$app"
ynh_backup "/etc/cron.d/${app}" "cron.conf"
ynh_backup "/etc/systemd/system/mastodon-web.service" "systemd_web.service" #=================================================
ynh_backup "/etc/systemd/system/mastodon-sidekiq.service" "systemd_sidekiq.service" # BACKUP THE sources.list FILES
ynh_backup "/etc/systemd/system/mastodon-streaming.service" "systemd_streaming.service" #=================================================
ynh_backup "/etc/apt/sources.list.d/backports.list" "apt_backports.list" ynh_backup "/etc/apt/sources.list.d/backports.list" "apt_backports.list"
ynh_backup "/etc/apt/sources.list.d/yarn.list" "apt_yarn.list" ynh_backup "/etc/apt/sources.list.d/yarn.list" "apt_yarn.list"
# final_path on nginx
sudo sed -i "s@__FINALPATH__@$final_path@g" /etc/nginx/conf.d/${domain}.d/${app}.conf
# Backup db
sudo su - postgres <<COMMANDS
pg_dump --role=mastodon -U postgres --no-password mastodon_production > mastodon_db.sql
COMMANDS
ynh_backup "/var/lib/postgresql/${app}_db.sql" "${app}_db.sql"
# Fix backup fail on yunohost 2.6
#ynh_secure_remove /var/lib/postgresql/mastodon_db.sql

View file

@ -8,6 +8,7 @@
source _common.sh source _common.sh
source /usr/share/yunohost/helpers source /usr/share/yunohost/helpers
source _future.sh
#================================================= #=================================================
# MANAGE SCRIPT FAILURE # MANAGE SCRIPT FAILURE
@ -38,6 +39,7 @@ app=$YNH_APP_INSTANCE_NAME
final_path=/var/www/$app final_path=/var/www/$app
test ! -e "$final_path" || ynh_die "This path already contains a folder" test ! -e "$final_path" || ynh_die "This path already contains a folder"
# TODO: remove this test, don't as password anymore, generate it and send it by email to admin with: https://github.com/YunoHost-Apps/Experimental_helpers/tree/master/send_readme_to_admin
[[ ${#admin_pass} -gt 7 ]] || ynh_die "Password is too weak, must be longer than 7 characters" [[ ${#admin_pass} -gt 7 ]] || ynh_die "Password is too weak, must be longer than 7 characters"
# Normalize the url path syntax # Normalize the url path syntax
@ -72,7 +74,7 @@ ynh_app_setting_set $app path $path_url
# Import debian archive pubkey, need on ARM arch # Import debian archive pubkey, need on ARM arch
arch=$(uname -m) arch=$(uname -m)
if [[ $arch = arm* ]]; then if [[ "$arch" = arm* ]]; then
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 8B48AD6246925553 apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 8B48AD6246925553
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 7638D0442B90D010 apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 7638D0442B90D010
fi fi
@ -84,7 +86,7 @@ cp ../conf/yarn.list /etc/apt/sources.list.d/
ynh_package_update ynh_package_update
# Creates the destination directory and stores its location. # Creates the destination directory and stores its location.
ynh_app_setting_set $app final_path $final_path ynh_app_setting_set "$app" final_path "$final_path"
# Install de Node.js # Install de Node.js
# TODO: use https://github.com/YunoHost-Apps/Experimental_helpers/blob/master/ynh_install_nodejs/ynh_install_nodejs # TODO: use https://github.com/YunoHost-Apps/Experimental_helpers/blob/master/ynh_install_nodejs/ynh_install_nodejs
@ -101,7 +103,7 @@ ynh_install_app_dependencies \
`# redis ` \ `# redis ` \
redis-server redis-tools \ redis-server redis-tools \
`# postgresql ` \ `# postgresql ` \
postgresql postgresql-contrib postgresql-server-dev-9.4 \ postgresql \
`# Ruby ` \ `# Ruby ` \
autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm3 libgdbm-dev \ autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm3 libgdbm-dev \
`# ffmpeg from backports ` \ `# ffmpeg from backports ` \
@ -116,37 +118,30 @@ ynh_install_app_dependencies \
# TODO: use non-official https://github.com/YunoHost-Apps/Experimental_helpers/blob/master/postgres/postgres # TODO: use non-official https://github.com/YunoHost-Apps/Experimental_helpers/blob/master/postgres/postgres
# TODO: this commands doesn't looks like a requirement, you may fully remove it # TODO: this commands doesn't looks like a requirement, you may fully remove it
# Set UTF8 encoding by default # Set UTF8 encoding by default
su -c "psql" postgres <<< \
"update pg_database set datistemplate='false' where datname='template1';"
su -c "psql" postgres <<< \
"drop database template1;"
su -c "psql" postgres <<< \
"create database template1 encoding='UTF8' template template0;"
su -c "psql" postgres <<< \
"update pg_database set datistemplate='true' where datname='template1';"
# Create DB without password ynh_psql_test_if_first_run
ynh_psql_create_db_without_password "$app"
systemctl restart postgresql db_user=$(ynh_sanitize_dbid "$app")
db_name=$(ynh_sanitize_dbid "$app")
db_pwd=$(ynh_string_random)
ynh_app_setting_set $app db_pwd $db_pwd
ynh_psql_setup_db "$db_user" "$db_name" "$db_pwd"
#================================================= #=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE # DOWNLOAD, CHECK AND UNPACK SOURCE
#================================================= #=================================================
# TODO: dont su as $app, work root and set corrects rights at the end of install
# Download all sources rbenv, ruby and mastodon # Download all sources rbenv, ruby and mastodon
(
su $app ynh_setup_source "$final_path/.rbenv" "app-rbenv"
git clone https://github.com/rbenv/rbenv.git $final_path/.rbenv ynh_setup_source "$final_path/.rbenv/plugins/ruby-build" "app-ruby-build"
git clone https://github.com/rbenv/ruby-build.git $final_path/.rbenv/plugins/ruby-build ynh_setup_source "$final_path/live" "app-mastodon"
git clone https://github.com/tootsuite/mastodon.git $final_path/live
)
#================================================= #=================================================
# NGINX CONFIGURATION # NGINX CONFIGURATION
#================================================= #=================================================
# TODO: use official helper # TODO: use official helper ynh_add_nginx_config
# Modify Nginx configuration file and copy it to Nginx conf directory # Modify Nginx configuration file and copy it to Nginx conf directory
sed -i "s@__PATH__@$app@g" ../conf/nginx.conf* sed -i "s@__PATH__@$app@g" ../conf/nginx.conf*
sed -i "s@__FINALPATH__@$final_path@g" ../conf/nginx.conf* sed -i "s@__FINALPATH__@$final_path@g" ../conf/nginx.conf*
@ -156,60 +151,44 @@ cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/$app.conf
# CREATE DEDICATED USER # CREATE DEDICATED USER
#================================================= #=================================================
# TODO: use official helper # TODO: use official helper ynh_system_user_create
# TODO: AFAIK, no app should change should be in /opt don't use it
# Create user unix # Create user unix
adduser $app --home /opt/$app --gecos "First Last,RoomNumber,WorkPhone,HomePhone" --disabled-password --disabled-login adduser $app --home $final_path --gecos "First Last,RoomNumber,WorkPhone,HomePhone" --disabled-password
# Switch branch to tagged release chown -R "$app" "$final_path"
cd $final_path/live
version=$(curl -s https://api.github.com/repos/tootsuite/mastodon/releases/latest | grep tag_name | cut -d\" -f4)
(
su $app
cd ~/live
git checkout $version
)
# TODO: try to use ynh_install_ruby from https://github.com/YunoHost-Apps/Experimental_helpers
# Install de rbenv # Install de rbenv
( (
su $app cd $final_path/.rbenv
cd ~/.rbenv
src/configure && make -C src src/configure && make -C src
echo 'export PATH="/opt/mastodon/.rbenv/bin:/opt/mastodon/live/bin:$PATH"' >> ~/.profile
echo 'export PATH="/opt/mastodon/.rbenv/bin:/opt/mastodon/live/bin:$PATH"' >> ~/.bashrc echo "export PATH=\"$final_path/.rbenv/bin:$final_path/live/bin:\$PATH\"
echo 'eval "\$(rbenv init -)"' >> ~/.profile eval \"\$(rbenv init -)\"" > $final_path/.profile
echo "export PATH=\"$final_path/.rbenv/bin:$final_path/live/bin:\$PATH\"" > $final_path/.bashrc
) )
# Install ruby-build # Install ruby-build
# TODO: /opt/mastodon looks like /opt/$app which is WRONG.
( (
su $app exec_as "$app" $final_path/.rbenv/bin/rbenv install 2.5.0
/opt/mastodon/.rbenv/bin/rbenv install 2.5.0 exec_as "$app" $final_path/.rbenv/bin/rbenv global 2.5.0
/opt/mastodon/.rbenv/versions/2.5.0/bin/ruby -v exec_as "$app" $final_path/.rbenv/versions/2.5.0/bin/ruby -v
) )
# Create symlink for ruby # Create symlink for ruby
rm /usr/bin/ruby || true rm /usr/bin/ruby || true
ln -s /opt/mastodon/.rbenv/versions/2.5.0/bin/ruby /usr/bin/ruby || true ln -s $final_path/.rbenv/versions/2.5.0/bin/ruby /usr/bin/ruby || true
# Install Mastodon
# TODO: /opt/mastodon looks like /opt/$app which is WRONG.
(
su $app
cd ~/live
/opt/mastodon/.rbenv/versions/2.5.0/bin/gem install bundler
bin/bundle install --deployment --without development test
yarn install --production
)
# Adjust Mastodon config # Adjust Mastodon config
# TODO: use official helper: ynh_replace_string # TODO: use official helper ynh_replace_string
# TODO: save the config file in conf folder, to make replacement easier to read
# TODO: use ynh_string_random
cp -a $final_path/live/.env.production.sample $final_path/live/.env.production cp -a $final_path/live/.env.production.sample $final_path/live/.env.production
sed -i "s@REDIS_HOST=redis@REDIS_HOST=127.0.0.1@g" "${final_path}/live/.env.production" sed -i "s@REDIS_HOST=redis@REDIS_HOST=127.0.0.1@g" "${final_path}/live/.env.production"
sed -i "s@DB_HOST=db@DB_HOST=/var/run/postgresql@g" "${final_path}/live/.env.production" sed -i "s@DB_HOST=db@DB_HOST=/var/run/postgresql@g" "${final_path}/live/.env.production"
sed -i "s@DB_USER=postgres@DB_USER=${app}@g" "${final_path}/live/.env.production" sed -i "s@DB_USER=postgres@DB_USER=${db_user}@g" "${final_path}/live/.env.production"
sed -i "s@DB_NAME=postgres@DB_NAME=${app}_production@g" "${final_path}/live/.env.production" sed -i "s@DB_NAME=postgres@DB_NAME=${db_name}@g" "${final_path}/live/.env.production"
sed -i "s@DB_PASS=@DB_PASS=${db_pwd}@g" "${final_path}/live/.env.production"
sed -i "s@LOCAL_DOMAIN=example.com@LOCAL_DOMAIN=${domain}@g" "${final_path}/live/.env.production" sed -i "s@LOCAL_DOMAIN=example.com@LOCAL_DOMAIN=${domain}@g" "${final_path}/live/.env.production"
language="$(echo $language | head -c 2)" language="$(echo $language | head -c 2)"
@ -231,57 +210,46 @@ sed -i "s@#SMTP_AUTH_METHOD=plain@SMTP_AUTH_METHOD=none@g" "${final_path}/live/.
sed -i "s@#SMTP_OPENSSL_VERIFY_MODE=peer@SMTP_OPENSSL_VERIFY_MODE=none@g" "${final_path}/live/.env.production" sed -i "s@#SMTP_OPENSSL_VERIFY_MODE=peer@SMTP_OPENSSL_VERIFY_MODE=none@g" "${final_path}/live/.env.production"
# Preconfig CSS & JS # Preconfig CSS & JS
# Install Mastodon
( (
su $app cd "$final_path/live"
cd ~/live su mastodon <<INSTALL
$final_path/.rbenv/versions/2.5.0/bin/gem install bundler
$final_path/live/bin/bundle install -j$(getconf _NPROCESSORS_ONLN) --deployment --without development test --quiet
yarn install --production --no-progress --non-interactive --silent
echo "SAFETY_ASSURED=1">> .env.production echo "SAFETY_ASSURED=1">> .env.production
RAILS_ENV=production bin/bundle exec rails db:setup RAILS_ENV=production $final_path/live/bin/bundle exec rails db:migrate --quiet
RAILS_ENV=production bin/bundle exec rails --trace assets:precompile RAILS_ENV=production $final_path/live/bin/bundle exec rails assets:precompile --quiet
INSTALL
) )
# init rbenv & create bundle # TODO: use ynh_find_port to have generic port selection for RAILS
( ynh_add_systemd_config "$app-web" "mastodon-web.service"
su $app # TODO: use ynh_find_port to have generic port selection for NODES
. ~/.profile ynh_add_systemd_config "$app-sidekiq" "mastodon-sidekiq.service"
type rbenv ynh_add_systemd_config "$app-streaming" "mastodon-streaming.service"
)
# TODO: use official helper ynh_add_systemd_config systemctl start "$app-web.service" "$app-sidekiq.service" "$app-streaming.service"
cp ../conf/mastodon-web.service /etc/systemd/system/mastodon-web.service
chown root: /etc/systemd/system/mastodon-web.service
cp ../conf/mastodon-sidekiq.service /etc/systemd/system/mastodon-sidekiq.service
chown root: /etc/systemd/system/mastodon-sidekiq.service
cp ../conf/mastodon-streaming.service /etc/systemd/system/mastodon-streaming.service
chown root: /etc/systemd/system/mastodon-streaming.service
systemctl daemon-reload
systemctl enable /etc/systemd/system/mastodon-*.service
systemctl start mastodon-web.service mastodon-sidekiq.service mastodon-streaming.service
# debug
systemctl status mastodon-web.service mastodon-sidekiq.service mastodon-streaming.service
# Create user # Create user
( (
su $app cd "$final_path/live"
cd ~/live su mastodon <<CREATEUSER
RAILS_ENV=production bundle exec rails c RAILS_ENV=production bin/bundle exec rails c
account = Account.create!(username: '$admin_mastodon') account = Account.create!(username: '$admin_mastodon')
user = User.create!(email: '$admin_mastodon_mail', password: '$admin_pass', account: account) user = User.create!(email: '$admin_mastodon_mail', password: '$admin_pass', account: account)
) CREATEUSER
su mastodon <<SETADMIN
# Create administrator & confirm user
(
su $app
cd ~/live
RAILS_ENV=production bin/bundle exec rails mastodon:make_admin USERNAME=$admin_mastodon RAILS_ENV=production bin/bundle exec rails mastodon:make_admin USERNAME=$admin_mastodon
RAILS_ENV=production bin/bundle exec rails mastodon:confirm_email USER_EMAIL=$admin_mastodon_mail RAILS_ENV=production bin/bundle exec rails mastodon:confirm_email USER_EMAIL=$admin_mastodon_mail
SETADMIN
) )
# Install crontab # Install crontab
cp ../conf/crontab_mastodon /etc/cron.d/$app cp ../conf/crontab_mastodon /etc/cron.d/$app
sed -i "s@__APP__@$app@g" /etc/cron.d/$app sed -i "s@__APP__@$app@g" /etc/cron.d/$app
#================================================= #=================================================
# GENERIC FINALIZATION # GENERIC FINALIZATION
#================================================= #=================================================
@ -289,16 +257,16 @@ sed -i "s@__APP__@$app@g" /etc/cron.d/$app
#================================================= #=================================================
# TODO:Set permissions to app files # TODO:Set permissions to app files
chown -R "$app" "$final_path"
#================================================= #=================================================
# ADVERTISE SERVICE IN ADMIN PANEL # ADVERTISE SERVICE IN ADMIN PANEL
#================================================= #=================================================
# Add service YunoHost # Add service YunoHost
yunohost service add mastodon-web yunohost service add "$app-web"
yunohost service add mastodon-sidekiq yunohost service add "$app-sidekiq"
yunohost service add mastodon-streaming yunohost service add "$app-streaming"
#================================================= #=================================================
# SETUP SSOWAT # SETUP SSOWAT

View file

@ -1,103 +1,114 @@
#!/bin/bash #!/bin/bash
# Exit on command errors and treat unset variables as an error #=================================================
set -u # GENERIC START
#=================================================
if [ ! -e _common.sh ]; then # IMPORT GENERIC HELPERS
# Get file fonction if not been to the current directory #=================================================
sudo cp ../settings/scripts/_common.sh ./_common.sh
sudo chmod a+rx _common.sh
fi
source _common.sh # Loads the generic functions usually used in the script source _common.sh # Loads the generic functions usually used in the script
# Source app helpers # Source app helpers
source /usr/share/yunohost/helpers source /usr/share/yunohost/helpers
source _future.sh
#=================================================
# LOAD SETTINGS
#=================================================
# Get multi-instances specific variables
app=$YNH_APP_INSTANCE_NAME app=$YNH_APP_INSTANCE_NAME
# Retrieve app settings # Retrieve app settings
domain=$(ynh_app_setting_get "$app" domain) domain=$(ynh_app_setting_get "$app" domain)
db_name=$(ynh_app_setting_get "$app" db_name)
db_user=$(ynh_sanitize_dbid "$app")
db_name=$(ynh_sanitize_dbid "$app")
final_path=$(ynh_app_setting_get "$app" final_path)
# Stop mastodon-web #=================================================
if [ -e "/etc/systemd/system/mastodon-web.service" ]; then # STANDARD REMOVE
echo "Delete systemd script" #=================================================
sudo systemctl stop mastodon-web.service # STOP AND REMOVE SERVICE
ynh_secure_remove "/etc/systemd/system/mastodon-web.service" #=================================================
sudo systemctl disable mastodon-web.service
fi
# Stop mastodon-sidekiq ynh_remove_systemd_config "$app-web"
if [ -e "/etc/systemd/system/mastodon-sidekiq.service" ]; then ynh_remove_systemd_config "$app-sidekiq"
echo "Delete systemd script" ynh_remove_systemd_config "$app-streaming"
sudo systemctl stop mastodon-sidekiq.service
ynh_secure_remove "/etc/systemd/system/mastodon-sidekiq.service"
sudo systemctl disable mastodon-sidekiq.service
fi
# Stop mastodon-sidekiq #=================================================
if [ -e "/etc/systemd/system/mastodon-streaming.service" ]; then # REMOVE SERVICE FROM ADMIN PANEL
echo "Delete systemd script" #==============================================
sudo systemctl stop mastodon-streaming.service
ynh_secure_remove "/etc/systemd/system/mastodon-streaming.service"
sudo systemctl disable mastodon-streaming.service
fi
# Delete service on Yunohost monitoring if yunohost service status | grep -q "$app-web"
if sudo yunohost service status | grep -q mastodon-web
then then
echo "Remove mastodon-web service" echo "Remove $app-web service"
sudo yunohost service remove mastodon-web yunohost service remove "$app-web"
fi fi
# Delete service on Yunohost monitoring if yunohost service status | grep -q "$app-sidekiq"
if sudo yunohost service status | grep -q mastodon-sidekiq
then then
echo "Remove mastodon-sidekiq service" echo "Remove $app-sidekiq service"
sudo yunohost service remove mastodon-sidekiq yunohost service remove "$app-sidekiq"
fi fi
# Delete service on Yunohost monitoring if yunohost service status | grep -q "$app-streaming"
if sudo yunohost service status | grep -q mastodon-streaming
then then
echo "Remove mastodon-streaming service" echo "Remove $app-streaming service"
sudo yunohost service remove mastodon-streaming yunohost service remove "$app-streaming"
fi fi
#=================================================
# REMOVE DEPENDENCIES
#=================================================
# Remove metapackage and its dependencies
ynh_remove_app_dependencies
#=================================================
# REMOVE THE PostgreSQL DATABASE
#=================================================
# delete postgresql database & user # delete postgresql database & user
ynh_psql_drop_db "${app}_production" ynh_psql_remove_db "$db_name" "$db_user"
ynh_psql_drop_role "${app}"
# Remove Debian package #=================================================
sudo apt-get remove --purge -y yarn # REMOVE APP MAIN DIR
#sudo apt-get remove --purge -y imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev file curl git #=================================================
# Delete redis package
#sudo apt-get remove --purge -y redis-server redis-tools
# Delete postgresql package
#sudo apt-get remove --purge -y postgresql postgresql-contrib
# Delete Ruby package
#sudo apt-get remove --purge -y autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm3 libgdbm-dev
# Delete app directory and configurations # Remove the app directory securely
ynh_secure_remove /opt/$app ynh_secure_remove "$final_path"
[[ -n $domain ]] && sudo rm -f "/etc/nginx/conf.d/${domain}.d/${app}.conf"
# Delete nginx configuration #=================================================
REMOVE_NGINX_CONF # REMOVE NGINX CONFIGURATION
#=================================================
ynh_secure_remove "/etc/nginx/conf.d/${domain}.d/${app}.conf"
systemctl reload nginx
#=================================================
# SPECIFIC REMOVE
#=================================================
# REMOVE THE CRON FILE
#=================================================
# Delete cronlog # Delete cronlog
ynh_secure_remove /etc/cron.d/$app ynh_secure_remove /etc/cron.d/$app
# Delete source.list
#=================================================
# REMOVE source.list
#=================================================
ynh_secure_remove /etc/apt/sources.list.d/backports.list ynh_secure_remove /etc/apt/sources.list.d/backports.list
ynh_secure_remove /etc/apt/sources.list.d/yarn.list ynh_secure_remove /etc/apt/sources.list.d/yarn.list
# Delete ruby exec # Delete ruby exec
ynh_secure_remove /usr/bin/ruby ynh_secure_remove /usr/bin/ruby
# Remove user #=================================================
sudo userdel -f $app # GENERIC FINALIZATION
#=================================================
# REMOVE DEDICATED USER
#=================================================
# Reload services userdel -f $app
sudo systemctl reload nginx
echo -e "\e[0m" # Restore normal color

View file

@ -1,191 +1,162 @@
#!/bin/bash #!/bin/bash
# This restore script is adapted to Yunohost >=2.4
# Exit on command errors and treat unset variables as an error #=================================================
set -eu # GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
if [ ! -e _common.sh ]; then if [ ! -e _common.sh ]; then
# Get file fonction if not been to the current directory # Get the _common.sh file if it's not in the current directory
sudo cp ../settings/scripts/_common.sh ./_common.sh cp ../settings/scripts/_common.sh ./_common.sh
sudo chmod a+rx _common.sh cp ../settings/scripts/_future.sh ./_future.sh
chmod a+rx _common.sh _future.sh
fi fi
# Loads the generic functions usually used in the script
source _common.sh source _common.sh
# Source app helpers
source /usr/share/yunohost/helpers source /usr/share/yunohost/helpers
source _future.sh
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
# LOAD SETTINGS
#=================================================
# The parameter $app is the id of the app instance ex: ynhexample__2
app=$YNH_APP_INSTANCE_NAME app=$YNH_APP_INSTANCE_NAME
# Get old parameter of the app # Get old parameter of the app
domain=$(ynh_app_setting_get $app domain) domain=$(ynh_app_setting_get $app domain)
path=$(ynh_app_setting_get $app path) path_url=$(ynh_app_setting_get $app path)
is_public=$(ynh_app_setting_get $app is_public) is_public=$(ynh_app_setting_get $app is_public)
final_path=$(ynh_app_setting_get "$app" final_path)
# Check domain/path availability #=================================================
sudo yunohost app checkurl "${domain}${path}" -a "$app" \ # CHECK IF THE APP CAN BE RESTORED
|| ynh_die "Path not available: ${domain}${path}" #=================================================
# Check $final_path ynh_webpath_available $domain $path_url \
final_path="/opt/${app}" || ynh_die "Path not available: ${domain}${path_url}"
if [ -d $final_path ]; then test ! -d $final_path \
ynh_die "There is already a directory: $final_path" || ynh_die "There is already a directory: $final_path "
fi
# Check configuration files nginx #=================================================
nginx_conf="/etc/nginx/conf.d/${domain}.d/${app}.conf" # STANDARD RESTORATION STEPS
if [ -f $nginx_conf ]; then #=================================================
ynh_die "The NGINX configuration already exists at '${nginx_conf}'. # RESTORE THE NGINX CONFIGURATION
You should safely delete it before restoring this app." #=================================================
fi
# Check configuration files php-fpm
crontab_conf="/etc/cron.d/${app}"
if [ -f $crontab_conf ]; then
ynh_die "The CRONTAB configuration already exists at '${crontab_conf}'.
You should safely delete it before restoring this app."
fi
# Restore services ynh_restore_file "/etc/nginx/conf.d/$domain.d/$app.conf"
web_systemd="/etc/systemd/system/${app}-web.service"
if [ -f "${web_systemd}" ]; then #=================================================
ynh_die "The MASTODON WEB configuration already exists at '${web_systemd}'. # RESTORE THE APP MAIN DIR
You should safely delete it before restoring this app." #=================================================
fi
sidekiq_systemd="/etc/systemd/system/${app}-sidekiq.service" ynh_restore_file "$final_path"
if [ -f "${sidekiq_systemd}" ]; then
ynh_die "The MASTODON SIDEKIQ configuration already exists at '${sidekiq_systemd}'. #=================================================
You should safely delete it before restoring this app." # RECREATE THE DEDICATED USER
fi #=================================================
streaming_systemd="/etc/systemd/system/${app}-streaming.service"
if [ -f "${streaming_systemd}" ]; then
ynh_die "The MASTODON STREAMING configuration already exists at '${streaming_systemd}'.
You should safely delete it before restoring this app."
fi
# Create user unix # Create user unix
sudo adduser $app --home /opt/$app --gecos "First Last,RoomNumber,WorkPhone,HomePhone" --disabled-password --disabled-login adduser $app --home $final_path --gecos "First Last,RoomNumber,WorkPhone,HomePhone" --disabled-password
# Reinstall dependencies #=================================================
# Install debian package # RESTORE USER RIGHTS
ynh_package_install imagemagick libpq-dev libxml2-dev libxslt1-dev file curl apt-transport-https pkg-config libprotobuf-dev protobuf-compiler #=================================================
# Install redis package # Restore permissions on app files
ynh_package_install redis-server redis-tools chown -R $app: $final_path
# Install postgresql #=================================================
ynh_package_install postgresql postgresql-contrib # SPECIFIC RESTORATION
#=================================================
# REINSTALL DEPENDENCIES
#=================================================
# Install Ruby # TODO: add in a clean way backports and yarn
ynh_package_install autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm3 libgdbm-dev
# Import debian archive pubkey, need on ARM arch
arch=$(uname -m)
if [[ "$arch" = arm* ]]; then
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 8B48AD6246925553
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 7638D0442B90D010
fi
# Install source.list debian package backports & yarn # Install source.list debian package backports & yarn
sudo cp ./apt_backports.list /etc/apt/sources.list.d/backports.list cp ../conf/backports.list /etc/apt/sources.list.d/
sudo curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
sudo cp ./apt_yarn.list /etc/apt/sources.list.d/yarn.list cp ../conf/yarn.list /etc/apt/sources.list.d/
ynh_package_update ynh_package_update
# Install debian package backports
sudo apt-get -t jessie-backports -y install ffmpeg
# Install de Node.js # Install de Node.js
pushd /opt # TODO: use https://github.com/YunoHost-Apps/Experimental_helpers/blob/master/ynh_install_nodejs/ynh_install_nodejs
curl -sL https://deb.nodesource.com/setup_6.x | sudo bash - (
sudo apt-get -y install nodejs cd /opt
curl -sL https://deb.nodesource.com/setup_6.x | bash -
apt-get -y install nodejs
)
# Install Yarn # TODO: use the same mecanism with other files
ynh_package_install yarn ynh_install_app_dependencies \
`# debian packages ` \
imagemagick libpq-dev libxml2-dev libxslt1-dev file curl apt-transport-https pkg-config libprotobuf-dev protobuf-compiler libicu-dev libidn11-dev \
`# redis ` \
redis-server redis-tools \
`# postgresql ` \
postgresql \
`# Ruby ` \
autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm3 libgdbm-dev \
`# ffmpeg from backports ` \
ffmpeg \
`# Yarn ` \
yarn
# Return to home #=================================================
popd # RESTORE THE PostgreSQL DATABASE
#=================================================
# Restore sources & data # Restore PostgreSQL database
sudo cp -a ./sources/. "$final_path" db_user=$(ynh_sanitize_dbid "$app")
db_name=$(ynh_sanitize_dbid "$app")
db_pwd=$(ynh_app_setting_get "$app" db_pwd)
# Set permissions ynh_psql_test_if_first_run
sudo chown -R $app: "$final_path" ynh_psql_setup_db "$db_name" "$db_name" "$db_pwd"
ynh_psql_execute_file_as_root ./db.sql "$db_name"
# Debug #=================================================
sudo ls -alh "$final_path" # ADVERTISE SERVICE IN ADMIN PANEL
#=================================================
# Set UTF8 encoding by default yunohost service add $app-web
sudo su -c "psql" postgres <<< \ yunohost service add $app-sidekiq
"update pg_database set datistemplate='false' where datname='template1';" yunohost service add $app-streaming
sudo su -c "psql" postgres <<< \
"drop database template1;"
sudo su -c "psql" postgres <<< \
"create database template1 encoding='UTF8' template template0;"
sudo su -c "psql" postgres <<< \
"update pg_database set datistemplate='true' where datname='template1';"
# Install rbenv #=================================================
sudo su - $app <<COMMANDS # RESTORE SYSTEMD
pushd ~/.rbenv #=================================================
src/configure && make -C src
echo 'export PATH="/opt/mastodon/.rbenv/bin:/opt/mastodon/live/bin:$PATH"' >> ~/.profile
echo 'export PATH="/opt/mastodon/.rbenv/bin:/opt/mastodon/live/bin:$PATH"' >> ~/.bashrc
echo 'eval "\$(rbenv init -)"' >> ~/.profile
COMMANDS
# Create user for db postgresql ynh_restore_file "/etc/systemd/system/$app-web.service"
ynh_psql_create_db_without_password "$app" ynh_restore_file "/etc/systemd/system/$app-sidekiq.service"
ynh_restore_file "/etc/systemd/system/$app-streaming.service"
systemctl enable "$app-web" "$app-sidekiq" "$app-streaming"
# Setup database #=================================================
#sudo su - $app <<SCOMMANDS # RESTORE THE CRON FILE
#cd ~/live #=================================================
#RAILS_ENV=production bin/bundle exec rails db:setup
#SCOMMANDS
# copy database dump ynh_restore_file "/etc/cron.d/$app"
sudo cp $YNH_APP_BACKUP_DIR/mastodon_db.sql $final_path
sudo chmod a+r $final_path/mastodon_db.sql
# Restore database dump #=================================================
sudo su - $app <<RECOMMANDS # GENERIC FINALIZATION
dropdb mastodon_production #=================================================
createdb mastodon_production # RELOAD NGINX AND PHP-FPM
psql mastodon_production < $final_path/mastodon_db.sql #=================================================
RECOMMANDS
# Remove dump systemctl reload "$app-web" "$app-sidekiq" "$app-streaming"
ynh_secure_remove $final_path/mastodon_db.sql systemctl reload nginx
# Create symlink for ruby
sudo ln -s /opt/mastodon/.rbenv/versions/2.4.1/bin/ruby /usr/bin/ruby || true
# Upgrade Mastodon
sudo su - $app <<RCOMMANDS
cd ~/live
bin/bundle install
yarn install --pure-lockfile
#RAILS_ENV=production bin/bundle exec rails db:migrate
#RAILS_ENV=production bundle exec rails assets:clean
#RAILS_ENV=production bin/bundle exec rails assets:precompile
RCOMMANDS
# Modify Nginx configuration file and copy it to Nginx conf directory
sudo sed -i "s@__PATH__@$app@g" ./nginx.conf
sudo sed -i "s@__FINALPATH__@$final_path@g" ./nginx.conf
sudo cp -a ./nginx.conf "$nginx_conf"
# Restore crontab
sudo cp -a ./cron.conf "$crontab_conf"
sudo cp ./systemd_web.service /etc/systemd/system/mastodon-web.service
sudo chown root: /etc/systemd/system/mastodon-web.service
sudo cp ./systemd_sidekiq.service /etc/systemd/system/mastodon-sidekiq.service
sudo chown root: /etc/systemd/system/mastodon-sidekiq.service
sudo cp ./systemd_streaming.service /etc/systemd/system/mastodon-streaming.service
sudo chown root: /etc/systemd/system/mastodon-streaming.service
sudo systemctl daemon-reload
sudo systemctl enable /etc/systemd/system/mastodon-*.service
sudo systemctl start mastodon-web.service mastodon-sidekiq.service mastodon-streaming.service
# debug
sudo systemctl status mastodon-web.service mastodon-sidekiq.service mastodon-streaming.service
# Add service YunoHost
sudo yunohost service add mastodon-web
sudo yunohost service add mastodon-sidekiq
sudo yunohost service add mastodon-streaming
# Reload services
sudo systemctl reload nginx

View file

@ -1,13 +1,18 @@
#!/bin/bash #!/bin/bash
# Exit on command errors and treat unset variables as an error #=================================================
set -eu # GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
# Loads the generic functions usually used in the script
source _common.sh source _common.sh
# Source YunoHost helpers
source /usr/share/yunohost/helpers source /usr/share/yunohost/helpers
#=================================================
# LOAD SETTINGS
#=================================================
# See comments in install script # See comments in install script
app=$YNH_APP_INSTANCE_NAME app=$YNH_APP_INSTANCE_NAME
@ -16,8 +21,23 @@ domain=$(ynh_app_setting_get "$app" domain)
path=$(ynh_app_setting_get "$app" path) path=$(ynh_app_setting_get "$app" path)
admin=$(ynh_app_setting_get "$app" admin) admin=$(ynh_app_setting_get "$app" admin)
language=$(ynh_app_setting_get "$app" language) language=$(ynh_app_setting_get "$app" language)
final_path=$(ynh_app_setting_get "$app" final_path)
CHECK_PATH # Checks and corrects the syntax of the path. #=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
# If db_name doesn't exist, create it
if [ -z "$db_name" ]; then
db_name=$(ynh_sanitize_dbid "$app")
ynh_app_setting_set "$app" db_name "$db_name"
fi
# If final_path doesn't exist, create it
if [ -z "$final_path" ]; then
final_path=/var/www/$app
ynh_app_setting_set "$app" final_path "$final_path"
fi
# Check if admin is not null # Check if admin is not null
if [[ "$admin" = "" || "$language" = "" ]]; then if [[ "$admin" = "" || "$language" = "" ]]; then
@ -25,7 +45,31 @@ if [[ "$admin" = "" || "$language" = "" ]]; then
ynh_die ynh_die
fi fi
final_path=/opt/$app #=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
# Backup the current version of the app
ynh_backup_before_upgrade
ynh_clean_setup () {
# restore it if the upgrade fails
ynh_restore_upgradebackup
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
# CHECK THE PATH
#=================================================
# Normalize the URL path syntax
path_url=$(ynh_normalize_url_path $path_url)
#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# TODO TODO TODO
#=================================================
db_name=$app db_name=$app