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

Standardization of restoration

This commit is contained in:
Jean-Baptiste Holcroft 2018-05-01 20:43:05 +02:00
parent c3f2f5c64f
commit 6be35a9f26

View file

@ -1,111 +1,124 @@
#!/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
# Install source.list debian package backports & yarn # Import debian archive pubkey, need on ARM arch
sudo cp ./apt_backports.list /etc/apt/sources.list.d/backports.list arch=$(uname -m)
sudo curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - if [[ "$arch" = arm* ]]; then
sudo cp ./apt_yarn.list /etc/apt/sources.list.d/yarn.list apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 8B48AD6246925553
ynh_package_update apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 7638D0442B90D010
fi
# Install debian package backports # Install source.list debian package backports & yarn
sudo apt-get -t jessie-backports -y install ffmpeg cp ../conf/backports.list /etc/apt/sources.list.d/
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
cp ../conf/yarn.list /etc/apt/sources.list.d/
ynh_package_update
# 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
sudo cp -a ./sources/. "$final_path"
# Set permissions
sudo chown -R $app: "$final_path"
# Debug
sudo ls -alh "$final_path"
# Restore PostgreSQL database # Restore PostgreSQL database
db_user=$(ynh_sanitize_dbid "$app") db_user=$(ynh_sanitize_dbid "$app")
@ -116,75 +129,34 @@ ynh_psql_test_if_first_run
ynh_psql_setup_db "$db_name" "$db_name" "$db_pwd" ynh_psql_setup_db "$db_name" "$db_name" "$db_pwd"
ynh_psql_execute_file_as_root ./db.sql "$db_name" ynh_psql_execute_file_as_root ./db.sql "$db_name"
# Install rbenv #=================================================
sudo su - $app <<COMMANDS # ADVERTISE SERVICE IN ADMIN PANEL
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 yunohost service add $app-web
ynh_psql_create_db_without_password "$app" yunohost service add $app-sidekiq
yunohost service add $app-streaming
# Setup database #=================================================
#sudo su - $app <<SCOMMANDS # RESTORE SYSTEMD
#cd ~/live #=================================================
#RAILS_ENV=production bin/bundle exec rails db:setup
#SCOMMANDS
# copy database dump ynh_restore_file "/etc/systemd/system/$app-web.service"
sudo cp $YNH_APP_BACKUP_DIR/mastodon_db.sql $final_path ynh_restore_file "/etc/systemd/system/$app-sidekiq.service"
sudo chmod a+r $final_path/mastodon_db.sql ynh_restore_file "/etc/systemd/system/$app-streaming.service"
systemctl enable "$app-web" "$app-sidekiq" "$app-streaming"
# Restore database dump #=================================================
sudo su - $app <<RECOMMANDS # RESTORE THE CRON FILE
dropdb mastodon_production #=================================================
createdb mastodon_production
psql mastodon_production < $final_path/mastodon_db.sql
RECOMMANDS
# Remove dump ynh_restore_file "/etc/cron.d/$app"
ynh_secure_remove $final_path/mastodon_db.sql
# Create symlink for ruby #=================================================
sudo ln -s /opt/mastodon/.rbenv/versions/2.4.1/bin/ruby /usr/bin/ruby || true # GENERIC FINALIZATION
#=================================================
# RELOAD NGINX AND PHP-FPM
#=================================================
# Upgrade Mastodon systemctl reload "$app-web" "$app-sidekiq" "$app-streaming"
sudo su - $app <<RCOMMANDS systemctl reload nginx
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