Skip to content

Commit

Permalink
Add support for template placeholders in recipes (openmediavault#1733)
Browse files Browse the repository at this point in the history
The following placeholders are supported:

- hostname()
- domain()
- fqdn()
- uid('<NAME>')
- gid('<NAME>')
- sharedfolder_path('<NAME>')
- config('<DB_OBJ_ID>', '<UUID>')

Examples:
```
apiVersion: helm.cattle.io/v1
kind: HelmChart
metadata:
  name: filebrowser
  namespace: filebrowser-app
spec:
  repo: https://utkuozdemir.org/helm-charts
  chart: filebrowser
  targetNamespace: filebrowser-app
  valuesContent: |-
    securityContext:
      runAsNonRoot: true
      runAsUser: {{ uid('filebrowser') }}
      runAsGroup: {{ gid("users") }}
    rootDir:
      type: hostPath
      hostPath:
        path: {{ sharedfolder_path('data') }}
```

```
args:
  - "--port 3000"
  - "--base /wetty"
  - "--force-ssh"
  - "--ssh-port {{ config('conf.service.ssh') | get('port') }}"
  - "--ssh-host={{ hostname }}"
```

Signed-off-by: Volker Theile <votdev@gmx.de>
  • Loading branch information
votdev committed Mar 25, 2024
1 parent 48a6b29 commit 42bee79
Show file tree
Hide file tree
Showing 23 changed files with 2,006 additions and 0 deletions.
12 changes: 12 additions & 0 deletions deb/openmediavault-k8s/debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
openmediavault-k8s (7.0.1-1) unstable; urgency=low

* Use Twig template engine to implement a DSL that allows users
to access data from the configuration database and other system
information inside their recipes.
Right at the moment the following functions are supported:
`hostname`, `domain`, `fqdn`, `uid`, `gid`, `sharedfolder_path`,
`config`. The following filters are added to those shipped with
Twig: `get`.

-- Volker Theile <volker.theile@openmediavault.org> Mon, 25 Mar 2024 19:44:24 +0100

openmediavault-k8s (7.0-6) unstable; urgency=low

* Initial release.
Expand Down
20 changes: 20 additions & 0 deletions deb/openmediavault-k8s/debian/copyright
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,23 @@ Source: http://www.openmediavault.org
Files: *
Copyright: 2009-2024 Volker Theile <volker.theile@openmediavault.org>
License: GPL-3

Files: usr/share/php/openmediavault/vendor/composer/*
Copyright: Copyright (c) Nils Adermann, Jordi Boggiano
License: MIT

Files: usr/share/php/openmediavault/vendor/symfony/polyfill-ctype/*
Copyright: 2018-present Fabien Potencier
License: MIT

Files: usr/share/php/openmediavault/vendor/symfony/polyfill-mbstring/*
Copyright: 2015-present Fabien Potencier
License: MIT

Files: usr/share/php/openmediavault/vendor/symfony/polyfill-php80/*
Copyright: 2020-present Fabien Potencier
License: MIT

Files: usr/share/php/openmediavault/vendor/twig/twig/*
Copyright: 2009-present by the Twig Team
License: BSD-3-Clause
1 change: 1 addition & 0 deletions deb/openmediavault-k8s/debian/openmediavault-k8s.install
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
etc/* etc/
srv/* srv/
usr/share/openmediavault/* usr/share/openmediavault/
usr/share/php/openmediavault/* usr/share/php/openmediavault/
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
namespace Engined\Rpc;

require_once("openmediavault/functions.inc");
require_once("openmediavault/vendor/autoload.php");

class OMVRpcServiceK8s extends \OMV\Rpc\ServiceAbstract {
private $recipesRepoDir = "/var/lib/openmediavault/k8s-recipes/";
Expand Down Expand Up @@ -213,6 +214,73 @@ class OMVRpcServiceK8s extends \OMV\Rpc\ServiceAbstract {
}

function applyRecipe($params, $context) {
// Replace placeholders in the recipe.
$db = \OMV\Config\Database::getInstance();
$functions = [
"hostname" => function() use($db) {
$obj = $db->get("conf.system.network.dns");
return $obj->get("hostname");
},
"domain" => function() use($db) {
$obj = $db->get("conf.system.network.dns");
return $obj->get("domainname");
},
"fqdn" => function() use($db) {
$obj = $db->get("conf.system.network.dns");
if (!$obj->isEmpty("domainname")) {
$result = sprintf("%s.%s", $obj->get("hostname"),
$obj->get("domainname"));
} else {
$result = $obj->get("hostname");
}
return $result;
},
"uid" => function($name) use($context) {
$result = \OMV\Rpc\Rpc::call("UserMgmt", "getUser", [
"name" => $name
], $context);
return $result['uid'];
},
"gid" => function($name) use($context) {
$result = \OMV\Rpc\Rpc::call("UserMgmt", "getGroup", [
"name" => $name
], $context);
return $result['gid'];
},
"sharedfolder_path" => function($name) use($db) {
$sfObject = $db->getByFilter("conf.system.sharedfolder", [
"operator" => "stringEquals",
"arg0" => "name",
"arg1" => $name
], 1);
$meObject = $db->get("conf.system.filesystem.mountpoint",
$sfObject->get("mntentref"));
return build_path(DIRECTORY_SEPARATOR, $meObject->get("dir"),
$sfObject->get("reldirpath"));
},
"config" => function($id, $uuid = NULL) use($db) {
return $db->getAssoc($id, $uuid);
}
];
$filters = [
"get" => function($value, $key, $default = NULL) {
$dict = new \OMV\Dictionary($value);
return $dict->get($key, $default);
}
];
$loader = new \Twig\Loader\ArrayLoader([
'manifest' => $params['manifest']
]);
$twig = new \Twig\Environment($loader);
foreach ($functions as $name => $callback) {
$function = new \Twig\TwigFunction($name, $callback);
$twig->addFunction($function);
}
foreach ($filters as $name => $callback) {
$filter = new \Twig\TwigFilter($name, $callback);
$twig->addFilter($filter);
}
$params['manifest'] = $twig->render("manifest");
return \OMV\Rpc\Rpc::call("Kubectl", "apply", $params, $context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"twig/twig": "^3.0"
}
}
Loading

0 comments on commit 42bee79

Please sign in to comment.