Skip to content

Commit

Permalink
allow passing e.g. --user args, don't use env-vars, refactor to str…
Browse files Browse the repository at this point in the history
…uct (#31)

* feat: allow passing additional args through SYSTEMCTL_ARGS

e.g. `--user` args can now be passed through SYSTEMCTL_ARGS environment variable

* chore: update Cargo.toml versions

* refactor: struct SystemCtl instead of functions

* refactor: SystemCtl struct: don't use Vecs

Vecs were not necessary since Command.args() requires IntoIterator not Vec

Unit required changes after refactor

made tests pass to fit refactor, tests run locally

* chore: bump version

* style: add docs and fix clippy

* Run cargo fmt

Signed-off-by: Guillaume W. Bres <[email protected]>

* feat: add `daemon-reload` command support

* docs: include README.md into docs

doc tests are failing

* docs: update README and make doc tests pass

I did not have `sshd` service but I had `ssh.service`

* style: cargo fmt

---------

Signed-off-by: Guillaume W. Bres <[email protected]>
Co-authored-by: Guillaume W. Bres <[email protected]>
  • Loading branch information
JakubKoralewski and gwbres authored Sep 25, 2024
1 parent 035ca00 commit b304a97
Show file tree
Hide file tree
Showing 3 changed files with 401 additions and 440 deletions.
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "systemctl"
version = "0.3.1"
version = "0.4.0"
license = "MIT OR Apache-2.0"
authors = ["Guillaume W. Bres <[email protected]>"]
description = "Small crate to interact with systemd units"
Expand All @@ -13,10 +13,11 @@ default = []
serde = ["dep:serde"]

[dependencies]
strum = "0.25"
strum_macros = "0.25"
itertools = "0.11"
strum = "0.26"
strum_macros = "0.26"
itertools = "0.13"
serde = { version = "1.0", optional = true, default-features = false, features = ["derive"] }
bon="2.3"

[dev-dependencies]
serde_json = "1.0"
42 changes: 17 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,59 +17,51 @@ Small rust crate to interact with systemd units

Currently SystemD Version <245 are not supported as unit-file-list changed from two column to three column setup. See: [SystemD Changelog](https://github.com/systemd/systemd/blob/16bfb12c8f815a468021b6e20871061d20b50f57/NEWS#L6073)

## Environment

`SYSTEMCTL_PATH` custom env. variable describes the absolute
location path of `systemctl` binary, by default this crate uses `/usr/bin/systemctl`,
but that can be customized:

```shell
SYSTEMCTL_PATH=/home/$me/bin/systemctl cargo build
```

## Unit / service operation

Nominal service operations:

```rust
systemctl::stop("systemd-journald.service")
let systemctl = systemctl::SystemCtl::default();
systemctl.stop("systemd-journald.service")
.unwrap();
systemctl::restart("systemd-journald.service")
systemctl.restart("systemd-journald.service")
.unwrap();

if let Ok(true) = systemctl::exists("ntpd") {
let is_active = systemctl::is_active("ntpd")
if let Ok(true) = systemctl.exists("ntpd") {
let is_active = systemctl.is_active("ntpd")
.unwrap();
}
```

## Service enumeration

```rust
use systemctl;
let systemctl = systemctl::SystemCtl::default();
// list all units
systemctl::list_units(None, None, None);
systemctl.list_units(None, None, None);

// list all services
// by adding a --type filter
systemctl::list_units(Some("service"), None, None);
systemctl.list_units(Some("service"), None, None);

// list all services currently `enabled`
// by adding a --state filter
systemctl::list_units(Some("service"), Some("enabled"), None);
systemctl.list_units(Some("service"), Some("enabled"), None);

// list all services starting with cron
systemctl::list_units(Some("service"), None, Some("cron*"));
systemctl.list_units(Some("service"), None, Some("cron*"));
```

## Unit structure

Use the unit structure for more information

```rust
let unit = systemctl::Unit::from_systemctl("sshd")
let systemctl = systemctl::SystemCtl::default();
let unit = systemctl.create_unit("ssh.service")
.unwrap();
unit.restart().unwrap();
systemctl.restart(&unit.name).unwrap();
println!("active: {}", unit.active);
println!("preset: {}", unit.preset);

Expand All @@ -84,11 +76,11 @@ if let Some(docs) = unit.docs { // doc pages available
}
}

println!("auto_start (enabled): {}", unit.auto_start);
println!("auto_start (enabled): {:?}", unit.auto_start);
println!("config script : {}", unit.script);
println!("pid: {}", unit.pid);
println!("Running task(s): {}", unit.tasks.unwrap());
println!("Memory consumption: {}", unit.memory.unwrap());
println!("pid: {:?}", unit.pid);
println!("Running task(s): {:?}", unit.tasks);
println!("Memory consumption: {:?}", unit.memory);
```

## TODO
Expand Down
Loading

0 comments on commit b304a97

Please sign in to comment.