Skip to content

Commit

Permalink
extensions: Write JSON to output dir
Browse files Browse the repository at this point in the history
Let's include the final extensions file in JSON format as part of the
output directory. A key difference from the input file (apart from YAML
vs JSON) is that this is post-filtering, so any extensions which were
removed because the architecture does not match are not present.

This JSON file will be used by cosa and the MCO. See discussions in:
openshift/os#409
  • Loading branch information
jlebon committed Jan 26, 2021
1 parent ea81a1e commit e958263
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
17 changes: 14 additions & 3 deletions rust/src/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use anyhow::{bail, Context, Result};
use openat_ext::OpenatDirExt;
use serde_derive::Deserialize;
use serde_derive::{Deserialize, Serialize};
use std::collections::HashMap;

use crate::cxxrsutil::*;
Expand All @@ -17,17 +17,19 @@ use crate::utils;

const RPMOSTREE_EXTENSIONS_STATE_FILE: &str = ".rpm-ostree-state-chksum";

#[derive(Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "kebab-case")]
pub struct Extensions {
extensions: HashMap<String, Extension>,
}

#[derive(Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "kebab-case")]
pub struct Extension {
packages: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
architectures: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
match_base_evr: Option<String>,
}

Expand Down Expand Up @@ -107,6 +109,15 @@ impl Extensions {
.write_file_contents(RPMOSTREE_EXTENSIONS_STATE_FILE, 0o644, chksum)
.with_context(|| format!("updating state file {}", RPMOSTREE_EXTENSIONS_STATE_FILE))?)
}

pub(crate) fn serialize_to_dir(&self, output_dir: &str) -> CxxResult<()> {
let output_dir = openat::Dir::open(output_dir)?;
Ok(output_dir
.write_file_with("extensions.json", 0o644, |w| -> Result<_> {
Ok(serde_json::to_writer_pretty(w, self)?)
})
.context("while serializing")?)
}
}

#[cfg(test)]
Expand Down
1 change: 1 addition & 0 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ mod ffi {
fn get_packages(&self) -> Vec<String>;
fn state_checksum_changed(&self, chksum: &str, output_dir: &str) -> Result<bool>;
fn update_state_checksum(&self, chksum: &str, output_dir: &str) -> Result<()>;
fn serialize_to_dir(&self, output_dir: &str) -> Result<()>;
}

// rpmutils.rs
Expand Down
1 change: 1 addition & 0 deletions src/app/rpmostree-compose-builtin-tree.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1612,6 +1612,7 @@ rpmostree_compose_builtin_extensions (int argc,
}

extensions->update_state_checksum (state_checksum, opt_extensions_output_dir);
extensions->serialize_to_dir (opt_extensions_output_dir);
if (!process_touch_if_changed (error))
return FALSE;

Expand Down
8 changes: 8 additions & 0 deletions tests/compose/test-basic-unified.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ extensions:
packages:
- dodo
- solitaire
another-arch:
packages:
- nonexistent
architectures:
- badarch
EOF

# we don't actually need root here, but in CI the cache may be in a qcow2 and
Expand All @@ -106,6 +111,9 @@ runasroot rpm-ostree compose extensions --repo=${repo} \

ls extensions/{dodo-1.0,dodo-base-1.0,solitaire-1.0}-*.rpm
test -f extensions-changed
assert_jq extensions/extensions.json \
'.extensions|length == 1' \
'.extensions["extinct-birds"]'
echo "ok extensions"

rm extensions-changed
Expand Down

0 comments on commit e958263

Please sign in to comment.