|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | +pub mod hostname; |
| 4 | +pub mod password; |
| 5 | +pub(crate) mod ssh; |
| 6 | +pub mod user; |
| 7 | + |
| 8 | +use strum::IntoEnumIterator; |
| 9 | +use tracing::instrument; |
| 10 | + |
| 11 | +use crate::error::Error; |
| 12 | +use crate::User; |
| 13 | + |
| 14 | +/// The interface for applying the desired configuration to the host. |
| 15 | +/// |
| 16 | +/// By default, all known tools for provisioning a particular resource are tried |
| 17 | +/// until one succeeds. Particular tools can be selected via the |
| 18 | +/// `*_provisioners()` methods ([`Provision::hostname_provisioners`], |
| 19 | +/// [`Provision::user_provisioners`], etc). |
| 20 | +/// |
| 21 | +/// To actually apply the configuration, use [`Provision::provision`]. |
| 22 | +#[derive(Clone)] |
| 23 | +pub struct Provision { |
| 24 | + hostname: String, |
| 25 | + user: User, |
| 26 | + hostname_backends: Option<Vec<hostname::Provisioner>>, |
| 27 | + user_backends: Option<Vec<user::Provisioner>>, |
| 28 | + password_backends: Option<Vec<password::Provisioner>>, |
| 29 | +} |
| 30 | + |
| 31 | +impl Provision { |
| 32 | + pub fn new(hostname: impl Into<String>, user: User) -> Self { |
| 33 | + Self { |
| 34 | + hostname: hostname.into(), |
| 35 | + user, |
| 36 | + hostname_backends: None, |
| 37 | + user_backends: None, |
| 38 | + password_backends: None, |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /// Specify the ways to set the virtual machine's hostname. |
| 43 | + /// |
| 44 | + /// By default, all known methods will be attempted. Use this function to |
| 45 | + /// restrict which methods are attempted. These will be attempted in the |
| 46 | + /// order provided until one succeeds. |
| 47 | + pub fn hostname_provisioners( |
| 48 | + mut self, |
| 49 | + backends: impl Into<Vec<hostname::Provisioner>>, |
| 50 | + ) -> Self { |
| 51 | + self.hostname_backends = Some(backends.into()); |
| 52 | + self |
| 53 | + } |
| 54 | + |
| 55 | + /// Specify the ways to create a user in the virtual machine |
| 56 | + /// |
| 57 | + /// By default, all known methods will be attempted. Use this function to |
| 58 | + /// restrict which methods are attempted. These will be attempted in the |
| 59 | + /// order provided until one succeeds. |
| 60 | + pub fn user_provisioners( |
| 61 | + mut self, |
| 62 | + backends: impl Into<Vec<user::Provisioner>>, |
| 63 | + ) -> Self { |
| 64 | + self.user_backends = Some(backends.into()); |
| 65 | + self |
| 66 | + } |
| 67 | + |
| 68 | + /// Specify the ways to set a users password. |
| 69 | + /// |
| 70 | + /// By default, all known methods will be attempted. Use this function to |
| 71 | + /// restrict which methods are attempted. These will be attempted in the |
| 72 | + /// order provided until one succeeds. Only relevant if a password has been |
| 73 | + /// provided with the [`User`]. |
| 74 | + pub fn password_provisioners( |
| 75 | + mut self, |
| 76 | + backend: impl Into<Vec<password::Provisioner>>, |
| 77 | + ) -> Self { |
| 78 | + self.password_backends = Some(backend.into()); |
| 79 | + self |
| 80 | + } |
| 81 | + |
| 82 | + /// Provision the host. |
| 83 | + /// |
| 84 | + /// Provisioning can fail if the host lacks the necessary tools. For example, |
| 85 | + /// if there is no `useradd` command on the system's `PATH`, or if the command |
| 86 | + /// returns an error, this will return an error. It does not attempt to undo |
| 87 | + /// partial provisioning. |
| 88 | + #[instrument(skip_all)] |
| 89 | + pub fn provision(self) -> Result<(), Error> { |
| 90 | + self.user_backends |
| 91 | + .unwrap_or_else(|| user::Provisioner::iter().collect()) |
| 92 | + .iter() |
| 93 | + .find_map(|backend| { |
| 94 | + backend |
| 95 | + .create(&self.user) |
| 96 | + .map_err(|e| { |
| 97 | + tracing::info!( |
| 98 | + error=?e, |
| 99 | + backend=?backend, |
| 100 | + resource="user", |
| 101 | + "Provisioning did not succeed" |
| 102 | + ); |
| 103 | + e |
| 104 | + }) |
| 105 | + .ok() |
| 106 | + }) |
| 107 | + .ok_or(Error::NoUserProvisioner)?; |
| 108 | + |
| 109 | + self.password_backends |
| 110 | + .unwrap_or_else(|| password::Provisioner::iter().collect()) |
| 111 | + .iter() |
| 112 | + .find_map(|backend| { |
| 113 | + backend |
| 114 | + .set(&self.user) |
| 115 | + .map_err(|e| { |
| 116 | + tracing::info!( |
| 117 | + error=?e, |
| 118 | + backend=?backend, |
| 119 | + resource="password", |
| 120 | + "Provisioning did not succeed" |
| 121 | + ); |
| 122 | + e |
| 123 | + }) |
| 124 | + .ok() |
| 125 | + }) |
| 126 | + .ok_or(Error::NoPasswordProvisioner)?; |
| 127 | + |
| 128 | + if !self.user.ssh_keys.is_empty() { |
| 129 | + let user = nix::unistd::User::from_name(&self.user.name)?.ok_or( |
| 130 | + Error::UserMissing { |
| 131 | + user: self.user.name, |
| 132 | + }, |
| 133 | + )?; |
| 134 | + ssh::provision_ssh(&user, &self.user.ssh_keys)?; |
| 135 | + } |
| 136 | + |
| 137 | + self.hostname_backends |
| 138 | + .unwrap_or_else(|| hostname::Provisioner::iter().collect()) |
| 139 | + .iter() |
| 140 | + .find_map(|backend| { |
| 141 | + backend |
| 142 | + .set(&self.hostname) |
| 143 | + .map_err(|e| { |
| 144 | + tracing::info!( |
| 145 | + error=?e, |
| 146 | + backend=?backend, |
| 147 | + resource="hostname", |
| 148 | + "Provisioning did not succeed" |
| 149 | + ); |
| 150 | + e |
| 151 | + }) |
| 152 | + .ok() |
| 153 | + }) |
| 154 | + .ok_or(Error::NoHostnameProvisioner)?; |
| 155 | + |
| 156 | + Ok(()) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +#[cfg(test)] |
| 161 | +mod tests { |
| 162 | + |
| 163 | + use crate::User; |
| 164 | + |
| 165 | + use super::{hostname, password, user, Provision}; |
| 166 | + |
| 167 | + #[test] |
| 168 | + fn test_successful_provision() { |
| 169 | + let _p = Provision::new( |
| 170 | + "my-hostname".to_string(), |
| 171 | + User::new("azureuser", vec![]), |
| 172 | + ) |
| 173 | + .hostname_provisioners([hostname::Provisioner::FakeHostnamectl]) |
| 174 | + .user_provisioners([user::Provisioner::FakeUseradd]) |
| 175 | + .password_provisioners([password::Provisioner::FakePasswd]) |
| 176 | + .provision() |
| 177 | + .unwrap(); |
| 178 | + } |
| 179 | +} |
0 commit comments