From f019188a9059287e94c45f65685200c31e896dd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Lebleu?= Date: Sat, 16 Apr 2016 16:07:24 +0200 Subject: [PATCH] [enh] Update mysql conf_regen hook and use helpers for password generation --- data/hooks/conf_regen-old/34-mysql | 35 ------------------------ data/hooks/conf_regen/34-mysql | 43 ++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 35 deletions(-) delete mode 100644 data/hooks/conf_regen-old/34-mysql create mode 100755 data/hooks/conf_regen/34-mysql diff --git a/data/hooks/conf_regen-old/34-mysql b/data/hooks/conf_regen-old/34-mysql deleted file mode 100644 index 95fc97808..000000000 --- a/data/hooks/conf_regen-old/34-mysql +++ /dev/null @@ -1,35 +0,0 @@ -set -e - -force=$1 - -function safe_copy () { - if [[ "$force" == "True" ]]; then - sudo yunohost service safecopy \ - -s mysql $1 $2 --force - else - sudo yunohost service safecopy \ - -s mysql $1 $2 - fi -} - -function randpass () { - [ "$2" == "0" ] && CHAR="[:alnum:]" || CHAR="[:graph:]" - cat /dev/urandom | tr -cd "$CHAR" | head -c ${1:-32} - echo -} - -cd /usr/share/yunohost/templates/mysql - -if [[ "$(safe_copy my.cnf /etc/mysql/my.cnf | tail -n1)" == "True" ]]; then - sudo service mysql restart -fi - -if [ ! -f /etc/yunohost/mysql ]; then - [[ $(/bin/ps aux | grep '[m]ysqld') == "0" ]] \ - && sudo service mysql start - - mysql_password=$(randpass 10 0) - sudo mysqladmin -u root -pyunohost password $mysql_password - echo $mysql_password | sudo tee /etc/yunohost/mysql - sudo chmod 400 /etc/yunohost/mysql -fi diff --git a/data/hooks/conf_regen/34-mysql b/data/hooks/conf_regen/34-mysql new file mode 100755 index 000000000..bed97b497 --- /dev/null +++ b/data/hooks/conf_regen/34-mysql @@ -0,0 +1,43 @@ +#!/bin/bash + +set -e + +do_pre_regen() { + pending_dir=$1 + + cd /usr/share/yunohost/templates/mysql + + install -D my.cnf "${pending_dir}/etc/mysql/my.cnf" +} + +do_post_regen() { + # TODO: only restart if conf changed + sudo service mysql restart + + if [ ! -f /etc/yunohost/mysql ]; then + # source string helpers + . /usr/share/yunohost/helpers.d/string + + mysql_password=$(ynh_string_random 10) + sudo mysqladmin -u root -pyunohost password "$mysql_password" + echo $mysql_password | sudo tee /etc/yunohost/mysql + sudo chmod 400 /etc/yunohost/mysql + fi +} + +FORCE=$2 + +case "$1" in + pre) + do_pre_regen $3 + ;; + post) + do_post_regen + ;; + *) + echo "hook called with unknown argument \`$1'" >&2 + exit 1 + ;; +esac + +exit 0