nut-server.init (3436B)
1 #!/bin/sh /etc/rc.common 2 # Copyright © 2012 OpenWrt.org 3 # 4 # This is free software, licensed under the GNU General Public License v2. 5 # See /LICENSE for more information. 6 # 7 START=50 8 9 RUN_D=/var/run 10 PID_F=$RUN_D/upsd.pid 11 UPS_C=/var/etc/nut/ups.conf 12 USERS_C=/var/etc/nut/upsd.users 13 UPSD_C=/var/etc/nut/upsd.conf 14 15 USE_PROCD=1 16 17 listen_address() { 18 local cfg="$1" 19 20 config_get address "$cfg" address "::1" 21 config_get port "$cfg" port 22 echo "LISTEN $address $port" >>$UPSD_C 23 } 24 25 upsd_statepath() { 26 local cfg="$1" 27 config_get statepath "$cfg" statepath 28 } 29 30 upsd_config() { 31 local cfg="$1" 32 local maxage maxconn certfile 33 34 # Note runas support requires you make sure USB device file is readable by 35 # the runas user 36 config_get runas "$cfg" runas 37 38 config_get maxage "$cfg" maxage 39 [ -n "$maxage" ] && echo "MAXAGE $maxage" >>$UPSD_C 40 41 config_get statepath "$cfg" statepath 42 [ -n "$statepath" ] && echo "STATEPATH $statepath" >>$UPSD_C 43 44 config_get maxconn "$cfg" maxconn 45 [ -n "$maxconn" ] && echo "MAXCONN $maxconn" >>$UPSD_C 46 47 #NOTE: certs only apply to SSL-enabled version 48 config_get certfile "$cfg" certfile 49 [ -n "$certfile" ] && echo "CERTFILE $certfile" >>$UPSD_C 50 } 51 52 nut_user_add() { 53 local cfg="$1" 54 local a 55 local val 56 57 config_get val "$cfg" username "$1" 58 echo "[$val]" >> $USERS_C 59 60 config_get val "$cfg" password 61 echo " password = $val" >> $USERS_C 62 63 config_get val "$cfg" actions 64 for a in $val; do 65 echo " actions = $a" >> $USERS_C 66 done 67 68 instcmd() { 69 local val="$1" 70 echo " instcmds = $val" >> $USERS_C 71 } 72 73 config_list_foreach "$cfg" instcmd instcmd 74 75 config_get val "$cfg" upsmon 76 if [ -n "$val" ]; then 77 echo " upsmon $val" >> $USERS_C 78 fi 79 } 80 81 start_service() { 82 local runas statepath 83 84 mkdir -p /var/etc/nut 85 chmod -R 750 /var/etc/nut 86 87 rm -f $UPSD_C 88 rm -f $USERS_C 89 rm -f $UPSD_C 90 rm -f /var/etc/nut/nut.conf 91 92 echo "# Config file automatically generated from UCI config" > $UPS_C 93 echo "# Config file automatically generated from UCI config" > $USERS_C 94 echo "# Config file automatically generated from UCI config" > $UPSD_C 95 96 local in_driver have_drivers 97 config_cb() { 98 if [ "$1" != "driver" ]; then 99 in_driver= 100 else 101 echo "[$2]" >> $UPS_C 102 in_driver=true 103 have_drivers=true 104 fi 105 } 106 option_cb() { 107 if [ "$in_driver" = "true" ]; then 108 echo " $1 = $2" >> $UPS_C 109 fi 110 } 111 112 config_load nut_server 113 114 config_foreach nut_user_add user 115 config_foreach upsd_config upsd 116 config_foreach listen_address listen_address 117 118 echo "MODE=netserver" >>/var/etc/nut/nut.conf 119 120 chmod 0640 $USERS_C 121 chmod 0640 $UPS_C 122 chmod 0640 $UPSD_C 123 chmod 0640 /var/etc/nut/nut.conf 124 [ -d "${statepath:-/var/run/nut}" ] || { 125 mkdir -m 0750 -p "${statepath:-/var/run/nut}" 126 chown $runas:$(id -gn $runas) "${statepath:-/var/run/nut}" 127 } 128 129 if [ -n "$runas" ]; then 130 chown -R $runas:$(id -gn $runas) /var/etc/nut 131 fi 132 133 if [ "$have_drivers" = "true" ]; then 134 $DEBUG /usr/sbin/upsd ${runas:+-u $runas} $OPTIONS 135 $DEBUG /usr/sbin/upsdrvctl ${runas:+-u $runas} start 136 fi 137 } 138 139 140 nut_driver_stop() { 141 local cfg="$1" 142 local driver 143 144 config_get driver "$cfg" driver 145 146 [ -r ${statepath:-/var/run/nut}/$driver-$cfg ] && /usr/sbin/upsdrvctl stop $cfg 147 } 148 149 stop_service() { 150 [ -r $PID_F ] && /usr/sbin/upsd -c stop 151 config_load ups 152 config_foreach upsd_statepath upsd 153 config_foreach nut_driver_stop driver 154 } 155 156 reload_service() { 157 upsd -c reload 158 }