Check user group commands exist during plan (#411)
* Check user/group related commands exist prior to execution/revert * Fixup * Don't check for executables on mac * fmt
This commit is contained in:
parent
9549d793cc
commit
4fc7a0db18
|
@ -35,6 +35,21 @@ impl AddUserToGroup {
|
||||||
groupname,
|
groupname,
|
||||||
gid,
|
gid,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
match OperatingSystem::host() {
|
||||||
|
OperatingSystem::MacOSX { .. } | OperatingSystem::Darwin => (),
|
||||||
|
_ => {
|
||||||
|
if !(which::which("addgroup").is_ok() || which::which("gpasswd").is_ok()) {
|
||||||
|
return Err(Self::error(ActionErrorKind::MissingAddUserToGroupCommand));
|
||||||
|
}
|
||||||
|
if !(which::which("delgroup").is_ok() || which::which("gpasswd").is_ok()) {
|
||||||
|
return Err(Self::error(
|
||||||
|
ActionErrorKind::MissingRemoveUserFromGroupCommand,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
// Ensure user does not exists
|
// Ensure user does not exists
|
||||||
if let Some(user) = User::from_name(name.as_str())
|
if let Some(user) = User::from_name(name.as_str())
|
||||||
.map_err(|e| ActionErrorKind::GettingUserId(name.clone(), e))
|
.map_err(|e| ActionErrorKind::GettingUserId(name.clone(), e))
|
||||||
|
@ -57,7 +72,7 @@ impl AddUserToGroup {
|
||||||
}
|
}
|
||||||
|
|
||||||
// See if group membership needs to be done
|
// See if group membership needs to be done
|
||||||
match target_lexicon::OperatingSystem::host() {
|
match OperatingSystem::host() {
|
||||||
OperatingSystem::MacOSX {
|
OperatingSystem::MacOSX {
|
||||||
major: _,
|
major: _,
|
||||||
minor: _,
|
minor: _,
|
||||||
|
@ -119,7 +134,11 @@ impl AddUserToGroup {
|
||||||
let user_in_group = output_str.split(" ").any(|v| v == &this.groupname);
|
let user_in_group = output_str.split(" ").any(|v| v == &this.groupname);
|
||||||
|
|
||||||
if user_in_group {
|
if user_in_group {
|
||||||
tracing::debug!("Creating user `{}` already complete", this.name);
|
tracing::debug!(
|
||||||
|
"Adding user `{}` to group `{}` already complete",
|
||||||
|
this.name,
|
||||||
|
this.groupname
|
||||||
|
);
|
||||||
return Ok(StatefulAction::completed(this));
|
return Ok(StatefulAction::completed(this));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use nix::unistd::Group;
|
use nix::unistd::Group;
|
||||||
|
use target_lexicon::OperatingSystem;
|
||||||
use tokio::process::Command;
|
use tokio::process::Command;
|
||||||
use tracing::{span, Span};
|
use tracing::{span, Span};
|
||||||
|
|
||||||
|
@ -23,6 +24,19 @@ impl CreateGroup {
|
||||||
name: name.clone(),
|
name: name.clone(),
|
||||||
gid,
|
gid,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
match OperatingSystem::host() {
|
||||||
|
OperatingSystem::MacOSX { .. } | OperatingSystem::Darwin => (),
|
||||||
|
_ => {
|
||||||
|
if !(which::which("groupadd").is_ok() || which::which("addgroup").is_ok()) {
|
||||||
|
return Err(Self::error(ActionErrorKind::MissingGroupCreationCommand));
|
||||||
|
}
|
||||||
|
if !(which::which("groupdel").is_ok() || which::which("delgroup").is_ok()) {
|
||||||
|
return Err(Self::error(ActionErrorKind::MissingGroupDeletionCommand));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
// Ensure group does not exists
|
// Ensure group does not exists
|
||||||
if let Some(group) = Group::from_name(name.as_str())
|
if let Some(group) = Group::from_name(name.as_str())
|
||||||
.map_err(|e| ActionErrorKind::GettingGroupId(name.clone(), e))
|
.map_err(|e| ActionErrorKind::GettingGroupId(name.clone(), e))
|
||||||
|
@ -75,8 +89,8 @@ impl Action for CreateGroup {
|
||||||
async fn execute(&mut self) -> Result<(), ActionError> {
|
async fn execute(&mut self) -> Result<(), ActionError> {
|
||||||
let Self { name, gid } = self;
|
let Self { name, gid } = self;
|
||||||
|
|
||||||
use target_lexicon::OperatingSystem;
|
use OperatingSystem;
|
||||||
match target_lexicon::OperatingSystem::host() {
|
match OperatingSystem::host() {
|
||||||
OperatingSystem::MacOSX {
|
OperatingSystem::MacOSX {
|
||||||
major: _,
|
major: _,
|
||||||
minor: _,
|
minor: _,
|
||||||
|
@ -142,8 +156,8 @@ impl Action for CreateGroup {
|
||||||
async fn revert(&mut self) -> Result<(), ActionError> {
|
async fn revert(&mut self) -> Result<(), ActionError> {
|
||||||
let Self { name, gid: _ } = self;
|
let Self { name, gid: _ } = self;
|
||||||
|
|
||||||
use target_lexicon::OperatingSystem;
|
use OperatingSystem;
|
||||||
match target_lexicon::OperatingSystem::host() {
|
match OperatingSystem::host() {
|
||||||
OperatingSystem::MacOSX {
|
OperatingSystem::MacOSX {
|
||||||
major: _,
|
major: _,
|
||||||
minor: _,
|
minor: _,
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use nix::unistd::User;
|
use nix::unistd::User;
|
||||||
|
use target_lexicon::OperatingSystem;
|
||||||
use tokio::process::Command;
|
use tokio::process::Command;
|
||||||
use tracing::{span, Span};
|
use tracing::{span, Span};
|
||||||
|
|
||||||
|
@ -35,6 +36,19 @@ impl CreateUser {
|
||||||
gid,
|
gid,
|
||||||
comment,
|
comment,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
match OperatingSystem::host() {
|
||||||
|
OperatingSystem::MacOSX { .. } | OperatingSystem::Darwin => (),
|
||||||
|
_ => {
|
||||||
|
if !(which::which("useradd").is_ok() || which::which("adduser").is_ok()) {
|
||||||
|
return Err(Self::error(ActionErrorKind::MissingUserCreationCommand));
|
||||||
|
}
|
||||||
|
if !(which::which("userdel").is_ok() || which::which("deluser").is_ok()) {
|
||||||
|
return Err(Self::error(ActionErrorKind::MissingUserDeletionCommand));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
// Ensure user does not exists
|
// Ensure user does not exists
|
||||||
if let Some(user) = User::from_name(name.as_str())
|
if let Some(user) = User::from_name(name.as_str())
|
||||||
.map_err(|e| ActionErrorKind::GettingUserId(name.clone(), e))
|
.map_err(|e| ActionErrorKind::GettingUserId(name.clone(), e))
|
||||||
|
@ -107,7 +121,7 @@ impl Action for CreateUser {
|
||||||
comment,
|
comment,
|
||||||
} = self;
|
} = self;
|
||||||
|
|
||||||
use target_lexicon::OperatingSystem;
|
use OperatingSystem;
|
||||||
match OperatingSystem::host() {
|
match OperatingSystem::host() {
|
||||||
OperatingSystem::MacOSX {
|
OperatingSystem::MacOSX {
|
||||||
major: _,
|
major: _,
|
||||||
|
@ -262,8 +276,8 @@ impl Action for CreateUser {
|
||||||
|
|
||||||
#[tracing::instrument(level = "debug", skip_all)]
|
#[tracing::instrument(level = "debug", skip_all)]
|
||||||
async fn revert(&mut self) -> Result<(), ActionError> {
|
async fn revert(&mut self) -> Result<(), ActionError> {
|
||||||
use target_lexicon::OperatingSystem;
|
use OperatingSystem;
|
||||||
match target_lexicon::OperatingSystem::host() {
|
match OperatingSystem::host() {
|
||||||
OperatingSystem::MacOSX {
|
OperatingSystem::MacOSX {
|
||||||
major: _,
|
major: _,
|
||||||
minor: _,
|
minor: _,
|
||||||
|
|
Loading…
Reference in a new issue