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

chore(root): document ctl package #17

Merged
merged 4 commits into from
May 27, 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
71 changes: 71 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,74 @@ timer
.setOnCalendar("daily")
.setUnit("postgresql-dump.target");
```

## @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")

```
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"name": "@systemd/root",
"version": "1.0.0",
"engines": {
"node": "20.x",
"yarn": "4.2.2"
"node": "^20.0.0"
},
"type": "module",
"workspaces": [
Expand Down
13 changes: 6 additions & 7 deletions packages/conf/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
"name": "@systemd-js/conf",
"version": "0.2.0",
"engines": {
"node": "20.x",
"yarn": "4.2.2"
"node": "^20.0.0"
},
"repository": {
"type": "git",
"url": "https://github.com/chyzwar/systemd"
"url": "https://github.com/systemd-js/systemd"
},
"sideEffects": false,
"publishConfig": {
Expand All @@ -17,12 +16,12 @@
"type": "module",
"author": "chyzwar",
"license": "Apache-2.0",
"description": "Reading and creating systemd configs",
"description": "Parsing and building systemd units",
"keywords": [
"systemd",
"ini",
"systemd-timer",
"systemd-service"
"unit",
"timer",
"service"
],
"scripts": {
"lint": "eslint . --cache",
Expand Down
14 changes: 14 additions & 0 deletions packages/ctl/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,17 @@ 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")

```
9 changes: 3 additions & 6 deletions packages/ctl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
"name": "@systemd-js/ctl",
"version": "0.2.0",
"engines": {
"node": "20.x",
"yarn": "4.x"
"node": "^20.0.0"
},
"repository": {
"type": "git",
"url": "https://github.com/chyzwar/systemd"
"url": "https://github.com/systemd-js/systemd"
},
"sideEffects": false,
"publishConfig": {
Expand All @@ -20,9 +19,7 @@
"description": "Controlling runtime of systemd units",
"keywords": [
"systemd",
"systemctl",
"systemd-timer",
"systemd-service"
"systemctl"
],
"scripts": {
"lint": "eslint . --cache",
Expand Down
25 changes: 18 additions & 7 deletions packages/ctl/src/ctl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const getPath = (name: string, type: string) => {
return `/etc/systemd/system/${name}.${type}`;
};

const getUnit = (unitName: string, type: string = getType(unitName)): Unit | undefined => {
function getUnit(unitName: string, type: string = getType(unitName)): Unit | undefined {
const name = getName(unitName);
const path = `/etc/systemd/system/${name}.${type}`;

Expand Down Expand Up @@ -105,9 +105,13 @@ export class Ctl {
public restart() {
execSync(`systemctl restart ${this.name}.${this.type}`);
}

public reload() {
execSync(`systemctl daemon-reload ${this.name}.${this.type}`);
}
}

export function createUnit(unitName: string, unit: Unit) {
export function create(unitName: string, unit: Unit) {
const name = getName(unitName);
const type = getType(unitName, unit);
const path = getPath (name, type);
Expand All @@ -121,35 +125,42 @@ export function createUnit(unitName: string, unit: Unit) {
}
}

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

execSync(`systemctl daemon-reload ${name}.${type}`);
}

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

execSync(`systemctl enable ${name}.${type}`);
}

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

execSync(`systemctl disable ${name}.${type}`);
}

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

execSync(`systemctl start ${name}.${type}`);
}

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

execSync(`systemctl stop ${name}.${type}`);
}

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

Expand Down
13 changes: 7 additions & 6 deletions packages/ctl/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
export {
Ctl,

createUnit,
enableUnit,
disableUnit,
startUnit,
stopUnit,
restartUnit,
reload,
create,
enable,
disable,
start,
stop,
restart,
} from "./ctl.js";
Loading