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

Use latest PostgreSQL helpers

This commit is contained in:
Jean-Baptiste Holcroft 2018-05-01 11:29:25 +02:00
parent 1925b3026b
commit f0b1322812
5 changed files with 171 additions and 85 deletions

View file

@ -1,73 +1,161 @@
#!/bin/bash #!/bin/bash
# Create a db without password #=================================================
# #
# usage: ynh_mysql_create_user user # POSTGRES HELPERS
# | arg: user - the user name to create #
ynh_psql_create_db_without_password() { # Point of contact : Jean-Baptiste Holcroft <jean-baptiste@holcroft.fr>
db=$1 #=================================================
sudo su -c "psql" postgres <<< \
"CREATE USER $db CREATEDB;" # Create a master password and set up global settings
# Please always call this script in install and restore scripts
#
# usage: ynh_psql_test_if_first_run
ynh_psql_test_if_first_run() {
if [ -f /etc/yunohost/psql ];
then
echo "PostgreSQL is already installed, no need to create master password"
else
pgsql=$(ynh_string_random)
pg_hba=""
echo "$pgsql" >> /etc/yunohost/psql
if [ -e /etc/postgresql/9.4/ ]
then
pg_hba=/etc/postgresql/9.4/main/pg_hba.conf
elif [ -e /etc/postgresql/9.6/ ]
then
pg_hba=/etc/postgresql/9.6/main/pg_hba.conf
else
ynh_die "postgresql shoud be 9.4 or 9.6"
fi
systemctl start postgresql
sudo --login --user=postgres psql -c"ALTER user postgres WITH PASSWORD '$pgsql'" postgres
# force all user to connect to local database using passwords
# https://www.postgresql.org/docs/current/static/auth-pg-hba-conf.html#EXAMPLE-PG-HBA.CONF
# Note: we can't use peer since YunoHost create users with nologin
# See: https://github.com/YunoHost/yunohost/blob/unstable/data/helpers.d/user
sed -i '/local\s*all\s*all\s*peer/i \
local all all password' "$pg_hba"
systemctl enable postgresql
systemctl reload postgresql
fi
} }
# Create a user # Open a connection as a user
# #
# usage: ynh_mysql_create_user user pwd [host] # example: ynh_psql_connect_as 'user' 'pass' <<< "UPDATE ...;"
# | arg: user - the user name to create # example: ynh_psql_connect_as 'user' 'pass' < /path/to/file.sql
# | arg: pwd - the password to identify user by #
ynh_psql_create_user() { # usage: ynh_psql_connect_as user pwd [db]
sudo su -c "psql" postgres <<< \ # | arg: user - the user name to connect as
"CREATE USER ${1} WITH PASSWORD '${2}';" # | arg: pwd - the user password
# | arg: db - the database to connect to
ynh_psql_connect_as() {
user="$1"
pwd="$2"
db="$3"
sudo --login --user=postgres PGUSER="$user" PGPASSWORD="$pwd" psql "$db"
} }
# Create a user without password # # Execute a command as root user
# #
# usage: ynh_mysql_create_user user pwd [host] # usage: ynh_psql_execute_as_root sql [db]
# | arg: user - the user name to create # | arg: sql - the SQL command to execute
ynh_psql_create_user_without_password() { # | arg: db - the database to connect to
sudo su -c "psql" postgres <<< \ ynh_psql_execute_as_root () {
"CREATE USER ${1};" sql="$1"
sudo --login --user=postgres psql <<< "$sql"
} }
# Create a database and grant optionnaly privilegies to a user # Execute a command from a file as root user
# #
# usage: ynh_mysql_create_db db [user [pwd]] # usage: ynh_psql_execute_file_as_root file [db]
# | arg: file - the file containing SQL commands
# | arg: db - the database to connect to
ynh_psql_execute_file_as_root() {
file="$1"
db="$2"
sudo --login --user=postgres psql "$db" < "$file"
}
# Create a database, an user and its password. Then store the password in the app's config
#
# After executing this helper, the password of the created database will be available in $db_pwd
# It will also be stored as "psqlpwd" into the app settings.
#
# usage: ynh_psql_setup_db user name [pwd]
# | arg: user - Owner of the database
# | arg: name - Name of the database
# | arg: pwd - Password of the database. If not given, a password will be generated
ynh_psql_setup_db () {
db_user="$1"
app="$1"
db_name="$2"
new_db_pwd=$(ynh_string_random) # Generate a random password
# If $3 is not given, use new_db_pwd instead for db_pwd.
db_pwd="${3:-$new_db_pwd}"
ynh_psql_create_db "$db_name" "$db_user" "$db_pwd" # Create the database
ynh_app_setting_set "$app" psqlpwd "$db_pwd" # Store the password in the app's config
}
# Create a database and grant privilegies to a user
#
# usage: ynh_psql_create_db db [user [pwd]]
# | arg: db - the database name to create # | arg: db - the database name to create
# | arg: user - the user to grant privilegies # | arg: user - the user to grant privilegies
# | arg: pwd - the password to identify user by # | arg: pwd - the user password
ynh_psql_create_db() { ynh_psql_create_db() {
db=$1 db="$1"
# grant all privilegies to user user="$2"
if [[ $# -gt 1 ]]; then pwd="$3"
ynh_psql_create_user ${2} "${3}" ynh_psql_create_user "$user" "$pwd"
sudo su -c "createdb -O ${2} $db" postgres sudo --login --user=postgres createdb --owner="$user" "$db"
else
sudo su -c "createdb $db" postgres
fi
}
# Drop a role
#
# usage: ynh_mysql_drop_role db
# | arg: db - the database name to drop
ynh_psql_drop_role() {
sudo su -c "psql" postgres <<< \
"DROP ROLE ${1};"
} }
# Drop a database # Drop a database
# #
# usage: ynh_mysql_drop_db db # usage: ynh_psql_drop_db db
# | arg: db - the database name to drop # | arg: db - the database name to drop
ynh_psql_drop_db() { # | arg: user - the user to drop
sudo su -c "dropdb ${1}" postgres ynh_psql_remove_db() {
db="$1"
user="$2"
sudo --login --user=postgres dropdb "$db"
ynh_psql_drop_user "$user"
}
# Dump a database
#
# example: ynh_psql_dump_db 'roundcube' > ./dump.sql
#
# usage: ynh_psql_dump_db db
# | arg: db - the database name to dump
# | ret: the psqldump output
ynh_psql_dump_db() {
db="$1"
sudo --login --user=postgres pg_dump "$db"
}
# Create a user
#
# usage: ynh_psql_create_user user pwd [host]
# | arg: user - the user name to create
ynh_psql_create_user() {
user="$1"
pwd="$2"
sudo --login --user=postgres psql -c"CREATE USER $user WITH PASSWORD '$pwd'" postgres
} }
# Drop a user # Drop a user
# #
# usage: ynh_mysql_drop_user user # usage: ynh_psql_drop_user user
# | arg: user - the user name to drop # | arg: user - the user name to drop
ynh_psql_drop_user() { ynh_psql_drop_user() {
sudo su -c "dropuser ${1}" postgres user="$1"
sudo --login --user=postgres dropuser "$user"
} }

View file

@ -39,9 +39,8 @@ ynh_backup "/etc/apt/sources.list.d/yarn.list" "apt_yarn.list"
sudo sed -i "s@__FINALPATH__@$final_path@g" /etc/nginx/conf.d/${domain}.d/${app}.conf sudo sed -i "s@__FINALPATH__@$final_path@g" /etc/nginx/conf.d/${domain}.d/${app}.conf
# Backup db # Backup db
sudo su - postgres <<COMMANDS db_name=$(ynh_sanitize_dbid "$app")
pg_dump --role=mastodon -U postgres --no-password mastodon_production > mastodon_db.sql ynh_psql_dump_db "$db_name"
COMMANDS
ynh_backup "/var/lib/postgresql/${app}_db.sql" "${app}_db.sql"
# Fix backup fail on yunohost 2.6 # Fix backup fail on yunohost 2.6
#ynh_secure_remove /var/lib/postgresql/mastodon_db.sql #ynh_secure_remove /var/lib/postgresql/mastodon_db.sql

View file

@ -116,31 +116,16 @@ ynh_install_app_dependencies \
# TODO: use non-official https://github.com/YunoHost-Apps/Experimental_helpers/blob/master/postgres/postgres # TODO: use non-official https://github.com/YunoHost-Apps/Experimental_helpers/blob/master/postgres/postgres
# TODO: this commands doesn't looks like a requirement, you may fully remove it # TODO: this commands doesn't looks like a requirement, you may fully remove it
# Set UTF8 encoding by default # Set UTF8 encoding by default
su -c "psql" postgres <<< \
"update pg_database set datistemplate='false' where datname='template1';"
su -c "psql" postgres <<< \
"drop database template1;"
su -c "psql" postgres <<< \
"create database template1 encoding='UTF8' template template0;"
su -c "psql" postgres <<< \
"update pg_database set datistemplate='true' where datname='template1';"
# Create DB without password ynh_psql_test_if_first_run
ynh_psql_create_db_without_password "$app"
systemctl restart postgresql db_user=$(ynh_sanitize_dbid "$app")
db_name=$(ynh_sanitize_dbid "$app")
db_pwd=$(ynh_string_random)
ynh_app_setting_set $app db_pwd $db_pwd
ynh_psql_setup_db "$db_user" "$db_name" "$db_pwd"
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
# TODO: dont su as $app, work root and set corrects rights at the end of install
# Download all sources rbenv, ruby and mastodon
(
su $app
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
)
#================================================= #=================================================
# NGINX CONFIGURATION # NGINX CONFIGURATION
@ -161,6 +146,19 @@ cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/$app.conf
# Create user unix # Create user unix
adduser $app --home /opt/$app --gecos "First Last,RoomNumber,WorkPhone,HomePhone" --disabled-password --disabled-login adduser $app --home /opt/$app --gecos "First Last,RoomNumber,WorkPhone,HomePhone" --disabled-password --disabled-login
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
# TODO: dont su as $app, work root and set corrects rights at the end of install
# Download all sources rbenv, ruby and mastodon
(
su $app
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
)
# Switch branch to tagged release # Switch branch to tagged release
cd $final_path/live cd $final_path/live
version=$(curl -s https://api.github.com/repos/tootsuite/mastodon/releases/latest | grep tag_name | cut -d\" -f4) version=$(curl -s https://api.github.com/repos/tootsuite/mastodon/releases/latest | grep tag_name | cut -d\" -f4)
@ -208,8 +206,9 @@ ln -s /opt/mastodon/.rbenv/versions/2.5.0/bin/ruby /usr/bin/ruby || true
cp -a $final_path/live/.env.production.sample $final_path/live/.env.production cp -a $final_path/live/.env.production.sample $final_path/live/.env.production
sed -i "s@REDIS_HOST=redis@REDIS_HOST=127.0.0.1@g" "${final_path}/live/.env.production" sed -i "s@REDIS_HOST=redis@REDIS_HOST=127.0.0.1@g" "${final_path}/live/.env.production"
sed -i "s@DB_HOST=db@DB_HOST=/var/run/postgresql@g" "${final_path}/live/.env.production" sed -i "s@DB_HOST=db@DB_HOST=/var/run/postgresql@g" "${final_path}/live/.env.production"
sed -i "s@DB_USER=postgres@DB_USER=${app}@g" "${final_path}/live/.env.production" sed -i "s@DB_USER=postgres@DB_USER=${db_user}@g" "${final_path}/live/.env.production"
sed -i "s@DB_NAME=postgres@DB_NAME=${app}_production@g" "${final_path}/live/.env.production" sed -i "s@DB_NAME=postgres@DB_NAME=${db_name}@g" "${final_path}/live/.env.production"
sed -i "s@DB_PASS=@DB_PASS=${db_name}@g" "${final_path}/live/.env.production"
sed -i "s@LOCAL_DOMAIN=example.com@LOCAL_DOMAIN=${domain}@g" "${final_path}/live/.env.production" sed -i "s@LOCAL_DOMAIN=example.com@LOCAL_DOMAIN=${domain}@g" "${final_path}/live/.env.production"
language="$(echo $language | head -c 2)" language="$(echo $language | head -c 2)"

View file

@ -65,8 +65,9 @@ then
fi fi
# delete postgresql database & user # delete postgresql database & user
ynh_psql_drop_db "${app}_production" db_user=$(ynh_sanitize_dbid "$app")
ynh_psql_drop_role "${app}" db_name=$(ynh_sanitize_dbid "$app")
ynh_psql_remove_db "$db_name" "$db_user"
# Remove Debian package # Remove Debian package
sudo apt-get remove --purge -y yarn sudo apt-get remove --purge -y yarn

View file

@ -107,15 +107,14 @@ sudo chown -R $app: "$final_path"
# Debug # Debug
sudo ls -alh "$final_path" sudo ls -alh "$final_path"
# Set UTF8 encoding by default # Restore PostgreSQL database
sudo su -c "psql" postgres <<< \ db_user=$(ynh_sanitize_dbid "$app")
"update pg_database set datistemplate='false' where datname='template1';" db_name=$(ynh_sanitize_dbid "$app")
sudo su -c "psql" postgres <<< \ db_pwd=$(ynh_app_setting_get "$app" db_pwd)
"drop database template1;"
sudo su -c "psql" postgres <<< \ ynh_psql_test_if_first_run
"create database template1 encoding='UTF8' template template0;" ynh_psql_setup_db "$db_name" "$db_name" "$db_pwd"
sudo su -c "psql" postgres <<< \ ynh_psql_execute_file_as_root ./db.sql "$db_name"
"update pg_database set datistemplate='true' where datname='template1';"
# Install rbenv # Install rbenv
sudo su - $app <<COMMANDS sudo su - $app <<COMMANDS