aboutsummaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authormagikcypress <cyp@rouquin.me>2017-04-10 04:55:10 +0200
committermagikcypress <cyp@rouquin.me>2017-04-10 04:55:10 +0200
commit4dfe2759421883575d4a63d2879d58b93bc56f6a (patch)
tree89c16c4295f356fe0c8b7b88f2b8f4f87b62d4db /scripts
parentd04d692b8adee05afaa664067d97727a54f3768c (diff)
downloadmastodon_ynh-4dfe2759421883575d4a63d2879d58b93bc56f6a.tar.gz
mastodon_ynh-4dfe2759421883575d4a63d2879d58b93bc56f6a.tar.bz2
mastodon_ynh-4dfe2759421883575d4a63d2879d58b93bc56f6a.zip
[fix] install + add file remove/restore/upgrade
Diffstat (limited to 'scripts')
-rw-r--r--scripts/.fonctions18
-rw-r--r--scripts/backup30
-rw-r--r--scripts/install41
-rw-r--r--scripts/remove59
-rw-r--r--scripts/restore83
-rw-r--r--scripts/upgrade54
6 files changed, 223 insertions, 62 deletions
diff --git a/scripts/.fonctions b/scripts/.fonctions
index 4557512..7d9c54d 100644
--- a/scripts/.fonctions
+++ b/scripts/.fonctions
@@ -135,18 +135,16 @@ SECURE_REMOVE () { # Deleting a folder with variable verification
fi
}
-REMOVE_BDD () { # Delete database and users
- # $1 = Database name
- # Uses '$app' as user name and database
- db_user=$1
- if mysqlshow -u root -p$(sudo cat $MYSQL_ROOT_PWD_FILE) | grep -q "^| $db_user"; then
- echo "Delete db"
- ynh_mysql_drop_db $db_user
- ynh_mysql_drop_user $db_user
- fi
+# 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
#
# usage: ynh_mysql_create_user user pwd [host]
diff --git a/scripts/backup b/scripts/backup
new file mode 100644
index 0000000..2ef6b86
--- /dev/null
+++ b/scripts/backup
@@ -0,0 +1,30 @@
+#!/bin/bash
+
+# Exit on command errors and treat unset variables as an error
+set -eu
+
+# Get multi-instances specific variables
+app=$YNH_APP_INSTANCE_NAME
+
+# Source app helpers
+source /usr/share/yunohost/helpers
+
+# Retrieve app settings
+domain=$(ynh_app_setting_get "$app" domain)
+
+# Copy the app files
+final_path="/opt/${app}"
+ynh_backup "$final_path" "sources" 1
+
+# Copy the nginx conf files
+ynh_backup "/etc/nginx/conf.d/${domain}.d/${app}.conf" "nginx.conf"
+ynh_backup "/etc/cron.d/${app}.conf" "cron.conf"
+ynh_backup "/etc/systemd/system/mastodon-web.service" "systemd_web.service"
+ynh_backup "/etc/systemd/system/mastodon-sidekiq.service" "systemd_sidekiq.service"
+ynh_backup "/etc/systemd/system/mastodon-streaming.service" "systemd_streaming.service"
+
+# Backup db
+sudo su - postgres <<COMMANDS
+pg_dump mastodon_production > /home/backup/mastodon_db.sql
+COMMANDS
+ynh_backup "/home/backup/mastodon_db.sql" "mastodon_db.sql" \ No newline at end of file
diff --git a/scripts/install b/scripts/install
index 8e086a7..41d430e 100644
--- a/scripts/install
+++ b/scripts/install
@@ -9,7 +9,7 @@ source /usr/share/yunohost/helpers # Source app helpers
CLEAN_SETUP () {
# Clean installation residues that are not supported by the remove script.
# Clean hosts
- sudo sed -i '/#MASTODON/d' /etc/hosts
+ echo ""
}
TRAP_ON # Active trap to stop the script if an error is detected.
@@ -17,7 +17,6 @@ domain=$YNH_APP_ARG_DOMAIN
path=$YNH_APP_ARG_PATH
admin_mastodon=$YNH_APP_ARG_ADMIN
is_public=$YNH_APP_ARG_IS_PUBLIC
-#language=$YNH_APP_ARG_LANGUAGE
app=$YNH_APP_INSTANCE_NAME
@@ -66,18 +65,13 @@ curl -sL https://deb.nodesource.com/setup_4.x | bash -
sudo apt-get -y install nodejs
npm install -g yarn
-## Install postgresql database
-dbname=$app
-dbuser=$app
-# Generate random password
-dbpass=$(ynh_string_random)
-ynh_psql_create_db "$dbname" "$dbuser" "$dbpass"
+# Create DB without password
+ynh_psql_create_db_without_password "$app"
# Download all Ruby source
sudo git clone https://github.com/rbenv/rbenv.git $final_path/.rbenv
git clone https://github.com/rbenv/ruby-build.git $final_path/.rbenv/plugins/ruby-build
git clone https://github.com/tootsuite/mastodon.git $final_path/live
-git clone git://github.com/dcarley/rbenv-sudo.git $final_path/.rbenv/plugins/rbenv-sudo
sudo chown -R $app: "${final_path}"
# Install de rbenv
@@ -86,7 +80,6 @@ pushd ~/.rbenv
src/configure && make -C src
echo 'export PATH="/opt/mastodon/.rbenv/bin:/opt/mastodon/live/bin:$PATH"' >> ~/.bashrc
echo 'eval "\$(rbenv init -)"' >> ~/.bashrc
-echo "alias su='env PATH=\$PATH'" >> ~/.bashrc
COMMANDS
# Install ruby-build
@@ -109,11 +102,12 @@ MCOMMANDS
# Adjust Mastodon config
pushd $final_path/live/
sudo cp -a .env.production.sample .env.production
-sudo sed -i "s@REDIS_HOST=localhost@REDIS_HOST=127.0.0.1@g" "${final_path}/live/.env.production"
+sudo sed -i "s@REDIS_HOST=redis@REDIS_HOST=127.0.0.1@g" "${final_path}/live/.env.production"
sudo sed -i "s@DB_HOST=db@DB_HOST=/var/run/postgresql@g" "${final_path}/live/.env.production"
-sudo sed -i "s@DB_USER=mastodon@DB_USER=${dbuser}@g" "${final_path}/live/.env.production"
-sudo sed -i "s@DB_NAME=mastodon@DB_NAME=${dbuser}@g" "${final_path}/live/.env.production"
-sudo sed -i "s@LOCAL_DOMAIN=domainedevotreinstance.tld@LOCAL_DOMAIN=${domain}@g" "${final_path}/live/.env.production"
+sudo sed -i "s@DB_USER=postgres@DB_USER=${app}@g" "${final_path}/live/.env.production"
+sudo sed -i "s@DB_NAME=postgres@DB_NAME=${app}_production@g" "${final_path}/live/.env.production"
+# sudo sed -i "s@DB_PASS=@DB_PASS=${dbpass}@g" "${final_path}/live/.env.production"
+sudo sed -i "s@LOCAL_DOMAIN=example.com@LOCAL_DOMAIN=${domain}@g" "${final_path}/live/.env.production"
sudo sed -i "s@PAPERCLIP_SECRET=@PAPERCLIP_SECRET=$(head -n32 /dev/urandom | tr -dc -d 'A-Za-z0-9' | head -c32)@g" "${final_path}/live/.env.production"
sudo sed -i "s@SECRET_KEY_BASE=@SECRET_KEY_BASE=$(head -n32 /dev/urandom | tr -dc -d 'A-Za-z0-9' | head -c32)@g" "${final_path}/live/.env.production"
@@ -124,12 +118,17 @@ sudo sed -i 's,SMTP_FROM_ADDRESS=notifications@example.com,SMTP_FROM_ADDRESS='${
# Create database
# Preconfig CSS & JS
+# Create admin user
+# Create confirm email
sudo su - $app <<ENDCOMMANDS
pushd ~/live
RAILS_ENV=production bin/bundle exec rails db:setup
RAILS_ENV=production bin/bundle exec rails assets:precompile
ENDCOMMANDS
+# 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@$domain
+
# Add Services
pushd /var/cache/yunohost/from_file/scripts
@@ -141,10 +140,9 @@ sudo cp ../conf/mastodon-streaming.service /etc/systemd/system/mastodon-streamin
sudo chown root: /etc/systemd/system/mastodon-streaming.service
sudo systemctl enable /etc/systemd/system/mastodon-*.service
-sudo systemctl daemon-reload
-sudo systemctl start mastodon-web.service mastodon-sidekiq.service mastodon-streaming.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
+# sudo systemctl status mastodon-web.service mastodon-sidekiq.service mastodon-streaming.service
# Add service YunoHost
sudo yunohost service add mastodon-web
@@ -158,7 +156,7 @@ sudo sed -i "s@__FINALPATH__@$final_path@g" /etc/nginx/conf.d/$domain.d/$app.con
# Install crontab
sudo cp ../conf/crontab_mastodon /etc/cron.d/$app
-sudo sed -i "s@__AP__@$app@g" /etc/cron.d/$app
+sudo sed -i "s@__APP__@$app@g" /etc/cron.d/$app
# Private or not
if [ "$is_public" = "Yes" ];
@@ -177,9 +175,4 @@ fi
sudo yunohost app ssowatconf
# Reload Nginx and regenerate SSOwat conf
-sudo systemctl reload nginx || true
-# debug
-sudo systemctl status nginx
-
-# Nettoyer hosts
-sudo sed -i '/#MASTODON/d' /etc/hosts \ No newline at end of file
+sudo systemctl reload nginx \ No newline at end of file
diff --git a/scripts/remove b/scripts/remove
index b8f2d6b..bbb9b5b 100644
--- a/scripts/remove
+++ b/scripts/remove
@@ -13,31 +13,10 @@ app=$YNH_APP_INSTANCE_NAME
# Retrieve app settings
domain=$(ynh_app_setting_get "$app" domain)
-# Delete service on Yunohost monitoring
-if sudo yunohost service status | grep -q mastodon-web
-then
- echo "Remove mastodon-web service"
- sudo yunohost service remove mastodon-web
-fi
-
-# Delete service on Yunohost monitoring
-if sudo yunohost service status | grep -q mastodon-sidekiq
-then
- echo "Remove mastodon-sidekiq service"
- sudo yunohost service remove mastodon-sidekiq
-fi
-
-# Delete service on Yunohost monitoring
-if sudo yunohost service status | grep -q mastodon-streaming
-then
- echo "Remove mastodon-streaming service"
- sudo yunohost service remove mastodon-streaming
-fi
-
# Stop mastodon-web
if [ -e "/etc/systemd/system/mastodon-web.service" ]; then
echo "Delete systemd script"
- sudo service mastodon-web.service stop
+ sudo systemctl stop mastodon-web.service
sudo rm "/etc/systemd/system/mastodon-web.service"
sudo systemctl disable mastodon-web.service
fi
@@ -45,7 +24,7 @@ fi
# Stop mastodon-sidekiq
if [ -e "/etc/systemd/system/mastodon-sidekiq.service" ]; then
echo "Delete systemd script"
- sudo service mastodon-sidekiq.service stop
+ sudo systemctl stop mastodon-sidekiq.service
sudo rm "/etc/systemd/system/mastodon-sidekiq.service"
sudo systemctl disable mastodon-sidekiq.service
fi
@@ -53,14 +32,34 @@ fi
# Stop mastodon-sidekiq
if [ -e "/etc/systemd/system/mastodon-streaming.service" ]; then
echo "Delete systemd script"
- sudo service mastodon-sidekiq.streaming stop
+ sudo systemctl stop mastodon-sidekiq.streaming
sudo rm "/etc/systemd/system/mastodon-streaming.service"
sudo systemctl disable mastodon-streaming.service
fi
+# Delete service on Yunohost monitoring
+if sudo yunohost service status | grep -q mastodon-web
+then
+ echo "Remove mastodon-web service"
+ sudo yunohost service remove mastodon-web
+fi
+
+# Delete service on Yunohost monitoring
+if sudo yunohost service status | grep -q mastodon-sidekiq
+then
+ echo "Remove mastodon-sidekiq service"
+ sudo yunohost service remove mastodon-sidekiq
+fi
+
+# Delete service on Yunohost monitoring
+if sudo yunohost service status | grep -q mastodon-streaming
+then
+ echo "Remove mastodon-streaming service"
+ sudo yunohost service remove mastodon-streaming
+fi
+
# delete postgresql database & user
-ynh_psql_drop_db $app
-ynh_psql_drop_user $app
+ynh_psql_drop_db "${app}_production"
# Remove Debian package
#sudo apt-get remove --purge -y imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev file curl git
@@ -75,9 +74,13 @@ ynh_psql_drop_user $app
SECURE_REMOVE '/opt/$app'
[[ -n $domain ]] && sudo rm -f "/etc/nginx/conf.d/${domain}.d/${app}.conf"
-REMOVE_NGINX_CONF # Suppression de la configuration nginx
+# Delete nginx configuration
+REMOVE_NGINX_CONF
-SECURE_REMOVE '/var/log/$app/' # Suppression des log
+# Delete log
+SECURE_REMOVE '/var/log/$app/'
+# Delete cronlog
+SECURE_REMOVE '/etc/cron.d/$app'
# Remove user
sudo userdel -f $app
diff --git a/scripts/restore b/scripts/restore
new file mode 100644
index 0000000..f1c80ee
--- /dev/null
+++ b/scripts/restore
@@ -0,0 +1,83 @@
+#!/bin/bash
+# This restore script is adapted to Yunohost >=2.4
+
+# Exit on command errors and treat unset variables as an error
+set -eu
+
+# The parameter $app is the id of the app instance ex: ynhexample__2
+app=$YNH_APP_INSTANCE_NAME
+
+# Source app helpers
+source /usr/share/yunohost/helpers
+
+# Get old parameter of the app
+domain=$(ynh_app_setting_get $app domain)
+path=$(ynh_app_setting_get $app path)
+is_public=$(ynh_app_setting_get $app is_public)
+
+# Check domain/path availability
+sudo yunohost app checkurl "${domain}${path}" -a "$app" \
+ || ynh_die "Path not available: ${domain}${path}"
+
+# Check $final_path
+final_path="/opt/${app}"
+if [ -d $final_path ]; then
+ ynh_die "There is already a directory: $final_path"
+fi
+
+# Check configuration files nginx
+nginx_conf="/etc/nginx/conf.d/${domain}.d/${app}.conf"
+if [ -f $nginx_conf ]; then
+ ynh_die "The NGINX configuration already exists at '${nginx_conf}'.
+ You should safely delete it before restoring this app."
+
+# 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
+web_systemd="/etc/systemd/system/${app}-web.service"
+if [ -f "${web_systemd}" ]; then
+ ynh_die "The MASTODON WEB configuration already exists at '${web_systemd}'.
+ You should safely delete it before restoring this app."
+fi
+sidekiq_systemd="/etc/systemd/system/${app}-sidekiq.service"
+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."
+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
+
+ # Restore sources & data
+sudo cp -a ./sources "$final_path"
+
+# Set permissions
+sudo chown -R $app: "$final_path"
+
+# Restore db
+ynh_psql_create_db_without_password "$app"
+sudo su - postgres <<COMMANDS
+pg_dump mastodon_production < ./mastodon_db.sql
+COMMANDS
+
+# Restore Mastodon
+sudo su - $app <<RCOMMANDS
+cd ~/live
+RAILS_ENV=production bin/bundle exec rails db:migrate
+RAILS_ENV=production bin/bundle exec rails assets:precompile
+RCOMMANDS
+
+# Restore nginx configuration files
+sudo cp -a ./nginx.conf "$nginx_conf"
+# Restore crontab
+sudo cp -a ./cron.conf "$crontab_conf"
+
+# Reload services
+sudo systemctl reload nginx \ No newline at end of file
diff --git a/scripts/upgrade b/scripts/upgrade
new file mode 100644
index 0000000..0260ee1
--- /dev/null
+++ b/scripts/upgrade
@@ -0,0 +1,54 @@
+#!/bin/bash
+
+# Exit on command errors and treat unset variables as an error
+set -eu
+
+source .fonctions # Loads the generic functions usually used in the script
+source /usr/share/yunohost/helpers # Source YunoHost helpers
+
+# See comments in install script
+app=$YNH_APP_INSTANCE_NAME
+
+# Retrieve app settings
+domain=$(ynh_app_setting_get "$app" domain)
+path=$(ynh_app_setting_get "$app" path)
+admin=$(ynh_app_setting_get "$app" admin)
+is_public=$(ynh_app_setting_get "$app" is_public)
+
+CHECK_PATH # Checks and corrects the syntax of the path.
+
+# Check if admin is not null
+if [[ "$admin" = "" || "$is_public" = "" || "$language" = "" ]]; then
+ echo "Unable to upgrade, please contact support"
+ ynh_die
+fi
+
+final_path=/opt/$app
+
+db_name=$app
+
+# Modify Nginx configuration file and copy it to Nginx conf directory
+sudo sed -i "s@__PATH__@$app@g" ../conf/nginx.conf*
+sudo sed -i "s@__FINALPATH__@$final_path/@g" ../conf/nginx.conf*
+
+# Update Mastodon
+sudo su - $app <<COMMANDS
+pushd ~/live
+git pull
+RAILS_ENV=production bin/bundle exec rails db:migrate
+RAILS_ENV=production bin/bundle exec rails assets:precompile
+COMMANDS
+
+# If app is public, add url to SSOWat conf as skipped_uris
+if [ $is_public = "Yes" ];
+then
+ ynh_app_setting_set "$app" unprotected_uris "/"
+ sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/$app.conf
+else
+ sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/$app.conf
+fi
+
+# Reload Nginx
+sudo systemctl reload nginx
+# Reload Mastodon
+sudo systemctl restart mastodon-*.service \ No newline at end of file