svgen

crates.io docs.rs

A small utility to generate runit services from templates.

Usage

This tool looks for service templates in the directory /etc/svgen/templates/ and instantiates them using instance descriptions in /etc/svgen/instances. A template is a directory containing at least a run file and optionally finish and log files. Each line in the instances file must be of the form <template-name>@<instance-name>. Instantiation means that the three files from the template are read, the string __INSTANCE__ is replaced with the instance name and then a new service directory under /etc/sv/generated/<instance-line> is created and the new files are written to run, finish and log/run respectively.

Example

In this example we build a template for [wg-quick], a simple program to setup wireguard VPN connections. This is a good example, since we usually have a bunch of VPN interfaces we want to set up and all the corresponding runit services would look nearly identical. Create a new directory /etc/svgen/templates/wg-quick/ and put the following into the run file:

```shell

!/bin/sh

wg-quick up "INSTANCE" exec chpst -b "wg-quick@INSTANCE" pause ```

The __INSTANCE__ string will get replaced upon instantiation of this template and takes the role of the interface name. In order to tear-down the network when the service is stopped, we also need a finish script:

```shell

!/bin/sh

wg-quick down "INSTANCE" ```

Now, let's imagine you have configured two wireguard networks, wg-net1 and wg-net2. In order to create services for them create the following /etc/svgen/instances file:

```

wireguard

wg-quick@wg-net1 wg-quick@wg-net2 ```

Now run bash svgen

This will generate two services under /etc/sv/generated/ and symlink to them from /service/.

Installation

To build the program, run bash cargo build --release

and afterwards run as root: bash ./install.sh

Full operational description

This program generates services from templates and instances in /etc/svgen. Templates are stored in /etc/svgen/templates/ and instances are configured in the file /etc/svgen/instances. The templates directory contains subdirectories that must contain a run file and optional finish and log files. The instances file contains lines of the format <template-name>@<instance-name>.

This program reads the instance file line by line and for each line searches the corresponding template directory. If found, it will read in the run and finish files and then does:

  1. generate a new service directory under /etc/sv/generated named <template-name>@<instance-name>
  2. replace all occourences of __INSTANCE__ in the run and finish scripts and write the output to corresponding files in the service directory
  3. create a new supervise symlink to a subdirectory of /run/runit
  4. create a log directory in the service directory
  5. symlink the service directory in /service/ using an absolute path

If the service directory already exists, it is skipped.

In addition, for each service directory that is found in /etc/sv/generated/ that has no corresponding instance in the instances file, it does: 1. stop the corresponding service and wait for it to finish (sv stop || sv kill) 2. remove the symlink in /service/ 3. remove the service directory and all of it's contents in /etc/sv/generated/

Empty lines and lines starting with a # in the instances file are ignored.

License

This project is licensed under the GNU AGPL version 3 or later, see the LICENSE file for more information.