ferrodock 1.28.4

A fast, native container manager for Docker, written in Rust.
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! Compose page: containers grouped by Docker Compose project, with
//! project-wide start / stop / restart / remove; bringing projects up from
//! their files, rebuilding services, and merged logs.

mod logs;
mod up;

pub use up::{pick_compose_file, up_op};

use eframe::egui::{self, Color32, RichText};

use crate::app::components::icons::Icon;
use crate::app::components::{details, table, widgets};
use crate::app::page::{
    Command, CommandRun, Confirmation, Intent, Page, PageCtx, toggle_selection,
};
use crate::app::store::Store;
use crate::docker::{
    Action, ComposeAction, ComposeOp, ComposeProject, ComposeTarget, Resource, Task, next_job,
    plural,
};
use crate::theme::Palette;

#[derive(Default, Clone, Copy, PartialEq, Eq)]
enum Tab {
    #[default]
    Overview,
    Logs,
}

#[derive(Default)]
pub struct ComposePage {
    /// Project name.
    selected: Option<String>,
    tab: Tab,
    /// What's typed in the Compose file box.
    up_path: String,
    /// The command whose output was closed.
    closed_output: Option<u64>,
    /// The service the logs are limited to.
    only: Option<String>,
}

/// The project as Compose sees it, if its files are on this computer.
fn target(project: &ComposeProject) -> Option<ComposeTarget> {
    let files: Vec<std::path::PathBuf> = project
        .config_files
        .iter()
        .map(std::path::PathBuf::from)
        .collect();
    (!files.is_empty() && files.iter().all(|f| f.is_file())).then(|| ComposeTarget {
        name: Some(project.name.clone()),
        files,
        dir: project.working_dir.clone().into(),
    })
}

fn run(ctx: &mut PageCtx, target: ComposeTarget, action: ComposeAction) {
    ctx.request(Intent::Compose(ComposeOp::Run {
        job: next_job(),
        target,
        action,
    }));
}

impl ComposePage {
    fn selected<'a>(&self, store: &'a Store) -> Option<&'a ComposeProject> {
        let name = self.selected.as_deref()?;
        store.projects.iter().find(|p| p.name == name)
    }
}

impl Page for ComposePage {
    fn title(&self) -> &'static str {
        "Compose"
    }

    fn icon(&self) -> Icon {
        Icon::Stack
    }

    fn resource(&self) -> Resource {
        Resource::Containers
    }

    fn badge(&self, store: &Store) -> String {
        store.projects.len().to_string()
    }

    fn summary(&self, store: &Store) -> String {
        let running = store.projects.iter().filter(|p| p.running() > 0).count();
        format!(
            "{} · {running} running",
            plural(store.projects.len(), "project")
        )
    }

    fn search_hint(&self) -> &'static str {
        "Search project or service"
    }

    fn show(&mut self, ui: &mut egui::Ui, ctx: &mut PageCtx) {
        let p = ctx.palette;
        let store = ctx.store;
        up::bar(ui, ctx, &mut self.up_path);
        let job = store.compose.run.as_ref().map(|j| j.id);
        if job.is_some() && job != self.closed_output && !up::output(ui, ctx) {
            self.closed_output = job;
        }
        let visible: Vec<&ComposeProject> = store
            .projects
            .iter()
            .filter(|project| ctx.matches_search(&[&project.name, &project.services().join(" ")]))
            .collect();
        if visible.is_empty() {
            super::nothing_to_show(
                ui,
                ctx,
                !store.projects.is_empty(),
                (
                    Icon::Stack,
                    "No Compose projects",
                    "Start one from its compose.yaml above, or with `docker compose up`.",
                ),
                None,
            );
            return;
        }

        let columns = vec![
            table::Col::fill("PROJECT"),
            table::Col::fixed("STATUS", 150.0),
            table::Col::fixed("", 68.0),
        ];
        let selected = self.selected.clone();
        let clicked = table::show(
            ui,
            p,
            "compose",
            columns,
            visible.len(),
            |i| selected.as_deref() == Some(visible[i].name.as_str()),
            |i, row| {
                let project = visible[i];
                let services = plural(project.services().len(), "service");
                row.col(|ui| {
                    let color = if project.running() > 0 {
                        p.success
                    } else {
                        p.muted
                    };
                    table::with_icon(ui, p, Icon::Stack, color, &project.name, &services)
                });
                row.col(|ui| state_pill(ui, p, project));
                row.col(|ui| {
                    ui.spacing_mut().item_spacing.x = 4.0;
                    for task in project_buttons(ui, store, project, false) {
                        ctx.request(Intent::Run(task));
                    }
                });
            },
        );
        if let Some(i) = clicked {
            toggle_selection(&mut self.selected, visible[i].name.clone());
        }
    }

    fn has_selection(&self, store: &Store) -> bool {
        self.selected(store).is_some()
    }

    fn show_details(&mut self, ui: &mut egui::Ui, ctx: &mut PageCtx) {
        let p = ctx.palette;
        let store = ctx.store;
        let Some(project) = self.selected(store) else {
            return;
        };

        if details::header(ui, p, &project.name) {
            self.selected = None;
        }
        state_pill(ui, p, project);
        ui.add_space(10.0);
        let target = target(project);
        ui.horizontal(|ui| {
            for task in project_buttons(ui, store, project, true) {
                ctx.request(Intent::Run(task));
            }
            if !is_busy(store, project) {
                remove_project_button(ui, ctx, project);
            }
        });
        ui.horizontal(|ui| {
            let missing = "Its Compose files aren't on this computer";
            let rebuild = ui
                .add_enabled(target.is_some(), egui::Button::new("Rebuild all").small())
                .on_hover_text("docker compose up -d --build")
                .on_disabled_hover_text(missing);
            let pull = ui
                .add_enabled(
                    target.is_some(),
                    egui::Button::new("Pull & recreate").small(),
                )
                .on_hover_text("docker compose pull, then up -d --force-recreate")
                .on_disabled_hover_text(missing);
            if let Some(target) = &target {
                if rebuild.clicked() {
                    run(ctx, target.clone(), ComposeAction::Rebuild(None));
                }
                if pull.clicked() {
                    run(ctx, target.clone(), ComposeAction::PullRecreate);
                }
            }
        });
        ui.add_space(12.0);

        widgets::segmented(ui, p, |ui| {
            for (tab, label) in [(Tab::Overview, "Overview"), (Tab::Logs, "Logs")] {
                if widgets::tab(ui, p, label, None, self.tab == tab).clicked() {
                    self.tab = tab;
                }
            }
        });
        ui.add_space(10.0);
        if self.tab == Tab::Logs {
            logs::show(ui, ctx, project, &mut self.only);
            return;
        }
        // Nobody's reading the logs: stop following them.
        if let Some(job) = store.compose.logs.as_ref().filter(|j| j.ended.is_none()) {
            ctx.request(Intent::Compose(ComposeOp::Cancel { job: job.id }));
        }

        egui::ScrollArea::vertical()
            .auto_shrink(false)
            .show(ui, |ui| {
                details::section(ui, p, "Services", |ui| {
                    for c in &project.containers {
                        let service = c.compose.as_ref().map_or("", |l| l.service.as_str());
                        ui.label(RichText::new(service).color(p.muted));
                        ui.horizontal(|ui| {
                            widgets::dot(ui, if c.is_active() { p.success } else { p.muted });
                            ui.label(RichText::new(&c.name).color(p.text));
                            super::containers::ports::links(ui, p, &c.ports);
                            if let Some(target) = &target {
                                let rebuild = ui.small_button("Rebuild").on_hover_text(format!(
                                    "docker compose up -d --build --no-deps {service}"
                                ));
                                if rebuild.clicked() {
                                    run(
                                        ctx,
                                        target.clone(),
                                        ComposeAction::Rebuild(Some(service.to_owned())),
                                    );
                                }
                            }
                        });
                        ui.end_row();
                    }
                });
                details::section(ui, p, "Files", |ui| {
                    details::field(ui, p, "Directory", &project.working_dir);
                    details::field(
                        ui,
                        p,
                        "Config",
                        &widgets::join_or_dash(&project.config_files),
                    );
                });
            });
    }

    fn clear_selection(&mut self) {
        self.selected = None;
    }

    fn select(&mut self, id: &str) {
        self.selected = Some(id.to_owned());
    }

    fn commands(&self, store: &Store) -> Vec<Command> {
        let mut list = vec![Command {
            label: "Open a Compose file…".into(),
            detail: "docker compose up -d".into(),
            run: CommandRun::Intents(vec![Intent::OpenComposeFile]),
        }];
        for project in &store.projects {
            let status = format!(
                "Compose project · {}/{} running",
                project.running(),
                project.containers.len()
            );
            list.push(Command::select(&project.name, status, &project.name));
            let toggle = if project.running() > 0 {
                (Action::Stop, "Stop")
            } else {
                (Action::Start, "Start")
            };
            for (action, verb) in [toggle, (Action::Restart, "Restart")] {
                list.push(Command {
                    label: format!("{verb} project {}", project.name),
                    detail: "Compose project".into(),
                    run: CommandRun::Intents(
                        tasks_for(project, action)
                            .into_iter()
                            .map(Intent::Run)
                            .collect(),
                    ),
                });
            }
        }
        list
    }
}

/// "Running 2/3": green when everything runs, amber when some do.
fn state_pill(ui: &mut egui::Ui, p: &Palette, project: &ComposeProject) {
    let total = project.containers.len();
    let running = project.running();
    let (label, color): (String, Color32) = match running {
        0 => ("Stopped".into(), p.muted),
        n if n == total => ("Running".into(), p.success),
        n => (format!("Running {n}/{total}"), p.warning),
    };
    widgets::pill(ui, &label, color);
}

fn is_busy(store: &Store, project: &ComposeProject) -> bool {
    project
        .containers
        .iter()
        .any(|c| store.pending.contains_key(&c.id))
}

/// Start-all / Stop-all and Restart-all. Returns the tasks to run when one
/// is clicked (empty otherwise).
fn project_buttons(
    ui: &mut egui::Ui,
    store: &Store,
    project: &ComposeProject,
    with_labels: bool,
) -> Vec<Task> {
    if is_busy(store, project) {
        ui.spinner();
        return Vec::new();
    }
    let any_running = project.running() > 0;
    let toggle = if any_running {
        (Action::Stop, Icon::Stop, "Stop all")
    } else {
        (Action::Start, Icon::Play, "Start all")
    };
    let mut clicked = None;
    for (action, icon, label) in [toggle, (Action::Restart, Icon::Restart, "Restart all")] {
        let response = if with_labels {
            widgets::button(ui, icon, label)
        } else {
            widgets::icon_button(ui, icon, label)
        };
        if response.clicked() {
            clicked = Some(action);
        }
    }
    clicked
        .map(|action| tasks_for(project, action))
        .unwrap_or_default()
}

/// One task per container the action applies to: stop only what runs,
/// start only what's stopped, restart everything.
fn tasks_for(project: &ComposeProject, action: Action) -> Vec<Task> {
    project
        .containers
        .iter()
        .filter(|c| match action {
            Action::Start => !c.is_active(),
            Action::Stop => c.is_active(),
            _ => true,
        })
        .map(|c| Task {
            action,
            id: c.id.clone(),
            name: c.name.clone(),
        })
        .collect()
}

fn remove_project_button(ui: &mut egui::Ui, ctx: &mut PageCtx, project: &ComposeProject) {
    if project.running() > 0 {
        ui.add_enabled(false, egui::Button::new("Remove"))
            .on_disabled_hover_text("Stop the project first");
    } else if widgets::danger_button(ui, ctx.palette, "Remove").clicked() {
        let tasks = tasks_for(project, Action::Remove(Resource::Containers));
        ctx.request(Intent::Confirm(Confirmation {
            title: format!("Remove project “{}”?", project.name),
            message: format!(
                "This removes its {}. Volumes, networks and images are kept, and \
                 `docker compose up` recreates everything.",
                plural(tasks.len(), "container")
            ),
            action_label: "Remove".into(),
            tasks,
            ..Default::default()
        }));
    }
}