Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 915687 - www-servers/caddy Include 'reload' in init.d
Summary: www-servers/caddy Include 'reload' in init.d
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: All Linux
: Normal enhancement (vote)
Assignee: Zac Medico
URL:
Whiteboard:
Keywords: PullRequest
Depends on:
Blocks:
 
Reported: 2023-10-13 14:53 UTC by Forza
Modified: 2023-10-30 02:07 UTC (History)
4 users (show)

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


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Forza 2023-10-13 14:53:52 UTC
It would be useful to have a 'reload' function in the init.d script.

Reproducible: Always




I think that the config file should be moved to its own variable to make a reload function work.

caddy reload --config "${config_file}"
Comment 1 Rahil Bhimjiani 2023-10-14 14:06:13 UTC
I added it in PR. Anyone with openrc please try it out and report back if it is working as intended.
Comment 2 Forza 2023-10-14 20:06:05 UTC
(In reply to Rahil Bhimjiani from comment #1)
> I added it in PR. Anyone with openrc please try it out and report back if it
> is working as intended.

Thanks. 

I made some adjustments. What do you think? 

# diff -u initd-2.7.5.1 initd-2.7.5.2
--- initd-2.7.5.1       2023-10-14 21:34:21.666613022 +0200
+++ initd-2.7.5.2       2023-10-14 22:01:16.583562342 +0200
@@ -1,13 +1,15 @@
 #!/sbin/openrc-run
+# shellcheck shell=bash
 description="Caddy web server"
 pidfile=${pidfile:-"/run/${RC_SVCNAME}.pid"}
 command="/usr/bin/caddy"
 command_user="${command_user:-"http:http"}"
-caddy_config="/etc/caddy/Caddyfile"
+caddy_config="${caddy_config:-"/etc/caddy/Caddyfile"}"
 command_args="${command_args:-"run --config ${caddy_config}"}"
 command_background="true"
-logfile=/var/log/${RC_SVCNAME}/${RC_SVCNAME}.log
+extra_started_commands="reload"
+logfile="${logfile:-"/var/log/${RC_SVCNAME}/${RC_SVCNAME}.log"}"
 start_stop_daemon_args="--user ${command_user%:*} --group ${command_user#*:}
        --stdout ${logfile} --stderr ${logfile}"
Comment 3 Rahil Bhimjiani 2023-10-14 20:11:21 UTC
Oops. Totally missed that caddy_config bug. Thanks for pointing out. Is providing logfile in conf.d a common practice? 

and can you elaborate on what does "extra_started_commands" do? I'm on phone so cand dig into openrc efficiently now
Comment 4 Forza 2023-10-14 20:36:26 UTC
(In reply to Rahil Bhimjiani from comment #3)
> Oops. Totally missed that caddy_config bug. Thanks for pointing out. Is
> providing logfile in conf.d a common practice? 

I don't know if it is best practice or not. Personally I wanted to set the logfile to the same directory as I keep other caddy per-domain access logs. 

> 
> and can you elaborate on what does "extra_started_commands" do? I'm on phone
> so cand dig into openrc efficiently now

extra_started_commands specifies what extra functions() that are defined. In this case we want extra_started_commands="reload" so that the reload function becomes available.
Comment 5 Rahil Bhimjiani 2023-10-15 01:09:00 UTC
Done. Mimicked the behaviour from openssh's initd. Forza, recheck the PR and field-test it please.
Comment 6 Forza 2023-10-15 08:28:25 UTC
(In reply to Rahil Bhimjiani from comment #5)
> Done. Mimicked the behaviour from openssh's initd. Forza, recheck the PR and
> field-test it please.

Looks good. I like that you added checkconfig.

I do get tge following outout on the console when running it:

rc-service caddy reload
{"level":"warn","ts":1697358267.8459482,"msg":"unable to determine directory for user configuration; falling back to current directory","error":"neither $XDG_CONFIG_HOME nor
$HOME are defined"}
{"level":"info","ts":1697358267.8470907,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":""}
{"level":"info","ts":1697358267.8813837,"msg":"redirected default logger","from":"stderr","to":"/var/log/caddy/caddy_main.log"}
 * Reloading caddy ...
2023/10/15 08:24:27.941 WARN    unable to determine directory for user configuration; falling back to current directory        {"error": "neither $XDG_CONFIG_HOME nor $HOME are defined"}
2023/10/15 08:24:27.942 INFO    using provided configuration    {"config_file": "/etc/caddy/Caddyfile", "config_adapter": ""}                                           [ ok ]



In conf.d, I think that caddy_config should be above command_args, so that the variable is set before used in command_args?
Comment 7 Forza 2023-10-15 09:09:39 UTC
I realise that the warning about $HOME isn't new. But maybe we should silence it with "2>&1 >/dev/null" ?
Comment 8 Rahil Bhimjiani 2023-10-15 10:05:33 UTC
> In conf.d, I think that caddy_config should be above command_args, so that the variable is set before used in command_args?

It's all commented out but fixed it anyway.

> But maybe we should silence it with "2>&1 >/dev/null" ?

I tried `caddy reload` & `caddy validate` with `> /dev/null` but it doesn't totally suppress it. If you find a way let me know.
Comment 9 Rahil Bhimjiani 2023-10-15 10:07:32 UTC
Also added an error when checkconfig fails
Comment 10 Forza 2023-10-15 11:33:09 UTC
This should work:

checkconfig() {
    if [ ! -f "${caddy_config}" ] ; then
        ewarn "${caddy_config} does not exist."
        return 1
    fi
    "${command}" validate --config "${caddy_config}" >> "${logfile}" 2>&1
}

...


reload() {
    if ! service_started "${SVCNAME}" ; then
        eerror "${SVCNAME} isn't running"
        return 1
    fi
    checkconfig || return 1
    ebegin "Reloading ${SVCNAME}"
    "${command}" reload --force --config "${caddy_config}"  >> "${logfile}" 2>&1
    eend $?
}

Instead of ">> ${logfile} 2>&1" you can also do "> /dev/null 2>&1"
Comment 11 Rahil Bhimjiani 2023-10-16 06:21:32 UTC
Improved. It should work now as you wanted, Forza.
Comment 12 Larry the Git Cow gentoo-dev 2023-10-30 02:07:57 UTC
The bug has been closed via the following commit(s):

https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=dcf6809eba95a9070a2768f60e1ba5f5a448d994

commit dcf6809eba95a9070a2768f60e1ba5f5a448d994
Author:     Rahil Bhimjiani <rahil3108@gmail.com>
AuthorDate: 2023-10-12 06:10:49 +0000
Commit:     Zac Medico <zmedico@gentoo.org>
CommitDate: 2023-10-30 02:07:51 +0000

    www-servers/caddy: add 2.7.5 & update live
    
    1. split out LICENSE
    2. Install Caddyfile as upstream intended.
    3. show correct version on `caddy version`
    4. misc improvements in initd & confd like validate config file, reload,
    add variables for config file & logfile
    
    Closes: https://bugs.gentoo.org/915687
    Signed-off-by: Rahil Bhimjiani <rahil3108@gmail.com>
    Signed-off-by: Zac Medico <zmedico@gentoo.org>

 www-servers/caddy/Manifest                         |  3 +
 www-servers/caddy/caddy-2.7.5.ebuild               | 98 ++++++++++++++++++++++
 www-servers/caddy/caddy-9999.ebuild                | 28 +++++--
 www-servers/caddy/files/confd-2.7.5                |  5 ++
 www-servers/caddy/files/initd-2.7.5                | 53 ++++++++++++
 .../remove-binary-altering-commands-2.7.5.patch    | 61 ++++++++++++++
 6 files changed, 240 insertions(+), 8 deletions(-)