Skip to content

Commit 2589e8f

Browse files
committed
Rebase after create_user PR
1 parent d1867e3 commit 2589e8f

File tree

1 file changed

+5
-92
lines changed
  • libazureinit/src/provision

1 file changed

+5
-92
lines changed

libazureinit/src/provision/mod.rs

Lines changed: 5 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -83,49 +83,8 @@ impl Provision {
8383
///
8484
/// Returns `Ok(())` if the hostname was set successfully and DHCP leases were renewed.
8585
/// Returns an error if hostname setting fails or DHCP renewal fails.
86-
///
8786
#[instrument(skip_all)]
88-
pub fn set_hostname(self) -> Result<(), Error> {
89-
#[cfg(not(test))]
90-
{
91-
self.set_hostname_with_dhcp_renewer(|| {
92-
Command::new("systemctl")
93-
.arg("restart")
94-
.arg("systemd-networkd")
95-
.output()
96-
})
97-
}
98-
#[cfg(test)]
99-
{
100-
self.set_hostname_with_dhcp_renewer(|| {
101-
Ok(Output {
102-
status: std::os::unix::process::ExitStatusExt::from_raw(0),
103-
stdout: b"DHCP renewal successful".to_vec(),
104-
stderr: b"".to_vec(),
105-
})
106-
})
107-
}
108-
}
109-
110-
/// Sets the system hostname using the hostname found in either IMDS or the OVF.
111-
///
112-
/// This function iterates over a list of available backends and attempts to
113-
/// set the hostname until one succeeds. Currently supported backends include:
114-
/// - `Hostnamectl`
115-
///
116-
/// Additional hostname provisioners can be set through config files.
117-
/// This function ends by renewing all DHCP leases.
118-
///
119-
/// # Returns
120-
///
121-
/// Returns `Ok(())` if the hostname was set successfully by any of the backends.
122-
/// Returns `Err(Error::NoHostnameProvisioner)` if no backends was able to set
123-
/// the hostname.
124-
#[instrument(skip_all)]
125-
fn set_hostname_with_dhcp_renewer(
126-
self,
127-
dhcp_renew_command_runner: impl Fn() -> io::Result<Output>,
128-
) -> Result<(), Error> {
87+
pub fn set_hostname(&self) -> Result<(), Error> {
12988
self.config
13089
.hostname_provisioners
13190
.backends
@@ -137,41 +96,7 @@ impl Provision {
13796
#[cfg(test)]
13897
HostnameProvisioner::FakeHostnamectl => Some(()),
13998
})
140-
.ok_or(Error::NoHostnameProvisioner)?;
141-
142-
Self::renew_dhcp_leases_with_runner(dhcp_renew_command_runner)?;
143-
144-
Ok(())
145-
}
146-
147-
/// Renews DHCP leases using a custom command runner.
148-
///
149-
/// # Arguments
150-
///
151-
/// * `dhcp_renew_command_runner` - A function that runs the DHCP renewal command and returns its output.
152-
///
153-
/// # Returns
154-
///
155-
/// Returns `Ok(())` if DHCP leases were renewed successfully.
156-
/// Returns an error if the command fails.
157-
#[instrument(skip_all)]
158-
fn renew_dhcp_leases_with_runner(
159-
dhcp_renew_command_runner: impl Fn() -> io::Result<Output>,
160-
) -> Result<(), Error> {
161-
let output = dhcp_renew_command_runner()?;
162-
163-
if !output.status.success() {
164-
tracing::error!(
165-
"Failed to renew DHCP leases: {}",
166-
String::from_utf8_lossy(&output.stderr)
167-
);
168-
return Err(Error::SubprocessFailed {
169-
command: "networkctl renew".to_string(),
170-
status: output.status,
171-
});
172-
}
173-
174-
Ok(())
99+
.ok_or(Error::NoHostnameProvisioner)
175100
}
176101

177102
/// Provisioning can fail if the host lacks the necessary tools. For example,
@@ -180,20 +105,7 @@ impl Provision {
180105
/// partial provisioning.
181106
#[instrument(skip_all)]
182107
pub fn provision(self) -> Result<(), Error> {
183-
self.clone().set_hostname()?;
184-
185-
self.config
186-
.hostname_provisioners
187-
.backends
188-
.iter()
189-
.find_map(|backend| match backend {
190-
HostnameProvisioner::Hostnamectl => {
191-
HostnameProvisioner::Hostnamectl.set(&self.hostname).ok()
192-
}
193-
#[cfg(test)]
194-
HostnameProvisioner::FakeHostnamectl => Some(()),
195-
})
196-
.ok_or(Error::NoHostnameProvisioner)?;
108+
self.set_hostname()?;
197109

198110
self.create_user()?;
199111

@@ -274,6 +186,7 @@ mod tests {
274186
use crate::config::{
275187
HostnameProvisioners, PasswordProvisioners, UserProvisioners,
276188
};
189+
use crate::error;
277190
use crate::error::Error;
278191
use crate::User;
279192

@@ -407,7 +320,7 @@ mod tests {
407320
"create_user should fail with no user provisioners"
408321
);
409322
assert!(
410-
matches!(result.unwrap_err(), error::Error::NoUserProvisioner),
323+
matches!(result.unwrap_err(), Error::NoUserProvisioner),
411324
"Should return NoUserProvisioner error"
412325
);
413326
}

0 commit comments

Comments
 (0)