Skip to content

Commit

Permalink
feat(ctl,conf): update docs, chaneg create to write
Browse files Browse the repository at this point in the history
  • Loading branch information
chyzwar committed Sep 21, 2024
1 parent ffa96cc commit 5cfb1ec
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 101 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -196,7 +199,7 @@ service

const ctl = new Ctl("example", service);

ctl.create();
ctl.write();
ctl.enable();
ctl.start();
```
Expand All @@ -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();
```
42 changes: 21 additions & 21 deletions packages/conf/Readme.md → packages/conf/README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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";
Expand Down Expand Up @@ -58,7 +59,6 @@ const ini = INI.fromString(unit).toObject();
User: "root",
},
};

```

Create service unit for ini string and modify service user definition.
Expand All @@ -82,46 +82,46 @@ 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();
```

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()
Expand All @@ -130,7 +130,7 @@ timer

timer
.getInstallSection()
.setWantedBy("multi-user.target");
.setWantedBy("multi-user.target");

timer
.getTimerSection()
Expand Down
74 changes: 74 additions & 0 deletions packages/ctl/README.md
Original file line number Diff line number Diff line change
@@ -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();
```
71 changes: 0 additions & 71 deletions packages/ctl/Readme.md

This file was deleted.

30 changes: 23 additions & 7 deletions packages/ctl/src/ctl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,25 @@ 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);

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) {
Expand Down Expand Up @@ -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";
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/ctl/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export {
Ctl,

reload,
create,
write,
enable,
disable,
start,
Expand Down

0 comments on commit 5cfb1ec

Please sign in to comment.