Skip to content

Commit bcbdcc2

Browse files
feat: Adds configuration variables for dispenser.toml (#15)
* feat: Adds support for variables in dispenser.toml * Closes #14 * chore: Adds CI unit testing
1 parent ebb28b1 commit bcbdcc2

File tree

15 files changed

+504
-83
lines changed

15 files changed

+504
-83
lines changed

.github/workflows/test.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
test:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- uses: actions/checkout@v4
17+
- name: Install Rust stable
18+
uses: dtolnay/rust-toolchain@stable
19+
- name: Run tests
20+
run: cargo test

Cargo.lock

Lines changed: 34 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "dispenser"
3-
version = "0.4.0"
3+
version = "0.5.0"
44
edition = "2021"
55
license = "MIT"
66

@@ -11,10 +11,12 @@ clap = { version = "4.5.18", features = ["derive"] }
1111
cron = { version = "0.15.0", features = ["serde"] }
1212
env_logger = "0.11.5"
1313
log = "0.4.22"
14+
minijinja = "2.12.0"
1415
rayon = "1.11.0"
1516
sd-notify = "0.4.2"
1617
serde = { version = "1.0.210", features = ["derive"] }
1718
serde_json = "1.0.128"
1819
signal-hook = "0.3.17"
20+
thiserror = "2.0.17"
1921
toml = "0.8.19"
2022
urlencoding = "2.1.3"

README.md

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ Download the latest `.deb` or `.rpm` package from the [releases page](https://gi
2727

2828
```sh
2929
# Download the .deb package
30-
# wget https://github.com/ixpantia/dispenser/releases/download/v0.3.0/dispenser-0.3-0.x86_64.deb
30+
# wget https://github.com/ixpantia/dispenser/releases/download/v0.5.0/dispenser-0.5-0.x86_64.deb
3131

32-
sudo apt install ./dispenser-0.3-0.x86_64.deb
32+
sudo apt install ./dispenser-0.5-0.x86_64.deb
3333
```
3434

3535
### RHEL / CentOS / Fedora
@@ -38,7 +38,7 @@ sudo apt install ./dispenser-0.3-0.x86_64.deb
3838
# Download the .rpm package
3939
# wget ...
4040

41-
sudo dnf install ./dispenser-0.3-0.x86_64.rpm
41+
sudo dnf install ./dispenser-0.5-0.x86_64.rpm
4242
```
4343

4444
The installation process will:
@@ -170,7 +170,63 @@ initialize = "on-trigger"
170170
```
171171
In this example, the service defined in the `backup-service` directory will only be started when the cron schedule is met. After its first run, it will continue to be managed by its cron schedule.
172172

173-
### Step 6: Start and Verify the Deployment
173+
### Step 6: Using Variables (Optional)
174+
175+
Dispenser supports using variables in your configuration file via `dispenser.vars`. This file allows you to define values that can be reused inside `dispenser.toml` using `{{ VARIABLE }}` syntax.
176+
177+
This is useful for reusing the same configuration in multiple deployments.
178+
179+
1. Create a `dispenser.vars` file in `/opt/dispenser`.
180+
181+
```sh
182+
vim dispenser.vars
183+
```
184+
185+
2. Define your variables in TOML format.
186+
187+
```toml
188+
registry_url = "ghcr.io"
189+
app_version = "latest"
190+
```
191+
192+
3. Use these variables in your `dispenser.toml`.
193+
194+
```toml
195+
[[instance]]
196+
path = "my-app"
197+
images = [{ registry = "{{ registry_url }}", name = "my-org/my-app", tag = "{{ app_version }}" }]
198+
```
199+
200+
### Step 7: Validating Configuration
201+
202+
Before applying changes, you can validate your configuration files (including variable substitution) to ensure there are no syntax errors or missing variables.
203+
204+
Run dispenser with the `--test` (or `-t`) flag:
205+
206+
```sh
207+
dispenser --test
208+
```
209+
210+
If the configuration is valid, it will output:
211+
```
212+
Dispenser config is ok.
213+
```
214+
215+
If there's an error `dispenser` will show you a detailed error message.
216+
217+
```
218+
---------------------------------- <string> -----------------------------------
219+
2 |
220+
3 | [[instance]]
221+
4 | path = "nginx"
222+
5 > images = [{ registry = "{{ missing }}", name = "nginx", tag = "latest" }]
223+
i ^^^^^^^ undefined value
224+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
225+
No referenced variables
226+
-------------------------------------------------------------------------------
227+
```
228+
229+
### Step 8: Start and Verify the Deployment
174230
175231
1. Exit the `dispenser` user session to return to your regular user.
176232
```sh

deb/DEBIAN/control

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Package: dispenser
2-
Version: 0.4
2+
Version: 0.5
33
Maintainer: ixpantia S.A.
44
Architecture: amd64
55
Description: Continously Deploy services with Docker Compose

example/dispenser.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ delay = 60
44
path = "nginx"
55
# This service will be started immediately on startup, which is the default behavior.
66
# It will be updated if a new 'nginx:latest' image is detected.
7-
images = [{ registry = "docker.io", name = "nginx", tag = "latest" }]
7+
images = [{ registry = "{{docker_io}}", name = "nginx", tag = "latest" }]
88

99
[[instance]]
1010
path = "hello-world"

example/dispenser.vars

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
docker_io="docker.io"

justfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# justfile for dispenser project
22

3-
DISPENSER_VERSION := "0.4"
3+
DISPENSER_VERSION := "0.5"
44
TARGET_BIN := "target/x86_64-unknown-linux-musl/release/dispenser"
55
USR_BIN_DEB := "deb/usr/local/bin/dispenser"
66
USR_BIN_RPM := "rpm/usr/local/bin/dispenser"

rpm/dispenser.spec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Name: dispenser
2-
Version: 0.4
2+
Version: 0.5
33
Release: 0
44
Summary: Continously Deploy services with Docker Compose
55
License: see /usr/share/doc/dispenser/copyright

src/cli.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ pub struct Args {
99
/// Path to the config file.
1010
#[arg(short, long, default_value = "dispenser.toml")]
1111
pub config: PathBuf,
12+
/// Path to the vars file.
13+
#[arg(short, long, default_value = "dispenser.vars")]
14+
pub vars: PathBuf,
15+
16+
/// Test the configuration file and exit.
17+
#[arg(short, long)]
18+
pub test: bool,
1219
}
1320

1421
static ARGS: OnceLock<Args> = OnceLock::new();

0 commit comments

Comments
 (0)