Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 494640 - net-nntp/sabnzbd - sabnzbd.initd: re-work stop() procedure and general tidy-ups
Summary: net-nntp/sabnzbd - sabnzbd.initd: re-work stop() procedure and general tidy-ups
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: All Linux
: Normal normal (vote)
Assignee: Justin Bronder (RETIRED)
URL:
Whiteboard:
Keywords: PATCH
Depends on:
Blocks:
 
Reported: 2013-12-18 11:42 UTC by eponymous
Modified: 2014-11-20 13:34 UTC (History)
0 users

See Also:
Package list:
Runtime testing required: ---


Attachments
sabnzbd init script - improved shutdown routine. (sabnzbd.initd,2.25 KB, text/plain)
2013-12-18 11:43 UTC, eponymous
Details
sabnzbd init script - improved shutdown routine. (patch against 0.7.16 ebuild) (sabnzbd.initd.patch,974 bytes, patch)
2013-12-18 11:44 UTC, eponymous
Details | Diff
sabnzbd init script - improved shutdown routine. (sabnzbd.initd,2.21 KB, text/plain)
2013-12-18 11:47 UTC, eponymous
Details
sabnzbd init script - improved shutdown routine. (patch against 0.7.16 ebuild) (sabnzbd.initd.patch,1.00 KB, patch)
2013-12-18 11:47 UTC, eponymous
Details | Diff
sabnzbd init script - improved shutdown routine. (patch against 0.7.16 ebuild) (sabnzbd.initd.patch,1.05 KB, patch)
2013-12-18 22:07 UTC, eponymous
Details | Diff
Diff against revision 1.7 of initd script. (sabnzbd.initd.patch,4.27 KB, patch)
2013-12-29 22:05 UTC, eponymous
Details | Diff
Diff against revision 1.7 of initd script. (sabnzbd.initd.patch,4.27 KB, patch)
2013-12-29 22:20 UTC, eponymous
Details | Diff
Diff against revision 1.7 of initd script. (sabnzbd.initd.patch,4.15 KB, patch)
2013-12-29 22:49 UTC, eponymous
Details | Diff
Diff against revision 1.7 of initd script. (sabnzbd.initd.patch,4.14 KB, patch)
2013-12-29 23:11 UTC, eponymous
Details | Diff
Diff against revision 1.7 of initd script. (sabnzbd.initd.patch,4.16 KB, patch)
2013-12-29 23:30 UTC, eponymous
Details | Diff
Diff against revision 1.7 of initd script - reworked. (sabnzbd.initd.patch,3.91 KB, patch)
2014-06-08 20:48 UTC, eponymous
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description eponymous 2013-12-18 11:42:14 UTC
Currently, the SABnzbd init script doesn't check if the API is enabled before trying to attempt a web-based shutdown. This is less than ideal.

The procedure should be re-worked to check this first then gracefully fall back to the normal SIGTERM method used by most start-stop-daemon scripts.
Comment 1 eponymous 2013-12-18 11:43:48 UTC
Created attachment 365582 [details]
sabnzbd init script - improved shutdown routine.

Improved shutdown routine and also removed spurious tab characters.
Comment 2 eponymous 2013-12-18 11:44:20 UTC
Created attachment 365584 [details, diff]
sabnzbd init script - improved shutdown routine. (patch against 0.7.16 ebuild)
Comment 3 eponymous 2013-12-18 11:47:06 UTC
Created attachment 365586 [details]
sabnzbd init script - improved shutdown routine.

Removed unnecessary status message.
Comment 4 eponymous 2013-12-18 11:47:33 UTC
Created attachment 365588 [details, diff]
sabnzbd init script - improved shutdown routine. (patch against 0.7.16 ebuild)

Removed unnecessary status message.
Comment 5 Jeroen Roovers (RETIRED) gentoo-dev 2013-12-18 15:30:31 UTC
Comment on attachment 365586 [details]
sabnzbd init script - improved shutdown routine.

--- files/sabnzbd.initd 2013-10-20 16:21:18.028447574 +0200
+++ -   2013-12-18 16:29:19.814976658 +0100
@@ -82,14 +82,18 @@
     local pidfile=$(get_pidfile)
     local rc=1
 
-    ebegin "Stopping SABnzbd @ ${addr}"
+    # This can only work if we have enabled the API
+    if [ -n "${api_key}" ]; then
 
-    # SABnzbd will return "ok" if shutdown is successful
-    rc=$(/usr/bin/curl -k -s "${addr}/sabnzbd/api?mode=shutdown&apikey=${api_key}")
-    if [ "${rc}" == "ok" ]; then
-               rc=0
-       else
-        einfo "Falling back to SIGTERM, this may not work if you restarted via the web interface"
+        einfo "Attempting web-based shutdown @ ${addr}"
+
+        # SABnzbd will return "ok" if shutdown is successful
+        rc=$(/usr/bin/curl -k -s "${addr}/sabnzbd/api?mode=shutdown&apikey=${api_key}")
+        [[ "${rc}" == "ok" ]] && rc=0
+    fi
+
+    if [ "${rc}" -eq 1 ]; then
+        einfo "Shutting down via SIGTERM"
         start-stop-daemon \
             --stop \
             --pidfile ${pidfile} \
Comment 6 eponymous 2013-12-18 22:07:17 UTC
Created attachment 365628 [details, diff]
sabnzbd init script - improved shutdown routine. (patch against 0.7.16 ebuild)

Also need to check that "disable_api_key" is not set when we have an api key present as this will still not allow us to perform an API shutdown call.
Comment 7 eponymous 2013-12-19 22:27:57 UTC
I'm going to go through this script and ensure I'm being consistent with my coding style throughout :) I think there are a few improvements I can make.
Comment 8 eponymous 2013-12-24 10:46:10 UTC
I should get this finished soon - little bit behind with it being Christmas and all :)
Comment 9 eponymous 2013-12-29 22:05:16 UTC
Created attachment 366486 [details, diff]
Diff against revision 1.7 of initd script.

+ Fixed indenting to more widely used 2-space.
+ Removed spurious tab characters.
+ Re-worked shutdown proc to be more robust.
+ Proper use of quoting in line with BASH good-practices.
Comment 10 eponymous 2013-12-29 22:20:26 UTC
Created attachment 366488 [details, diff]
Diff against revision 1.7 of initd script.

+ Fixed missed out variable.
Comment 11 eponymous 2013-12-29 22:49:27 UTC
Created attachment 366498 [details, diff]
Diff against revision 1.7 of initd script.

+ Removed unecessary variables.
Comment 12 eponymous 2013-12-29 23:11:08 UTC
Created attachment 366500 [details, diff]
Diff against revision 1.7 of initd script.

+ Fixed "any-address" comparison.
Comment 13 eponymous 2013-12-29 23:30:07 UTC
Created attachment 366504 [details, diff]
Diff against revision 1.7 of initd script.

+ Further tidy-ups.
Comment 14 Justin Bronder (RETIRED) gentoo-dev 2013-12-30 16:23:34 UTC
(In reply to eponymous from comment #13)
> Created attachment 366504 [details, diff] [details, diff]
> Diff against revision 1.7 of initd script.
> 
> + Further tidy-ups.

All set for me to review this now or do you have more pending changes?
Comment 15 eponymous 2013-12-30 18:21:07 UTC
(In reply to Justin Bronder from comment #14)
> (In reply to eponymous from comment #13)
> > Created attachment 366504 [details, diff] [details, diff] [details, diff]
> > Diff against revision 1.7 of initd script.
> > 
> > + Further tidy-ups.
> 
> All set for me to review this now or do you have more pending changes?

Hehe all done :)

Yep if you could please then I can get any review actions done asap.

Cheers!
Comment 16 Justin Bronder (RETIRED) gentoo-dev 2014-02-10 15:48:34 UTC
Alright.  It seems the meat of this diff is just to check for the api_key and to make sure it wasn't disabled.  Is there anything else that is functionally changed?

For the other changes:
    * Spacing, if changed at all should be to tabs to be consistent with Gentoo style guides.
    * I'm not sure I see the point of changing "if/else" blocks to use "test && statement".  I have no real preference, but changing all of this just seems like churn that could introduce bugs.
    * Similar to above, the same goes for changing from using local variables to save the output of function calls against using them inline.  In start_pre(), you're actually uselessly calling get_pidfile over and over for no gain.

I'm happy to put the other changes in if you have a strong preference but let's fix the actual bug in the init script first so the changelog is clean.
Comment 17 eponymous 2014-06-08 20:47:32 UTC
>> Alright.  It seems the meat of this diff is just to check for the api_key and to make sure it wasn't disabled.  Is there anything else that is functionally changed?

Yes that was the main reason for this patch. Everything else is just stylistic changes to make things consistent.

>>     * Spacing, if changed at all should be to tabs to be consistent with Gentoo style guides.

Sorry about that, Vim hiccup. I changed my default tab spacing to be two characters rather than four - not sure why. Fixed now.

>>     * I'm not sure I see the point of changing "if/else" blocks to use "test && statement".  I have no real preference, but changing all of this just seems like churn that could introduce bugs.

This was simply to avoid unecessarily long bash if, then, else, fi blocks - they're ugly for small tests & sets.

>>     * Similar to above, the same goes for changing from using local variables to save the output of function calls against using them inline.  In start_pre(), you're actually uselessly calling get_pidfile over and over for no gain.

Yep that was indeed silly. Good spot - I've fixed this now.
Comment 18 eponymous 2014-06-08 20:48:20 UTC
Created attachment 378530 [details, diff]
Diff against revision 1.7 of initd script - reworked.
Comment 19 Justin Bronder (RETIRED) gentoo-dev 2014-06-19 16:52:25 UTC
  19 Jun 2014; Justin Bronder <jsbronder@gentoo.org> files/sabnzbd.initd:
  Check that api is not disabled before attempting to use it for shutdown.
  Thanks to eponymous, #494640
Comment 20 eponymous 2014-10-22 17:28:41 UTC
I've just noticed that we seem to have an issue with the init script you've added to the repository in that it doesn't match the diff attached to this bug.

For example, take this line:

-        --user ${SABNZBD_USER} \
-        --group ${SABNZBD_GROUP} \
+        --user "${SABNZBD_USER}" \
+        --group "${SABNZBD_GROUP}" \

But in the initd file we have:

47         --user ${SABNZBD_USER} \
48         --group ${SABNZBD_GROUP} \

Are you sure you've applied the diff correctly?
Comment 21 Justin Bronder (RETIRED) gentoo-dev 2014-10-22 18:55:49 UTC
(In reply to eponymous from comment #20)
> I've just noticed that we seem to have an issue with the init script you've
> added to the repository in that it doesn't match the diff attached to this
> bug.
> 
> For example, take this line:
> 
> -        --user ${SABNZBD_USER} \
> -        --group ${SABNZBD_GROUP} \
> +        --user "${SABNZBD_USER}" \
> +        --group "${SABNZBD_GROUP}" \
> 
> But in the initd file we have:
> 
> 47         --user ${SABNZBD_USER} \
> 48         --group ${SABNZBD_GROUP} \
> 
> Are you sure you've applied the diff correctly?

I handpicked the relevant parts that were not stylistic.

You can see the diff here.

http://sources.gentoo.org/cgi-bin/viewvc.cgi/gentoo-x86/net-nntp/sabnzbd/files/sabnzbd.initd?r1=1.7&r2=1.8

However, for the lines you picked, that should have been applied.  Are you sure you have the latest ebuild?
Comment 22 Justin Bronder (RETIRED) gentoo-dev 2014-10-22 18:56:43 UTC
(In reply to Justin Bronder from comment #21)
> (In reply to eponymous from comment #20)
> 
> However, for the lines you picked, that should have been applied.  Are you
> sure you have the latest ebuild?

Whoops, I take that back.  Quotes were not added there.  Are you using users/groups with spaces?
Comment 23 eponymous 2014-10-24 15:27:29 UTC
So now I'm confused. I agree that *some* of my changes were stylistic but we should be consistent throughout the file.

One recommended approach to variable use is to surround them with quotes and braces which I've adopted:
"${VAR_NAME}" and we should either have that convention throughout the entire file or not, rather than a mix of two. I don't mind really.

I've just checked: /usr/portage/net-nntp/sabnzbd/files/sabnzbd.initd again after an emerge --sync and the inconsistency still exists.
Comment 24 Justin Bronder (RETIRED) gentoo-dev 2014-10-24 16:06:39 UTC
(In reply to eponymous from comment #23)
> So now I'm confused. I agree that *some* of my changes were stylistic but we
> should be consistent throughout the file.
> 
> One recommended approach to variable use is to surround them with quotes and
> braces which I've adopted:
> "${VAR_NAME}" and we should either have that convention throughout the
> entire file or not, rather than a mix of two. I don't mind really.
> 
> I've just checked: /usr/portage/net-nntp/sabnzbd/files/sabnzbd.initd again
> after an emerge --sync and the inconsistency still exists.

That's a good rule for paths, but I don't think it makes much sense for things that cannot reasonably have spaces in them.  Bash also doesn't need quoting when performing assignment [1].

My personal preference is to keep diffs as closely tied to a specific functional change as possible.  This makes is far easier to debug months later when I've forgotten why changes were made in the first place.


1. 
$ b=$(echo some thing)
$ a=$b
$ echo $a
some thing
Comment 25 eponymous 2014-11-20 13:34:25 UTC
> That's a good rule for paths, but I don't think it makes much sense for
> things that cannot reasonably have spaces in them.  Bash also doesn't need
> quoting when performing assignment [1].

There are other instances where it is useful such as if you want to delimit where a variable name ends in a joined string.


> My personal preference is to keep diffs as closely tied to a specific
> functional change as possible.  This makes is far easier to debug months
> later when I've forgotten why changes were made in the first place.

Yes that's reasonable.

I'd say we should probably just leave it as is then, since in hindsight, it's probably not worth re-opening the bug and committing a new init script for stylistic changes.