ferrodock 1.32.1

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
407
408
409
//! Networks page: Docker networks, their addressing and attached containers.

use eframe::egui;

use super::create_dialog::{CreateForm, Kind};
use super::marks::{Item, Marks};
use super::{confirm_removal, inspect_section, remove_button, remove_task, remove_unused_button};
use crate::app::components::icons::Icon;
use crate::app::components::{details, table, widgets};
use crate::app::page::{Command, Intent, Page, PageCtx, toggle_selection};
use crate::app::store::Store;
use crate::docker::{Change, NetworkInfo, Resource, Task, plural, time_ago};

#[derive(Default)]
pub struct NetworksPage {
    selected: Option<String>,
    /// The New network dialog, while it's open.
    create: Option<CreateForm>,
    /// The container picked in "Connect a container".
    connect: String,
    /// Rows ticked for removing together.
    marks: Marks,
    /// Whether Docker's own networks (bridge, host, none) are listed too.
    show_builtin: bool,
}

impl NetworksPage {
    fn selected<'a>(&self, store: &'a Store) -> Option<&'a NetworkInfo> {
        let id = self.selected.as_deref()?;
        store.networks.iter().find(|n| n.id == id)
    }
}

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

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

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

    /// Your own networks: Docker's built-in ones are always there.
    fn badge(&self, store: &Store) -> String {
        custom_count(store).to_string()
    }

    fn summary(&self, store: &Store) -> String {
        let builtin = store.networks.len() - custom_count(store);
        format!(
            "{} ยท {builtin} built into Docker",
            plural(custom_count(store), "network")
        )
    }

    fn search_hint(&self) -> &'static str {
        "Search name, driver or subnet"
    }

    fn toolbar(&mut self, ui: &mut egui::Ui, ctx: &mut PageCtx) {
        remove_unused_button(ui, ctx, "network", unused(ctx.store));
        if widgets::button(ui, Icon::Network, "New network").clicked() {
            self.create = Some(CreateForm::new(Kind::Network));
        }
    }

    fn has_overlay(&self) -> bool {
        self.create.is_some()
    }

    fn show(&mut self, ui: &mut egui::Ui, ctx: &mut PageCtx) {
        super::create_dialog(ui, ctx, &mut self.create);
        let p = ctx.palette;
        let store = ctx.store;
        // Docker's own networks only on request (or when searched for):
        // they're always there and can't be changed much.
        let with_builtin = self.show_builtin || !ctx.search_is_empty();
        let mut visible: Vec<&NetworkInfo> = store
            .networks
            .iter()
            .filter(|n| with_builtin || !n.is_builtin())
            .filter(|n| ctx.matches_search(&[&n.name, &n.driver, &n.subnets.join(" ")]))
            .collect();
        // Yours first, then Docker's.
        visible.sort_by_key(|n| n.is_builtin());
        let builtin = store.networks.len() - custom_count(store);
        if visible.is_empty() && ctx.search_is_empty() && !store.networks.is_empty() {
            if widgets::empty_state(
                ui,
                p,
                Icon::Network,
                "No networks of your own yet",
                "Containers join Docker's built-in bridge network unless you make one. \
                 Make one to let a group of containers reach each other by name.",
                Some("New network"),
            ) {
                self.create = Some(CreateForm::new(Kind::Network));
            }
            ui.vertical_centered(|ui| self.builtin_toggle(ui, builtin));
            return;
        }
        if visible.is_empty() {
            super::nothing_to_show(
                ui,
                ctx,
                !store.networks.is_empty(),
                (
                    Icon::Network,
                    "No networks",
                    "Docker's own networks appear here once it's running.",
                ),
                None,
            );
            return;
        }

        let items: Vec<Item> = visible.iter().map(|n| item(store, n)).collect();
        self.marks
            .bar(ui, ctx, "network", Resource::Networks, &items);
        let marks = &self.marks;
        let mut ticks = Vec::new();
        // With the details pane open, the subnet stays longest; all three
        // are in the details pane too.
        let fit = table::fitting(ui, &[28.0], &[150.0, 100.0, 130.0]);
        let (show_subnet, show_attached, show_created) = (fit[0], fit[1], fit[2]);
        let mut columns = vec![marks.column(&items), table::Col::fill("NAME")];
        if show_subnet {
            columns.push(table::Col::fixed("SUBNET", 150.0));
        }
        if show_attached {
            columns.push(table::Col::fixed("CONTAINERS", 100.0));
        }
        if show_created {
            columns.push(table::Col::fixed("CREATED", 130.0));
        }
        let selected = self.selected.clone();
        let clicked = table::show(
            ui,
            p,
            "networks",
            columns,
            visible.len(),
            |i| selected.as_deref() == Some(visible[i].id.as_str()) || marks.contains(items[i].id),
            |i| format!("Network {}", visible[i].name),
            |i, row| {
                let net = visible[i];
                let attached = store.containers_on_network(&net.name).len();
                // Docker's own can't be removed, so there's nothing to tick.
                if net.is_builtin() {
                    row.col(|_| {});
                } else {
                    row.col(|ui| ticks.extend(marks.cell(ui, p, &items[i])));
                }
                let line = builtin_role(&net.name).unwrap_or(&net.driver);
                row.col(|ui| table::with_icon(ui, p, Icon::Network, p.muted, &net.name, line));
                if show_subnet {
                    row.col(|ui| table::text(ui, p, &widgets::join_or_dash(&net.subnets)));
                }
                if show_attached {
                    row.col(|ui| table::text(ui, p, &attached.to_string()));
                }
                if show_created {
                    row.col(|ui| {
                        table::muted(ui, p, &net.created.map(time_ago).unwrap_or_default())
                    });
                }
            },
        );
        self.marks.apply(ui, "networks", &items, ticks);
        if let Some(i) = clicked {
            toggle_selection(&mut self.selected, visible[i].id.clone());
        }
        if ctx.search_is_empty() {
            ui.add_space(8.0);
            self.builtin_toggle(ui, builtin);
        }
    }

    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 Some(net) = self.selected(ctx.store) else {
            return;
        };
        let attached = ctx.store.containers_on_network(&net.name);

        if details::header(ui, p, &net.name) {
            self.selected = None;
        }
        if let Some(role) = builtin_role(&net.name) {
            // Nothing to remove: say what it is instead.
            widgets::pill(ui, "Built into Docker", p.muted);
            ui.add_space(6.0);
            ui.label(egui::RichText::new(role).color(p.text));
            ui.label(
                egui::RichText::new("Every Docker engine has it, and it can't be removed.")
                    .size(12.5)
                    .color(p.muted),
            );
        } else {
            ui.add_space(10.0);
            let blocked = (!attached.is_empty()).then_some("Containers are attached to it");
            let task = remove_task(Resource::Networks, &net.id, &net.name);
            if !ctx.store.pending.contains_key(&net.id) {
                remove_button(ui, ctx, "network", task, blocked);
            } else {
                ui.spinner();
            }
        }
        ui.add_space(16.0);

        egui::ScrollArea::vertical()
            .auto_shrink(false)
            .show(ui, |ui| {
                details::section(ui, p, "Overview", |ui| {
                    details::copy_field(ui, p, "ID", net.short_id(), &net.id);
                    details::field(ui, p, "Driver", driver_label(&net.driver));
                    details::field(ui, p, "Scope", &net.scope);
                    details::field(ui, p, "Internal", if net.internal { "Yes" } else { "No" });
                    details::field(
                        ui,
                        p,
                        "Created",
                        &net.created.map(time_ago).unwrap_or_default(),
                    );
                });
                details::section(ui, p, "Addressing", |ui| {
                    details::field(ui, p, "Subnet", &widgets::join_or_dash(&net.subnets));
                    details::field(ui, p, "Gateway", &widgets::join_or_dash(&net.gateways));
                });
                connected(ui, ctx, net, &attached, &mut self.connect);
                inspect_section(ui, ctx, Resource::Networks, &net.id);
            });
    }

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

    fn select_all(&mut self, ctx: &PageCtx) {
        let items: Vec<Item> = ctx
            .store
            .networks
            .iter()
            .filter(|n| !n.is_builtin())
            .filter(|n| ctx.matches_search(&[&n.name, &n.driver, &n.subnets.join(" ")]))
            .map(|n| item(ctx.store, n))
            .collect();
        self.marks.select_all(&items);
    }

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

    fn commands(&self, store: &Store) -> Vec<Command> {
        let mut list: Vec<Command> = store
            .networks
            .iter()
            .map(|n| Command::select(&n.name, format!("Network ยท {}", n.driver), &n.id))
            .collect();
        let unused = unused(store);
        if !unused.is_empty() {
            list.push(Command::intent(
                "Remove unused networks",
                plural(unused.len(), "network"),
                confirm_removal("network", unused),
            ));
        }
        list
    }
}

impl NetworksPage {
    /// "Show Docker's built-in networks (3)", or Hide them again.
    fn builtin_toggle(&mut self, ui: &mut egui::Ui, count: usize) {
        if count == 0 {
            return;
        }
        let label = if self.show_builtin {
            "Hide Docker's built-in networks".to_owned()
        } else {
            format!("Show Docker's built-in networks ({count})")
        };
        if ui
            .link(label)
            .on_hover_text("bridge, host and none: every Docker engine has them")
            .clicked()
        {
            self.show_builtin = !self.show_builtin;
        }
    }
}

/// How many networks are your own, not Docker's.
fn custom_count(store: &Store) -> usize {
    store.networks.iter().filter(|n| !n.is_builtin()).count()
}

/// What one of Docker's own networks is for, in plain words.
fn builtin_role(name: &str) -> Option<&'static str> {
    Some(match name {
        "bridge" => "The default network: containers join it unless told otherwise.",
        "host" => "Containers on it share this computer's network directly.",
        "none" => "Containers on it have no network at all.",
        _ => return None,
    })
}

/// Docker calls the `none` network's driver "null".
fn driver_label(driver: &str) -> &str {
    if driver == "null" {
        "none (no networking)"
    } else {
        driver
    }
}

/// A network as a row to tick; built-in ones count as in use, since they
/// can't be removed.
fn item<'a>(store: &Store, n: &'a NetworkInfo) -> Item<'a> {
    Item {
        id: &n.id,
        name: &n.name,
        in_use: n.is_builtin() || !store.containers_on_network(&n.name).is_empty(),
    }
}

fn unused(store: &Store) -> Vec<Task> {
    store
        .networks
        .iter()
        .filter(|n| !n.is_builtin() && store.containers_on_network(&n.name).is_empty())
        .map(|n| remove_task(Resource::Networks, &n.id, &n.name))
        .collect()
}

/// The containers on a network, each with Disconnect, and a picker to
/// connect another. Docker's `host` and `none` networks can't be changed.
fn connected(
    ui: &mut egui::Ui,
    ctx: &mut PageCtx,
    net: &NetworkInfo,
    attached: &[&str],
    pick: &mut String,
) {
    let p = ctx.palette;
    let fixed = matches!(net.name.as_str(), "host" | "none");
    details::section(ui, p, "Connected", |ui| {
        if attached.is_empty() {
            details::field(ui, p, "Containers", "");
        }
        for &name in attached {
            ui.label(egui::RichText::new(name).color(p.text));
            let id = format!("{}+{name}", net.name);
            if ctx.store.pending.contains_key(&id) {
                ui.spinner();
            } else if !fixed && ui.small_button("Disconnect").clicked() {
                ctx.request(Intent::Change(Change::Disconnect {
                    network: net.name.clone(),
                    container: name.to_owned(),
                }));
            } else if fixed {
                ui.label("");
            }
            ui.end_row();
        }
    });
    if fixed {
        return;
    }
    let others: Vec<&str> = ctx
        .store
        .containers
        .iter()
        .map(|c| c.name.as_str())
        .filter(|name| !attached.contains(name))
        .collect();
    if others.is_empty() {
        return;
    }
    if !others.contains(&pick.as_str()) {
        *pick = others[0].to_owned();
    }
    ui.horizontal(|ui| {
        egui::ComboBox::from_id_salt(("connect", &net.id))
            .selected_text(pick.as_str())
            .show_ui(ui, |ui| {
                for name in &others {
                    ui.selectable_value(pick, (*name).to_owned(), *name);
                }
            });
        if ui.button("Connect").clicked() {
            ctx.request(Intent::Change(Change::Connect {
                network: net.name.clone(),
                container: pick.clone(),
            }));
        }
    });
    ui.add_space(14.0);
}