1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/diaspora_ynh.git synced 2024-09-03 18:26:13 +02:00
diaspora_ynh/scripts/install

249 lines
9.8 KiB
Text
Raw Normal View History

2015-02-17 03:39:33 +01:00
#!/bin/bash
2019-12-02 21:01:24 +01:00
# TODO
# - which service to register to ynuhosto? diaspora.target only ? All of them ?
# - backup / restore
# - changeurl ? Is that possible ? or even a good idea ?
# - make an admin automatically
2019-12-16 11:11:02 +01:00
# - integration with ssowat? Or not? How exactly?
# - a setting to enable / disable registration
# - say something about the registration to https://the-federation.info/
2019-12-02 21:01:24 +01:00
2017-08-06 19:56:16 +02:00
#=================================================
# GENERIC START
#=================================================
2017-06-09 15:45:50 +02:00
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
2017-08-06 19:56:16 +02:00
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
2017-06-09 16:36:14 +02:00
2017-08-06 19:56:16 +02:00
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
# RETRIEVE ARGUMENTS FROM THE MANIFEST
#=================================================
2017-06-09 14:50:09 +02:00
2017-06-09 16:09:19 +02:00
domain=$YNH_APP_ARG_DOMAIN
2017-08-06 19:56:16 +02:00
path_url=$YNH_APP_ARG_PATH
admin=$YNH_APP_ARG_ADMIN
2019-12-16 11:11:02 +01:00
admin_password=$YNH_APP_ARG_ADMIN_PASSWORD
admin_email=$(ynh_user_get_info --username=$admin --key=mail)
2017-08-06 20:21:40 +02:00
2017-08-06 19:56:16 +02:00
# This is a multi-instance app, meaning it can be installed several times independently
# The id of the app as stated in the manifest is available as $YNH_APP_ID
# The instance number is available as $YNH_APP_INSTANCE_NUMBER (equals "1", "2", ...)
# The app instance name is available as $YNH_APP_INSTANCE_NAME
# - the first time the app is installed, YNH_APP_INSTANCE_NAME = ynhexample
# - the second time the app is installed, YNH_APP_INSTANCE_NAME = ynhexample__2
# - ynhexample__{N} for the subsequent installations, with N=3,4, ...
# The app instance name is probably what you are interested the most, since this is
# guaranteed to be unique. This is a good unique identifier to define installation path,
# db names, ...
app=$YNH_APP_INSTANCE_NAME
2017-06-09 13:06:41 +02:00
2017-08-06 19:56:16 +02:00
#=================================================
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
#=================================================
2019-11-03 23:23:00 +01:00
ynh_script_progression --message="Validating installation parameters..." --time --weight=1
2017-06-09 13:06:41 +02:00
2017-08-06 19:56:16 +02:00
# Normalize the url path syntax
path_url=$(ynh_normalize_url_path $path_url)
2015-02-17 03:39:33 +01:00
2017-08-06 19:56:16 +02:00
# Check web path availability
2019-11-03 23:23:00 +01:00
ynh_webpath_available --domain=$domain --path_url=$path_url
# check path availability
2017-08-06 19:56:16 +02:00
final_path=/var/www/$app
test ! -e "$final_path" || ynh_die "This path already contains a folder"
2019-11-03 23:23:00 +01:00
# Register (book) web path
ynh_webpath_register --app=$app --domain=$domain --path_url=$path_url
2015-02-17 03:39:33 +01:00
2017-08-06 19:56:16 +02:00
#=================================================
# STORE SETTINGS FROM MANIFEST
#=================================================
2019-11-04 16:11:13 +01:00
ynh_script_progression --message="Saving app settings..." --time --weight=1
2019-12-02 21:01:24 +01:00
ynh_app_setting_set --app=$app --key=domain --value=$domain
ynh_app_setting_set --app=$app --key=path --value=$path_url
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
2015-02-17 03:39:33 +01:00
2017-08-06 19:56:16 +02:00
#=================================================
# STANDARD MODIFICATIONS
#=================================================
#=================================================
# INSTALL DEPENDENCIES
#=================================================
2019-12-02 21:01:24 +01:00
ynh_script_progression --message="Installing dependencies..." --time --weight=27
ynh_install_app_dependencies $pkg_dependencies $ruby_build_dependencies
2015-02-17 03:39:33 +01:00
2017-08-06 19:56:16 +02:00
#=================================================
2019-11-03 23:23:00 +01:00
# CREATE A POSTGRESQL DATABASE
2017-08-06 19:56:16 +02:00
#=================================================
2019-11-04 16:11:13 +01:00
ynh_script_progression --message="Creating database..." --time --weight=1
2017-08-06 19:56:16 +02:00
db_name=$(ynh_sanitize_dbid $app)
2019-11-03 23:23:00 +01:00
ynh_app_setting_set --app=$app --key=db_name --value=$db_name
2019-11-04 16:11:13 +01:00
ynh_psql_test_if_first_run
2019-11-03 23:23:00 +01:00
ynh_psql_setup_db $db_name $db_name
2019-12-02 21:01:24 +01:00
db_pass=$(ynh_app_setting_get --app=$app --key=psqlpwd)
2017-10-06 10:49:33 +02:00
#=================================================
# CREATE DEDICATED USER
#=================================================
2019-12-02 21:01:24 +01:00
ynh_script_progression --message="Creating user..." --time --weight=1
2017-10-06 10:49:33 +02:00
# Create a system user
2019-12-02 21:01:24 +01:00
ynh_system_user_create --username=$app --home_dir=$final_path --use_shell
mkdir -p $final_path
chown $app:$app $final_path
2017-10-06 11:04:47 +02:00
2019-12-02 21:01:24 +01:00
# SWITCH TO NEW USER UNTIL EOF
2017-10-06 10:49:33 +02:00
2017-08-06 19:56:16 +02:00
#=================================================
2019-12-02 21:01:24 +01:00
# INSTALL RVM AND RUBY FOR CURRENT USER
2017-08-06 19:56:16 +02:00
#=================================================
2019-12-02 21:01:24 +01:00
ynh_script_progression --message="Installing rvm..." --time --weight=10
sudo -u $app gpg --import ../conf/piotr.kuczynski\@gmail.com.pgp ../conf/mpapis\@gmail.com.pgp
pushd $final_path
sudo -u $app curl -sSL https://get.rvm.io | sudo -u $app bash -s stable
ynh_script_progression --message="Installing ruby 2.4 (this can take a long time)..." --time --weight=230
sudo -u $app $final_path/.rvm/bin/rvm autolibs read-fail
sudo -u $app $final_path/.rvm/bin/rvm install 2.4
2017-06-09 17:13:14 +02:00
2017-08-06 19:56:16 +02:00
#=================================================
2019-12-02 21:01:24 +01:00
# DOWNLOAD, CHECK AND UNPACK SOURCE
2017-08-06 19:56:16 +02:00
#=================================================
2019-12-02 21:01:24 +01:00
# Download, check integrity, unucompress and patch the source from app.src
ynh_script_progression --message="Download the sources..." --time --weight=16
2020-04-19 15:26:58 +02:00
sudo -u $app git clone https://github.com/diaspora/diaspora.git -b v0.7.13.0
2019-12-02 21:01:24 +01:00
popd
2017-06-09 16:17:52 +02:00
2017-08-06 19:56:16 +02:00
#=================================================
2019-12-02 21:01:24 +01:00
# EXPORT VARIABLES FOR TEMPLATING
2017-08-06 19:56:16 +02:00
#=================================================
2019-12-02 21:01:24 +01:00
export app
export domain
export path_url
export db_pass
export final_path
export admin
2015-02-17 03:39:33 +01:00
2017-08-06 19:56:16 +02:00
#=================================================
2019-12-02 21:01:24 +01:00
# CONFIGURE DIASPORA
2017-08-06 19:56:16 +02:00
#=================================================
2019-12-02 21:01:24 +01:00
ynh_script_progression --message="Configure diaspora..." --time --weight=1
ynh_render_template ../conf/diaspora.yml $final_path/diaspora/config/diaspora.yml
ynh_render_template ../conf/database.yml $final_path/diaspora/config/database.yml
2015-05-30 23:06:45 +02:00
2017-08-06 19:56:16 +02:00
#=================================================
2019-12-02 21:01:24 +01:00
# Bundle the ruby app
2017-08-06 19:56:16 +02:00
#=================================================
2019-12-02 21:01:24 +01:00
pushd $final_path/diaspora
ynh_script_progression --message="bundle the app..." --time --weight=1000
# here we *absolutely* need bash (not dash) because dash does not understand what rvm puts in .profile
# (wtf rvm for assuming everybody uses bash as default shell??)
# we also need a login shell to make sure .profile is loaded
sudo -u $app /bin/bash --login << EOF
rvm use --default 2.4
2020-04-19 15:26:58 +02:00
rvm 2.4 do gem install bundler:1.17.3
2019-12-02 21:01:24 +01:00
script/configure_bundler
bin/bundle install --full-index --with=postgresql
EOF
ynh_script_progression --message="Create db schema..." --time --weight=22
sudo -u $app /bin/bash --login << EOF
RAILS_ENV=production bundle exec rake db:migrate
EOF
2019-12-02 21:01:24 +01:00
#=================================================
# ASSETS PRECOMPILATION
#=================================================
ynh_script_progression --message="Precompile assets..." --time --weight=400
sudo -u $app /bin/bash --login << EOF
RAILS_ENV=production bin/rake assets:precompile
EOF
popd
2015-05-30 23:06:45 +02:00
2019-12-02 21:01:24 +01:00
#=================================================
# NGINX CONFIGURATION
#=================================================
# TODO serve public/ ?
# Create a dedicated nginx config
ynh_script_progression --message="configure nginx..." --time --weight=1
ynh_add_nginx_config
2015-02-17 03:39:33 +01:00
2019-12-02 21:01:24 +01:00
#=================================================
# SETUP SYSTEMD
#=================================================
# Create a dedicated systemd config
# TODO add service in yunohost panel ?
ynh_script_progression --message="configure systemd unit..." --time --weight=1
ynh_render_template ../conf/diaspora_sidekiq.service /etc/systemd/system/${app}_sidekiq.service
ynh_render_template ../conf/diaspora_web.service /etc/systemd/system/${app}_web.service
ynh_render_template ../conf/diaspora.tmpfiles.d /etc/tmpfiles.d/${app}.conf
ynh_render_template ../conf/diaspora.target /etc/systemd/system/${app}.target
systemctl daemon-reload
systemd-tmpfiles --create
systemctl enable ${app}.target ${app}_sidekiq.service ${app}_web.service
systemctl restart ${app}.target
2015-02-17 03:39:33 +01:00
2017-08-06 19:56:16 +02:00
#=================================================
# STORE THE CHECKSUM OF THE CONFIG FILE
#=================================================
# Calculate and store the config file checksum into the app settings
2019-12-02 21:01:24 +01:00
ynh_store_file_checksum --file="$final_path/diaspora/config/diaspora.yml"
ynh_store_file_checksum --file="$final_path/diaspora/config/database.yml"
2015-05-31 11:35:04 +02:00
2017-08-06 19:56:16 +02:00
#=================================================
# GENERIC FINALIZATION
#=================================================
2015-02-17 03:39:33 +01:00
2017-08-06 19:56:16 +02:00
#=================================================
# SETUP LOGROTATE
#=================================================
# Use logrotate to manage application logfile(s)
ynh_use_logrotate
#=================================================
# ADVERTISE SERVICE IN ADMIN PANEL
#=================================================
yunohost service add postgresql --log /var/log/postgresql/postgresql-9.4-main.log --description "PostgreSQL RDBMS"
yunohost service add $app.target\
--log $final_path/diaspora/log/production.log \
$final_path/diaspora/log/unicorn-stderr.log\
$final_path/diaspora/log/unicorn-stdout.log\
$final_path/diaspora/log/sidekiq.log\
--description "Diaspora service (unicorn web and sidekiq)"
2015-02-17 03:39:33 +01:00
2017-08-06 19:56:16 +02:00
#=================================================
# SETUP SSOWAT
#=================================================
# unprotected_uris allows SSO credentials to be passed anyway.
ynh_app_setting_set $app unprotected_uris "/"
2017-08-06 19:56:16 +02:00
#=================================================
# RELOAD NGINX
#=================================================
2019-12-02 21:01:24 +01:00
ynh_script_progression --message="Reload nginx..." --time --weight=1
2017-08-06 19:56:16 +02:00
systemctl reload nginx
2019-12-16 11:11:02 +01:00
#=================================================
# CREATE AN ADMIN
#=================================================
ynh_script_progression --message="Create admin..." --time --weight=1
pushd $final_path/diaspora
sudo -u diaspora /bin/bash --login << EOF
RAILS_ENV=production bundle exec rails console << END
user = User.build({username: '$admin', email: '$admin_email', password: '$admin_password', password_confirmation: '$admin_password' })
user.seed_aspects
user.save
Role.add_admin user.person
END
2019-12-16 11:11:02 +01:00
EOF
popd