Skip to content

Commit

Permalink
Add FreeBSD service
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesús Daniel Colmenares Oviedo authored and Jesús Daniel Colmenares Oviedo committed Jul 25, 2022
1 parent 20f8c51 commit 740ee63
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 0 deletions.
46 changes: 46 additions & 0 deletions contrib/FreeBSD/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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 may be changed with the variable `sopel_profiles` in `/etc/rc.conf`. For example:

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

The configuration files should be `${sopel_confdir}/${sopel_prefix}profile1.cfg`, `${sopel_confdir}/${sopel_prefix}profile2.cfg` and `${sopel_confdir}/${sopel_prefix}profile3.cfg`. The variable `sopel_confdir` is the default configuration directory (by default `/usr/local/etc`). `sopel_prefix`is the prefix for all files (by default `sopel-`).

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

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

The default configuration file must be installed in ${sopel_confdir}. 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"
```

If you want to change the python version, the variable `sopel_interpreter` (by default `/usr/local/bin/python3.9`) must be changed.

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}
```
120 changes: 120 additions & 0 deletions contrib/FreeBSD/sopel
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
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 @@
#
# 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
#
[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

0 comments on commit 740ee63

Please sign in to comment.