From 51fb9cb7cd6aa9388289d347e18cfa6fbca8b74c Mon Sep 17 00:00:00 2001 From: Matthieu Fronton Date: Wed, 15 Aug 2012 16:00:54 +0200 Subject: [PATCH 1/7] fix some basic wording issues --- install_yunohost | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/install_yunohost b/install_yunohost index 6e424d8..b793573 100755 --- a/install_yunohost +++ b/install_yunohost @@ -15,7 +15,7 @@ then mkdir /etc/yunohost/ cat << EOF > /etc/yunohost/yunohost.conf #Yunohost custom config -#for enable yunohost custom config change no by yes +#to enable yunohost custom config change no by yes amavis=no apache2=no @@ -48,10 +48,10 @@ then DOMAIN=$(hostname -d) fi -whiptail --title "Yunohost Installation" --yesno "Caution : your config file for postfix,dovecot,mysql,apache,ejabberd,radicale will be overwirte\nDo you want continue install Yunhost?" 8 78 +whiptail --title "Yunohost Installation" --yesno "Caution : your config files for postfix,dovecot,mysql,apache,ejabberd,radicale will be overwritten\nDo you want to proceed install of Yunohost?" 8 78 if [ $? = 0 ]; then - echo "======== Update hostname ========" + echo "======== Update hostname ========" # Update hostname echo "yunohost" > /etc/hostname grep -q "yunohost.$DOMAIN" /etc/hosts @@ -111,11 +111,11 @@ then service iptables start service nscd restart service nslcd restart - echo "======== Installation succes ========" + echo "======== Installation success ========" fi else - echo "======== Installation cancel ========" + echo "======== Installation cancelled ========" exit 1 fi From 33a32b47dba22e43df452276af32e4b748080f9b Mon Sep 17 00:00:00 2001 From: Matthieu Fronton Date: Wed, 15 Aug 2012 16:51:39 +0200 Subject: [PATCH 2/7] use of the enhanced version of test builtin command (double brackets) fix some misuse test command ( quote variable strings and use eq/ne for integers ) remove unecessary semi-colon at the end of each if ( use it only if the "then" is on the same line ) adding some TODO's and may be other minor changes... --- install_yunohost | 79 ++++++++++++++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 30 deletions(-) diff --git a/install_yunohost b/install_yunohost index b793573..426bdeb 100755 --- a/install_yunohost +++ b/install_yunohost @@ -1,16 +1,21 @@ #!/bin/bash +# TODO : test if the script is executed as root + echo "======== YunoHost Installation ========" echo "======== Check dependences ========" -DOMAIN=$(hostname -d) + +DOMAIN="$(hostname -d)" + dpkg -l | grep -q lsb-release -if [ $? = 1 ]; +# @abeudin : Using double brackets is more "secure" (kind of) => http://tldp.org/LDP/abs/html/testconstructs.html#DBLBRACKETS +if [[ $? -eq 1 ]] then apt-get update -qq - apt-get install lsb-release -y + apt-get install lsb-release -y fi -if [ ! -f /etc/yunohost/yunohost.conf ]; +if [[ ! -f /etc/yunohost/yunohost.conf ]] then mkdir /etc/yunohost/ cat << EOF > /etc/yunohost/yunohost.conf @@ -35,28 +40,28 @@ fi echo "======== Check domain ========" whiptail --title "Yunohost Domain" --yesno "Your domain is $DOMAIN\nDo you want to change this?" 8 78 YESNO=$? -if [ $YESNO = 255 ]; +if [[ $YESNO -eq 255 ]] then - exit 1 -elif [ $YESNO = 0 ]; + exit 1 +elif [[ $YESNO -eq 0 ]] then - DOMAIN=$(whiptail --inputbox "Enter your new domain please" 8 78 --title "Yunohost Domain" 3>&1 1>&2 2>&3) + DOMAIN=$(whiptail --inputbox "Enter your new domain please" 8 78 --title "Yunohost Domain" 3>&1 1>&2 2>&3) fi -if [ $DOMAIN = '' ]; +if [[ -z "$DOMAIN" ]] then - DOMAIN=$(hostname -d) + DOMAIN="$(hostname -d)" fi whiptail --title "Yunohost Installation" --yesno "Caution : your config files for postfix,dovecot,mysql,apache,ejabberd,radicale will be overwritten\nDo you want to proceed install of Yunohost?" 8 78 -if [ $? = 0 ]; -then +if [ $? -eq 0 ] +then echo "======== Update hostname ========" # Update hostname echo "yunohost" > /etc/hostname grep -q "yunohost.$DOMAIN" /etc/hosts - if [ $? = 1 ]; - then + if [[ $? -eq 1 ]] + then echo -e "127.0.0.1\tyunohost.$DOMAIN\tyunohost" > /etc/hosts /etc/init.d/hostname.sh start fi @@ -65,19 +70,22 @@ then #Get gpg key wget -O- http://lemonldap-ng.org/_media/rpm-gpg-key-ow2 -q | apt-key add - -qq wget -O- http://repo.yunohost.org/yunohost.asc -q | apt-key add - -qq + + # TODO : use custom yunohost.sources.lits file and store it in sources.list.d + grep -q "lemonldap-ng.org" /etc/apt/sources.list - if [ $? = 1 ]; + if [[ $? -eq 1 ]] then echo "deb http://lemonldap-ng.org/deb squeeze main" >> /etc/apt/sources.list fi grep "yunohost" /etc/apt/sources.list - if [ $? = 1 ]; - then - if [ $(lsb_release -is) = "Debian" ]; + if [[ $? -eq 1 ]] + then + if [[ "$(lsb_release -is)" = "Debian" ]] then echo "deb http://repo.yunohost.org/ squeeze main" >> /etc/apt/sources.list echo "deb http://repo.yunohost.org/ apps main" >> /etc/apt/sources.list - elif [ $(lsb_release -is) = "Ubuntu" ]; + elif [[ "$(lsb_release -is)" = "Ubuntu" ]] then echo "deb http://repo.yunohost.org/ precise main" >> /etc/apt/sources.list echo "deb http://repo.yunohost.org/ apps main" >> /etc/apt/sources.list @@ -86,24 +94,36 @@ then #Update repo apt-get update -qq - if [ $? != 0 ] ; + if [[ $? -ne 0 ]] then - echo "Update Repo Failure" - exit 1 + # TODO : rollback (restore legacy configuration for hostname, sources.list, ...) + echo "Update Repo Failure" + exit 1 fi echo "======== Install ========" #add answer in debconf db debconf-set-selections debconf - + #Install yunohost packages - debconf-apt-progress --logfile /var/log/yunohost.log --logstderr /var/log/yunohost.error -- apt-get -y install yunohost yunohost-config yunohost-config-postfix postfix postfix-ldap postfix-policyd-spf-perl - if [ $? != 0 ] ; + debconf-apt-progress \ + --logfile /var/log/yunohost.log \ + --logstderr /var/log/yunohost.error \ + -- \ + apt-get -y install \ + yunohost \ + yunohost-config \ + yunohost-config-postfix \ + postfix postfix-ldap \ + postfix-policyd-spf-perl + + if [[ $? -ne 0 ]] then - echo "Install yunohost Failure" - exit 1 + # TODO : warn the user that no rollback will be performed and list modified files + echo "======== Installation failed ========" + exit 1 else - service slapd restart + service slapd restart service apache2 restart service dovecot restart service postfix restart @@ -112,10 +132,9 @@ then service nscd restart service nslcd restart echo "======== Installation success ========" - fi -else +else echo "======== Installation cancelled ========" exit 1 fi From 33e06849040fda87dd9a93b1102fc4bc9e9ce4eb Mon Sep 17 00:00:00 2001 From: Matthieu Fronton Date: Wed, 15 Aug 2012 16:51:39 +0200 Subject: [PATCH 3/7] Various bash coding improvements Adding backup feature for config files Using custom source list file for APT --- install_yunohost | 86 ++++++++++++++++++++++++++++-------------------- 1 file changed, 51 insertions(+), 35 deletions(-) diff --git a/install_yunohost b/install_yunohost index 426bdeb..7ff60a0 100755 --- a/install_yunohost +++ b/install_yunohost @@ -1,12 +1,23 @@ #!/bin/bash +function bck { + FULLPATH="$(readlink -f "$1")" + DST="${2%/}/$(dirname $FULLPATH)" + mkdir -p "$DST" + cp -r --preserve=all "$FULLPATH" "$DST/$(basename $FULLPATH)" +} + +function restore { + # TODO +} + # TODO : test if the script is executed as root +set -u + echo "======== YunoHost Installation ========" echo "======== Check dependences ========" -DOMAIN="$(hostname -d)" - dpkg -l | grep -q lsb-release # @abeudin : Using double brackets is more "secure" (kind of) => http://tldp.org/LDP/abs/html/testconstructs.html#DBLBRACKETS if [[ $? -eq 1 ]] @@ -37,59 +48,64 @@ ssh=yes EOF fi -echo "======== Check domain ========" +echo "======== Checking domain ========" whiptail --title "Yunohost Domain" --yesno "Your domain is $DOMAIN\nDo you want to change this?" 8 78 YESNO=$? -if [[ $YESNO -eq 255 ]] -then - exit 1 -elif [[ $YESNO -eq 0 ]] -then - DOMAIN=$(whiptail --inputbox "Enter your new domain please" 8 78 --title "Yunohost Domain" 3>&1 1>&2 2>&3) -fi -if [[ -z "$DOMAIN" ]] -then - DOMAIN="$(hostname -d)" -fi +case $YESNO in + 0) DOMAIN=$(whiptail --inputbox "Enter your new domain please" 8 78 --title "Yunohost Domain" 3>&1 1>&2 2>&3);; # + 1) ;; # => Noting to do here. Just keeping it for the record + *) echo >&2 "ERROR: Unknown error ($YESNO) occured. Exiting"; exit $YESNO;; # ERROR +esac + +[[ -z "$DOMAIN" ]] && DOMAIN="$(hostname -d)" whiptail --title "Yunohost Installation" --yesno "Caution : your config files for postfix,dovecot,mysql,apache,ejabberd,radicale will be overwritten\nDo you want to proceed install of Yunohost?" 8 78 -if [ $? -eq 0 ] +YESNO=$? + +if [ $YESNO -eq 0 ] then - echo "======== Update hostname ========" + # Backup folder for legacy config files + LEGACY=/etc/yunohost/.legacy + mkdir -p "$LEGACY" + + echo "======== Updating hostname ========" # Update hostname + bck /etc/hostname "$LEGACY" echo "yunohost" > /etc/hostname - grep -q "yunohost.$DOMAIN" /etc/hosts - if [[ $? -eq 1 ]] - then + # Update hosts + if [[ $(grep -c "yunohost.$DOMAIN" /etc/hosts) -eq 0 ]] + then + bck /etc/hosts "$LEGACY" echo -e "127.0.0.1\tyunohost.$DOMAIN\tyunohost" > /etc/hosts /etc/init.d/hostname.sh start fi - echo "======== Add repository ========" + echo "======== Adding repositories ========" #Get gpg key wget -O- http://lemonldap-ng.org/_media/rpm-gpg-key-ow2 -q | apt-key add - -qq wget -O- http://repo.yunohost.org/yunohost.asc -q | apt-key add - -qq - # TODO : use custom yunohost.sources.lits file and store it in sources.list.d - - grep -q "lemonldap-ng.org" /etc/apt/sources.list + CUSTOMAPT=/etc/apt/sources.list.d/yunohost.list + grep -qri "lemonldap-ng.org" /etc/apt/sources.list* if [[ $? -eq 1 ]] then - echo "deb http://lemonldap-ng.org/deb squeeze main" >> /etc/apt/sources.list + echo "deb http://lemonldap-ng.org/deb squeeze main" >> $CUSTOMAPT fi - grep "yunohost" /etc/apt/sources.list + + grep -qri "yunohost" /etc/apt/sources.list* if [[ $? -eq 1 ]] - then - if [[ "$(lsb_release -is)" = "Debian" ]] - then - echo "deb http://repo.yunohost.org/ squeeze main" >> /etc/apt/sources.list - echo "deb http://repo.yunohost.org/ apps main" >> /etc/apt/sources.list - elif [[ "$(lsb_release -is)" = "Ubuntu" ]] - then - echo "deb http://repo.yunohost.org/ precise main" >> /etc/apt/sources.list - echo "deb http://repo.yunohost.org/ apps main" >> /etc/apt/sources.list - fi + then + case "$(lsb_release -is)" in + "Debian") + echo "deb http://repo.yunohost.org/ squeeze main" >> $CUSTOMAPT + echo "deb http://repo.yunohost.org/ apps main" >> $CUSTOMAPT + ;; + "Ubuntu") + echo "deb http://repo.yunohost.org/ precise main" >> $CUSTOMAPT + echo "deb http://repo.yunohost.org/ apps main" >> $CUSTOMAPT + ;; + esac fi #Update repo From 918647940e9c834b2b567f3b7a240ed6870264f1 Mon Sep 17 00:00:00 2001 From: Matthieu Fronton Date: Thu, 16 Aug 2012 09:01:16 +0200 Subject: [PATCH 4/7] forgotten double bracket fix --- install_yunohost | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install_yunohost b/install_yunohost index 7ff60a0..aced745 100755 --- a/install_yunohost +++ b/install_yunohost @@ -63,7 +63,7 @@ esac whiptail --title "Yunohost Installation" --yesno "Caution : your config files for postfix,dovecot,mysql,apache,ejabberd,radicale will be overwritten\nDo you want to proceed install of Yunohost?" 8 78 YESNO=$? -if [ $YESNO -eq 0 ] +if [[ $YESNO -eq 0 ]] then # Backup folder for legacy config files LEGACY=/etc/yunohost/.legacy From 7c744aa45a4f9470964725d4180c76f53cc814a0 Mon Sep 17 00:00:00 2001 From: Matthieu Fronton Date: Thu, 16 Aug 2012 09:02:51 +0200 Subject: [PATCH 5/7] adding rollback feature of the config files (only if install haven't started yet) --- install_yunohost | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/install_yunohost b/install_yunohost index aced745..7fe6df1 100755 --- a/install_yunohost +++ b/install_yunohost @@ -7,8 +7,10 @@ function bck { cp -r --preserve=all "$FULLPATH" "$DST/$(basename $FULLPATH)" } -function restore { - # TODO +function rst { + [[ ! -d "$LEGACY" ]] && echo >&2 "Rollback failed : Unknown folder $LEGACY" && exit $ERR_FAIL_RESTORE + mv -fv "$LEGACY"/* / + [[ $? -ne 0 ]] && echo >&2 "Rollback failed" && exit $ERR_FAIL_RESTORE } # TODO : test if the script is executed as root @@ -113,8 +115,9 @@ then if [[ $? -ne 0 ]] then # TODO : rollback (restore legacy configuration for hostname, sources.list, ...) - echo "Update Repo Failure" - exit 1 + echo "Update Repo Failure : Rolling back" + rst "$LEGACY" + exit $ERR_FAIL_UPDATE fi echo "======== Install ========" From 3089dc1016ea5d1dee48c05893316f5c1771bd0a Mon Sep 17 00:00:00 2001 From: Matthieu Fronton Date: Thu, 16 Aug 2012 09:05:23 +0200 Subject: [PATCH 6/7] adding msg for manual rollback if install failed --- install_yunohost | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/install_yunohost b/install_yunohost index 7fe6df1..fdc2fb5 100755 --- a/install_yunohost +++ b/install_yunohost @@ -1,5 +1,11 @@ #!/bin/bash +SUCCESS=0 +ERR_FAIL_RESTORE=1 +ERR_FAIL_UPDATE=2 +ERR_FAIL_INSTALL=3 +ERR_CANCEL_INSTALL=4 + function bck { FULLPATH="$(readlink -f "$1")" DST="${2%/}/$(dirname $FULLPATH)" @@ -114,7 +120,6 @@ then apt-get update -qq if [[ $? -ne 0 ]] then - # TODO : rollback (restore legacy configuration for hostname, sources.list, ...) echo "Update Repo Failure : Rolling back" rst "$LEGACY" exit $ERR_FAIL_UPDATE @@ -138,9 +143,11 @@ then if [[ $? -ne 0 ]] then - # TODO : warn the user that no rollback will be performed and list modified files echo "======== Installation failed ========" - exit 1 + echo "Rolling back have to be done manually !" + echo "Check your legacy configuration files => '$LEGACY'" + echo "Check install logs => '/var/log/yunohost.log' and '/var/log/yunohost.error'" + exit $ERR_FAIL_INSTALL else service slapd restart service apache2 restart From 307fa4b0a8fd8391ae5be4caffd1252d9397b012 Mon Sep 17 00:00:00 2001 From: Matthieu Fronton Date: Thu, 16 Aug 2012 09:06:41 +0200 Subject: [PATCH 7/7] adding specific exit code variable for each error type --- install_yunohost | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/install_yunohost b/install_yunohost index fdc2fb5..12c7f55 100755 --- a/install_yunohost +++ b/install_yunohost @@ -5,6 +5,7 @@ ERR_FAIL_RESTORE=1 ERR_FAIL_UPDATE=2 ERR_FAIL_INSTALL=3 ERR_CANCEL_INSTALL=4 +ERR_IMPOSSIBLE=-1 function bck { FULLPATH="$(readlink -f "$1")" @@ -158,9 +159,13 @@ then service nscd restart service nslcd restart echo "======== Installation success ========" + exit $SUCCESS fi else echo "======== Installation cancelled ========" - exit 1 + exit $ERR_CANCEL_INSTALL fi + +# Security : we shouldn't be able to exit here +exit $ERR_IMPOSSIBLE