Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update docs, change create to write #34

Merged
merged 2 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

47 changes: 38 additions & 9 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 @@ -123,14 +130,24 @@ export function isActive(unitName: string, unit?: Unit) {
const type = getType(unitName, unit);
const name = getName(unitName);

return execSync(`systemctl is-active ${name}.${type}`).toString().trim() === "active";
try {
return execSync(`systemctl is-active ${name}.${type}`).toString().trim() === "active";
}
catch {
return false;
}
}

export function isEnabled(unitName: string, unit?: Unit) {
const type = getType(unitName, unit);
const name = getName(unitName);

return execSync(`systemctl is-enabled ${name}.${type}`).toString().trim() === "enabled";
try {
return execSync(`systemctl is-enabled ${name}.${type}`).toString().trim() === "enabled";
}
catch {
return false;
}
}

export function daemonReload() {
Expand Down Expand Up @@ -160,17 +177,29 @@ 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";
}

/**
* Enabled Unit will be started on next boot
*/
public enable() {
execSync(`systemctl enable ${this.name}.${this.type}`);
}
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