Beautify install script

This commit is contained in:
Julien Malik 2015-09-01 09:45:13 +02:00
parent e648b905c2
commit 05008d9678

View file

@ -1,66 +1,71 @@
#!/bin/bash #!/bin/bash
SUCCESS=0 print() {
ERR_FAIL_RESTORE=1 printf "%s\n" "$*";
ERR_FAIL_UPDATE=2
ERR_FAIL_INSTALL=3
ERR_CANCEL_INSTALL=4
ERR_IMPOSSIBLE=-1
function bck {
FULLPATH="$(readlink -f "$1")"
DST="${2%/}/$(dirname $FULLPATH)"
mkdir -p "$DST"
cp -r --preserve=all "$FULLPATH" "$DST/$(basename $FULLPATH)"
} }
function rst { die() {
[[ ! -d "$LEGACY" ]] && echo >&2 "Rollback failed : Unknown folder $LEGACY" && exit $ERR_FAIL_RESTORE print "
cp -rf "$LEGACY"/* / Yunohost installation error :
[[ $? -ne 0 ]] && echo >&2 "Rollback failed" && exit $ERR_FAIL_RESTORE
$1" 1>&2
exit ${2:-1}
} }
set -u this_script_path() {
echo "======== Get path of current script ======="
# http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in # http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in
SOURCE="${BASH_SOURCE[0]}" local SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" local DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")" SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
echo $DIR
}
echo "Running from $DIR" ensure_root() {
if [ "$(id -u)" != "0" ] ;
echo "======== Check rights ========"
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
echo "======== YunoHost Installation ========"
echo "======== Check dependencies ========"
apt-get update -qq
for i in lsb-release wget dialog whiptail
do
dpkg -l | grep -q $i
if [[ $? -eq 1 ]]
then then
apt-get install $i -y return 1
fi fi
done return 0
}
# Fix install on jessie installscript_dependencies() {
if [ $(lsb_release -c | awk '{print $2}') = jessie ]; # install dependencies of the install script itself
local packages="lsb-release wget whiptail"
debconf-apt-progress \
--logfile /var/log/yunohost-update.log \
-- \
apt-get update \
|| return 1
debconf-apt-progress \
--logfile /var/log/yunohost.log \
-- \
apt-get -o Dpkg::Options::="--force-confold" \
-y install \
$packages \
|| return 2
# on Jessie, we need python-xmpp
# TODO : add a comment for why we need this
if [ "$(lsb_release -c | awk '{print $2}')" == "jessie" ] ;
then then
apt-get install python-xmpp -y debconf-apt-progress \
--logfile /var/log/yunohost.log \
-- \
apt-get -o Dpkg::Options::="--force-confold" \
-y install \
python-xmpp \
|| return 3
fi fi
return 0
}
create_custom_config() {
if [[ ! -f /etc/yunohost/yunohost.conf ]] if [[ ! -f /etc/yunohost/yunohost.conf ]]
then then
mkdir /etc/yunohost/ mkdir /etc/yunohost/
@ -84,121 +89,179 @@ ssh=yes
ssowat=no ssowat=no
EOF EOF
fi fi
}
echo "======== Checking domain ========" set_domain() {
DOMAIN=$(hostname -d) # Configure hostname to yunohost.yunohost.org if not already set
if [[ "${DOMAIN:-1}" = 1 ]] # NOTE: This is done also during postinstall "to avoid amavis bug"
then # so we might not need it here
hostname yunohost.yunohost.org [ hostname -d ] || hostname yunohost.yunohost.org
DOMAIN='yunohost.org' }
fi
whiptail --title "Yunohost Installation" --yesno "Caution : your config files for postfix,dovecot,mysql,nginx,metronome will be overwritten\nDo you want to proceed install of Yunohost?" 8 78 confirm_installation() {
YESNO=$? local text="
Caution !
if [[ $YESNO -eq 0 ]] Your configuration files for :
then - postfix
# Backup folder for legacy config files - dovecot
LEGACY=/etc/yunohost/.legacy - mysql
mkdir -p "$LEGACY" - nginx
- metronome
will be overwritten !
echo "======== Adding repositories ========" Are you sure you want to proceed with the installation of Yunohost?
"
whiptail --title "Yunohost Installation" --yesno "$text" 20 78
}
CUSTOMAPT=/etc/apt/sources.list setup_package_source() {
local CUSTOMAPT=/etc/apt/sources.list.d/yunohost.list
grep -qri "yunohost" $CUSTOMAPT # In any case we need the main stable repo
if [[ $? -eq 1 ]] echo "deb http://repo.yunohost.org/ megusta main" > $CUSTOMAPT
then
echo "deb http://repo.yunohost.org/ megusta main" >> $CUSTOMAPT
fi
# Also add repositories for 'testing' and/or 'unstable' if the script has been called with those arguments
if [ $# -gt 0 ]; then if [ $# -gt 0 ]; then
if [[ "$1" == "test" ]] || [[ "$1" == "testing" ]] ; then if [[ "$1" == "test" ]] || [[ "$1" == "testing" ]] ; then
echo "deb http://daily.yunohost.org/ testing main" >> $CUSTOMAPT echo "deb http://repo.yunohost.org/ testing main" >> $CUSTOMAPT
fi fi
if [[ "$1" == "daily" ]] || [[ "$1" == "unstable" ]] ; then if [[ "$1" == "daily" ]] || [[ "$1" == "unstable" ]] ; then
echo "deb http://daily.yunohost.org/ testing main" >> $CUSTOMAPT echo "deb http://repo.yunohost.org/ testing main" >> $CUSTOMAPT
echo "deb http://daily.yunohost.org/ unstable main" >> $CUSTOMAPT echo "deb http://repo.yunohost.org/ unstable main" >> $CUSTOMAPT
fi fi
fi fi
#Get gpg key # Add YunoHost repository key to the keyring
wget -O- http://repo.yunohost.org/yunohost.asc -q | apt-key add - -qq wget -O- http://repo.yunohost.org/yunohost.asc -q | apt-key add - -qq
}
#Update repo apt_update() {
debconf-apt-progress \ debconf-apt-progress \
--logfile /var/log/yunohost-update.log \ --logfile /var/log/yunohost-update.log \
-- \ -- \
apt-get update apt-get update
}
if [[ $? -ne 0 ]] register_debconf() {
then
echo "Update Repo Failure : Rolling back"
rst "$LEGACY"
exit $ERR_FAIL_UPDATE
fi
echo "======== Install ========"
#add answer in debconf db
if [ $(lsb_release -c | awk '{print $2}') = jessie ]; if [ $(lsb_release -c | awk '{print $2}') = jessie ];
then then
debconf-set-selections $DIR/debconfjessie debconf-set-selections $(this_script_path)/debconfjessie
else else
debconf-set-selections $DIR/debconfv2 debconf-set-selections $(this_script_path)/debconfv2
fi fi
}
#Install yunohost packages install_yunohost_packages() {
debconf-apt-progress \ debconf-apt-progress \
--logfile /var/log/yunohost.log \ --logfile /var/log/yunohost.log \
-- \ -- \
apt-get -o Dpkg::Options::="--force-confold" \ apt-get -o Dpkg::Options::="--force-confold" \
-y install \ -y install \
yunohost \ yunohost
yunohost-config \ }
yunohost-config-postfix \
postfix postfix-ldap \
postfix-policyd-spf-perl
if [[ $? -ne 0 ]] restart_services() {
then
echo "======== Installation failed ========"
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 slapd restart
if [ -f /etc/init.d/yunohost-firewall ]; # service yunohost-firewall start
then # service nscd restart
service yunohost-firewall start # service nslcd restart
fi
service nscd restart # NOTE : We don't fail if slapd fails to restart...
service nslcd restart return 0
}
post_install() {
local text="
Yunohost packages have been installed successfully!
You can now proceed with Yunohost post-installation.
This is where you will be asked for :
- the main DNS domain name of your server
- the administration password
You can also perform this step later on your own :
- either from a shell, by running 'yunohost tools postinstall'
as root
- either from your web browser, by accessing https://yunohost.local
Please refer to https://yunohost.org/#/postinstall
for additionnal information.
Do you want to proceed with YunoHost post-installation now?
"
whiptail --title "Post-installation" --yesno "$text" 25 78 \
|| return 0
echo -e "\n"
whiptail --title "Post-installation" --yesno "Proceed to post-installation?" 8 78
YESNO=$?
RESULT=1
while [ $RESULT -gt 0 ]; do
if [[ $YESNO -eq 0 ]]; then
echo -e "\n"
/usr/bin/yunohost tools postinstall /usr/bin/yunohost tools postinstall
let RESULT=$?
if [ $RESULT -gt 0 ]; then local POSTINSTALL_EXIT_CODE="$?"
echo -e "\n" while [ "$POSTINSTALL_EXIT_CODE" -gt 0 ] ;
whiptail --title "Post-installation" --yesno "Post-installation failed, retry ?" 8 78 do
let YESNO=$? local text_retry="
fi Yunohost post-installation has failed.
else
exit 0 Do you want to try again now?
fi "
whiptail --title "Post-installation" --yesno "$text_retry" 12 78 --defaultno \
&& return $POSTINSTALL_EXIT_CODE
/usr/bin/yunohost tools postinstall
POSTINSTALL_EXIT_CODE="$?"
done done
return 0
}
set -u
if ! ensure_root ; then
die "This script must be run as root" 1
fi fi
else if ! installscript_dependencies ; then
echo "======== Installation cancelled ========" die "Unable to install dependencies to install script" 2
exit $ERR_CANCEL_INSTALL
fi fi
# Security : we shouldn't be able to exit here if ! create_custom_config ; then
exit $ERR_IMPOSSIBLE die "Creating custom configuration file /etc/yunohost/yunohost.conf failed" 3
fi
if ! set_domain ; then
die "Setting hostname failed" 4
fi
if ! confirm_installation ; then
die "Installation cancelled at your request" 5
fi
if ! setup_package_source "$@" ; then
die "Setting up deb package sources failed" 6
fi
if ! apt_update ; then
die "Error caught during 'apt-get update'" 7
fi
if ! register_debconf ; then
die "Unable to insert new values into debconf database" 8
fi
if ! install_yunohost_packages ; then
die "\
Installation of Yunohost packages failed
You can check the install logs saved in /var/log/yunohost.log
" 9
fi
if ! restart_services ; then
die "Error caught during services restart" 10
fi
if ! post_install ; then
die "Post-installation failed" 11
fi
# Success !
exit 0