|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +//! Types related to rack updates |
| 6 | +
|
| 7 | +use semver::Version; |
| 8 | +use serde::{Deserialize, Serialize}; |
| 9 | +use sha3::Digest; |
| 10 | +use sha3::Sha3_256; |
| 11 | +use snafu::prelude::*; |
| 12 | +use std::fs::File; |
| 13 | +use std::path::{Path, PathBuf}; |
| 14 | + |
| 15 | +#[derive(Debug, Snafu)] |
| 16 | +pub enum UpdateError { |
| 17 | + #[snafu(display("File access error: {}", path.display()))] |
| 18 | + Io { source: std::io::Error, path: PathBuf }, |
| 19 | + #[snafu(display("serde_json error: {}", path.display()))] |
| 20 | + Json { source: serde_json::Error, path: PathBuf }, |
| 21 | + #[snafu(display("Path must be relative: {}", path.display()))] |
| 22 | + RelativePath { path: PathBuf }, |
| 23 | +} |
| 24 | + |
| 25 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 26 | +pub struct Sha3_256Digest(#[serde(with = "hex::serde")] [u8; 32]); |
| 27 | + |
| 28 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 29 | +pub enum ArtifactType { |
| 30 | + // Sled Artifacts |
| 31 | + SledSp, |
| 32 | + SledRoT, |
| 33 | + HostPhase1, |
| 34 | + HostPhase2, |
| 35 | + |
| 36 | + // PSC Artifacts |
| 37 | + PscSp, |
| 38 | + PscRot, |
| 39 | + |
| 40 | + // Switch Artifacts |
| 41 | + SwitchSp, |
| 42 | + SwitchRot, |
| 43 | +} |
| 44 | + |
| 45 | +/// A description of a software artifact that can be installed on the rack |
| 46 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 47 | +pub struct Artifact { |
| 48 | + filename: PathBuf, |
| 49 | + artifact_type: ArtifactType, |
| 50 | + version: Version, |
| 51 | + digest: Sha3_256Digest, |
| 52 | + length: u64, |
| 53 | +} |
| 54 | + |
| 55 | +/// Attempt to convert an ArtifactSpec to an Artifact |
| 56 | +/// by reading from the filesysetm and hashing. |
| 57 | +impl TryFrom<ArtifactSpec> for Artifact { |
| 58 | + type Error = UpdateError; |
| 59 | + fn try_from(spec: ArtifactSpec) -> Result<Self, Self::Error> { |
| 60 | + let mut hasher = Sha3_256::new(); |
| 61 | + let mut file = File::open(&spec.filename) |
| 62 | + .context(IoSnafu { path: &spec.filename })?; |
| 63 | + let length = std::io::copy(&mut file, &mut hasher) |
| 64 | + .context(IoSnafu { path: &spec.filename })?; |
| 65 | + let digest = Sha3_256Digest(*hasher.finalize().as_ref()); |
| 66 | + Ok(Artifact { |
| 67 | + filename: spec.filename, |
| 68 | + artifact_type: spec.artifact_type, |
| 69 | + version: spec.version, |
| 70 | + digest, |
| 71 | + length, |
| 72 | + }) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/// User Input that describes artifacts as part of the [`RackUpdateSpec`] |
| 77 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 78 | +pub struct ArtifactSpec { |
| 79 | + filename: PathBuf, |
| 80 | + artifact_type: ArtifactType, |
| 81 | + version: Version, |
| 82 | +} |
| 83 | + |
| 84 | +/// The set of all artifacts in an update |
| 85 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 86 | +pub struct Manifest { |
| 87 | + version: Version, |
| 88 | + artifacts: Vec<Artifact>, |
| 89 | +} |
| 90 | + |
| 91 | +impl Manifest { |
| 92 | + pub fn dump<P: AsRef<Path>>( |
| 93 | + &self, |
| 94 | + path: P, |
| 95 | + ) -> Result<PathBuf, UpdateError> { |
| 96 | + let path = path.as_ref().join("manifest.json"); |
| 97 | + let mut file = File::create(&path).context(IoSnafu { path: &path })?; |
| 98 | + serde_json::to_writer(&mut file, self) |
| 99 | + .context(JsonSnafu { path: &path })?; |
| 100 | + Ok(path) |
| 101 | + } |
| 102 | + |
| 103 | + /// Unpack a Tar archive into `unpack_dir`, read the manifest, and return |
| 104 | + /// the manifest. |
| 105 | + pub fn load( |
| 106 | + tarfile_path: impl AsRef<Path>, |
| 107 | + unpack_dir: impl AsRef<Path>, |
| 108 | + ) -> Result<Manifest, UpdateError> { |
| 109 | + let tarfile = File::open(tarfile_path.as_ref()) |
| 110 | + .context(IoSnafu { path: tarfile_path.as_ref() })?; |
| 111 | + let mut archive = tar::Archive::new(tarfile); |
| 112 | + archive |
| 113 | + .unpack(&unpack_dir) |
| 114 | + .context(IoSnafu { path: tarfile_path.as_ref() })?; |
| 115 | + let path = unpack_dir.as_ref().join("manifest.json"); |
| 116 | + let file = File::open(&path).context(IoSnafu { path: &path })?; |
| 117 | + let manifest = |
| 118 | + serde_json::from_reader(file).context(JsonSnafu { path: &path })?; |
| 119 | + Ok(manifest) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +/// The user input description of a [`RackUpdate`] |
| 124 | +/// |
| 125 | +/// Files are read and processed into a [`RackUpdate`] according to the |
| 126 | +/// [`RackUpdateSpec`]. |
| 127 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 128 | +pub struct RackUpdateSpec { |
| 129 | + version: Version, |
| 130 | + artifacts: Vec<ArtifactSpec>, |
| 131 | +} |
| 132 | + |
| 133 | +impl RackUpdateSpec { |
| 134 | + /// Create a new RackUpdateSpec. |
| 135 | + /// |
| 136 | + /// Typically this will be created by reading from a `rack-update- |
| 137 | + /// spec.toml` file. |
| 138 | + pub fn new( |
| 139 | + version: Version, |
| 140 | + artifacts: Vec<ArtifactSpec>, |
| 141 | + ) -> RackUpdateSpec { |
| 142 | + RackUpdateSpec { version, artifacts } |
| 143 | + } |
| 144 | + |
| 145 | + /// Return the name of the given release |
| 146 | + pub fn release_name(&self) -> String { |
| 147 | + format!("oxide-release-{}", self.version) |
| 148 | + } |
| 149 | + |
| 150 | + /// Create a Tar archive file including a generated manifest and all release |
| 151 | + /// files described by `self.artifacts`. |
| 152 | + /// |
| 153 | + /// Return the path of the created archive. |
| 154 | + /// |
| 155 | + /// This archive file can be loaded into a [`RackUpdate`] via |
| 156 | + /// [`RackUpdate::load`]. |
| 157 | + pub fn create_archive( |
| 158 | + self, |
| 159 | + output_dir: PathBuf, |
| 160 | + ) -> Result<PathBuf, UpdateError> { |
| 161 | + let mut artifacts = vec![]; |
| 162 | + let mut filename = output_dir.clone(); |
| 163 | + filename.push(self.release_name()); |
| 164 | + filename.set_extension("tar"); |
| 165 | + let tarfile = File::create(&filename) |
| 166 | + .context(IoSnafu { path: filename.clone() })?; |
| 167 | + let mut builder = tar::Builder::new(tarfile); |
| 168 | + for artifact_spec in self.artifacts { |
| 169 | + builder |
| 170 | + .append_path_with_name( |
| 171 | + &artifact_spec.filename, |
| 172 | + &artifact_spec.filename.file_name().ok_or( |
| 173 | + UpdateError::RelativePath { |
| 174 | + path: artifact_spec.filename.clone(), |
| 175 | + }, |
| 176 | + )?, |
| 177 | + ) |
| 178 | + .context(IoSnafu { path: &artifact_spec.filename })?; |
| 179 | + artifacts.push(artifact_spec.try_into()?); |
| 180 | + } |
| 181 | + let manifest = Manifest { version: self.version, artifacts }; |
| 182 | + let manifest_path = manifest.dump(output_dir)?; |
| 183 | + builder |
| 184 | + .append_path_with_name( |
| 185 | + &manifest_path, |
| 186 | + &manifest_path.file_name().unwrap(), |
| 187 | + ) |
| 188 | + .context(IoSnafu { path: &manifest_path })?; |
| 189 | + builder.finish().context(IoSnafu { path: &manifest_path })?; |
| 190 | + Ok(filename) |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +#[cfg(test)] |
| 195 | +mod tests { |
| 196 | + use super::*; |
| 197 | + use std::fs::File; |
| 198 | + use std::io::Write; |
| 199 | + use tempfile::TempDir; |
| 200 | + |
| 201 | + fn test_spec() -> (TempDir, RackUpdateSpec) { |
| 202 | + let tmp_dir = TempDir::new().unwrap(); |
| 203 | + let sled_sp = ArtifactSpec { |
| 204 | + filename: tmp_dir.path().join("sled-sp-img-1.0.0.tar.gz"), |
| 205 | + artifact_type: ArtifactType::SledSp, |
| 206 | + version: Version::new(1, 0, 0), |
| 207 | + }; |
| 208 | + |
| 209 | + // Create a file of junk data for testing |
| 210 | + let mut file = File::create(&sled_sp.filename).unwrap(); |
| 211 | + writeln!( |
| 212 | + file, |
| 213 | + "This is not a real SP Image. Hell it's not even a tarball!" |
| 214 | + ) |
| 215 | + .unwrap(); |
| 216 | + let spec = RackUpdateSpec::new(Version::new(1, 0, 0), vec![sled_sp]); |
| 217 | + |
| 218 | + (tmp_dir, spec) |
| 219 | + } |
| 220 | + |
| 221 | + #[test] |
| 222 | + fn generate_update_archive_then_load_manifest() { |
| 223 | + let (input_dir, spec) = test_spec(); |
| 224 | + let output_dir = TempDir::new().unwrap(); |
| 225 | + let update_path = |
| 226 | + spec.create_archive(input_dir.path().to_owned()).unwrap(); |
| 227 | + let manifest = Manifest::load(&update_path, output_dir.path()).unwrap(); |
| 228 | + assert_eq!(manifest.artifacts.len(), 1); |
| 229 | + assert_eq!(manifest.version, Version::new(1, 0, 0)); |
| 230 | + } |
| 231 | +} |
0 commit comments