Fix bug in retry handling: if all retries fail then error is swallowed

Moved to using a decorator to add the retry - should be clearer what is happening.

I had to make some changes to the retry decorator, which I've pushed upstream to https://github.com/saltycrane/retry-decorator
Back out change to retry handling, as Fred has fixed another way
Remove the file from that last commit
Merge remote-tracking branch 'upstream/master'
Remove the debian symlink for the removed file now..
Merge remote-tracking branch 'upstream/master'
Use of "bc" in calculating the BAT_GOOD value was wrong - scale default to 2, and so the division was being rounded down to the nearest integer.

In the worst case this meant BAT_GOOD and BAT_LOW were the same values which doens't work well.

pi@raspberrypi ~/mopi/pi-tronics/simbamon $ cc_wrap() { calculate_config $* ; ST=`supply_type $SUPPLY_TYPE` ; NC=`num_cells $NUM_CELLS`; BC=`bat_chemistry $CHEMISTRY`; echo "ST $ST; NC $NC; BC $BC; BAT_FLOOR $BAT_FLOOR; BAT_LOW $BAT_LOW; BAT_GOOD $BAT_GOOD; BAT_FULL $BAT_FULL"; }
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ . simbamond.default
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ cc_wrap 1 8 2
ST "Rechargeable batteries"; NC "Eight cells (e.g. eight AAs)"; BC "NiMH"; BAT_FLOOR 8.0; BAT_LOW 8.8; BAT_GOOD 9.8; BAT_FULL 11.2
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ cc_wrap 2 6 3
ST "Non-rechargeable batteries"; NC "Six cells (e.g. a 9V PP3, or 12V car)"; BC "Alkaline"; BAT_FLOOR 4.8; BAT_LOW 5.2; BAT_GOOD 7.2; BAT_FULL 9.6
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ cc_wrap 1 2 6
ST "Rechargeable batteries"; NC "Two cells (e.g. double 3.6V LiPo)"; BC "LiPo"; BAT_FLOOR 6.0; BAT_LOW 6.6; BAT_GOOD 6.6; BAT_FULL 8.4
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ vi simbamond.default
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ . simbamond.default
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ cc_wrap 1 8 2
ST "Rechargeable batteries"; NC "Eight cells (e.g. eight AAs)"; BC "NiMH"; BAT_FLOOR 8.0; BAT_LOW 8.8; BAT_GOOD 10.000; BAT_FULL 11.2
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ cc_wrap 2 6 3
ST "Non-rechargeable batteries"; NC "Six cells (e.g. a 9V PP3, or 12V car)"; BC "Alkaline"; BAT_FLOOR 4.8; BAT_LOW 5.2; BAT_GOOD 7.400; BAT_FULL 9.6
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ cc_wrap 1 2 6
ST "Rechargeable batteries"; NC "Two cells (e.g. double 3.6V LiPo)"; BC "LiPo"; BAT_FLOOR 6.0; BAT_LOW 6.6; BAT_GOOD 7.500; BAT_FULL 8.4
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
Fix simbamond.init handling

See below for details of what is being fixed, and before+after test logs

1) First log without any changes, see some problems:
- start doesn't check if daemon already running (and so we get processes we can't control)
- [errors on shutdown - as simbamon kills itself but leaves the PID file, and simbamond.init errors if the process isn't there or if the PID file isn't there]
- if a process won't respond to SIGTERM (eg. due to being halted) then stopping it fails.

Anthony-Tooles-MacBook-Pro:Flickr Walk Through anthony$ ssh pi@192.168.1.12
pi@192.168.1.12's password:
Linux raspberrypi 3.12.20+ #687 PREEMPT Fri May 30 16:39:11 BST 2014 armv6l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Wed Jun 11 12:23:35 2014 from 192.168.1.5
pi@raspberrypi ~ $
pi@raspberrypi ~ $
pi@raspberrypi ~ $ ps aux | grep simba
root      2077  0.4  0.6   2944   784 ?        S    12:25   0:00 /bin/bash /usr/sbin/simbamon
root      2112  0.0  0.4   1696   560 ?        Ss   12:25   0:00 startpar -f -- simbamond
pi        2239  0.0  0.6   3552   852 pts/0    S+   12:26   0:00 grep --color=auto simba
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond status
[ ok ] simbamon is running.
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond stop
[....] simbamon:: stopping...pi@raspberrypi ~ $
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond status
[FAIL] simbamon is not running ... failed!
pi@raspberrypi ~ $ ps aux | grep simba
pi        2333  0.0  0.6   3548   800 pts/0    S+   12:26   0:00 grep --color=auto simba
pi@raspberrypi ~ $
pi@raspberrypi ~ $
pi@raspberrypi ~ $ # stopping works.
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond start
[ ok ] Started simbamon.
pi@raspberrypi ~ $
pi@raspberrypi ~ $
pi@raspberrypi ~ $ ps aux | grep simba
root      2353  0.6  0.6   4476   804 pts/0    S    12:27   0:00 /bin/bash /usr/sbin/simbamon
pi        2389  0.0  0.6   3548   800 pts/0    S+   12:27   0:00 grep --color=auto simba
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond status
[ ok ] simbamon is running.
pi@raspberrypi ~ $ # starting works
pi@raspberrypi ~ $
pi@raspberrypi ~ $
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond restart
[ ok ] Restarting SimBaMon: a simple battery monitor daemon: simbamon[....] simbamon:: stopping...[....] Started simbamon.
. ok
pi@raspberrypi ~ $
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond status
[ ok ] simbamon is running.
pi@raspberrypi ~ $ ps aux | grep simba
root      2467  0.5  0.6   4476   804 pts/0    S    12:27   0:00 /bin/bash /usr/sbin/simbamon
pi        2554  0.0  0.6   3548   800 pts/0    S+   12:27   0:00 grep --color=auto simba
pi@raspberrypi ~ $ # restart did what it should
pi@raspberrypi ~ $
pi@raspberrypi ~ $
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond start
[ ok ] Started simbamon.
pi@raspberrypi ~ $
pi@raspberrypi ~ $ ps aux | grep simba
root      2467  0.5  0.6   4476   804 pts/0    S    12:27   0:00 /bin/bash /usr/sbin/simbamon
root      2597  0.5  0.6   4476   804 pts/0    S    12:27   0:00 /bin/bash /usr/sbin/simbamon
pi        2641  0.0  0.6   3548   800 pts/0    S+   12:27   0:00 grep --color=auto simba
pi@raspberrypi ~ $ # not good: now got two processes, and one we won't be able to stop
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond status
[ ok ] simbamon is running.
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond stop
[....] simbamon:: stopping...pi@raspberrypi ~ $ close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr

pi@raspberrypi ~ $
pi@raspberrypi ~ $ ps aux | grep simba
root      2467  0.4  0.6   4476   804 pts/0    S    12:27   0:00 /bin/bash /usr/sbin/simbamon
pi        2769  0.0  0.6   3548   800 pts/0    S+   12:28   0:00 grep --color=auto simba
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond status
[ ok ] simbamon is running.
pi@raspberrypi ~ $
pi@raspberrypi ~ $
pi@raspberrypi ~ $ # so we've stopped the last one we started, but can't touch the first started one - yet status knows it is there
pi@raspberrypi ~ $
pi@raspberrypi ~ $
pi@raspberrypi ~ $ sudo kill -9 2467
pi@raspberrypi ~ $
pi@raspberrypi ~ $ ps aux | grep simba
pi        2858  0.0  0.6   3548   800 pts/0    S+   12:28   0:00 grep --color=auto simba
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond start
[ ok ] Started simbamon.
pi@raspberrypi ~ $ ps aux | grep simba
root      2878  0.3  0.6   4476   804 pts/0    S    12:28   0:00 /bin/bash /usr/sbin/simbamon
pi        2911  0.0  0.6   3548   800 pts/0    S+   12:28   0:00 grep --color=auto simba
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond status
[ ok ] simbamon is running.
pi@raspberrypi ~ $
pi@raspberrypi ~ $
pi@raspberrypi ~ $ # back to normal
pi@raspberrypi ~ $
pi@raspberrypi ~ $
pi@raspberrypi ~ $ kill -s SIGSTOP 2878
-bash: kill: (2878) - Operation not permitted
pi@raspberrypi ~ $ sudo kill -s SIGSTOP 2878
pi@raspberrypi ~ $ ps aux | grep simba
root      2878  0.4  0.6   4476   804 pts/0    T    12:28   0:00 /bin/bash /usr/sbin/simbamon
pi        2975  0.0  0.6   3548   800 pts/0    S+   12:29   0:00 grep --color=auto simba
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond status
[ ok ] simbamon is running.
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond stop
[....] simbamon:: stopping...pi@raspberrypi ~ $
pi@raspberrypi ~ $
pi@raspberrypi ~ $ # so that appeared to work
pi@raspberrypi ~ $ ps aux | grep simba
root      2878  0.2  0.6   4476   804 pts/0    T    12:28   0:00 /bin/bash /usr/sbin/simbamon
pi        3022  0.0  0.6   3548   800 pts/0    S+   12:29   0:00 grep --color=auto simba
pi@raspberrypi ~ $ # but did nothing
pi@raspberrypi ~ $
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond status
[ ok ] simbamon is running.
pi@raspberrypi ~ $
pi@raspberrypi ~ $
pi@raspberrypi ~ $ kill -sSIGCONT 2878
-bash: kill: sSIGCONT: invalid signal specification
pi@raspberrypi ~ $ sudo kill -s SIGCONT 2878
pi@raspberrypi ~ $ # should have now handled the SIGTERM
pi@raspberrypi ~ $ ps aux | grep simba
pi        3055  0.0  0.6   3548   800 pts/0    S+   12:30   0:00 grep --color=auto simba
pi@raspberrypi ~ $ sudo /etc/init.d/simbamond status
[FAIL] simbamon is not running ... failed!
pi@raspberrypi ~ $

2) Now with the fixes:
- cannot start more than one process
- a SIGHALT'd process will now be correctly killed
- see no errors on system shutdown

pi@raspberrypi ~/mopi/pi-tronics/simbamon $ ps aux | grep simba
root      2065  0.4  0.6   2944   784 ?        S    13:13   0:01 /bin/bash /usr/sbin/simbamon
root      2105  0.0  0.4   1696   560 ?        Ss   13:13   0:00 startpar -f -- simbamond
pi        2804  0.0  0.6   3552   852 pts/0    S+   13:19   0:00 grep --color=auto simba
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond status
[ ok ] simbamon is running.
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond stop
[ ok ] simbamon:: stopping...[....] simbamon stopped.
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond status
[FAIL] simbamon is not running ... failed!
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ ps aux | grep simba
pi        2922  0.0  0.6   3548   800 pts/0    S+   13:19   0:00 grep --color=auto simba
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ # stopping works
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond start
[ ok ] Started simbamon.
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ ps aux | grep simba
root      2942  0.4  0.6   4476   804 pts/0    S    13:19   0:00 /bin/bash /usr/sbin/simbamon
pi        2998  0.0  0.6   3548   800 pts/0    S+   13:20   0:00 grep --color=auto simba
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond status
[ ok ] simbamon is running.
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ # starting works
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond restart
[ ok ] Restarting SimBaMon: a simple battery monitor daemon: simbamon[....] simbamon:: stopping...[....] simbamon stopped.
[ ok ] Started simbamon.
. ok
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond status
[ ok ] simbamon is running.
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ ps aux | grep simba
root      3112  0.4  0.6   4476   804 pts/0    S    13:20   0:00 /bin/bash /usr/sbin/simbamon
pi        3226  0.0  0.6   3548   800 pts/0    S+   13:20   0:00 grep --color=auto simba
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ # restart works
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond start
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ ps aux | grep simba
root      3112  0.4  0.6   4476   804 pts/0    S    13:20   0:00 /bin/bash /usr/sbin/simbamon
pi        3273  0.0  0.6   3548   800 pts/0    S+   13:21   0:00 grep --color=auto simba
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond status
[ ok ] simbamon is running.
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ cat /var/run/simbamond.pid
3112
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ # good - start no longer starts a second process, and existing process looks fine
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond stop
[ ok ] simbamon:: stopping...[....] simbamon stopped.
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ ps aux | grep simba
pi        3427  0.0  0.6   3548   800 pts/0    S+   13:22   0:00 grep --color=auto simba
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond status
[FAIL] simbamon is not running ... failed!
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ # can still stop the process, and status is right
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond start
[ ok ] Started simbamon.
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ ps aux | grep simba
root      3477  0.6  0.6   4476   804 pts/0    S    13:22   0:00 /bin/bash /usr/sbin/simbamon
pi        3513  0.0  0.6   3548   800 pts/0    S+   13:22   0:00 grep --color=auto simba
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond status
[ ok ] simbamon is running.
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ # back to normal, now lets SIGHALT daemon
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ kill -s SIGSTOP 3477
-bash: kill: (3477) - Operation not permitted
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo kill -s SIGSTOP 3477
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ ps aux | grep simba
root      3477  0.4  0.6   4476   804 pts/0    T    13:22   0:00 /bin/bash /usr/sbin/simbamon
pi        3591  0.0  0.6   3548   800 pts/0    S+   13:23   0:00 grep --color=auto simba
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond status
[ ok ] simbamon is running.
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond stop
[ ok ] simbamon:: stopping...[....] simbamon stopped.
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ # that took a while, presumably 30s + then SIGKILL did the job
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ ps aux | grep simba
pi        3658  0.0  0.6   3548   800 pts/0    S+   13:24   0:00 grep --color=auto simba
pi@raspberrypi ~/mopi/pi-tronics/simbamon $ sudo /etc/init.d/simbamond status
[FAIL] simbamon is not running ... failed!
pi@raspberrypi ~/mopi/pi-tronics/simbamon $
Merge pull request #4 from stoduk/master

Fix dameon handling and BAT_GOOD calculation