Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FreeBSD service #2333

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions contrib/FreeBSD/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
This folder contains `sopel` and `sopel-default.cfg` designed to be distributed by third parties such as FreeBSD.

`sopel-default.cfg` is a default configuration file for sopel.

The -default prefix is mandatory and will be used as a 'profile'.

The profiles started by the service may be changed with the variable `sopel_profiles` in `/etc/rc.conf`, or with the `sysrc` command:

```sh
sysrc sopel_profiles="profile1 profile2 profile3"
```

By default, the configuration directory is `/usr/local/etc`.

For the above example, the configuration files `sopel-profile1.cfg`, `sopel-profile2.cfg`, and `sopel-profile3.cfg` must be present there.

The service must be installed in `/usr/local/etc/rc.d`:

```sh
cp sopel.sh /usr/local/etc/rc.d/sopel
chmod +x /usr/local/etc/rc.d/sopel
```

The default configuration file must be installed in the configuration directory as another profile. For example:

```sh
cp sopel-default.cfg /usr/local/etc
```

If you want to run sopel at startup, the variable `sopel_enable` must be set to `YES`:

```sh
sysrc sopel_enable="YES"
```

Now, you can run the service with the `service(8)` command, but sopel needs to be configured first. You can configure it with `service sopel configure`. When sopel is configured, it can be started:

```sh
service sopel start
```

If you want to change the python version, the variable `sopel_interpreter` must be changed. For example:
half-duplex marked this conversation as resolved.
Show resolved Hide resolved

```sh
sysrc sopel_interpreter="/usr/local/bin/python3.8"
```

You can set any other variable, but by default, the sopel service has many default values:

```sh
$ egrep -E '^: \$\{sopel_.+:=.+}' sopel
: ${sopel_enable:="NO"}
: ${sopel_piddir:="/var/run/sopel"}
: ${sopel_confdir:=/usr/local/etc}
: ${sopel_flags:=--config-dir "${sopel_confdir}"}
: ${sopel_program:="/usr/local/bin/sopel"}
: ${sopel_user:=${name}}
: ${sopel_interpreter:=/usr/local/bin/python3.9}
: ${sopel_profiles:=default}
: ${sopel_prefix:=sopel-}
: ${sopel_output:=/dev/null}
```
26 changes: 26 additions & 0 deletions contrib/FreeBSD/sopel-default.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#
DtxdF marked this conversation as resolved.
Show resolved Hide resolved
# IMPORTANT NOTES!
# You must delete the not_configured line in order for the bot to work,
# otherwise it will refuse to start.
#
# You must create homedir. Sopel should create the others directories:
# mkdir -p $homedir
# The service file has a variable called 'sopel_user'. This user name
# must exist. After creating the user, the owner of the logdir,
# pid_dir and homedir directories must be changed. By default,
# the value is 'sopel':
# chown sopel:sopel $logdir
# chown sopel:sopel $pid_dir
# chown sopel:sopel $homedir
half-duplex marked this conversation as resolved.
Show resolved Hide resolved
#
[core]
nick=sopel
not_configured=True
host=irc.libera.chat
port=6697
use_ssl=True
verify_ssl=True
owner=
logdir=/var/log/sopel
pid_dir=/var/run/sopel
homedir=/var/db/sopel
120 changes: 120 additions & 0 deletions contrib/FreeBSD/sopel.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#!/bin/sh

# PROVIDE: sopel
# REQUIRE: FILESYSTEM NETWORKING usr

. /etc/rc.subr

name="sopel"
desc="Simple, easy-to-use, open-source IRC utility bot, written in Python"
rcvar="${name}_enable"
start_cmd="sopel_start"
stop_cmd="sopel_stop"
restart_cmd="sopel_restart"
status_cmd="sopel_status"
configure_cmd="sopel_configure"
extra_commands="configure"

load_rc_config "${name}"

: ${sopel_enable:="NO"}
: ${sopel_piddir:="/var/run/sopel"}
: ${sopel_confdir:=/usr/local/etc}
: ${sopel_flags:=--config-dir "${sopel_confdir}"}
: ${sopel_program:="/usr/local/bin/sopel"}
: ${sopel_user:=${name}}
: ${sopel_interpreter:=/usr/local/bin/python3.9}
: ${sopel_profiles:=default}
: ${sopel_prefix:=sopel-}
: ${sopel_output:=/dev/null}

sopel_start()
{
local profile

profile="$1"; shift

echo "Starting sopel profile '${profile}'." && sleep 1
/usr/sbin/daemon \
-o "${sopel_output}" \
-t "${desc}" \
-u "${sopel_user}" \
${sopel_interpreter} \
${sopel_program} start ${sopel_flags} \
-c "${sopel_prefix}${profile}" $@
}

sopel_stop()
{
local pid pidfile profile

profile="$1"; shift

pidfile="${sopel_piddir}/${sopel_prefix}${sopel_prefix}${profile}.pid"
if ! [ -f "${pidfile}" ]; then
return 1
fi

pid=`cat ${pidfile}`

echo "Stopping sopel profile '${profile}'."
/usr/sbin/daemon \
-o "${sopel_output}" \
${sopel_interpreter} \
${sopel_program} stop ${sopel_flags} \
-c "${sopel_prefix}${profile}" $@

wait_for_pids $pid
}

sopel_restart()
{
local profile

profile="$1"; shift

run_rc_command stop "${profile}" $@ &&
sleep 1 &&
run_rc_command start "${profile}" $@
}

sopel_status()
{
local profile pid

profile="$1"; shift

pid=`check_pidfile \
"${sopel_piddir}/${sopel_prefix}${sopel_prefix}${profile}.pid" \
"${sopel_program}" \
"${sopel_interpreter}"`

if [ -n "${pid}" ]; then
echo "Sopel profile '${profile}' is running as pid ${pid}."
else
echo "Sopel profile '${profile}' is not running."
fi
}

sopel_configure()
{
local profile

profile="$1"; shift

echo "Configuring profile '${profile}'..."

${sopel_interpreter} \
${sopel_program} configure ${sopel_flags} \
-c "${sopel_confdir}/${sopel_prefix}${profile}" $@
}

cmd="$1"; shift
for profile in $sopel_profiles; do
if ! [ -f "${sopel_confdir}/${sopel_prefix}${profile}.cfg" ]; then
echo "Sopel profile '${profile}' does not exist."
continue
fi

run_rc_command "${cmd}" "${profile}" $@
done