patches and low-level development discussion
 help / color / mirror / code / Atom feed
blob 16da1977460303f46714ecf3b560a7cef366a2d9 6821 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
 
// 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;
use iced::{
    Length, Subscription, Task,
    widget::{Space, column, container, row, text},
};
use iced_aw::Spinner;
use iced_term::Terminal;
use lsblk::BlockDevice;
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: BlockDevice) -> 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 any other way of obtaining the commands result.
                        String::from(
                            [
                                "( /usr/bin/systemd-repart \
                                 --definitions=/usr/share/spectrum-installer/repart.d \
                                 --empty=force \
                                 --dry-run=no ",
                                &device.fullname.display().to_string(),
                                "; echo $? > ",
                                RESULT_CODE_FILE,
                                ") 2>&1 | tee ",
                                LOG_FILE,
                            ]
                            .concat(),
                        ),
                    ]),
                },
                ..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 {
            return Subscription::run_with_id(term.id, term.subscription())
                .map::<_, _>(|t| Event::PageInstallation(PageInstallationEvent::Terminal(t)));
        }

        Subscription::none()
    }

    fn view(&self) -> iced::Element<'_, Event> {
        let title_row: iced::Element<Event> = if !self.installation_completed {
            row![
                Spinner::new().height(25),
                Space::with_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::with_height(MARGIN_VERTICAL),
            center_horizontal_in_container!(title_row),
            Space::with_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 16da197 ...
found 16da197 in https://inbox.spectrum-os.org/spectrum-devel/20260104140102.106960-5-johannes.suellner@mailbox.org/

applying [1/1] https://inbox.spectrum-os.org/spectrum-devel/20260104140102.106960-5-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..16da197

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

index at:
100644 16da1977460303f46714ecf3b560a7cef366a2d9	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).