Skip to content

Commit

Permalink
Merge pull request #19 from YunoHost-Apps/testing
Browse files Browse the repository at this point in the history
  • Loading branch information
autra authored May 21, 2020
2 parents c4a8e6d + 9efa5b7 commit c27edb1
Show file tree
Hide file tree
Showing 12 changed files with 275 additions and 132 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
diaspora_ynh
==========

[![Integration level](https://dash.yunohost.org/integration/diaspora.svg)](https://dash.yunohost.org/appci/app/diaspora) ![](https://ci-apps.yunohost.org/ci/badges/diaspora.status.svg) ![](https://ci-apps.yunohost.org/ci/badges/diaspora.maintain.svg)
[![Integration level](https://dash.yunohost.org/integration/diaspora.svg)](https://dash.yunohost.org/appci/app/diaspora) ![](https://ci-apps.yunohost.org/ci/badges/diaspora.status.svg) ![](https://ci-apps.yunohost.org/ci/badges/diaspora.maintain.svg)[![Shipped version](https://img.shields.io/github/v/release/yunohost-apps/diaspora_ynh)](https://github.com/yunohost-apps/diaspora_ynh/releases)

[![Install diaspora with YunoHost](https://install-app.yunohost.org/install-with-yunohost.png)](https://install-app.yunohost.org/?app=diaspora)

## Overview
Expand All @@ -25,7 +26,4 @@ Installation effects:
- The installation directory can take up to 900MB and app start time can be take 5 minutes


**TODO**
* Add restore script

Report a bug: https://github.com/YunoHost-Apps/diaspora_ynh/issues
6 changes: 3 additions & 3 deletions check_process
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
setup_root=1
setup_nourl=0
upgrade=1
# upgrade=1 from_commit=CommitHash
backup_restore=0
upgrade=1 from_commit=0.7.13.0-ynh1
backup_restore=1
multi_instance=1
port_already_use=0
change_url=0
change_url=0 # not supported upstream
;;; Levels
# If the level 5 (Package linter) is forced to 1. Please add justifications here.
Level 5=auto
Expand Down
2 changes: 1 addition & 1 deletion conf/diaspora.target
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[Unit]
Description=Diaspora social network (instance {{ app}})
Description=Diaspora social network (instance __APP__)
Wants=redis.service postgresql.service
After=redis.service postgresql.service

Expand Down
2 changes: 1 addition & 1 deletion conf/diaspora.tmpfiles.d
Original file line number Diff line number Diff line change
@@ -1 +1 @@
d /run/{{ app }} 0755 {{ app }} {{ app }} - -
d /run/__APP__ 0755 __APP__ __APP__ - -
10 changes: 5 additions & 5 deletions conf/diaspora_sidekiq.service
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[Unit]
Description=Diaspora social network (sidekiq - instance {{ app }})
PartOf={{ app }}.target
Description=Diaspora social network (sidekiq - instance __APP__)
PartOf=__APP__.target

[Service]
User={{ app }}
User=__APP__
Environment=RAILS_ENV=production
WorkingDirectory={{ final_path }}/diaspora
WorkingDirectory=__FINALPATH__/diaspora
ExecStart=/bin/bash -lc "bin/bundle exec sidekiq"
Restart=always

[Install]
WantedBy={{ app }}.target
WantedBy=__APP__.target
12 changes: 6 additions & 6 deletions conf/diaspora_web.service
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
[Unit]
Description=Diaspora social network (unicorn - instance {{ app }})
PartOf={{ app }}.target
Description=Diaspora social network (unicorn - instance __APP__)
PartOf=__APP__.target

[Service]
User={{ app }}
User=__APP__
Environment=RAILS_ENV=production
WorkingDirectory={{ final_path }}/diaspora
PIDFile=/run/{{ app }}/diaspora.pid
WorkingDirectory=__FINALPATH__/diaspora
PIDFile=/run/__APP__/diaspora.pid
ExecStart=/bin/bash -lc "bin/bundle exec unicorn -c config/unicorn.rb -E production"
ExecReload=/bin/kill -USR2 $MAINPID
Restart=always

[Install]
WantedBy={{ app }}.target
WantedBy=__APP__.target
23 changes: 23 additions & 0 deletions scripts/bundle_app
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash
pushd $final_path/diaspora
# 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
rvm 2.4 do gem install bundler:1.17.3
script/configure_bundler
bin/bundle install --full-index --with=postgresql
EOF
sudo -u $app /bin/bash --login << EOF
RAILS_ENV=production bundle exec rake db:migrate
EOF

#=================================================
# ASSETS PRECOMPILATION
#=================================================
sudo -u $app /bin/bash --login << EOF
RAILS_ENV=production bin/rake assets:precompile
EOF
popd

40 changes: 40 additions & 0 deletions scripts/create_services
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash
# sidekiq
echo $app $final_path
install -T --mode=0644 -v ../conf/diaspora_sidekiq.service /etc/systemd/system/${app}_sidekiq.service
ynh_replace_string --match_string=__APP__ --replace_string=$app --target_file=/etc/systemd/system/${app}_sidekiq.service
ynh_replace_string --match_string=__FINALPATH__ --replace_string=$final_path --target_file=/etc/systemd/system/${app}_sidekiq.service
# web
install -T --mode=0644 -v ../conf/diaspora_web.service /etc/systemd/system/${app}_web.service
ynh_replace_string --match_string=__APP__ --replace_string=$app --target_file=/etc/systemd/system/${app}_web.service
ynh_replace_string --match_string=__FINALPATH__ --replace_string=$final_path --target_file=/etc/systemd/system/${app}_web.service
# tmp files
install -T --mode=0644 -v ../conf/diaspora.tmpfiles.d /etc/tmpfiles.d/${app}.conf
ynh_replace_string --match_string=__APP__ --replace_string=$app --target_file=/etc/tmpfiles.d/${app}.conf
# target unit
install -T --mode=0644 -v ../conf/diaspora.target /etc/systemd/system/${app}.target
ynh_replace_string --match_string=__APP__ --replace_string=$app --target_file=/etc/systemd/system/${app}.target
# reload, create, enable and start stuff
systemctl daemon-reload
systemd-tmpfiles --create
systemctl enable ${app}.target ${app}_sidekiq.service ${app}_web.service
systemctl restart ${app}.target

# wait for startup
timeout=100
for i in $(seq 1 $timeout)
do
# Read the log until the sentence is found, that means the app finished to start. Or run until the timeout
if grep --extended-regexp --quiet "listening on addr" "$final_path/diaspora/log/unicorn-stderr.log"
then
ynh_print_info --message="Diaspora* is up and running\!"
break
fi
if [ $i -eq 3 ]; then
echo -n "Please wait, diaspora* is starting" >&2
fi
if [ $i -ge 3 ]; then
echo -n "." >&2
fi
sleep 1
done
137 changes: 48 additions & 89 deletions scripts/install
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@

# TODO
# - which service to register to ynuhosto? diaspora.target only ? All of them ?
# - backup / restore
# - a setting to enable / disable registration
# - say something about the registration to https://the-federation.info/
# - ...

## vars for remove script
can_remove_db=0
can_remove_home=0
can_remove_user=0

# flag to make clean_setup display rvm log if rvm build has failed
should_display_rvm_log=0

#=================================================
# GENERIC START
Expand All @@ -18,8 +26,13 @@ source /usr/share/yunohost/helpers
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================

# Exit if an error occurs during the execution of the script
ynh_clean_setup() {
# print rvm logs
if [ $should_display_rvm_log -eq 1 ]; then
find $final_path/.rvm/log/ -name make.log -exec cat {} \;
fi
}
ynh_abort_if_errors

#=================================================
Expand All @@ -31,29 +44,18 @@ admin=$YNH_APP_ARG_ADMIN
admin_password=$YNH_APP_ARG_ADMIN_PASSWORD
admin_email=$(ynh_user_get_info --username=$admin --key=mail)


# 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
final_path=/var/www/$app

#=================================================
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
#=================================================
ynh_script_progression --message="Validating installation parameters..." --weight=1

# Check web path availability
ynh_webpath_available --domain=$domain --path_url=/
# check path availability
final_path=/var/www/$app
test ! -e "$final_path" || ynh_die "This path already contains a folder"
can_remove_home=1
# Register (book) web path
ynh_webpath_register --app=$app --domain=$domain --path_url=/

Expand All @@ -76,45 +78,50 @@ ynh_script_progression --message="Installing dependencies..." --weight=27
ynh_install_app_dependencies $pkg_dependencies $ruby_build_dependencies

#=================================================
# CREATE A POSTGRESQL DATABASE
# CHECK DB AVAILABILITY
#=================================================
ynh_script_progression --message="Creating database..." --weight=1
db_name=$(ynh_sanitize_dbid $app)
ynh_app_setting_set --app=$app --key=db_name --value=$db_name
ynh_psql_test_if_first_run
ynh_psql_setup_db $db_name $db_name
db_pass=$(ynh_app_setting_get --app=$app --key=psqlpwd)
ynh_script_progression --message="Check DB availability"
# now that we have psql for sure, test db existence
ynh_script_progression --message="Checking DB availability" --weight=1
ynh_psql_database_exists --database $app && ynh_die --message="There is already a database: $app"
can_remove_db=1

#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Creating user..." --weight=1
# Create a system user
ynh_system_user_create --username=$app --home_dir=$final_path --use_shell
can_remove_user=1
mkdir -p $final_path
chown $app:$app $final_path

# SWITCH TO NEW USER UNTIL EOF
chmod 0750 $final_path -R
chown $app:www-data $final_path

#=================================================
# INSTALL RVM AND RUBY FOR CURRENT USER
#=================================================
ynh_script_progression --message="Installing rvm..." --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)..." --weight=230
sudo -u $app $final_path/.rvm/bin/rvm autolibs read-fail
sudo -u $app $final_path/.rvm/bin/rvm install 2.4
ynh_script_progression --message="Installing rvm and ruby..." --weight=240
source ./install_rvm_ruby

#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
# Download, check integrity, unucompress and patch the source from app.src
pushd $final_path
ynh_script_progression --message="Download the sources..." --weight=16
sudo -u $app git clone https://github.com/diaspora/diaspora.git -b v0.7.13.0
popd

#=================================================
# CREATE A POSTGRESQL DATABASE
#=================================================
ynh_script_progression --message="Creating database..." --weight=1
db_name=$(ynh_sanitize_dbid $app)
ynh_app_setting_set --app=$app --key=db_name --value=$db_name
ynh_psql_test_if_first_run
ynh_psql_setup_db --db_user=$db_name --db_name=$db_name
db_pass=$(ynh_app_setting_get --app=$app --key=psqlpwd)

#=================================================
# EXPORT VARIABLES FOR TEMPLATING
#=================================================
Expand All @@ -132,32 +139,17 @@ ynh_render_template ../conf/diaspora.yml $final_path/diaspora/config/diaspora.ym
ynh_render_template ../conf/database.yml $final_path/diaspora/config/database.yml

#=================================================
# Bundle the ruby app
# STORE THE CHECKSUM OF THE CONFIG FILE
#=================================================
pushd $final_path/diaspora
ynh_script_progression --message="bundle the app..." --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
rvm 2.4 do gem install bundler:1.17.3
script/configure_bundler
bin/bundle install --full-index --with=postgresql
EOF
ynh_script_progression --message="Create db schema..." --weight=22
sudo -u $app /bin/bash --login << EOF
RAILS_ENV=production bundle exec rake db:migrate
EOF
# Calculate and store the config file checksum into the app settings
ynh_store_file_checksum --file="$final_path/diaspora/config/diaspora.yml"
ynh_store_file_checksum --file="$final_path/diaspora/config/database.yml"

#=================================================
# ASSETS PRECOMPILATION
# Bundle the ruby app
#=================================================
ynh_script_progression --message="Precompile assets..." --weight=400
sudo -u $app /bin/bash --login << EOF
RAILS_ENV=production bin/rake assets:precompile
EOF
popd
source ./bundle_app

#=================================================
# NGINX CONFIGURATION
Expand All @@ -170,40 +162,13 @@ ynh_add_nginx_config
# SETUP SYSTEMD
#=================================================
# Create a dedicated systemd config
# TODO add service in yunohost panel ?
ynh_script_progression --message="configure systemd unit..." --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

#=================================================
# STORE THE CHECKSUM OF THE CONFIG FILE
#=================================================
# Calculate and store the config file checksum into the app settings
ynh_store_file_checksum --file="$final_path/diaspora/config/diaspora.yml"
ynh_store_file_checksum --file="$final_path/diaspora/config/database.yml"

#=================================================
# GENERIC FINALIZATION
#=================================================

#=================================================
# SETUP LOGROTATE
#=================================================

# Use logrotate to manage application logfile(s)
ynh_use_logrotate
source ./create_services

#=================================================
# 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\
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\
Expand All @@ -216,12 +181,6 @@ yunohost service add $app.target\
# unprotected_uris allows SSO credentials to be passed anyway.
ynh_app_setting_set $app unprotected_uris "/"

#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reload nginx..." --weight=1
systemctl reload nginx

#=================================================
# CREATE AN ADMIN
#=================================================
Expand All @@ -230,8 +189,8 @@ pushd $final_path/diaspora
sudo -u $app /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
user.seed_aspects
Role.add_admin user.person
END
EOF
Expand Down
Loading

0 comments on commit c27edb1

Please sign in to comment.