2014-12-09 21:00:17 +01:00
#!/bin/bash
# Retrieve arguments
domain=$1
path=$2
2014-12-16 00:14:01 +01:00
is_public=$3
2015-02-27 14:36:54 +01:00
port=$4
2016-11-30 17:41:41 +01:00
app=jenkins
2014-12-09 21:00:17 +01:00
2015-02-27 14:36:54 +01:00
sudo yunohost app checkport $port
2014-12-09 21:00:17 +01:00
if [[ ! $? -eq 0 ]]; then
2015-02-27 14:36:54 +01:00
echo "Port $port is not available. Aborting..."
2014-12-09 21:00:17 +01:00
exit 1
fi
# Check domain/path availability
sudo yunohost app checkurl $domain$path -a jenkins
if [[ ! $? -eq 0 ]]; then
exit 1
fi
echo "Install dependencies..."
2016-11-30 17:41:41 +01:00
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
2014-12-09 21:00:17 +01:00
sudo apt-get update
sudo apt-get install jenkins -y
2014-12-16 08:14:48 +01:00
2016-11-30 17:43:02 +01:00
sudo sed -i "s/^HTTP_PORT=.*/HTTP_PORT=$port/g" /etc/default/jenkins
2014-12-16 08:14:48 +01:00
if [[ "$path" != "/" ]];
then
# $ means 'process only the last line'
2016-11-30 17:43:02 +01:00
sudo sed -i "$ s@--httpPort=\$HTTP_PORT@& --prefix=$path@g" /etc/default/jenkins
2014-12-16 08:14:48 +01:00
fi
2016-11-30 17:41:41 +01:00
sudo sed -i "s/-Djava.awt.headless=true/& -Djenkins.install.runSetupWizard=false/g" /etc/default/jenkins
sudo service $app restart # Redémarre jenkins
tempfile="$(mktemp)"
tail -f -n1 /var/log/$app/$app.log > "$tempfile" & # Suit le démarrage dans le log
PID_TAIL=$! # Récupère le PID de la commande tail, qui est passée en arrière plan.
2014-12-09 21:00:17 +01:00
sudo yunohost service add jenkins -l /var/log/jenkins/jenkins.log
echo "Nginx configuration (sso disabled)..."
2016-11-30 17:41:41 +01:00
sed -i "s@__PATH__@$path@g" ../conf/nginx.conf
sed -i "s@__PORT__@$port@g" ../conf/nginx.conf
sed -i "s@__DOMAIN__@$domain@g" ../conf/nginx.conf
2014-12-09 21:00:17 +01:00
sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/jenkins.conf
2014-12-16 00:14:01 +01:00
sudo yunohost app setting jenkins is_public -v $is_public
if [ "$is_public" = "Yes" ];
then
sudo yunohost app setting jenkins unprotected_uris -v "/"
fi
2014-12-09 21:00:17 +01:00
sudo service nginx reload
sudo yunohost app ssowatconf
2016-11-30 17:41:41 +01:00
# Surveille le démarrage du service.
config_OK=0
for i in `seq 1 60`
do # La boucle attend le démarrage de jenkins Ou 60 secondes.
if grep -q "INFOS: Jenkins is fully up and running" "$tempfile" && [ "$config_OK" -eq 0 ]; then
# Si le log annonce une première fois le démarrage de jenkins. Fait sauter la sécurité de la première connexion de l'admin. Qui bloque curl et demande une identification inexistante une fois sur deux...
sudo wget -nv --no-check-certificate https://$domain$path/jnlpJars/jenkins-cli.jar -O /var/lib/jenkins/jenkins-cli.jar
for i in `seq 1 60`
do # La boucle attend la mise à jour des dépôts de plugins. Ou 20 secondes.
if test -e /var/lib/jenkins/updates/default.json; then
break;
else
echo -n "." >&2
sleep 1
fi
done
java -jar /var/lib/jenkins/jenkins-cli.jar -s https://$domain$path install-plugin reverse-proxy-auth-plugin
sudo cp ../conf/config.xml /var/lib/jenkins/
sudo chown jenkins: /var/lib/jenkins/config.xml
sudo service $app restart # Puis redémarre à nouveau le service.
cat /dev/null > "$tempfile" # Purge le log de suivi du démarrage.
config_OK=1
i=1;
fi
if grep -q "INFOS: Jenkins is fully up and running" "$tempfile" && [ "$config_OK" -eq 1 ]; then
break # Si le log annonce une deuxième fois le démarrage d'etherpad, sort de la boucle.
fi
echo -n "." >&2
sleep 1
done
kill -s 15 $PID_TAIL > /dev/null # Arrête l'exécution de tail.
sudo rm "$tempfile"