Skip to content

Commit a0c87d4

Browse files
authored
Merge pull request #14 from 51yu/unit
allow custom object path
2 parents 0f589b7 + f24a11e commit a0c87d4

File tree

10 files changed

+319
-43
lines changed

10 files changed

+319
-43
lines changed

README.md

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,11 @@ fn main() -> Result<()> {
2424
create and start service
2525
```rust
2626
use systemd_client::{
27-
build_blocking_client,
28-
create_unit_configuration_file,
29-
manager::blocking::OrgFreedesktopSystemd1Manager,
30-
Result,
31-
ServiceConfiguration,
32-
ServiceUnitConfiguration,
33-
SystemdObjectType,
34-
UnitConfiguration,
27+
build_blocking_client, create_unit_configuration_file,
28+
manager::blocking::OrgFreedesktopSystemd1Manager, models::IntoModel,
29+
unit::blocking::UnitProperties, Result, ServiceConfiguration, ServiceUnitConfiguration,
30+
SystemdObjectType, UnitActiveStateType, UnitConfiguration, UnitLoadStateType, UnitProps,
31+
UnitSubStateType,
3532
};
3633

3734
/*
@@ -43,7 +40,7 @@ use systemd_client::{
4340
*/
4441
fn main() -> Result<()> {
4542
let unit_builder = UnitConfiguration::builder().description("test service");
46-
let svc_builder = ServiceConfiguration::builder().exec_start(vec!["/bin/sleep", "10"]);
43+
let svc_builder = ServiceConfiguration::builder().exec_start(vec!["/bin/sleep", "3"]);
4744
let svc_unit = ServiceUnitConfiguration::builder()
4845
.unit(unit_builder)
4946
.service(svc_builder)
@@ -56,6 +53,22 @@ fn main() -> Result<()> {
5653
println!("{}", job_path);
5754
let svc_unit_path = client.get_unit("test.service")?;
5855
println!("{}", svc_unit_path);
56+
// verify unit state given unit path
57+
let client = build_blocking_client(SystemdObjectType::Unit(svc_unit_path))?;
58+
let unit_props = client.get_unit_properties()?;
59+
let unit_props: UnitProps = unit_props.into_model()?;
60+
println!("{:?}", unit_props);
61+
assert_eq!(unit_props.load_state, UnitLoadStateType::Loaded);
62+
assert_eq!(unit_props.active_state, UnitActiveStateType::Active);
63+
assert_eq!(unit_props.sub_state, UnitSubStateType::Running);
64+
std::thread::sleep(std::time::Duration::from_secs(4));
65+
// service should exit after 3 sec
66+
let unit_props = client.get_unit_properties()?;
67+
let unit_props: UnitProps = unit_props.into_model()?;
68+
println!("{:?}", unit_props);
69+
assert_eq!(unit_props.load_state, UnitLoadStateType::Loaded);
70+
assert_eq!(unit_props.active_state, UnitActiveStateType::Inactive);
71+
assert_eq!(unit_props.sub_state, UnitSubStateType::Dead);
5972
Ok(())
6073
}
6174
```
@@ -85,14 +98,11 @@ pub async fn main() -> Result<()> {
8598
create and start service
8699
```rust
87100
use systemd_client::{
88-
build_nonblock_client,
89-
create_unit_configuration_file,
90-
manager::nonblock::OrgFreedesktopSystemd1Manager,
91-
Result,
92-
ServiceConfiguration,
93-
ServiceUnitConfiguration,
94-
SystemdObjectType,
95-
UnitConfiguration,
101+
build_nonblock_client, create_unit_configuration_file,
102+
manager::nonblock::OrgFreedesktopSystemd1Manager, models::IntoModel,
103+
unit::nonblock::UnitProperties, Result, ServiceConfiguration, ServiceUnitConfiguration,
104+
SystemdObjectType, UnitActiveStateType, UnitConfiguration, UnitLoadStateType, UnitProps,
105+
UnitSubStateType,
96106
};
97107

98108
/*
@@ -105,7 +115,7 @@ use systemd_client::{
105115
#[tokio::main]
106116
async fn main() -> Result<()> {
107117
let unit_builder = UnitConfiguration::builder().description("test service");
108-
let svc_builder = ServiceConfiguration::builder().exec_start(vec!["/bin/sleep", "10"]);
118+
let svc_builder = ServiceConfiguration::builder().exec_start(vec!["/bin/sleep", "3"]);
109119
let svc_unit = ServiceUnitConfiguration::builder()
110120
.unit(unit_builder)
111121
.service(svc_builder)
@@ -120,6 +130,24 @@ async fn main() -> Result<()> {
120130
println!("{}", svc_unit_path);
121131
// close connection
122132
jh.abort();
133+
// verify unit state given unit path
134+
let (client, jh) = build_nonblock_client(SystemdObjectType::Unit(svc_unit_path))?;
135+
let unit_props = client.get_unit_properties().await?;
136+
let unit_props: UnitProps = unit_props.into_model()?;
137+
println!("{:?}", unit_props);
138+
assert_eq!(unit_props.load_state, UnitLoadStateType::Loaded);
139+
assert_eq!(unit_props.active_state, UnitActiveStateType::Active);
140+
assert_eq!(unit_props.sub_state, UnitSubStateType::Running);
141+
std::thread::sleep(std::time::Duration::from_secs(4));
142+
// service should exit after 3 sec
143+
let unit_props = client.get_unit_properties().await?;
144+
let unit_props: UnitProps = unit_props.into_model()?;
145+
println!("{:?}", unit_props);
146+
assert_eq!(unit_props.load_state, UnitLoadStateType::Loaded);
147+
assert_eq!(unit_props.active_state, UnitActiveStateType::Inactive);
148+
assert_eq!(unit_props.sub_state, UnitSubStateType::Dead);
149+
// close connection
150+
jh.abort();
123151
Ok(())
124152
}
125153
```

examples/start_service_blocking.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use systemd_client::{
22
build_blocking_client, create_unit_configuration_file,
3-
manager::blocking::OrgFreedesktopSystemd1Manager, Result, ServiceConfiguration,
4-
ServiceUnitConfiguration, SystemdObjectType, UnitConfiguration,
3+
manager::blocking::OrgFreedesktopSystemd1Manager, models::IntoModel,
4+
unit::blocking::UnitProperties, Result, ServiceConfiguration, ServiceUnitConfiguration,
5+
SystemdObjectType, UnitActiveStateType, UnitConfiguration, UnitLoadStateType, UnitProps,
6+
UnitSubStateType,
57
};
68

79
/*
@@ -13,7 +15,7 @@ use systemd_client::{
1315
*/
1416
fn main() -> Result<()> {
1517
let unit_builder = UnitConfiguration::builder().description("test service");
16-
let svc_builder = ServiceConfiguration::builder().exec_start(vec!["/bin/sleep", "10"]);
18+
let svc_builder = ServiceConfiguration::builder().exec_start(vec!["/bin/sleep", "3"]);
1719
let svc_unit = ServiceUnitConfiguration::builder()
1820
.unit(unit_builder)
1921
.service(svc_builder)
@@ -26,5 +28,21 @@ fn main() -> Result<()> {
2628
println!("{}", job_path);
2729
let svc_unit_path = client.get_unit("test.service")?;
2830
println!("{}", svc_unit_path);
31+
// verify unit state given unit path
32+
let client = build_blocking_client(SystemdObjectType::Unit(svc_unit_path))?;
33+
let unit_props = client.get_unit_properties()?;
34+
let unit_props: UnitProps = unit_props.into_model()?;
35+
println!("{:?}", unit_props);
36+
assert_eq!(unit_props.load_state, UnitLoadStateType::Loaded);
37+
assert_eq!(unit_props.active_state, UnitActiveStateType::Active);
38+
assert_eq!(unit_props.sub_state, UnitSubStateType::Running);
39+
std::thread::sleep(std::time::Duration::from_secs(4));
40+
// service should exit after 3 sec
41+
let unit_props = client.get_unit_properties()?;
42+
let unit_props: UnitProps = unit_props.into_model()?;
43+
println!("{:?}", unit_props);
44+
assert_eq!(unit_props.load_state, UnitLoadStateType::Loaded);
45+
assert_eq!(unit_props.active_state, UnitActiveStateType::Inactive);
46+
assert_eq!(unit_props.sub_state, UnitSubStateType::Dead);
2947
Ok(())
3048
}

examples/start_service_nonblock.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use systemd_client::{
22
build_nonblock_client, create_unit_configuration_file,
3-
manager::nonblock::OrgFreedesktopSystemd1Manager, Result, ServiceConfiguration,
4-
ServiceUnitConfiguration, SystemdObjectType, UnitConfiguration,
3+
manager::nonblock::OrgFreedesktopSystemd1Manager, models::IntoModel,
4+
unit::nonblock::UnitProperties, Result, ServiceConfiguration, ServiceUnitConfiguration,
5+
SystemdObjectType, UnitActiveStateType, UnitConfiguration, UnitLoadStateType, UnitProps,
6+
UnitSubStateType,
57
};
68

79
/*
@@ -14,7 +16,7 @@ use systemd_client::{
1416
#[tokio::main]
1517
async fn main() -> Result<()> {
1618
let unit_builder = UnitConfiguration::builder().description("test service");
17-
let svc_builder = ServiceConfiguration::builder().exec_start(vec!["/bin/sleep", "10"]);
19+
let svc_builder = ServiceConfiguration::builder().exec_start(vec!["/bin/sleep", "3"]);
1820
let svc_unit = ServiceUnitConfiguration::builder()
1921
.unit(unit_builder)
2022
.service(svc_builder)
@@ -29,5 +31,23 @@ async fn main() -> Result<()> {
2931
println!("{}", svc_unit_path);
3032
// close connection
3133
jh.abort();
34+
// verify unit state given unit path
35+
let (client, jh) = build_nonblock_client(SystemdObjectType::Unit(svc_unit_path))?;
36+
let unit_props = client.get_unit_properties().await?;
37+
let unit_props: UnitProps = unit_props.into_model()?;
38+
println!("{:?}", unit_props);
39+
assert_eq!(unit_props.load_state, UnitLoadStateType::Loaded);
40+
assert_eq!(unit_props.active_state, UnitActiveStateType::Active);
41+
assert_eq!(unit_props.sub_state, UnitSubStateType::Running);
42+
std::thread::sleep(std::time::Duration::from_secs(4));
43+
// service should exit after 3 sec
44+
let unit_props = client.get_unit_properties().await?;
45+
let unit_props: UnitProps = unit_props.into_model()?;
46+
println!("{:?}", unit_props);
47+
assert_eq!(unit_props.load_state, UnitLoadStateType::Loaded);
48+
assert_eq!(unit_props.active_state, UnitActiveStateType::Inactive);
49+
assert_eq!(unit_props.sub_state, UnitSubStateType::Dead);
50+
// close connection
51+
jh.abort();
3252
Ok(())
3353
}

src/constants.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub const DESTINATION_SYSTEMD: &str = "org.freedesktop.systemd1";
2+
pub const OBJECT_PATH_SYSTEMD_MANAGER: &str = "/org/freedesktop/systemd1";
3+
pub const INTERFACE_SYSTEMD_UNIT: &str = "org.freedesktop.systemd1.Unit";
4+
pub const CONNECTION_TIMEOUT_SECS: u64 = 5;
5+
pub const SYSTEMD_UNIT_CONFIGURATION_DIRECTORY: &str = "/etc/systemd/system";

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
mod constants;
12
pub mod errors;
23
pub mod manager;
34
pub mod models;
45
pub mod templates;
6+
pub mod unit;
57
pub mod utils;
68

9+
pub(crate) use constants::*;
710
pub use errors::*;
811
pub use models::*;
912
pub use templates::*;
13+
pub use unit::*;
1014
pub use utils::*;

0 commit comments

Comments
 (0)