diff --git a/openwisp-config/files/openwisp.init b/openwisp-config/files/openwisp.init index 509acab..cd49606 100755 --- a/openwisp-config/files/openwisp.init +++ b/openwisp-config/files/openwisp.init @@ -1,129 +1,95 @@ #!/bin/sh /etc/rc.common START=99 -STOP=15 + USE_PROCD=1 PROG="/usr/sbin/openwisp_config" -PROG_NAME="OpenWISP config agent" +CONTROL_FILE=/tmp/openwisp/applying_conf + +add_option() { + local cfg="$1" + local flag="$2" + local option="$3" + local default="$4" + local value + + config_get value "$cfg" "$option" "$default" + [ -n "$value" ] && procd_append_param command "$flag" "$value" +} + +parse_config() { + local cfg="$1" + + add_option "$cfg" "--url" url + add_option "$cfg" "--interval" interval + add_option "$cfg" "--verify-ssl" verify_ssl + add_option "$cfg" "--uuid" uuid + add_option "$cfg" "--key" key + add_option "$cfg" "--shared-secret" shared_secret + add_option "$cfg" "--consistent-key" consistent_key + add_option "$cfg" "--hardware-id-script" hardware_id_script + add_option "$cfg" "--hardware-id-key" hardware_id_key + add_option "$cfg" "--bootup-delay" bootup_delay + add_option "$cfg" "--merge-config" merge_config + add_option "$cfg" "--test-config" test_config + add_option "$cfg" "--test-retries" test_retries + add_option "$cfg" "--test-script" test_script + add_option "$cfg" "--connect-timeout" connect_timeout + add_option "$cfg" "--max-time" max_time + add_option "$cfg" "--capath" capath + add_option "$cfg" "--cacert" cacert + add_option "$cfg" "--mac-interface" mac_interface + add_option "$cfg" "--management-interface" management_interface + add_option "$cfg" "--default-hostname" default_hostname + add_option "$cfg" "--pre-reload-hook" pre_reload_hook + add_option "$cfg" "--post-reload-hook" post_reload_hook + add_option "$cfg" "--post-reload-delay" post_reload_delay + add_option "$cfg" "--post-registration-hook" post_registration_hook +} start_service() { config_load openwisp - url=$(config_get http url) - interval=$(config_get http interval) - verify_ssl=$(config_get http verify_ssl) - uuid=$(config_get http uuid) - key=$(config_get http key) - shared_secret=$(config_get http shared_secret) - consistent_key=$(config_get http consistent_key) - hardware_id_script=$(config_get http hardware_id_script) - hardware_id_key=$(config_get http hardware_id_key) - bootup_delay=$(config_get http bootup_delay) - unmanaged=$(config_get http unmanaged) - merge_config=$(config_get http merge_config) - test_config=$(config_get http test_config) - test_retries=$(config_get http test_retries) - test_script=$(config_get http test_script) - connect_timeout=$(config_get http connect_timeout) - max_time=$(config_get http max_time) - capath=$(config_get http capath) - cacert=$(config_get http cacert) - mac_interface=$(config_get http mac_interface) - management_interface=$(config_get http management_interface) - default_hostname=$(config_get http default_hostname) - pre_reload_hook=$(config_get http pre_reload_hook) - post_reload_hook=$(config_get http post_reload_hook) - post_reload_delay=$(config_get http post_reload_delay) - post_registration_hook=$(config_get http post_registration_hook) + respawn_threshold=$(config_get http respawn_threshold) respawn_timeout=$(config_get http respawn_timeout) respawn_retry=$(config_get http respawn_retry) - if [ $url ]; then url="--url $url"; fi - if [ $interval ]; then interval="--interval $interval"; fi - if [ $verify_ssl ]; then verify_ssl="--verify-ssl $verify_ssl"; fi - if [ $uuid ]; then uuid="--uuid $uuid"; fi - if [ $key ]; then key="--key $key"; fi - if [ $shared_secret ]; then shared_secret="--shared-secret $shared_secret"; fi - if [ $consistent_key ]; then consistent_key="--consistent-key $consistent_key"; fi - if [ $hardware_id_script ]; then hardware_id_script="--hardware-id-script $hardware_id_script"; fi - if [ $hardware_id_key ]; then hardware_id_key="--hardware-id-key $hardware_id_key"; fi - if [ $bootup_delay ]; then bootup_delay="--bootup-delay $bootup_delay"; fi + + # @todo This should be changed to a list element. + # So that we no longer need this workaround. And so we could also use + # the new add_option function. However, the agent must also be changed! + unmanaged=$(config_get http unmanaged) if [ -n "$unmanaged" ]; then # replace spaces with commas to avoid problems when # passing this arg to procd_set_param command unmanaged=$(echo $unmanaged | tr ' ' ',') unmanaged="--unmanaged $unmanaged"; fi - if [ $merge_config ]; then merge_config="--merge-config $merge_config"; fi - if [ $test_config ]; then test_config="--test-config $test_config"; fi - if [ $test_retries ]; then test_retries="--test-retries $test_retries"; fi - if [ $test_script ]; then test_script="--test-script $test_script"; fi - if [ $connect_timeout ]; then connect_timeout="--connect-timeout $connect_timeout"; fi - if [ $max_time ]; then max_time="--max-time $max_time"; fi - if [ $capath ]; then capath="--capath $capath"; fi - if [ $cacert ]; then cacert="--cacert $cacert"; fi - if [ $mac_interface ]; then mac_interface="--mac-interface $mac_interface"; fi - if [ $management_interface ]; then management_interface="--management-interface $management_interface"; fi - if [ $default_hostname ]; then default_hostname="--default-hostname $default_hostname"; fi - if [ $pre_reload_hook ]; then pre_reload_hook="--pre-reload-hook $pre_reload_hook"; fi - if [ $post_reload_hook ]; then post_reload_hook="--post-reload-hook $post_reload_hook"; fi - if [ $post_reload_delay ]; then post_reload_delay="--post-reload-delay $post_reload_delay"; fi - if [ $post_registration_hook ]; then post_registration_hook="--post-registration-hook $post_registration_hook"; fi - - if [ -z "$url" ]; then - logger -s "url is not set, please add it in /etc/config/openwisp" \ - -t openwisp \ - -p daemon.err - exit 1 - fi - - if ([ -z "$uuid" ] || [ -z "$key" ]) && [ -z "$shared_secret" ]; then - logger -s "you must either set uuid and key, or shared_secret in /etc/config/openwisp" \ - -t openwisp \ - -p daemon.err - exit 1 - fi procd_open_instance - procd_set_param command $PROG $url $interval $verify_ssl $uuid $key $shared_secret \ - $consistent_key $hardware_id_script $hardware_id_key \ - $bootup_delay $unmanaged $merge_config $test_config \ - $test_retries $test_script $connect_timeout $max_time $capath \ - $cacert $mac_interface $management_interface $default_hostname \ - $pre_reload_hook $post_reload_hook $post_reload_delay \ - $post_registration_hook + procd_set_param command $PROG $unmanaged + + config_foreach parse_config controller + procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5} procd_close_instance - logger -s "$PROG_NAME started" \ - -t openwisp \ - -p daemon.info } service_triggers() { procd_add_reload_trigger openwisp } -stop_service() { - logger -s "$PROG_NAME stopping" \ - -t openwisp \ - -p daemon.info -} - reload_service() { - logger -s "$PROG_NAME received reload trigger" \ - -t openwisp \ - -p daemon.info - local control_file=/tmp/openwisp/applying_conf # avoid reloading while configuration is being applied # will wait for a maximum of 30 seconds for i in $(seq 1 30); do - if [ -f $control_file ]; then + if [ -f "$CONTROL_FILE" ]; then sleep 1 else break fi done - # ensure control file is removed - rm $control_file - # reload + + rm -f "$CONTROL_FILE" + start }