2017-04-10 04:55:10 +02:00
|
|
|
#!/bin/bash
|
|
|
|
# This restore script is adapted to Yunohost >=2.4
|
|
|
|
|
|
|
|
# Exit on command errors and treat unset variables as an error
|
|
|
|
set -eu
|
|
|
|
|
2018-04-06 19:58:23 +02:00
|
|
|
if [ ! -e _common.sh ]; then
|
2017-04-19 02:37:40 +02:00
|
|
|
# Get file fonction if not been to the current directory
|
2018-04-06 19:58:23 +02:00
|
|
|
sudo cp ../settings/scripts/_common.sh ./_common.sh
|
|
|
|
sudo chmod a+rx _common.sh
|
2017-04-19 02:37:40 +02:00
|
|
|
fi
|
|
|
|
# Loads the generic functions usually used in the script
|
2018-04-06 19:58:23 +02:00
|
|
|
source _common.sh
|
2017-04-10 04:55:10 +02:00
|
|
|
# Source app helpers
|
|
|
|
source /usr/share/yunohost/helpers
|
|
|
|
|
2017-04-19 02:37:40 +02:00
|
|
|
# The parameter $app is the id of the app instance ex: ynhexample__2
|
|
|
|
app=$YNH_APP_INSTANCE_NAME
|
|
|
|
|
2017-04-10 04:55:10 +02:00
|
|
|
# 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" \
|
2017-04-29 16:00:41 +02:00
|
|
|
|| ynh_die "Path not available: ${domain}${path}"
|
2017-04-10 04:55:10 +02:00
|
|
|
|
|
|
|
# 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
|
2017-05-26 21:20:07 +02:00
|
|
|
ynh_die "The NGINX configuration already exists at '${nginx_conf}'.
|
2017-04-10 04:55:10 +02:00
|
|
|
You should safely delete it before restoring this app."
|
2017-04-19 02:37:40 +02:00
|
|
|
fi
|
2017-04-10 04:55:10 +02:00
|
|
|
# Check configuration files php-fpm
|
|
|
|
crontab_conf="/etc/cron.d/${app}"
|
2017-05-26 21:20:07 +02:00
|
|
|
if [ -f $crontab_conf ]; then
|
|
|
|
ynh_die "The CRONTAB configuration already exists at '${crontab_conf}'.
|
2017-04-10 04:55:10 +02:00
|
|
|
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
|
2017-05-26 21:20:07 +02:00
|
|
|
ynh_die "The MASTODON WEB configuration already exists at '${web_systemd}'.
|
2017-04-10 04:55:10 +02:00
|
|
|
You should safely delete it before restoring this app."
|
|
|
|
fi
|
|
|
|
sidekiq_systemd="/etc/systemd/system/${app}-sidekiq.service"
|
|
|
|
if [ -f "${sidekiq_systemd}" ]; then
|
2017-05-26 21:20:07 +02:00
|
|
|
ynh_die "The MASTODON SIDEKIQ configuration already exists at '${sidekiq_systemd}'.
|
2017-04-10 04:55:10 +02:00
|
|
|
You should safely delete it before restoring this app."
|
|
|
|
fi
|
|
|
|
streaming_systemd="/etc/systemd/system/${app}-streaming.service"
|
|
|
|
if [ -f "${streaming_systemd}" ]; then
|
2017-05-26 21:20:07 +02:00
|
|
|
ynh_die "The MASTODON STREAMING configuration already exists at '${streaming_systemd}'.
|
2017-04-10 04:55:10 +02:00
|
|
|
You should safely delete it before restoring this app."
|
|
|
|
fi
|
|
|
|
|
2017-04-19 02:37:40 +02:00
|
|
|
# Create user unix
|
|
|
|
sudo adduser $app --home /opt/$app --gecos "First Last,RoomNumber,WorkPhone,HomePhone" --disabled-password --disabled-login
|
|
|
|
|
2017-05-06 22:30:44 +02:00
|
|
|
# Reinstall dependencies
|
|
|
|
# Install debian package
|
2017-05-25 00:07:57 +02:00
|
|
|
ynh_package_install imagemagick libpq-dev libxml2-dev libxslt1-dev file curl apt-transport-https pkg-config libprotobuf-dev protobuf-compiler
|
2017-05-06 22:30:44 +02:00
|
|
|
|
|
|
|
# Install redis package
|
|
|
|
ynh_package_install redis-server redis-tools
|
|
|
|
|
|
|
|
# Install postgresql
|
|
|
|
ynh_package_install postgresql postgresql-contrib
|
|
|
|
|
|
|
|
# Install Ruby
|
|
|
|
ynh_package_install autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm3 libgdbm-dev
|
|
|
|
|
|
|
|
# Install source.list debian package backports & yarn
|
2017-05-07 14:18:57 +02:00
|
|
|
sudo cp ./apt_backports.list /etc/apt/sources.list.d/backports.list
|
2017-05-06 22:30:44 +02:00
|
|
|
sudo curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
|
2017-05-07 14:18:57 +02:00
|
|
|
sudo cp ./apt_yarn.list /etc/apt/sources.list.d/yarn.list
|
2017-05-06 22:30:44 +02:00
|
|
|
ynh_package_update
|
|
|
|
|
|
|
|
# Install debian package backports
|
|
|
|
sudo apt-get -t jessie-backports -y install ffmpeg
|
|
|
|
|
|
|
|
# Install de Node.js
|
|
|
|
pushd /opt
|
2017-05-25 14:09:20 +02:00
|
|
|
curl -sL https://deb.nodesource.com/setup_6.x | sudo bash -
|
2017-05-06 22:30:44 +02:00
|
|
|
sudo apt-get -y install nodejs
|
|
|
|
|
|
|
|
# Install Yarn
|
|
|
|
ynh_package_install yarn
|
|
|
|
|
2017-05-06 22:48:26 +02:00
|
|
|
# Return to home
|
|
|
|
popd
|
2017-05-26 21:20:07 +02:00
|
|
|
|
2017-04-10 04:55:10 +02:00
|
|
|
# Restore sources & data
|
2017-04-29 16:00:41 +02:00
|
|
|
sudo cp -a ./sources/. "$final_path"
|
2017-04-10 04:55:10 +02:00
|
|
|
|
|
|
|
# Set permissions
|
|
|
|
sudo chown -R $app: "$final_path"
|
|
|
|
|
2017-04-30 23:49:00 +02:00
|
|
|
# Debug
|
2017-04-29 16:00:41 +02:00
|
|
|
sudo ls -alh "$final_path"
|
|
|
|
|
2018-05-01 11:29:25 +02:00
|
|
|
# Restore PostgreSQL database
|
|
|
|
db_user=$(ynh_sanitize_dbid "$app")
|
|
|
|
db_name=$(ynh_sanitize_dbid "$app")
|
|
|
|
db_pwd=$(ynh_app_setting_get "$app" db_pwd)
|
|
|
|
|
|
|
|
ynh_psql_test_if_first_run
|
|
|
|
ynh_psql_setup_db "$db_name" "$db_name" "$db_pwd"
|
|
|
|
ynh_psql_execute_file_as_root ./db.sql "$db_name"
|
2017-04-18 02:47:58 +02:00
|
|
|
|
2017-05-26 21:20:07 +02:00
|
|
|
# Install rbenv
|
|
|
|
sudo su - $app <<COMMANDS
|
|
|
|
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
|
|
|
|
|
2017-04-29 16:00:41 +02:00
|
|
|
# Create user for db postgresql
|
2017-04-10 04:55:10 +02:00
|
|
|
ynh_psql_create_db_without_password "$app"
|
|
|
|
|
2017-04-29 16:00:41 +02:00
|
|
|
# Setup database
|
2017-05-26 21:20:07 +02:00
|
|
|
#sudo su - $app <<SCOMMANDS
|
|
|
|
#cd ~/live
|
|
|
|
#RAILS_ENV=production bin/bundle exec rails db:setup
|
|
|
|
#SCOMMANDS
|
2017-04-10 04:55:10 +02:00
|
|
|
|
2017-05-01 21:26:46 +02:00
|
|
|
# copy database dump
|
|
|
|
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
|
|
|
|
dropdb mastodon_production
|
|
|
|
createdb mastodon_production
|
|
|
|
psql mastodon_production < $final_path/mastodon_db.sql
|
|
|
|
RECOMMANDS
|
|
|
|
|
|
|
|
# Remove dump
|
2017-05-06 19:37:08 +02:00
|
|
|
ynh_secure_remove $final_path/mastodon_db.sql
|
2017-04-29 16:00:41 +02:00
|
|
|
|
|
|
|
# Create symlink for ruby
|
|
|
|
sudo ln -s /opt/mastodon/.rbenv/versions/2.4.1/bin/ruby /usr/bin/ruby || true
|
|
|
|
|
2017-05-26 21:20:07 +02:00
|
|
|
# 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
|
|
|
|
|
2017-07-13 00:12:42 +02:00
|
|
|
# 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
|
2017-04-10 04:55:10 +02:00
|
|
|
sudo cp -a ./nginx.conf "$nginx_conf"
|
|
|
|
# Restore crontab
|
|
|
|
sudo cp -a ./cron.conf "$crontab_conf"
|
|
|
|
|
2017-04-29 16:00:41 +02:00
|
|
|
sudo cp ./systemd_web.service /etc/systemd/system/mastodon-web.service
|
2017-04-19 02:37:40 +02:00
|
|
|
sudo chown root: /etc/systemd/system/mastodon-web.service
|
2017-04-29 16:00:41 +02:00
|
|
|
sudo cp ./systemd_sidekiq.service /etc/systemd/system/mastodon-sidekiq.service
|
2017-04-19 02:37:40 +02:00
|
|
|
sudo chown root: /etc/systemd/system/mastodon-sidekiq.service
|
2017-04-29 16:00:41 +02:00
|
|
|
sudo cp ./systemd_streaming.service /etc/systemd/system/mastodon-streaming.service
|
2017-04-19 02:37:40 +02:00
|
|
|
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
|
|
|
|
|
2017-04-10 04:55:10 +02:00
|
|
|
# Reload services
|
2017-04-29 21:43:36 +02:00
|
|
|
sudo systemctl reload nginx
|