Skip to content

Commit

Permalink
add syslog,ssh,ntp command
Browse files Browse the repository at this point in the history
  • Loading branch information
syncpark authored and syncpark committed Mar 21, 2024
1 parent ce1fef5 commit b7a609b
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 53 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ file is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and
this project adheres to [Semantic
Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- Add `syslog, ssh, ntp` control function.

### Changed

- Limit the PATH of `roxy` program to `/usr/local/aice/bin`

## [0.2.1] - 2023-09-06

### Added
Expand Down
46 changes: 7 additions & 39 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ sysinfo = "0.29"
systemctl = "0.3"
thiserror = "1"
tokio = { version = "1", features = ["time"] }
uptime_lib = "0.2"
uptime_lib = "0.3"
1 change: 1 addition & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
doc-valid-idents = ["REview"]
110 changes: 106 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,27 @@ pub fn init_syslog_servers() -> Result<String> {
}
}

/// (Re)start syslog services.
///
/// # Errors
///
/// The following errors are possible:
///
/// * If serialization of command arguments does not succeed, then an error
/// is returned.
/// * If spawning the roxy executable fails, then an error is returned.
/// * If delivering a command to roxy fails, then an error is returned.
/// * If a response message from roxy is invalid regarding JSON syntax or
/// is not successfully base64-decoded, then an error is returned.
/// * If it fails to restart rsyslogd service, then an error is returned.
pub fn start_syslog_servers() -> Result<bool> {
if let Ok(req) = NodeRequest::new::<Option<String>>(Node::Syslog(SubCommand::Enable), None) {
run_roxy::<bool>(req)
} else {
Err(anyhow!(FAIL_REQUEST))
}
}

/// Returns the list of interface names.
///
/// # Errors
Expand Down Expand Up @@ -333,6 +354,90 @@ pub fn power_off() -> Result<String> {
}
}

/// Return configured sshd port number.
///
/// # Errors
///
/// * Return error if it fails to build request message
/// * Return error if `run_roxy` function returns error
pub fn get_sshd() -> Result<u16> {
if let Ok(req) = NodeRequest::new::<Option<String>>(Node::Sshd(SubCommand::Get), None) {
run_roxy::<u16>(req)
} else {
Err(anyhow!(FAIL_REQUEST))
}
}

/// Restart sshd service.
///
/// # Errors
///
/// * Return error if it fails to build request message
/// * Return error if `run_roxy` function returns error
pub fn start_sshd() -> Result<bool> {
if let Ok(req) = NodeRequest::new::<Option<String>>(Node::Sshd(SubCommand::Enable), None) {
run_roxy::<bool>(req)
} else {
Err(anyhow!(FAIL_REQUEST))
}
}

/// Return configured NTP server FQDNs
///
/// # Errors
///
/// * Return error if it fails to build request message
/// * Return error if `run_roxy` function returns error
pub fn get_ntp() -> Result<Option<Vec<String>>> {
if let Ok(req) = NodeRequest::new::<Option<String>>(Node::Ntp(SubCommand::Get), None) {
run_roxy::<Option<Vec<String>>>(req)
} else {
Err(anyhow!(FAIL_REQUEST))
}
}

/// Set ntp servers
///
/// # Errors
///
/// * Return error if it fails to build request message
/// * Return error if `run_roxy` function returns error
pub fn set_ntp(servers: Vec<String>) -> Result<bool> {
if let Ok(req) = NodeRequest::new::<Vec<String>>(Node::Ntp(SubCommand::Get), servers) {
run_roxy::<bool>(req)
} else {
Err(anyhow!(FAIL_REQUEST))
}
}

/// (Re)Start ntp service
///
/// # Errors
///
/// * Return error if it fails to build request message
/// * Return error if `run_roxy` function returns error
pub fn start_ntp() -> Result<bool> {
if let Ok(req) = NodeRequest::new::<Option<String>>(Node::Ntp(SubCommand::Enable), None) {
run_roxy::<bool>(req)
} else {
Err(anyhow!(FAIL_REQUEST))
}
}

/// Stop ntp service
///
/// # Errors
///
/// * Return error if it fails to build request message
/// * Return error if `run_roxy` function returns error
pub fn stop_ntp() -> Result<bool> {
if let Ok(req) = NodeRequest::new::<Option<String>>(Node::Ntp(SubCommand::Disable), None) {
run_roxy::<bool>(req)
} else {
Err(anyhow!(FAIL_REQUEST))
}
}

/// Response message from Roxy to caller
#[derive(Deserialize, Debug)]
pub enum TaskResult {
Expand All @@ -358,10 +463,7 @@ where
T: serde::de::DeserializeOwned,
{
let mut child = Command::new("roxy")
.env(
"PATH",
"/usr/local/aice/bin:/usr/sbin:/usr/bin:/sbin:/bin:.",
)
.env("PATH", "/usr/local/aice/bin")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()?;
Expand Down
9 changes: 5 additions & 4 deletions src/root/ntp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
};

const NTP_CONF: &str = "/etc/ntp.conf";
const NTP_SERVICE_UNIT: &str = "ntp";

// Set NTP server addresses.
//
Expand Down Expand Up @@ -42,7 +43,7 @@ pub(crate) fn set(servers: &[String]) -> Result<bool> {

file.write_all(new_contents.as_bytes())?;

systemctl::restart("ntp")
systemctl::restart(NTP_SERVICE_UNIT)
.map(|status| status.success())
.map_err(Into::into)
}
Expand Down Expand Up @@ -77,7 +78,7 @@ pub(crate) fn get() -> Result<Option<Vec<String>>> {
// True if ntp service is active
#[must_use]
pub(crate) fn is_active() -> bool {
systemctl::is_active("ntp").map_or(false, |ret| ret)
systemctl::is_active(NTP_SERVICE_UNIT).map_or(false, |ret| ret)
}

// Start ntp client service
Expand All @@ -86,7 +87,7 @@ pub(crate) fn is_active() -> bool {
//
// * systemctl return error when starting ntp service
pub(crate) fn enable() -> Result<bool> {
systemctl::restart("ntp")
systemctl::restart(NTP_SERVICE_UNIT)
.map(|status| status.success())
.map_err(Into::into)
}
Expand All @@ -97,7 +98,7 @@ pub(crate) fn enable() -> Result<bool> {
//
// * systemctl return error when stopping ntp service
pub(crate) fn disable() -> Result<bool> {
systemctl::stop("ntp")
systemctl::stop(NTP_SERVICE_UNIT)
.map(|status| status.success())
.map_err(Into::into)
}
10 changes: 9 additions & 1 deletion src/root/sshd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{

const SSHD_CONFIG: &str = "/etc/ssh/sshd_config";
const SSHD_DEFAULT_PORT: u16 = 22;
const SSHD_SERVICE_UNIT: &str = "sshd";

// Sets sshd port.
//
Expand Down Expand Up @@ -42,7 +43,7 @@ pub(crate) fn set(port: &str) -> Result<bool> {

file.write_all(new_contents.as_bytes())?;

systemctl::restart("sshd")
systemctl::restart(SSHD_SERVICE_UNIT)
.map(|status| status.success())
.map_err(Into::into)
}
Expand All @@ -68,3 +69,10 @@ pub(crate) fn get() -> Result<u16> {
}
Ok(SSHD_DEFAULT_PORT)
}

// (re)start sshd service
pub(crate) fn start() -> Result<bool> {
systemctl::restart(SSHD_SERVICE_UNIT)
.map(|status| status.success())
.map_err(Into::into)
}
10 changes: 9 additions & 1 deletion src/root/syslog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{

const RSYSLOG_CONF: &str = "/etc/rsyslog.d/50-default.conf";
const DEFAULT_FACILITY: &str = "user.*";
const SYSLOG_SERVICE_UNIT: &str = "rsyslog";

// Sets or init rsyslog remote servers. Currently the facility is fixed to `user.*`.
//
Expand Down Expand Up @@ -64,7 +65,7 @@ pub(crate) fn set(remote_addrs: &Option<Vec<String>>) -> Result<bool> {

file.write_all(new_contents.as_bytes())?;

systemctl::restart("rsyslog")
systemctl::restart(SYSLOG_SERVICE_UNIT)
.map(|status| status.success())
.map_err(Into::into)
}
Expand Down Expand Up @@ -124,3 +125,10 @@ pub(crate) fn get() -> Result<Option<Vec<(String, String, String)>>> {
Ok(Some(ret))
}
}

// (re)start rsyslog service
pub(crate) fn start() -> Result<bool> {
systemctl::restart(SYSLOG_SERVICE_UNIT)
.map(|status| status.success())
.map_err(Into::into)
}
Loading

0 comments on commit b7a609b

Please sign in to comment.