1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/limesurvey_ynh.git synced 2024-09-03 19:36:32 +02:00

[enh] Add upgrade method

This commit is contained in:
ljf 2017-02-06 23:40:55 +01:00
parent b92d40f1c6
commit 96e0301040
6 changed files with 262 additions and 76 deletions

View file

@ -0,0 +1,6 @@
INSERT INTO `{{ prefix }}plugin_settings` (`plugin_id`, `model`, `model_id`, `key`, `value`) VALUES ( (SELECT id FROM `{{ prefix }}plugins` WHERE name='Authwebserver'),NULL,NULL,'is_default','\"1\"')
ON DUPLICATE KEY UPDATE value='\"1\"';
{% if is_public == "1" %}
UPDATE `{{ prefix }}plugin_settings` ps, `lime_plugins` p SET ps.value='\"0\"' WHERE p.name='Authwebserver' AND p.id=ps.plugin_id AND ps.`key`='is_default';
{% endif %}

View file

@ -0,0 +1,106 @@
From 7ca49326dafe4d9896e67aebe414edcd3ce8c1af Mon Sep 17 00:00:00 2001
From: Denis Chenu <denis@sondages.pro>
Date: Thu, 19 Jan 2017 00:02:45 +0100
Subject: [PATCH] [feature] Allow update via PHP cli
---
application/commands/UpdateCommand.php | 90 ++++++++++++++++++++++++++++++++++
1 files changed, 90 insertions(+)
create mode 100644 application/commands/UpdateCommand.php
diff --git a/application/commands/UpdateCommand.php b/application/commands/UpdateCommand.php
new file mode 100644
index 0000000..41ef107
--- /dev/null
+++ b/application/commands/UpdateCommand.php
@@ -0,0 +1,90 @@
+<?php
+ /*
+ * @author Denis Chenu <denis@sondages.pro>
+ * @license GPL v3
+ * @version 0.1
+ *
+ * Copyright (C) 2017 Denis Chenu
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ */
+class UpdateCommand extends CConsoleCommand
+{
+ public function run(){
+ $this->_setConfigs();
+ $newDbVersion = (float)Yii::app()->getConfig('dbversionnumber');
+ $currentDbVersion = (float)Yii::app()->getConfig('DBVersion');
+ if($newDbVersion > $currentDbVersion){
+ echo "Update ".Yii::app()->db->connectionString.", prefix :".Yii::app()->db->tablePrefix." from {$currentDbVersion} to {$newDbVersion}\n";
+ Yii::import('application.helpers.common_helper', true);
+ Yii::import('application.helpers.update.updatedb_helper', true);
+ $result=db_upgrade_all($currentDbVersion);/* @todo : fix bad echoing here */
+ if ($result) {
+ //printf(gT("Database has been successfully upgraded to version %s"),$dbversionnumber)."\n";
+ echo "Database has been successfully upgraded to version $newDbVersion \n";
+ } else {
+ //echo gT("Please fix this error in your database and try again")."\n";
+ echo "Please fix this error in your database and try again\n";
+ }
+ } else {
+ echo "no need update ".$newDbVersion ." ". $currentDbVersion ."\n";
+ }
+ }
+
+ /**
+ * Fonction to set all needed (and unneeded) config
+ * @return void
+ */
+ private function _setConfigs(){
+ /* default config */
+ $aDefaultConfigs = require(Yii::app()->basePath. DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'config-defaults.php');
+ foreach($aDefaultConfigs as $sConfig=>$defaultConfig){
+ Yii::app()->setConfig($sConfig,$defaultConfig);
+ }
+ /* Fix for badly set rootdir */
+ $sRootDir=realpath(Yii::app()->basePath. DIRECTORY_SEPARATOR . "..") ;
+ Yii::app()->setConfig('rootdir',$sRootDir);
+ Yii::app()->setConfig('publicdir',$sRootDir);
+ Yii::app()->setConfig('homedir',$sRootDir);
+ Yii::app()->setConfig('tempdir',$sRootDir.DIRECTORY_SEPARATOR."tmp");
+ Yii::app()->setConfig('imagedir',$sRootDir.DIRECTORY_SEPARATOR."images");
+ Yii::app()->setConfig('uploaddir',$sRootDir.DIRECTORY_SEPARATOR."upload");
+ Yii::app()->setConfig('standardtemplaterootdir',$sRootDir.DIRECTORY_SEPARATOR."templates");
+ Yii::app()->setConfig('usertemplaterootdir',$sRootDir.DIRECTORY_SEPARATOR."upload".DIRECTORY_SEPARATOR."templates");
+ Yii::app()->setConfig('styledir',$sRootDir.DIRECTORY_SEPARATOR."styledir");
+ /* version */
+ $aVersionConfigs = require(Yii::app()->basePath. DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'version.php');
+ foreach($aVersionConfigs as $sConfig=>$versionConfig){
+ Yii::app()->setConfig($sConfig,$versionConfig);
+ }
+ /* LS 3 version */
+ Yii::app()->setConfig('runtimedir',$sRootDir.DIRECTORY_SEPARATOR."tmp".DIRECTORY_SEPARATOR."runtime");
+ if(file_exists(Yii::app()->basePath. DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'config.php'))
+ {
+ $config = require(Yii::app()->basePath. DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'config.php');
+ if(is_array($config['config']) && !empty($config['config']))
+ {
+ foreach($config['config'] as $key=>$value)
+ Yii::app()->setConfig($key,$value);
+ }
+ }
+ $oSettings=SettingGlobal::model()->findAll();
+ if (count($oSettings) > 0)
+ {
+ foreach ($oSettings as $oSetting)
+ {
+ Yii::app()->setConfig($oSetting->getAttribute('stg_name'), $oSetting->getAttribute('stg_value'));
+ }
+ }
+ }
+
+}
+?>

View file

@ -15,7 +15,7 @@ ynh_exit_properly () {
trap '' EXIT trap '' EXIT
set +eu set +eu
echo -e "\e[91m \e[1m" echo -e "\e[91m \e[1m"
echo -e "!!\n $app install's script has encountered an error. Installation was cancelled.\n!!" >&2 err "$app script has encountered an error."
if type -t CLEAN_SETUP > /dev/null; then if type -t CLEAN_SETUP > /dev/null; then
CLEAN_SETUP CLEAN_SETUP
@ -185,8 +185,59 @@ ynh_setup_source () {
|| ynh_die "Unable to apply patches" || ynh_die "Unable to apply patches"
fi fi
# Apply persistent modules (upgrade only)
ynh_restore_persistent modules
# Apply persistent data (upgrade only)
ynh_restore_persistent data
} }
# TODO support SOURCE_ID
ynh_save_persistent () {
local TYPE=$1
local DIR=/tmp/ynh-persistent/$TYPE/$app/app
sudo mkdir -p $DIR
sudo touch $DIR/dir_names
shift
i=1
for PERSISTENT_DIR in $@;
do
if [ -e $local_path/$PERSISTENT_DIR ]; then
sudo mv $local_path/$PERSISTENT_DIR $DIR/$i
sudo su -c "echo -n '$PERSISTENT_DIR ' >> $DIR/dir_names"
((i++))
fi
done
}
# TODO support SOURCE_ID
ynh_restore_persistent () {
local TYPE=$1
local DIR=/tmp/ynh-persistent/$TYPE/$app/app
shift
if [ -d $DIR ]; then
i=1
for PERSISTENT_DIR in $(cat $DIR/dir_names);
do
if [ "$TYPE" = "modules" ]; then
for updated_subdir in $(ls $local_path/$PERSISTENT_DIR);
do
sudo rm -Rf $DIR/$i/$updated_subdir
done
fi
if [ -d $DIR/$i ]; then
sudo mv $DIR/$i/* $local_path/$PERSISTENT_DIR/ 2> /dev/null || true
else
sudo mv $DIR/$i $local_path/$PERSISTENT_DIR 2> /dev/null || true
fi
((i++))
done
sudo rm -Rf $DIR
fi
}
ynh_mv_to_home () { ynh_mv_to_home () {
local APP_PATH="/home/yunohost.app/$app/" local APP_PATH="/home/yunohost.app/$app/"
local DATA_PATH="$1" local DATA_PATH="$1"
@ -215,10 +266,46 @@ ynh_sso_access () {
sudo yunohost app ssowatconf sudo yunohost app ssowatconf
} }
ynh_read_manifest () { ynh_exit_if_up_to_date () {
python3 -c "import sys, json;print(json.load(open('../manifest.json'))['$1'])" if [ "${version}" = "${last_version}" ]; then
info "Up-to-date, nothing to do"
exit 0
fi
} }
function log() {
echo "${1}"
}
function info() {
log "[INFO] ${1}"
}
function warn() {
log "[WARN] ${1}"
}
function err() {
log "[ERR] ${1}"
}
function to_logs() {
# When yunohost --verbose or bash -x
if $_ISVERBOSE; then
cat
else
cat > /dev/null
fi
}
ynh_read_json () {
sudo python3 -c "import sys, json;print(json.load(open('$1'))['$2'])"
}
ynh_read_manifest () {
ynh_read_json '../manifest.json' "$1"
}
ynh_app_dependencies (){ ynh_app_dependencies (){
export dependencies=$1 export dependencies=$1

View file

@ -10,6 +10,7 @@ user=$app
# Retrieve arguments # Retrieve arguments
ynh_export domain path admin is_public language ynh_export domain path admin is_public language
export local_path=/var/www/$app export local_path=/var/www/$app
export prefix=lime_
#================================================= #=================================================
# CHECK IF THE APP CAN BE INSTALLED WITH THIS ARGS # CHECK IF THE APP CAN BE INSTALLED WITH THIS ARGS
@ -19,13 +20,12 @@ ynh_user_exists "$admin" || ynh_die "User does not exist: $admin"
ynh_normalize_url_path "$path" ynh_normalize_url_path "$path"
ynh_path_validity "$domain$path" ynh_path_validity "$domain$path"
ynh_local_path_available "$local_path" ynh_local_path_available "$local_path"
ynh_local_path_available "$data_path"
#================================================= #=================================================
# SETUP THE APP BY MODIFYING THE SYSTEM # SETUP THE APP BY MODIFYING THE SYSTEM
#================================================= #=================================================
ynh_save_args domain path admin is_public language local_path ynh_save_args domain path admin is_public language local_path prefix
ynh_app_dependencies php5-imap ynh_app_dependencies php5-imap
@ -38,16 +38,16 @@ ynh_setup_source "$local_path" "$user"
ynh_configure config.php "$local_path/application/config/config.php" ynh_configure config.php "$local_path/application/config/config.php"
# Fill LimeSurvey database # Fill LimeSurvey database
sed 's/`prefix_/`lime_/g' $local_path/installer/sql/create-mysql.sql > ./structure.sql sed "s/\`prefix_/\`$prefix/g" $local_path/installer/sql/create-mysql.sql > ./structure.sql
mysql -u $db_user -p$db_pwd $db_user < ./structure.sql mysql -u $db_user -p$db_pwd $db_user < ./structure.sql
ynh_configure data.sql ./data.sql ynh_configure data.sql ./data.sql
mysql -u $db_user -p$db_pwd $db_user < ./data.sql mysql -u $db_user -p$db_pwd $db_user < ./data.sql
# Set permissions # Set permissions
ynh_set_default_perm $local_path ynh_set_default_perm $local_path
sudo chmod u+w $local_path/tmp sudo chmod -R u+w $local_path/tmp
sudo chmod u+w $local_path/upload sudo chmod -R u+w $local_path/upload
sudo chmod u+w $local_path/application/config/ sudo chmod -R u+w $local_path/application/config/
sudo yunohost app addaccess $app -u $admin sudo yunohost app addaccess $app -u $admin
ynh_sso_access "/index.php?r=admin,/index.php?r=plugins,/scripts" ynh_sso_access "/index.php?r=admin,/index.php?r=plugins,/scripts"

0
scripts/restore Normal file
View file

View file

@ -1,81 +1,68 @@
#!/bin/bash #!/bin/bash
source /usr/share/yunohost/helpers
source _common.sh
app="limesurvey" ynh_trap_on
domain=$(sudo yunohost app setting $app domain) export app=$YNH_APP_INSTANCE_NAME
path=$(sudo yunohost app setting $app path) user=$app
admin=$(sudo yunohost app setting $app admin)
export domain=$(sudo yunohost app setting $app domain)
export path=$(sudo yunohost app setting $app path)
export admin=$(sudo yunohost app setting $app admin)
db_pwd=$(sudo yunohost app setting $app mysqlpwd) db_pwd=$(sudo yunohost app setting $app mysqlpwd)
export local_path=$(sudo yunohost app setting $app local_path)
export is_public=$(sudo yunohost app setting $app is_public || echo 0)
export prefix=$(sudo yunohost app setting $app prefix || echo 'prefix_')
version=$(ynh_read_json "/etc/yunohost/apps/$app/manifest.json" 'version' 2> /dev/null || echo '2.0.5')
last_version=$(ynh_read_manifest 'version')
db_user=$app ynh_exit_if_up_to_date
ynh_check_var "$app" "app name not set"
ynh_user_exists "$admin" || err "User does not exist: $admin"
ynh_normalize_url_path "$path"
# Remove trailing "/" for next commands if [ "${version}" = "2.0.5" ]; then
path=${path%/}
final_path=/var/www/$app ynh_save_args path is_public prefix
upload_path=/home/yunohost.app/$app/upload
sudo mkdir -p $final_path ynh_app_dependencies php5-imap
sudo mkdir -p $upload_path
ynh_system_user_create "$user" "$local_path"
# Move the upload dir to local_path if needed
# The upload dir can't be symlinked in home (not supported by LS)
upload_path=/home/yunohost.app/$app/upload
if [ -h $local_path/upload ]; then
sudo rm $local_path/upload
sudo mv $upload_path $local_path/
fi
sudo yunohost app setting $app skipped_uris -d
ynh_sso_access "/index.php?r=admin,/index.php?r=plugins,/scripts"
ynh_configure_php_fpm
ynh_configure_nginx
ynh_configure migrations/2.0.5.sql ./2.0.5.sql
mysql -u $app -p$db_pwd $app < ./2.0.5.sql
fi
# Backup config, plugins and themes and delete previous files # Backup config, plugins and themes and delete previous files
sudo mv $final_path/application/config/config.php $final_path/ ynh_save_persistent modules themes plugins
old_pwd=$(pwd) ynh_save_persistent data application/config/config.php upload
cd $final_path
sudo mkdir -p old_plugins
sudo mv plugins/* old_plugins \
|| echo "No app to backup"
sudo mkdir -p old_themes
sudo mv themes/* old_themes \
|| echo "No themes to backup"
if [ ! -h $final_path/upload ]; then # Copie new files and restore config, plugins, upload and themes
sudo mv $final_path/upload/* $upload_path/ \ sudo rm -Rf $local_path
|| echo "No upload to move" ynh_setup_source "$local_path" "$user"
fi
shopt -s extglob
sudo rm -Rf !(old_plugins|old_themes|config.php)
shopt -u extglob
# Copie new files and restore config, plugins and themes
cd $old_pwd
sudo cp -a ../sources/* $final_path
cd $final_path/old_plugins
sudo rm -Rf $(ls $final_path/plugins)
cd $final_path/old_themes
sudo rm -Rf $(ls $final_path/themes)
cd $old_pwd
sudo cp -a $final_path/old_plugins/* $final_path/plugins/ \
|| echo "No plugin to restore"
sudo cp -a $final_path/old_themes/* $final_path/themes/ \
|| echo "No theme to restore"
sudo mv $final_path/config.php $final_path/application/config/
# Reset Upload symbolic link
sudo rm -Rf $final_path/upload
sudo ln -s $upload_path ../sources/upload
# Create user
for user in $(sudo yunohost user list | grep '^ [^ ]*:' | sed -e "s/^ \([^ ]*\):/\1/g")
do
if [ $user != $admin ];
then
mysql -u $db_user -p$db_pwd $db_user -e "INSERT IGNORE INTO prefix_users (users_name) VALUES ('$user');INSERT IGNORE INTO prefix_permissions (entity,entity_id,uid,permission,create_p,read_p,update_p,delete_p,import_p,export_p) SELECT 'global',0,uid,'surveys',1,1,1,1,0,1 FROM prefix_users WHERE users_name='$user'"
fi
done
# Set permissions # Set permissions
sudo chown -R www-data:www-data $final_path ynh_set_default_perm $local_path
sudo chown -R www-data:www-data $upload_path sudo chmod -R u+w $local_path/tmp
sudo chmod -R 664 $final_path sudo chmod -R u+w $local_path/upload
sudo find $final_path -type d -print0 | xargs -0 sudo chmod 775 \ sudo chmod -R u+w $local_path/application/config/
|| echo "No file to modify"
sudo chmod -R 664 $upload_path
sudo find $upload_path -type d -print0 | xargs -0 sudo chmod 775 \
|| echo "No file to modify"
sudo chmod u+w $final_path/tmp
sudo chmod u+w $upload_path
sudo chmod u+w $final_path/application/config/
# Migrate DB
cd $local_path/application/commands
ynh_exec_as "$app" php console.php update