aboutsummaryrefslogtreecommitdiff
path: root/src/images.rs
blob: 11daaea738e8cfc425c52cfc90ddb9cfa28b3c7b (plain)
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
// Copyright (C) 2026 Doan Luu Duc Tai <doan@luuduct.ai>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use std::collections::BTreeMap;
use std::fs::{self, File};
use std::path::Path;

use anyhow::{Context, Result, ensure};
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::project::{Mode, Project, Settings, field};
use crate::{files, process};

#[derive(Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
struct Image {
    reference: String,
    architecture: String,
    layers: Value,
    id: String,
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
struct ImageLock {
    format: u32,
    platform: String,
    services: BTreeMap<String, Image>,
}

fn inspect(project: &Project, name: &str) -> Result<Value> {
    let records: Vec<Value> = serde_json::from_slice(
        &process::capture(project.command("podman")?.args(["image", "inspect", name]))?.stdout,
    )?;
    records
        .into_iter()
        .next()
        .context("Docker returned no image")
}

pub(crate) fn save(project: &Project, containers: &[Value], destination: &Path) -> Result<()> {
    fs::create_dir_all(destination)?;
    let platform = project.platform()?;
    let mut services = BTreeMap::new();
    let mut new_tags = Vec::new();
    let result = save_inner(
        project,
        containers,
        destination,
        &mut services,
        &mut new_tags,
    );
    for tag in new_tags {
        if let Err(error) = process::capture(project.command("podman")?.args(["image", "rm", &tag]))
        {
            process::error(format_args!(
                "Could not remove temporary image tag {tag}: {error:#}"
            ))?;
        }
    }
    result?;
    let lock = ImageLock {
        format: 2,
        platform,
        services,
    };
    files::write(
        &destination.join("image-lock.json"),
        &serde_json::to_vec_pretty(&lock)?,
        0o600,
    )
}

fn save_inner(
    project: &Project,
    containers: &[Value],
    destination: &Path,
    services: &mut BTreeMap<String, Image>,
    new_tags: &mut Vec<String>,
) -> Result<()> {
    for container in containers {
        let service = field(container, &["Config", "Labels", "io.stack.service"])?
            .as_str()
            .context("Missing service label")?;
        let id = field(container, &["Image"])?
            .as_str()
            .context("Missing image ID")?;
        let suffix: String = id.trim_start_matches("sha256:").chars().take(16).collect();
        let alias = format!("localhost/stack-export-{service}:{suffix}");
        if !process::available(
            project
                .command("podman")?
                .args(["image", "inspect", &alias]),
        ) {
            process::capture(
                project
                    .command("podman")?
                    .args(["image", "tag", id, &alias]),
            )?;
            new_tags.push(alias.clone());
        }
        let actual = inspect(project, &alias)?;
        services.insert(
            service.to_owned(),
            Image {
                reference: alias,
                architecture: field(&actual, &["Architecture"])?
                    .as_str()
                    .context("Missing architecture")?
                    .to_owned(),
                layers: field(&actual, &["RootFS", "Layers"])?.clone(),
                id: field(&actual, &["Id"])?
                    .as_str()
                    .context("Missing image ID")?
                    .trim_start_matches("sha256:")
                    .to_owned(),
            },
        );
    }
    let mut command = project.command("podman")?;
    command
        .args([
            "save",
            "--multi-image-archive",
            "--format",
            "docker-archive",
            "--output",
        ])
        .arg(destination.join("images.tar"));
    for image in services.values() {
        command.arg(&image.reference);
    }
    process::run(&mut command)
}

pub(crate) fn prepare(project: &Project, settings: &mut Settings) -> Result<()> {
    let path = project.root.join("var/image-lock.json");
    if settings.online || !path.is_file() {
        return Ok(());
    }
    let lock: ImageLock = serde_json::from_reader(File::open(path)?)?;
    ensure!(lock.format == 2, "Unsupported image lock format");
    let platform = project.platform()?;
    if platform.trim() != lock.platform {
        ensure!(
            settings.mode == Mode::Local,
            "Release images target {}, but this server is {}; build a native release locally",
            lock.platform,
            platform.trim()
        );
        settings.online = true;
        return process::line(format_args!(
            "Using native images/builds for {}",
            platform.trim()
        ));
    }
    let missing = lock.services.values().any(|item| {
        project.command("podman").is_ok_and(|mut command| {
            !process::available(command.args(["image", "inspect", &item.reference]))
        })
    });
    if missing {
        let archive = project.root.join("var/images.tar");
        ensure!(
            archive.is_file(),
            "Release image archive is missing; deploy a complete release"
        );
        process::run(
            project
                .command("podman")?
                .args(["image", "load", "--input"])
                .arg(archive),
        )?;
    }
    for (service, image) in lock.services {
        let actual = inspect(project, &image.reference)?;
        ensure!(
            field(&actual, &["Architecture"])?.as_str() == Some(image.architecture.as_str())
                && field(&actual, &["RootFS", "Layers"])? == &image.layers
                && field(&actual, &["Id"])?
                    .as_str()
                    .context("Missing image ID")?
                    .trim_start_matches("sha256:")
                    == image.id,
            "Loaded image does not match its content lock: {service}"
        );
    }
    Ok(())
}

pub(crate) fn configured(project: &Project, settings: &Settings) -> Result<Vec<Value>> {
    let config = project.configuration(settings)?;
    let mut records = Vec::new();
    for (service, value) in config.services {
        let actual = inspect(project, &value.image)?;
        records.push(serde_json::json!({
            "Image": field(&actual, &["Id"])?,
            "Config": {"Labels": {"io.stack.service": service}}
        }));
    }
    Ok(records)
}