DEV Community

Alexey Melezhik
Alexey Melezhik

Posted on

2

Developing a simple Sparrow plugin with Raku and Bash

Sparrow is very efficient in writing scripts to automate system management using Raku and Bash, in today's post I am going to show an example ...


Deploy configuration file and restart service upon changes

Let's create a simple plugin that enable / disable some configuration flags in service configuration file and if any changes occur reload a service. Format of flags in configuration file is following:

var=true|false
Enter fullscreen mode Exit fullscreen mode

Here is an example of configuration file:

config.txt

debug=true
sentry=false
tls=false
Enter fullscreen mode Exit fullscreen mode

Usually services are managed as systemd units, so service reload could be used to reload service configuration.

β€”β€”

Let’s get started and create root folder for the plugin:

#!bash

mkdir -p service-config-reload
cd service-config-reload
Enter fullscreen mode Exit fullscreen mode

Now inside plugin root directory:

hook.raku:

#!raku

my $cfg-orig = config()<path>.IO.slurp();

my $cfg = $cfg-orig;

for config()<enable><> -> $f {
   $cfg ~~ s/<?wb> "$f=" \S+ /$f=true/;
}

for config()<disable><> -> $f {
   $cfg ~~ s/<?wb> "$f=" \S+ /$f=false/;
}

set_stdout($cfg);

if $cfg ne $cfg-orig {
   set_stdout("config changed");
   config()<path>.IO.spurt($cfg);
   set_stdout("config updated");
   run_task "service_restart", %(
       service => config()<service>,
       path => config()<path>,
   );
}
Enter fullscreen mode Exit fullscreen mode
mkdir -p tasks/service_restart/
Enter fullscreen mode Exit fullscreen mode

tasks/service_restart/task.bash

#!bash

sudo service $service reload
Enter fullscreen mode Exit fullscreen mode

In real life applications services might have a linter capability where before configuration is applied it's checked and if any error occur application of changes is terminated:

#!bash

set -e
sudo /usr/bin/$service --check $path
sudo service $service reload
Enter fullscreen mode Exit fullscreen mode

Publish plugin

Packaging scenario as a plugin will allow users to reuse it as a library.

sparrow.json

{
  "name" : "service-config-reload",
  "description" : "Deploy configuration file and restart service upon changes",
  "version" : "0.0.1",
  "category" : "linux",
  "url" : "https://github.com/melezhik/sparrow-plugins/tree/master/service-config-reload"
}
Enter fullscreen mode Exit fullscreen mode

This last command will upload plugin to local Sparrow repository (see this doc for more details)

#!bash

s6 --upload
Enter fullscreen mode Exit fullscreen mode

Use of plugins

Anywhere in Raku scenario, just do this:

#!raku

use Sparrow6::DSL;

task-run "apply and reload", "service-config-reload", %(
   :path<config.txt>,
   :service<app>,
   enable => <tls sentry>, # enable TLS and use of Sentry for production
   disable => <debug>, # disable debug mode for production 
)
Enter fullscreen mode Exit fullscreen mode

Thanks for reading!

PS

Source code of the plugin could be found here - https://github.com/melezhik/sparrow-plugins/tree/master/service-config-reload

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

πŸ‘‹ Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay