From 5cfb1ec46f88cc390ef53f4a9aa4445e8dd4593a Mon Sep 17 00:00:00 2001 From: chyzwar Date: Sat, 21 Sep 2024 22:34:34 +0200 Subject: [PATCH] feat(ctl,conf): update docs, chaneg create to write --- README.md | 9 +++- packages/conf/{Readme.md => README.md} | 42 +++++++-------- packages/ctl/README.md | 74 ++++++++++++++++++++++++++ packages/ctl/Readme.md | 71 ------------------------ packages/ctl/src/ctl.ts | 30 ++++++++--- packages/ctl/src/index.ts | 2 +- 6 files changed, 127 insertions(+), 101 deletions(-) rename packages/conf/{Readme.md => README.md} (75%) create mode 100644 packages/ctl/README.md delete mode 100644 packages/ctl/Readme.md diff --git a/README.md b/README.md index f6c1899..49cd9ad 100644 --- a/README.md +++ b/README.md @@ -166,6 +166,9 @@ import { Ctl } from "@systemd-js/ctl"; const ctl = new Ctl("test.service"); +ctl.isActive(); +ctl.isEnabled(); +ctl.write(); ctl.disable(); ctl.enable(); ctl.stop(); @@ -196,7 +199,7 @@ service const ctl = new Ctl("example", service); -ctl.create(); +ctl.write(); ctl.enable(); ctl.start(); ``` @@ -206,10 +209,14 @@ In addition to `Ctl` class, package expose functions to call systemctl directly. ```ts import { restart, start, stop } from "@systemd-js/ctl"; +write("example.service"); stop("example.service"); start("example.service"); enable("example.service"); disable("example.service"); reload("example.service"); restart("example.service"); +isActive("example.service"); +isEnabled("example.service"); +daemonReload(); ``` diff --git a/packages/conf/Readme.md b/packages/conf/README.md similarity index 75% rename from packages/conf/Readme.md rename to packages/conf/README.md index 8688f1c..a700c39 100644 --- a/packages/conf/Readme.md +++ b/packages/conf/README.md @@ -1,7 +1,7 @@ ## @systemd-js/conf -INI parser for systemd config files. Set of fluent builders to create and modify unit files. -Based on systemd v255.4 +INI parser for systemd config files. Set of fluent builders to create and modify +unit files. Based on systemd v255.4 ### Installation @@ -13,7 +13,8 @@ yarn add @systemd-js/conf Parse systemd ini file into object. -Note: Ini parser is not fully implemented, lacks support for escaping and quoting. +Note: Ini parser is not fully implemented, lacks support for escaping and +quoting. ```ts import {INI} from "@systemd-js/conf"; @@ -58,7 +59,6 @@ const ini = INI.fromString(unit).toObject(); User: "root", }, }; - ``` Create service unit for ini string and modify service user definition. @@ -82,12 +82,12 @@ PrivateTmp=yes User=root `; -const ini = INI.fromString(unit) -const service = Service.fromINI(ini) +const ini = INI.fromString(unit); +const service = Service.fromINI(ini); service .getServiceSection() - .setUser("test") + .setUser("test"); service.toINIString(); ``` @@ -95,33 +95,33 @@ service.toINIString(); Create service unit using fluent builder ```ts -import {Service} from "@systemd-js/config"; +import { Service } from "@systemd-js/config"; const service = new Service(); service .getUnitSection() - .setDescription("This is a example unit") + .setDescription("This is a example unit"); service .getInstallSection() - .setWantedBy("multi-user.target") - + .setWantedBy("multi-user.target"); + service - .getServiceSection() - .setType("simple") - .setWorkingDirectory("/tmp") - .setRestart("always") - .setExecStartPre("/usr/bin/echo 'Before'") - .setExecStart("/usr/bin/echo 'Hello World'") - .setExecStartPost("/usr/bin/echo 'After'") + .getServiceSection() + .setType("simple") + .setWorkingDirectory("/tmp") + .setRestart("always") + .setExecStartPre("/usr/bin/echo 'Before'") + .setExecStart("/usr/bin/echo 'Hello World'") + .setExecStartPost("/usr/bin/echo 'After'"); ``` Create timer unit using fluent builder ```ts -import {Timer} from "@systemd-js/config"; -const timer = new Timer() +import { Timer } from "@systemd-js/config"; +const timer = new Timer(); timer .getUnitSection() @@ -130,7 +130,7 @@ timer timer .getInstallSection() - .setWantedBy("multi-user.target"); + .setWantedBy("multi-user.target"); timer .getTimerSection() diff --git a/packages/ctl/README.md b/packages/ctl/README.md new file mode 100644 index 0000000..7c08e77 --- /dev/null +++ b/packages/ctl/README.md @@ -0,0 +1,74 @@ +## @systemd-js/ctl + +Control over units. Interface to systemctl. At the moment this lack proper error +handling. + +### Installation + +```sh +yarn add @systemd-js/ctl +``` + +### Examples + +State manipulation of existing service. + +```ts +import { Ctl } from "@systemd-js/ctl"; + +const ctl = new Ctl("test.service"); + +ctl.isActive(); +ctl.isEnabled(); +ctl.write(); +ctl.disable(); +ctl.enable(); +ctl.stop(); +ctl.start(); +ctl.restart(); +``` + +Creation of new service "example.service" + +```ts +import { Service } from "@systemd-js/config"; +import { Ctl } from "@systemd-js/ctl"; + +const service = new Service(); + +service + .getUnitSection() + .setDescription("This is a example unit"); + +service + .getInstallSection() + .setWantedBy("multi-user.target"); + +service + .getServiceSection() + .setType("simple") + .setExecStart("/usr/bin/echo 'Hello World'"); + +const ctl = new Ctl("example", service); + +ctl.write(); +ctl.enable(); +ctl.start(); +``` + +In addition to `Ctl` class, package expose functions to call systemctl directly. + +```ts +import { restart, start, stop } from "@systemd-js/ctl"; + +write("example.service"); +stop("example.service"); +start("example.service"); +enable("example.service"); +disable("example.service"); +reload("example.service"); +restart("example.service"); +isActive("example.service"); +isEnabled("example.service"); +daemonReload(); +``` diff --git a/packages/ctl/Readme.md b/packages/ctl/Readme.md deleted file mode 100644 index 1612c26..0000000 --- a/packages/ctl/Readme.md +++ /dev/null @@ -1,71 +0,0 @@ - -## @systemd-js/ctl - -Control over units. Interface to systemctl. -At the moment this lack proper error handling. - -### Installation - -```sh -yarn add @systemd-js/ctl -``` - -### Examples - -State manipulation of existing service. - -```ts -import {Ctl} from "@systemd-js/ctl"; - -const ctl = new Ctl("test.service") - -ctl.disable() -ctl.enable() -ctl.stop() -ctl.start() -ctl.restart() - -``` - -Creation of new service "example.service" - -```ts -import {Service} from "@systemd-js/config"; -import {Ctl} from "@systemd-js/ctl"; - -const service = new Service(); - -service - .getUnitSection() - .setDescription("This is a example unit") - -service - .getInstallSection() - .setWantedBy("multi-user.target") - -service - .getServiceSection() - .setType("simple") - .setExecStart("/usr/bin/echo 'Hello World'") - -const ctl = new Ctl("example", service) - -ctl.create() -ctl.enable() -ctl.start() - -``` - -In addition to `Ctl` class, package expose functions to call systemctl directly. - -```ts -import {restart, start, stop} from "@systemd-js/ctl"; - -stop("example.service") -start("example.service") -enable("example.service") -disable("example.service") -reload("example.service") -restart("example.service") - -``` diff --git a/packages/ctl/src/ctl.ts b/packages/ctl/src/ctl.ts index f675052..2d4f37f 100644 --- a/packages/ctl/src/ctl.ts +++ b/packages/ctl/src/ctl.ts @@ -57,9 +57,11 @@ function getUnit(unitName: string, type: string = getType(unitName)): Unit | und }; /** - * Create a unit file if it does not exist or if it is different from the current unit. + * Write unit file to filesystem. + * Create the unit file if it does not or update if it is different from the current unit. + * @returns "created" | "updated" | "unchanged" */ -export function create(unitName: string, unit: Unit) { +export function write(unitName: string, unit: Unit) { const name = getName(unitName); const type = getType(unitName, unit); const path = getPath (name, type); @@ -67,8 +69,13 @@ export function create(unitName: string, unit: Unit) { const current = getUnit(name, type); if (unit.equals(current)) { - writeFileSync(path, unit.toINIString()); + return "unchanged"; } + writeFileSync(path, unit.toINIString()); + + return current + ? "updated" + : "created"; } export function reload(unitName: string, unit?: Unit) { @@ -170,15 +177,24 @@ export class Ctl { } /** - * Create the unit file if it does not exist or if it is different from the current unit. + * Write unit file to filesystem. + * Create the unit file if it does not or update if it is different from the current unit. + * @returns "created" | "updated" | "unchanged" */ - public create() { + public write() { if (!this.unit) { throw new Error("Unit not found"); } - if (!this.unit.equals(this.current)) { - writeFileSync(this.path, this.unit.toINIString()); + + if (this.unit.equals(this.current)) { + return "unchanged"; } + + writeFileSync(this.path, this.unit.toINIString()); + + return this.current + ? "updated" + : "created"; } /** diff --git a/packages/ctl/src/index.ts b/packages/ctl/src/index.ts index 41b37fa..b8f100c 100644 --- a/packages/ctl/src/index.ts +++ b/packages/ctl/src/index.ts @@ -2,7 +2,7 @@ export { Ctl, reload, - create, + write, enable, disable, start,