Correction : vm_hosted : etc/init.d/tmpfs -> etc/sv/*/run .
[lhc/ateliers.git] / vm_hosted
index ed42445..3259ef1 100755 (executable)
--- a/vm_hosted
+++ b/vm_hosted
@@ -299,7 +299,7 @@ rule_boot_configure () {
  }
 rule_dovecot_configure () {
        rule apt_get_install dovecot-imapd dovecot-managesieved dovecot-sieve
-       rule insserv_remove dovecot
+       rule insserv_remove  dovecot
        local hint="run vm_remote dovecot_key_send before"
        assert "sudo test -f /etc/dovecot/\"$vm_domainname\"/imap/x509/key.pem" hint
        sudo install -m 400 -o root -g root \
@@ -324,8 +324,7 @@ rule_dovecot_configure () {
                \$USER:\$(/usr/bin/doveadm pw -s SHA512-CRYPT):::::::
                _EOF
                EOF
-       rule runit_sv_configure dovecot
-       rule runit_sv_restart   dovecot
+       rule runit_configure dovecot
  }
 rule_etckeeper_configure () {
        sudo install -m 644 -o root -g root /dev/stdin /etc/etckeeper/etckeeper.conf <<-EOF
@@ -418,8 +417,9 @@ rule_initramfs_configure () {
  }
 rule_insserv_remove () { # SYNTAX: $sv
        local sv="$1"
-       sudo chmod u+x /etc/init.d/"$sv"
+       #sudo chmod u+x /etc/init.d/"$sv"
        sudo insserv --force --remove "$sv"
+       sudo test ! -x /etc/init.d/"$sv" ||
        sudo /etc/init.d/"$sv" stop
        sudo chmod ugo-x /etc/init.d/"$sv"
  }
@@ -528,7 +528,6 @@ rule_gitolite_configure () {
         do test ! -d /home/git/etc/gitolite/"$d" ||
                rmdir /home/git/etc/gitolite/"$d"
         done
-       rule tmpfs_configure
  }
 rule_locales_configure () {
        sudo debconf-set-selections <<-EOF
@@ -568,7 +567,7 @@ rule_mail_configure () {
  }
 rule_mysql_configure () {
        rule apt_get_install mysql-server-5.5
-       rule insserv_remove mysql
+       rule insserv_remove  mysql
        rule adduser mysql \
         --disabled-login \
         --disabled-password \
@@ -607,9 +606,7 @@ rule_mysql_configure () {
                 --datadir=/home/mysql/data \
                 --no-defaults
         fi
-       rule tmpfs_configure
-       rule runit_sv_configure mysql
-       rule runit_sv_restart   mysql
+       rule runit_configure mysql
        while ! sudo -u mysql mysql -u mysql </dev/null
        do sleep 0.3; done
        # NOTE:
@@ -741,9 +738,7 @@ rule_nginx_configure () {
                test ! -r "$tool"/etc/nginx/site.d/"$site"/configure.sh ||
                .         "$tool"/etc/nginx/site.d/"$site"/configure.sh
         done
-       rule tmpfs_configure
-       rule runit_sv_configure nginx
-       rule runit_sv_restart   nginx
+       rule runit_configure nginx
  }
 rule_nsd3_configure () { # NOTE: DNS autoritaire uniquement
        local -; set +f
@@ -787,15 +782,13 @@ rule_nsd3_configure () { # NOTE: DNS autoritaire uniquement
        sudo install -m 640 -o root -g nsd /dev/stdin \
         /etc/nsd3/nsd.conf
        sudo nsdc rebuild
-       rule runit_sv_configure nsd3
-       rule runit_sv_restart   nsd3
+       rule runit_configure nsd3
  }
 rule_ntp_configure () {
        # NOTE: http://my.opera.com/marcomarongiu/blog/2011/01/05/independent-wallclock-in-xen-4
-       rule apt_get_install    ntp
-       rule insserv_remove     ntp
-       rule runit_sv_configure ntp
-       rule runit_sv_restart   ntp
+       rule apt_get_install ntp
+       rule insserv_remove  ntp
+       rule runit_configure ntp
  }
 rule_openerp_configure () {
        sudo install -m 660 -o root -g root /dev/stdin /etc/apt/sources.list.d/openerp.list <<-EOF
@@ -909,9 +902,7 @@ rule_php5_fpm_configure () {
                 "$tool"/etc/php5/fpm/php.ini \
                        /etc/php5/fpm/php.ini
         done
-       rule tmpfs_configure
-       rule runit_sv_configure php5-fpm
-       rule runit_sv_restart   php5-fpm
+       rule runit_configure php5-fpm
  }
 rule_postfix_configure () {
        local hint="run vm_remote postfix_key_send before"
@@ -1002,13 +993,12 @@ rule_postfix_configure () {
         "$tool"/etc/postfix/$vm_domainname/virtual_alias \
                /etc/postfix/$vm_domainname/virtual_alias
        sudo postmap hash:/etc/postfix/$vm_domainname/virtual_alias
-       rule runit_sv_configure postfix
-       rule runit_sv_restart   postfix
+       rule runit_configure postfix
  }
 rule_postgresql_configure () {
  # DOC: http://wiki.postgresql.org/wiki/Shared_Database_Hosting
        rule apt_get_install postgresql-9.1
-       rule insserv_remove postgresql
+       rule insserv_remove  postgresql
        rule adduser postgres \
         --disabled-login \
         --disabled-password \
@@ -1041,7 +1031,6 @@ rule_postgresql_configure () {
         /home/postgresql/log \
         /home/postgresql/log/9.1 \
         /home/postgresql/log/9.1/main
-       rule tmpfs_configure
        if sudo test ! -d /home/postgresql/data
         then
                sudo install -d -m 750 -o postgres -g postgres \
@@ -1072,8 +1061,7 @@ rule_postgresql_configure () {
        sudo install -m 640 -o postgres -g postgres-data \
         "$tool"/etc/postgresql/9.1/main/postgresql.conf \
                /etc/postgresql/9.1/main/postgresql.conf
-       rule runit_sv_configure postgres
-       rule runit_sv_restart   postgres
+       rule runit_configure postgres
        while ! sudo -u postgres psql </dev/null
        do sleep 1; done
        # NOTE: supprime l'accès au schéma public depuis public,
@@ -1193,9 +1181,7 @@ rule_postgresql_db_user_add () { # SYNTAX: $db $user
 rule_postgrey_configure () {
        rule apt_get_install    postgrey
        rule insserv_remove     postgrey
-       rule tmpfs_configure
-       rule runit_sv_configure postgrey
-       rule runit_sv_restart   postgrey
+       rule runit_configure postgrey
  }
 rule_procmail_configure () {
        rule apt_get_install procmail
@@ -1208,39 +1194,31 @@ rule_procmail_configure () {
         "$tool"/etc/skel/etc/mail/delivery.procmailrc \
                /etc/skel/etc/mail/delivery.procmailrc
  }
-rule_runit_configure () { # SYNTAX: $service
+rule_runit_configure () { # SYNTAX: $sv
        rule apt_get_install runit
        local -; set +f
-       for sv in ${1-/etc/service/*}
-        # NOTE: stoppe les services en retenant leur status de départ
-        do sv=${sv##*/}
-               local sv_hash=$(printf %s "$sv" | sha1sum | cut -f 1 -d ' ')
-               local sv_status
-               IFS= read -r sv_status_$sv_hash <<-EOF
-                       $(sudo sv status "$sv" || true)
-                       EOF
-               sudo rm -f /etc/service/"$sv"
-        done
+       sudo find /etc/sv -mindepth 1 -maxdepth 1 -type d -name "${1:-*}" -exec \
+               /bin/sh -efux -c 'case $(sv stop "$1") in
+                (*": runsv not running") true;;
+                (*": unable to open supervise/ok: file does not exist") true;;
+                ("ok: down:"*) true;;
+                (*) false;;
+                esac' '' {} +
        set -$- ${1-"$tool"/etc/sv/*}
        while test -n "$*"
-        # NOTE: configure et (re-)démarre les services
         do local first=yes
                for sv in "$@"
                do sv=${sv##*/}
                        case $first in
                         (yes) shift $#; first=;;
                         esac
-                       local sv_hash=$(printf %s "$sv" | sha1sum | cut -f 1 -d ' ')
-                       rule runit_sv_configure "$sv"
-                       eval local sv_status=\"\${sv_status_$sv_hash-}\"
-                       case $sv_status in
-                        ("") true;;
-                        (run:*|fail:*) rule runit_sv_restart "$sv";;
-                        esac
+                       rule _runit_sv_configure "$sv"
+                       rule runit_sv_restart    "$sv"
                 done
         done
+       sudo find -L /etc/service -type l -delete
  }
-rule_runit_sv_configure () { # SYNTAX: $sv
+rule__runit_sv_configure () { # SYNTAX: $sv
        local sv="$1"
        sudo install -d -m 770 -o root -g root \
         /etc/sv/"$sv"
@@ -1267,7 +1245,6 @@ rule_runit_sv_configure () { # SYNTAX: $sv
  }
 rule_runit_sv_restart () { # SYNTAX: $sv
        local sv="$1"
-       rule runit_sv_configure "$sv"
        while true
         do case $(sudo sv restart "$sv") in
                 ("fail: $sv: runsv not running") sleep 1;;
@@ -1339,8 +1316,7 @@ rule_ssh_configure () {
        sudo install -m 644 -o root -g root \
         "$tool"/etc/ssh/ssh_config \
                /etc/ssh/ssh_config
-       rule runit_sv_configure sshd
-       rule runit_sv_restart   sshd
+       rule runit_configure sshd
  }
 rule_sysctl_configure () {
        local -; set +f
@@ -1367,11 +1343,6 @@ rule_tmpfs_configure () {
                TMP_SIZE=200m
                TMPFS_SIZE=20%VM
                EOF
-       sudo install -m 775 -o root -g root \
-        "$tool"/etc/init.d/tmpfs \
-               /etc/init.d/tmpfs
-       sudo update-rc.d tmpfs defaults
-       sudo service tmpfs restart
  }
 rule_time_configure () {
        sudo install -m 644 -o root -g root /dev/stdin /etc/timezone <<-EOF
@@ -1386,7 +1357,7 @@ rule_time_configure () {
  }
 rule_unbound_configure () {
        sudo apt-get install unbound
-       rule insserv_remove unbound
+       rule insserv_remove  unbound
        sudo install -m 644 -o root -g root /dev/stdin /etc/resolv.conf <<-EOF
                search ${vm_host#*.}
                nameserver 127.0.0.1
@@ -1400,8 +1371,7 @@ rule_unbound_configure () {
         <"$tool"/etc/unbound/unbound.conf |
        sudo install -m 440 -o unbound -g unbound /dev/stdin \
         /etc/unbound/unbound.conf
-       rule runit_sv_configure unbound
-       rule runit_sv_restart   unbound
+       rule runit_configure unbound
  }
 rule_user_add () { # SYNTAX: $user
        local user="$1"; shift