patches and low-level development discussion
 help / color / mirror / code / Atom feed
blob 9295eeadee5c35b8f404d7ce3764c26e347fac05 6978 bytes (raw)
name: tools/spectrum-installer/src/pages/installation.rs 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
 
// SPDX-License-Identifier: EUPL-1.2+
// SPDX-FileCopyrightText: 2025 Johannes Süllner <johannes.suellner@mailbox.org>

use super::{
    BUTTON_TEXT_SIZE, Event, InstallResult, MARGIN_VERTICAL, Page, TITLE_TEXT_SIZE, UpdateResult,
};
use crate::{center_horizontal_in_container, destination_device::DestinationDevice};
use iced::{
    Length, Subscription, Task,
    widget::{column, container, row, space, text},
};
use iced_aw::Spinner;
use iced_term::Terminal;
use std::fs;

const RESULT_CODE_FILE: &str = "/tmp/spectrum-installer_result-code";
const LOG_FILE: &str = "/tmp/spectrum-installer_log";

pub struct PageInstallation {
    terminal: Option<Terminal>,
    installation_completed: bool,
    installation_result: Option<InstallResult>,
}

impl PageInstallation {
    pub fn new(device: DestinationDevice) -> Self {
        let term = Terminal::new(
            0,
            iced_term::settings::Settings {
                backend: iced_term::settings::BackendSettings {
                    program: String::from("/usr/bin/sh"),
                    args: Vec::from([
                        String::from("-c"),
                        // Install enforcing a new partition table.
                        // HACK: Logging to /tmp as iced_term does not offer a way of obtaining
                        // the command's result.
                        String::from(
                            [
                                "set -o pipefail; \
                                ( /usr/bin/systemd-repart \
                                 --pretty=false \
                                 --definitions=/usr/share/spectrum-installer/repart.d \
                                 --empty=force \
                                 --dry-run=no '",
                                &device.path,
                                "'; echo $? > '",
                                RESULT_CODE_FILE,
                                "') 2>&1 | tee '",
                                LOG_FILE,
                                "'",
                            ]
                            .concat(),
                        ),
                    ]),
                    ..Default::default()
                },
                ..Default::default()
            },
        );

        match term {
            Ok(term) => Self {
                terminal: Some(term),
                installation_completed: false,
                installation_result: None,
            },
            Err(e) => Self {
                terminal: None,
                installation_completed: true,
                installation_result: Some(Err((0, e.to_string()))),
            },
        }
    }
}

#[derive(Clone, Debug)]
pub enum PageInstallationEvent {
    Terminal(iced_term::Event),
    Finish,
}

impl Page for PageInstallation {
    fn update(&mut self, event: Event) -> UpdateResult {
        if let Event::PageInstallation(page_event) = event {
            match page_event {
                PageInstallationEvent::Finish => {
                    if let Some(installation_result) = self.installation_result.clone() {
                        return (
                            Some(Box::new(super::completion::PageCompletion::new(
                                installation_result,
                            ))),
                            Task::none(),
                        );
                    }
                }
                PageInstallationEvent::Terminal(iced_term::Event::BackendCall(_, cmd)) => {
                    if let Some(term) = &mut self.terminal {
                        match term.handle(iced_term::Command::ProxyToBackend(cmd)) {
                            iced_term::actions::Action::Shutdown => {
                                let code = fs::read_to_string(RESULT_CODE_FILE);
                                let log = fs::read_to_string(LOG_FILE);
                                self.installation_result =
                                    if let (Ok(code_as_str), Ok(log)) = (code, log) {
                                        if let Ok(code) = code_as_str.trim().parse::<u32>() {
                                            self.installation_completed = true;
                                            if code == 0 {
                                                Some(Ok(log))
                                            } else {
                                                Some(Err((code, log)))
                                            }
                                        } else {
                                            None
                                        }
                                    } else {
                                        None
                                    };
                            }
                            _ => {}
                        }
                    }
                }
            }
        }
        (None, Task::none())
    }

    fn subscription(&self) -> Subscription<Event> {
        if let Some(term) = &self.terminal {
            term.subscription()
                .map(|t| Event::PageInstallation(PageInstallationEvent::Terminal(t)))
        } else {
            Subscription::none()
        }
    }

    fn view(&self) -> iced::Element<'_, Event> {
        let title_row: iced::Element<Event> = if !self.installation_completed {
            row![
                Spinner::new().height(25),
                space().width(MARGIN_VERTICAL),
                text("Installing ...").size(TITLE_TEXT_SIZE)
            ]
            .into()
        } else {
            text(
                if self.installation_result.as_ref().is_some_and(|r| r.is_ok()) {
                    "Installation completed."
                } else {
                    "Installation failed."
                },
            )
            .size(TITLE_TEXT_SIZE)
            .into()
        };

        let installation_status: iced::Element<Event> = if let Some(term) = &self.terminal {
            iced_term::TerminalView::show(term)
                .map::<_>(|t| Event::PageInstallation(PageInstallationEvent::Terminal(t)))
                .into()
        } else {
            text("Could not start installation.").into()
        };

        let main_content = column![
            space().height(MARGIN_VERTICAL),
            center_horizontal_in_container!(title_row),
            space().height(MARGIN_VERTICAL),
            container(installation_status)
                .width(Length::Fill)
                .center_x(Length::Fill)
                .height(Length::Fill),
        ];

        super::layout::bottom_buttons_layout(
            [[(
                text("Finish").size(BUTTON_TEXT_SIZE).into(),
                if self.installation_completed {
                    Some(Event::PageInstallation(PageInstallationEvent::Finish))
                } else {
                    None
                },
            )]],
            main_content.into(),
        )
    }
}

debug log:

solving 9295eea ...
found 9295eea in https://inbox.spectrum-os.org/spectrum-devel/20260204175543.22164-3-johannes.suellner@mailbox.org/

applying [1/1] https://inbox.spectrum-os.org/spectrum-devel/20260204175543.22164-3-johannes.suellner@mailbox.org/
diff --git a/tools/spectrum-installer/src/pages/installation.rs b/tools/spectrum-installer/src/pages/installation.rs
new file mode 100644
index 0000000..9295eea

Checking patch tools/spectrum-installer/src/pages/installation.rs...
Applied patch tools/spectrum-installer/src/pages/installation.rs cleanly.

index at:
100644 9295eeadee5c35b8f404d7ce3764c26e347fac05	tools/spectrum-installer/src/pages/installation.rs

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://spectrum-os.org/git/crosvm
	https://spectrum-os.org/git/doc
	https://spectrum-os.org/git/mktuntap
	https://spectrum-os.org/git/nixpkgs
	https://spectrum-os.org/git/spectrum
	https://spectrum-os.org/git/ucspi-vsock
	https://spectrum-os.org/git/www

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).