Skip to main content

isoloom_render/configure/
powershell.rs

1//! The PowerShell rendering of a Windows recipe (design §2.1–§2.2): the library
2//! (`configure.ps1`, one function per step kind) and one call per step. A Windows VM's set-up
3//! runs the calls after the library. Every argument is a single-quoted literal.
4
5use isoloom_plan::configure::{CommandPlan, FilePlan, Resource, ServicePlan, ServiceSource, UserPlan};
6use isoloom_spec::shell::base64;
7use isoloom_spec::syntax::{FileState, Guard, Invocation};
8
9use super::Configured;
10use crate::powershell::quote;
11use crate::steps::WINDOWS_PROJECT_DIR;
12use crate::{GeneratedFile, header};
13
14/// The library, as Isoloom ships it.
15pub const LIBRARY: &str = include_str!("powershell/configure.ps1");
16
17/// Where the library is generated, from the project folder.
18pub const LIBRARY_PATH: &str = ".isoloom/configure/lib/configure.ps1";
19
20/// The file the restart rule reads on a Windows VM.
21const CHANGED: &str = "C:\\Windows\\Temp\\isoloom-configure.changed";
22
23/// The library as a generated file, at [`LIBRARY_PATH`].
24#[must_use]
25pub fn library_file() -> GeneratedFile {
26    GeneratedFile {
27        path: LIBRARY_PATH.to_owned(),
28        contents: format!("{}{LIBRARY}", header("#")),
29    }
30}
31
32/// A Windows VM's set-up step for `resources` (see [`super::shell::script`]): the library, then
33/// each step's call, from the project folder, stopping at the first error.
34#[must_use]
35pub fn script(c: &Configured<'_>, resources: &[Resource], fresh: bool) -> String {
36    let calls: String = resources.iter().filter_map(|r| call(c, r)).collect();
37    let mark = if fresh {
38        "Remove-Item -LiteralPath $env:ISOLOOM_CHANGED -Force -ErrorAction SilentlyContinue"
39    } else {
40        "Set-Content -LiteralPath $env:ISOLOOM_CHANGED -Value 'changed'"
41    };
42    let library = super::compact(LIBRARY);
43    format!("$ErrorActionPreference = 'Stop'\n{library}$env:ISOLOOM_CHANGED = '{CHANGED}'\n{mark}\nSet-Location -LiteralPath '{WINDOWS_PROJECT_DIR}'\n{calls}")
44}
45
46/// Step `r`'s call(s), one per line; `None` for a step the library doesn't run (a script, a
47/// reboot, a playbook) or one Windows can't carry (a repository: lowering refuses it).
48#[must_use]
49pub fn call(c: &Configured<'_>, r: &Resource) -> Option<String> {
50    let line = match r {
51        Resource::Packages(ps) => format!("Install-IsoloomFeature -Name {}", list(ps.iter().map(|p| p.name.to_string()))),
52        Resource::Copy(cp) => format!("Copy-IsoloomItem -From {} -To {}", quote(cp.from.as_str()), quote(cp.to.as_str())),
53        Resource::Template(t) => match c.template(&t.from) {
54            Ok(text) => format!("Install-IsoloomTemplate -To {} -Base64 '{}'", quote(t.to.as_str()), base64(text.as_bytes())),
55            Err(why) => format!("throw {}", quote(&format!("isoloom: {why}"))),
56        },
57        Resource::File(f) => file(f)?,
58        Resource::User(u) => user(u),
59        Resource::Group(g) => format!("Add-IsoloomGroup -Name {}", quote(g.name.as_str())),
60        Resource::Service(s) => service(s),
61        Resource::Command(cmd) => command(cmd),
62        Resource::Firewall(ports) => ports
63            .iter()
64            .map(|p| {
65                format!(
66                    "Enable-IsoloomFirewallPort -Port {} -Protocol {}",
67                    p.port,
68                    if p.protocol.is_tcp() { "TCP" } else { "UDP" }
69                )
70            })
71            .collect::<Vec<_>>()
72            .join("\n"),
73        _ => return None,
74    };
75    (!line.is_empty()).then(|| format!("{line}\n"))
76}
77
78/// A PowerShell array of literals (`'a', 'b'`).
79fn list<I: IntoIterator<Item = String>>(items: I) -> String {
80    items.into_iter().map(|i| quote(&i)).collect::<Vec<_>>().join(", ")
81}
82
83/// A file's state (a link is refused on Windows: lowering reports it).
84fn file(f: &FilePlan) -> Option<String> {
85    let path = quote(f.path.as_str());
86    Some(match &f.state {
87        FileState::Directory => format!("Install-IsoloomFile -Path {path} -State directory"),
88        FileState::Absent => format!("Install-IsoloomFile -Path {path} -State absent"),
89        FileState::Download { url, sha256 } => format!(
90            "Install-IsoloomFile -Path {path} -State download -Url {} -Sha256 '{sha256}'",
91            quote(url.as_str())
92        ),
93        _ => return None,
94    })
95}
96
97/// A local user: its password (plain: Windows takes no hash) and its groups, its primary group
98/// first.
99fn user(u: &UserPlan) -> String {
100    let phrase = u.password.as_ref().map(|p| format!(" -Phrase {}", quote(p.as_str()))).unwrap_or_default();
101    let groups: Vec<String> = u.group.iter().chain(&u.groups).map(ToString::to_string).collect();
102    let groups = if groups.is_empty() {
103        String::new()
104    } else {
105        format!(" -Groups {}", list(groups))
106    };
107    format!("Add-IsoloomUser -Name {}{phrase}{groups}", quote(u.name.as_str()))
108}
109
110/// A service: a Windows service's state, or, for one Isoloom defines, its startup task's.
111fn service(s: &ServicePlan) -> String {
112    let name = quote(s.unit.as_str());
113    let enabled = if s.enabled { "$true" } else { "$false" };
114    match &s.source {
115        ServiceSource::Defined { run, cwd, .. } => {
116            let dir = cwd.as_ref().map(|d| format!(" -Directory {}", quote(d.as_str()))).unwrap_or_default();
117            format!(
118                "Register-IsoloomTask -Name {name} -Run {}{dir}\nRegister-IsoloomTaskState -Name {name} -Enabled {enabled}",
119                list(run.args().iter().cloned())
120            )
121        }
122        _ => format!("Register-IsoloomService -Name {name} -Enabled {enabled}"),
123    }
124}
125
126/// A command behind its guard.
127fn command(cmd: &CommandPlan) -> String {
128    let guard = match &cmd.guard {
129        Guard::Creates(path) => format!(" -Creates {}", quote(path.as_str())),
130        Guard::Unless(check) => format!(" -Unless {}", quote(&text(check))),
131        _ => String::new(),
132    };
133    format!("Invoke-IsoloomCommand -Run {}{guard}", quote(&text(&cmd.run)))
134}
135
136/// An invocation as PowerShell's text: a text as it is, a program and its arguments after `&`.
137fn text(i: &Invocation) -> String {
138    match i {
139        Invocation::Exec(a) => format!("& {}", a.args().iter().map(|w| quote(w)).collect::<Vec<_>>().join(" ")),
140        Invocation::Shell(t) => t.clone(),
141        _ => "throw 'this Isoloom cannot render that command'".to_owned(),
142    }
143}
144
145#[cfg(test)]
146mod tests;