From 67328ed1a70a946a44bb3d6d61c320b314b441c2 Mon Sep 17 00:00:00 2001 From: Sebastien Badia Date: Wed, 13 May 2015 01:25:10 +0200 Subject: [PATCH 1/6] systemd: Convert services (SySv init) to systemctl (systemd) commands --- README.md | 4 ++-- conf/init_ynh-vpnclient | 18 +++++++++--------- scripts/install | 7 ++++--- scripts/remove | 4 ++-- sources/controller.php | 6 +++--- 5 files changed, 20 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 3f5b555..ee36a69 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,6 @@ This app works with a non-stable version of YunoHost. Until this version is available (coming soon!) as an official stable release, you need to execute some commands before installing this app: - # service bind9 stop - # update-rc.d bind9 remove + # systemctl stop bind9 + # systemctl disable bind9 # apt-get install dnsmasq diff --git a/conf/init_ynh-vpnclient b/conf/init_ynh-vpnclient index e2506e0..0be6ec1 100644 --- a/conf/init_ynh-vpnclient +++ b/conf/init_ynh-vpnclient @@ -69,7 +69,7 @@ is_serverip6route_set() { } is_openvpn_running() { - service openvpn status client &> /dev/null + systemctl is-active openvpn@client.service --quiet &> /dev/null } is_running() { @@ -128,7 +128,7 @@ start_openvpn() { sed 's|^||' -i /etc/openvpn/client.conf fi - service openvpn start client + systemctl start openvpn@client.service --quiet } ## Unsetters @@ -146,7 +146,7 @@ unset_serverip6route() { } stop_openvpn() { - service openvpn stop + systemctl stop openvpn.service --quiet } ## Tools @@ -181,7 +181,7 @@ if [ "$1" != restart ]; then # Restart php5-fpm at the first start (it needs to be restarted after the slapd start) if [ ! -e /tmp/.ynh-vpnclient-boot ]; then touch /tmp/.ynh-vpnclient-boot - service php5-fpm restart + systemctl restart php5-fpm --quiet fi # Check configuration consistency @@ -287,12 +287,12 @@ case "${1}" in moulinette_set wired_device "${new_wired_device}" # Restart dhcpd - service bind9 stop &> /dev/null - service dnsmasq restart + systemctl stop bind9 --quiet &> /dev/null + systemctl restart dnsmasq --quiet # Restart hotspot if needed if has_hotspot_app && ! is_hotspot_knowme; then - service ynh-hotspot start + systemctl start ynh-hotspot --quiet fi fi ;; @@ -322,10 +322,10 @@ case "${1}" in fi if has_hotspot_app && is_hotspot_knowme; then - service ynh-hotspot start + systemctl start ynh-hotspot --quiet fi - service dnsmasq restart + systemctl restart dnsmasq --quiet ;; restart) $0 stop diff --git a/scripts/install b/scripts/install index 63cc0c5..5fdfa5a 100644 --- a/scripts/install +++ b/scripts/install @@ -176,9 +176,9 @@ sudo yunohost service enable php5-fpm sudo yunohost service add ynh-vpnclient sudo yunohost service enable ynh-vpnclient -sudo service ynh-vpnclient start +sudo systemctl start ynh-vpnclient --quiet -sudo service nginx reload +sudo systemctl reload nginx --quiet # Update SSO for vpnadmin sudo yunohost app ssowatconf @@ -186,7 +186,8 @@ sudo yunohost app ssowatconf # Restart hotspot service if installed (and started) to change NAT configuration (now on tun0) # A new start will fix the interface without unsetting all stuff if [ -e /tmp/.ynh-hotspot-started ]; then - sudo service ynh-hotspot start + sudo systemctl restart ynh-hotspot --quiet + sudo systemctl restart dnsmasq --quiet fi # Check configuration consistency diff --git a/scripts/remove b/scripts/remove index a0e3369..6b2fcee 100644 --- a/scripts/remove +++ b/scripts/remove @@ -21,7 +21,7 @@ domain=$(sudo yunohost app setting vpnclient domain) # The End -sudo service ynh-vpnclient stop +sudo systemctl stop ynh-vpnclient --quiet sudo yunohost service remove ynh-vpnclient sudo rm -f /etc/init.d/ynh-vpnclient sudo rm -f /tmp/.ynh-vpnclient-* @@ -37,7 +37,7 @@ sudo rm -rf /etc/openvpn/keys/ # Restart services sudo yunohost service stop php5-fpm sudo yunohost service start php5-fpm -sudo service nginx reload +sudo systemctl reload nginx --quiet # Remove sources sudo rm -rf /var/www/vpnadmin/ diff --git a/sources/controller.php b/sources/controller.php index 86fbe8d..e8a7136 100644 --- a/sources/controller.php +++ b/sources/controller.php @@ -27,17 +27,17 @@ function moulinette_set($var, $value) { } function stop_service() { - exec('sudo service ynh-vpnclient stop'); + exec('sudo systemctl stop ynh-vpnclient'); } function start_service() { - exec('sudo service ynh-vpnclient start', $output, $retcode); + exec('sudo systemctl start ynh-vpnclient', $output, $retcode); return $retcode; } function service_status() { - exec('sudo service ynh-vpnclient status', $output); + exec('sudo systemctl is-active ynh-vpnclient', $output); return $output; } From 810ec32947c5777f89b763754dc6adb37ef3abcd Mon Sep 17 00:00:00 2001 From: Sebastien Badia Date: Wed, 13 May 2015 01:25:38 +0200 Subject: [PATCH 2/6] systemd: Add a service unit for ynh-vpnclient --- conf/ynh-vpnclient.service | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 conf/ynh-vpnclient.service diff --git a/conf/ynh-vpnclient.service b/conf/ynh-vpnclient.service new file mode 100644 index 0000000..75a97f7 --- /dev/null +++ b/conf/ynh-vpnclient.service @@ -0,0 +1,15 @@ +[Unit] +Description=YunoHost VPN Client. +Requires=network.target +After=network.target + +[Service] +Type=oneshot +ExecStart=/usr/local/bin/ynh-vpnclient start +ExecRestart=/usr/local/bin/ynh-vpnclient restart +ExecStop=/usr/local/bin/ynh-vpnclient stop +RemainAfterExit=yes + +[Install] +WantedBy=multi-user.target +Alias=ynh-vpnclient.service From 52254fad0e8c5c2f053e6540118ec391f93e40bf Mon Sep 17 00:00:00 2001 From: Sebastien Badia Date: Wed, 13 May 2015 01:26:14 +0200 Subject: [PATCH 3/6] systemd: migrate the SySv init script to a simple bash script --- conf/{init_ynh-vpnclient => ynh-vpnclient} | 0 scripts/install | 3 ++- scripts/remove | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) rename conf/{init_ynh-vpnclient => ynh-vpnclient} (100%) diff --git a/conf/init_ynh-vpnclient b/conf/ynh-vpnclient similarity index 100% rename from conf/init_ynh-vpnclient rename to conf/ynh-vpnclient diff --git a/scripts/install b/scripts/install index 5fdfa5a..2aab9b5 100644 --- a/scripts/install +++ b/scripts/install @@ -162,7 +162,8 @@ sudo sed 's||/var/www/vpnadmin/|g' -i /etc/php5/fpm/pool.d/v sudo sed "s||${url_path}|g" -i /var/www/vpnadmin/config.php # Copy init script -sudo install -o root -g root -m 0755 ../conf/init_ynh-vpnclient /etc/init.d/ynh-vpnclient +sudo install -o root -g root -m 0755 ../conf/ynh-vpnclient /usr/local/bin/ +sudo install -o root -g root -m 0755 ../conf/ynh-vpnclient.service /lib/systemd/system/ynh-vpnclient.service # Set default inits # The openvpn configuration is modified before the start, so the service is disabled by default diff --git a/scripts/remove b/scripts/remove index 6b2fcee..52f5bff 100644 --- a/scripts/remove +++ b/scripts/remove @@ -23,7 +23,7 @@ domain=$(sudo yunohost app setting vpnclient domain) # The End sudo systemctl stop ynh-vpnclient --quiet sudo yunohost service remove ynh-vpnclient -sudo rm -f /etc/init.d/ynh-vpnclient +sudo rm -f /lib/systemd/system/ynh-vpnclient.service sudo rm -f /tmp/.ynh-vpnclient-* # Remove confs From f8257cb41b35d10975fb845d517a6bcfbb9290f2 Mon Sep 17 00:00:00 2001 From: Sebastien Badia Date: Thu, 14 May 2015 16:30:07 -0700 Subject: [PATCH 4/6] systemd: Add missing clean for init sh script --- scripts/remove | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/remove b/scripts/remove index 52f5bff..5592b40 100644 --- a/scripts/remove +++ b/scripts/remove @@ -23,7 +23,7 @@ domain=$(sudo yunohost app setting vpnclient domain) # The End sudo systemctl stop ynh-vpnclient --quiet sudo yunohost service remove ynh-vpnclient -sudo rm -f /lib/systemd/system/ynh-vpnclient.service +sudo rm -f /lib/systemd/system/ynh-vpnclient.service /usr/local/bin/ynh-vpnclient sudo rm -f /tmp/.ynh-vpnclient-* # Remove confs From bf7f9aca56fdd2d09edd621039dedb58ec9393a4 Mon Sep 17 00:00:00 2001 From: Sebastien Badia Date: Thu, 14 May 2015 16:35:54 -0700 Subject: [PATCH 5/6] conf: Remove un-needed headers --- conf/ynh-vpnclient | 47 ++++++++++++++++++---------------------------- scripts/remove | 1 + 2 files changed, 19 insertions(+), 29 deletions(-) diff --git a/conf/ynh-vpnclient b/conf/ynh-vpnclient index 0be6ec1..212d0eb 100644 --- a/conf/ynh-vpnclient +++ b/conf/ynh-vpnclient @@ -1,30 +1,19 @@ #!/bin/bash -### BEGIN INIT INFO -# Provides: ynh-vpnclient -# Required-Start: $network $remote_fs $syslog yunohost-api -# Required-Stop: $network $remote_fs $syslog -# Should-Start: ynh-hotspot -# Should-Stop: ynh-hotspot -# Default-Start: 2 3 4 5 -# Default-Stop: 0 1 6 -# Short-Description: Start VPN client. -# Description: Start VPN client. -### END INIT INFO -# VPN Client app for YunoHost +# VPN Client app for YunoHost # Copyright (C) 2015 Julien Vaubourg # Contribute at https://github.com/jvaubourg/vpnclient_ynh -# +# # 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 Affero General Public License for more details. -# +# # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . @@ -162,7 +151,7 @@ moulinette_get() { fi echo "${value}" -} +} moulinette_set() { var=${1} @@ -183,49 +172,49 @@ if [ "$1" != restart ]; then touch /tmp/.ynh-vpnclient-boot systemctl restart php5-fpm --quiet fi - + # Check configuration consistency - + if [[ ! "${1}" =~ stop ]]; then exitcode=0 - + if [ ! -e /etc/openvpn/keys/ca-server.crt ]; then echo "[WARN] You need a CA server (you can add it through the web admin)" exitcode=1 fi - + empty=$(find /etc/openvpn/keys/ -empty -name credentials &> /dev/null | wc -l) if [ "${empty}" -gt 0 -a ! -e /etc/openvpn/keys/user.key ]; then echo "[WARN] You need either a client certificate, either a username, or both (you can add one through the web admin)" exitcode=1 fi - + [ "${exitcode}" -ne 0 ] && exit ${exitcode} fi - + # Variables - + echo -n "Retrieving Yunohost settings... " - + ynh_service_enabled=$(moulinette_get service_enabled) ynh_server_name=$(moulinette_get server_name) ynh_server_port=$(moulinette_get server_port) ynh_server_proto=$(moulinette_get server_proto) ynh_ip6_addr=$(moulinette_get ip6_addr) ynh_login_user=$(moulinette_get login_user) - + old_ip6_gw=$(moulinette_get ip6_gw) old_wired_device=$(moulinette_get wired_device) old_server_ip6=$(moulinette_get server_ip6) - + new_ip6_gw=$(ip -6 route | grep default\ via | awk '{ print $3 }') new_wired_device=$(ip route | awk '/default via/ { print $NF; }') new_server_ip6=$(host "${ynh_server_name}" | awk '/IPv6/ { print $NF; }') - + if [ -z "${new_server_ip6}" ]; then new_server_ip6=$(host "${ynh_server_name}" 80.67.188.188 | awk '/IPv6/ { print $NF; }') fi - + echo "OK" fi @@ -309,7 +298,7 @@ case "${1}" in echo "Unset IPv6 server route" unset_serverip6route "${old_server_ip6}" "${old_ip6_gw}" "${old_wired_device}" fi - + if is_openvpn_running; then echo "Stop openvpn" stop_openvpn diff --git a/scripts/remove b/scripts/remove index 5592b40..119ecdb 100644 --- a/scripts/remove +++ b/scripts/remove @@ -22,6 +22,7 @@ domain=$(sudo yunohost app setting vpnclient domain) # The End sudo systemctl stop ynh-vpnclient --quiet +sudo systemctl disable ynh-vpnclient --quiet sudo yunohost service remove ynh-vpnclient sudo rm -f /lib/systemd/system/ynh-vpnclient.service /usr/local/bin/ynh-vpnclient sudo rm -f /tmp/.ynh-vpnclient-* From 6ffe098dcc56e3fb81defe9cfef6fac7f02e42b2 Mon Sep 17 00:00:00 2001 From: Julien VAUBOURG Date: Mon, 25 May 2015 02:23:45 +0200 Subject: [PATCH 6/6] Ready for Jessie (+ some improvements) --- README.md | 9 ++----- conf/ynh-vpnclient | 50 ++++++++++++++++++++------------------ conf/ynh-vpnclient.service | 4 +-- scripts/install | 29 ++++++++++------------ scripts/remove | 12 ++++----- sources/controller.php | 2 +- 6 files changed, 50 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index ee36a69..fba7375 100644 --- a/README.md +++ b/README.md @@ -24,10 +24,5 @@ This YunoHost app is a part of the "[La Brique Internet](http://labriqueinter.ne ## Prerequisites -This app works with a non-stable version of YunoHost. - -Until this version is available (coming soon!) as an official stable release, you need to execute some commands before installing this app: - - # systemctl stop bind9 - # systemctl disable bind9 - # apt-get install dnsmasq +* Debian Jessie +* YunoHost >= 2.2.0 diff --git a/conf/ynh-vpnclient b/conf/ynh-vpnclient index 212d0eb..bee0e76 100644 --- a/conf/ynh-vpnclient +++ b/conf/ynh-vpnclient @@ -33,12 +33,17 @@ has_hotspot_app() { } is_hotspot_knowme() { - value=$(yunohost app setting hotspot vpnclient) + gotcha=0 - if [[ "${value}" =~ "An instance is already running" ]]; then - echo "${value}" >&2 - exit 1 - fi + while [ "${gotcha}" -eq 0 ]; do + value=$(yunohost app setting hotspot vpnclient) + + if [[ "${value}" =~ "An instance is already running" ]]; then + sleep $(($((RANDOM%5)) + 1)) + else + gotcha=1 + fi + done [ "${value}" == yes ] } @@ -58,7 +63,7 @@ is_serverip6route_set() { } is_openvpn_running() { - systemctl is-active openvpn@client.service --quiet &> /dev/null + systemctl is-active openvpn@client.service &> /dev/null } is_running() { @@ -117,7 +122,7 @@ start_openvpn() { sed 's|^||' -i /etc/openvpn/client.conf fi - systemctl start openvpn@client.service --quiet + systemctl start openvpn@client.service } ## Unsetters @@ -135,20 +140,24 @@ unset_serverip6route() { } stop_openvpn() { - systemctl stop openvpn.service --quiet + systemctl stop openvpn.service } ## Tools moulinette_get() { var=${1} + gotcha=0 - value=$(yunohost app setting vpnclient "${var}") + while [ "${gotcha}" -eq 0 ]; do + value=$(yunohost app setting vpnclient "${var}") - if [[ "${value}" =~ "An instance is already running" ]]; then - echo "${value}" >&2 - exit 1 - fi + if [[ "${value}" =~ "An instance is already running" ]]; then + sleep $(($((RANDOM%5)) + 1)) + else + gotcha=1 + fi + done echo "${value}" } @@ -170,7 +179,7 @@ if [ "$1" != restart ]; then # Restart php5-fpm at the first start (it needs to be restarted after the slapd start) if [ ! -e /tmp/.ynh-vpnclient-boot ]; then touch /tmp/.ynh-vpnclient-boot - systemctl restart php5-fpm --quiet + systemctl restart php5-fpm fi # Check configuration consistency @@ -275,13 +284,9 @@ case "${1}" in moulinette_set ip6_gw "${new_ip6_gw}" moulinette_set wired_device "${new_wired_device}" - # Restart dhcpd - systemctl stop bind9 --quiet &> /dev/null - systemctl restart dnsmasq --quiet - - # Restart hotspot if needed + # Fix configuration if has_hotspot_app && ! is_hotspot_knowme; then - systemctl start ynh-hotspot --quiet + ynh-hotspot start fi fi ;; @@ -310,11 +315,10 @@ case "${1}" in done fi + # Fix configuration if has_hotspot_app && is_hotspot_knowme; then - systemctl start ynh-hotspot --quiet + ynh-hotspot start fi - - systemctl restart dnsmasq --quiet ;; restart) $0 stop diff --git a/conf/ynh-vpnclient.service b/conf/ynh-vpnclient.service index 75a97f7..739d304 100644 --- a/conf/ynh-vpnclient.service +++ b/conf/ynh-vpnclient.service @@ -2,14 +2,14 @@ Description=YunoHost VPN Client. Requires=network.target After=network.target +Wants=ynh-hotspot.service +Before=ynh-hotspot.service [Service] Type=oneshot ExecStart=/usr/local/bin/ynh-vpnclient start -ExecRestart=/usr/local/bin/ynh-vpnclient restart ExecStop=/usr/local/bin/ynh-vpnclient stop RemainAfterExit=yes [Install] WantedBy=multi-user.target -Alias=ynh-vpnclient.service diff --git a/scripts/install b/scripts/install index 2aab9b5..dd79568 100644 --- a/scripts/install +++ b/scripts/install @@ -163,33 +163,30 @@ sudo sed "s||${url_path}|g" -i /var/www/vpnadmin/config.php # Copy init script sudo install -o root -g root -m 0755 ../conf/ynh-vpnclient /usr/local/bin/ -sudo install -o root -g root -m 0755 ../conf/ynh-vpnclient.service /lib/systemd/system/ynh-vpnclient.service +sudo install -o root -g root -m 0644 ../conf/ynh-vpnclient.service /etc/systemd/system/ # Set default inits -# The openvpn configuration is modified before the start, so the service is disabled by default -# and the ynh-vpnclient service handles it. -sudo yunohost service add openvpn -sudo yunohost service stop openvpn -sudo yunohost service disable openvpn +# The boot order of these services are important, so they are disabled by default +# and the ynh-vpnclient service handles them. +sudo systemctl disable openvpn +sudo systemctl stop openvpn -sudo yunohost service add php5-fpm -sudo yunohost service enable php5-fpm +sudo systemctl enable php5-fpm +sudo systemctl restart php5-fpm -sudo yunohost service add ynh-vpnclient -sudo yunohost service enable ynh-vpnclient -sudo systemctl start ynh-vpnclient --quiet +sudo systemctl reload nginx -sudo systemctl reload nginx --quiet +sudo systemctl enable ynh-vpnclient +sudo systemctl start ynh-vpnclient # Update SSO for vpnadmin sudo yunohost app ssowatconf # Restart hotspot service if installed (and started) to change NAT configuration (now on tun0) # A new start will fix the interface without unsetting all stuff -if [ -e /tmp/.ynh-hotspot-started ]; then - sudo systemctl restart ynh-hotspot --quiet - sudo systemctl restart dnsmasq --quiet -fi +#if [ -e /tmp/.ynh-hotspot-started ]; then +# sudo ynh-hotspot start +#fi # Check configuration consistency diff --git a/scripts/remove b/scripts/remove index 119ecdb..62dd392 100644 --- a/scripts/remove +++ b/scripts/remove @@ -21,10 +21,9 @@ domain=$(sudo yunohost app setting vpnclient domain) # The End -sudo systemctl stop ynh-vpnclient --quiet -sudo systemctl disable ynh-vpnclient --quiet -sudo yunohost service remove ynh-vpnclient -sudo rm -f /lib/systemd/system/ynh-vpnclient.service /usr/local/bin/ynh-vpnclient +sudo systemctl stop ynh-vpnclient +sudo systemctl disable ynh-vpnclient +sudo rm -f /etc/systemd/system/ynh-vpnclient.service /usr/local/bin/ynh-vpnclient sudo rm -f /tmp/.ynh-vpnclient-* # Remove confs @@ -36,9 +35,8 @@ sudo rm -f /etc/php5/fpm/pool.d/vpnadmin.conf sudo rm -rf /etc/openvpn/keys/ # Restart services -sudo yunohost service stop php5-fpm -sudo yunohost service start php5-fpm -sudo systemctl reload nginx --quiet +sudo systemctl restart php5-fpm +sudo systemctl reload nginx # Remove sources sudo rm -rf /var/www/vpnadmin/ diff --git a/sources/controller.php b/sources/controller.php index e8a7136..1c26179 100644 --- a/sources/controller.php +++ b/sources/controller.php @@ -37,7 +37,7 @@ function start_service() { } function service_status() { - exec('sudo systemctl is-active ynh-vpnclient', $output); + exec('sudo ynh-vpnclient status', $output); return $output; }