-
Notifications
You must be signed in to change notification settings - Fork 10
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
add Azure Linux support #87
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ pub trait Distribution { | |
pub enum Distributions { | ||
Debian, | ||
Ubuntu, | ||
AzureLinux, | ||
} | ||
|
||
impl Distribution for Distributions { | ||
|
@@ -26,10 +27,78 @@ impl Distribution for Distributions { | |
password: &str, | ||
) -> Result<i32, String> { | ||
match self { | ||
Distributions::AzureLinux => { | ||
let mut home_path = "/home/".to_string(); | ||
home_path.push_str(username); | ||
println!("Creating user in AzureLinux: {}", username); | ||
match Command::new("useradd") | ||
.arg(username) | ||
.arg("--comment") | ||
.arg( | ||
"Provisioning agent created this user based on username provided in IMDS", | ||
) | ||
.arg("-U") | ||
.arg("--groups") | ||
.arg("wheel,sudo") | ||
.arg("-d") | ||
.arg(home_path.clone()) | ||
.arg("-m") | ||
.status(){ | ||
Ok(_)=>(), | ||
Err(err) => return Err(err.to_string()), | ||
}; | ||
|
||
if password.is_empty() { | ||
match Command::new("passwd") | ||
.arg("-d") | ||
.arg(username) | ||
.status() | ||
{ | ||
Ok(status_code) => { | ||
if !status_code.success() { | ||
return Err("Failed to create user".to_string()); | ||
} | ||
} | ||
Err(err) => return Err(err.to_string()), | ||
}; | ||
println!("User created without password") | ||
} else { | ||
let input = format!("{}:{}", username, password); | ||
|
||
let mut output = Command::new("chpasswd") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I am not an expert of Azure Linux, but is there a specific reason why you need to do password-based authentication? Previously we had the password authentication in the code base, but the PR #57 removed that part mainly for security. Though there still seems to be cases where password authentication would be necessary. I am just curious what the situation of Azure Linux would be. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No actually, I believe we do want to eliminate password auth, this was a hold over from me re-implementing the debian logic of an older branch 👍 Thank you for the catch |
||
.stdin(Stdio::piped()) | ||
.stdout(Stdio::null()) | ||
.stderr(Stdio::inherit()) | ||
.spawn() | ||
.expect("Failed to run chpasswd."); | ||
|
||
let mut stdin = | ||
output.stdin.as_ref().ok_or("Failed to open stdin")?; | ||
|
||
stdin.write_all(input.as_bytes()).map_err(|error| { | ||
format!("Failed to write to stdin: {}", error) | ||
})?; | ||
|
||
let status = output.wait().map_err(|error| { | ||
format!("Failed to wait for stdin command: {}", error) | ||
})?; | ||
|
||
if !status.success() { | ||
return Err(format!( | ||
"Chpasswd command failed with exit code {}", | ||
status.code().unwrap_or(-1) | ||
)); | ||
} | ||
println!("User created with password") | ||
} | ||
|
||
Ok(0) | ||
}, | ||
Distributions::Debian | Distributions::Ubuntu => { | ||
let mut home_path = "/home/".to_string(); | ||
home_path.push_str(username); | ||
|
||
println!("Creating user in Debian/Ubuntu: {}", username); | ||
match Command::new("useradd") | ||
.arg(username) | ||
.arg("--comment") | ||
|
@@ -94,6 +163,16 @@ impl Distribution for Distributions { | |
} | ||
fn set_hostname(&self, hostname: &str) -> Result<i32, String> { | ||
match self { | ||
Distributions::AzureLinux => { | ||
match Command::new("hostnamectl") | ||
.arg("set-hostname") | ||
.arg(hostname) | ||
.status() | ||
{ | ||
Ok(status_code) => Ok(status_code.code().unwrap_or(1)), | ||
Err(err) => Err(err.to_string()), | ||
} | ||
}, | ||
Distributions::Debian | Distributions::Ubuntu => { | ||
match Command::new("hostnamectl") | ||
.arg("set-hostname") | ||
|
@@ -112,6 +191,7 @@ impl From<&str> for Distributions { | |
match s { | ||
"debian" => Distributions::Debian, | ||
"ubuntu" => Distributions::Ubuntu, | ||
"azurelinux" => Distributions::AzureLinux, | ||
_ => panic!("Unknown distribution"), | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As being implemented in the other PR #86, we should not check for each distro, to be flexible and scalable.
I will soon try to update the PR to make it reviewable. Once it got merged, I hope this PR could be rebased and written without the distro checks.