Skip to content

Commit

Permalink
treefile: Add new add-commit-metadata key
Browse files Browse the repository at this point in the history
Add support for a new `add-commit-metadata` key in the treefile so that
we can directly specify commit metadata we want to inject from there.

This will be useful in Fedora CoreOS, where we'll have separate
treefiles for each streams, each with stream-specific metadata values
required.
  • Loading branch information
jlebon committed Jul 8, 2019
1 parent 3a3f2d3 commit 9142595
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 5 deletions.
45 changes: 43 additions & 2 deletions rust/src/treefile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,20 @@ fn merge_vec_field<T>(dest: &mut Option<Vec<T>>, src: &mut Option<Vec<T>>) {
}
}

/// Merge a map field similarly to Python's `dict.update()`. In case of
/// duplicate keys, `dest` wins (`src` is the "included" config).
fn merge_map_field<T>(
dest: &mut Option<HashMap<String, T>>,
src: &mut Option<HashMap<String, T>>)
{
if let Some(mut srcv) = src.take() {
if let Some(mut destv) = dest.take() {
srcv.extend(destv.drain());
}
*dest = Some(srcv);
}
}

/// Given two configs, merge them.
fn treefile_merge(dest: &mut TreeComposeConfig, src: &mut TreeComposeConfig) {
macro_rules! merge_basics {
Expand All @@ -291,6 +305,11 @@ fn treefile_merge(dest: &mut TreeComposeConfig, src: &mut TreeComposeConfig) {
$( merge_vec_field(&mut dest.$field, &mut src.$field); )*
}};
};
macro_rules! merge_maps {
( $($field:ident),* ) => {{
$( merge_map_field(&mut dest.$field, &mut src.$field); )*
}};
};

merge_basics!(
treeref,
Expand Down Expand Up @@ -328,6 +347,9 @@ fn treefile_merge(dest: &mut TreeComposeConfig, src: &mut TreeComposeConfig) {
remove_files,
remove_from_packages
);
merge_maps!(
add_commit_metadata
);
}

/// Merge the treefile externals. There are currently only two keys that
Expand Down Expand Up @@ -663,6 +685,9 @@ struct TreeComposeConfig {
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "remove-from-packages")]
remove_from_packages: Option<Vec<Vec<String>>>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "add-commit-metadata")]
add_commit_metadata: Option<HashMap<String, serde_json::Value>>,

#[serde(flatten)]
legacy_fields: LegacyTreeComposeConfigFields,
Expand Down Expand Up @@ -1011,24 +1036,40 @@ rojig:
#[test]
fn test_treefile_merge() {
let basearch = Some(ARCH_X86_64);
let mut base_input = io::BufReader::new(VALID_PRELUDE.as_bytes());
let mut base = treefile_parse_stream(InputFormat::YAML, &mut base_input, basearch).unwrap();
let mut base = append_and_parse(
r###"
add-commit-metadata:
my-first-key: "please don't override me"
my-second-key: "override me"
"###,
);
let mut mid_input = io::BufReader::new(
r###"
packages:
- some layered packages
add-commit-metadata:
my-second-key: "something better"
my-third-key: 1000
my-fourth-key:
nested: table
"###
.as_bytes(),
);
let mut mid = treefile_parse_stream(InputFormat::YAML, &mut mid_input, basearch).unwrap();
let mut top_input = io::BufReader::new(ROJIG_YAML.as_bytes());
let mut top = treefile_parse_stream(InputFormat::YAML, &mut top_input, basearch).unwrap();
assert!(top.add_commit_metadata.is_none());
treefile_merge(&mut mid, &mut base);
treefile_merge(&mut top, &mut mid);
let tf = &top;
assert!(tf.packages.as_ref().unwrap().len() == 8);
let rojig = tf.rojig.as_ref().unwrap();
assert!(rojig.name == "exampleos");
let data = tf.add_commit_metadata.as_ref().unwrap();
assert!(data.get("my-first-key").unwrap().as_str().unwrap() == "please don't override me");
assert!(data.get("my-second-key").unwrap().as_str().unwrap() == "something better");
assert!(data.get("my-third-key").unwrap().as_i64().unwrap() == 1000);
assert!(data.get("my-fourth-key").unwrap().as_object().unwrap().get("nested").unwrap().as_str().unwrap() == "table");
}

#[test]
Expand Down
14 changes: 14 additions & 0 deletions src/app/rpmostree-compose-builtin-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -711,19 +711,33 @@ rpm_ostree_compose_context_new (const char *treefile_pathstr,

self->metadata = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
(GDestroyNotify)g_variant_unref);

/* metadata from the treefile itself has the lowest priority */
JsonNode *add_commit_metadata_node =
json_object_get_member (self->treefile, "add-commit-metadata");
if (add_commit_metadata_node)
{
if (!rpmostree_composeutil_read_json_metadata (add_commit_metadata_node,
self->metadata, error))
return FALSE;
}

/* then --add-metadata-from-json */
if (opt_metadata_json)
{
if (!rpmostree_composeutil_read_json_metadata_from_file (opt_metadata_json,
self->metadata, error))
return FALSE;
}

/* and finally --add-metadata-string */
if (opt_metadata_strings)
{
if (!parse_metadata_keyvalue_strings (opt_metadata_strings, self->metadata, error))
return FALSE;
}


g_auto(GStrv) layers = ror_treefile_get_all_ostree_layers (self->treefile_rs);
if (layers && *layers && !opt_unified_core)
return glnx_throw (error, "ostree-layers requires unified-core mode");
Expand Down
4 changes: 4 additions & 0 deletions tests/compose-tests/libbasic-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ ostree --repo=${repobuild} show --print-metadata-key exampleos.tests ${treeref}
assert_file_has_content meta.txt 'smoketested.*e2e'
ostree --repo=${repobuild} show --print-metadata-key rpmostree.rpmmd-repos ${treeref} > meta.txt
assert_file_has_content meta.txt 'id.*fedora.*timestamp'
ostree --repo=${repobuild} show --print-metadata-key foobar ${treeref} > meta.txt
assert_file_has_content meta.txt 'bazboo'
ostree --repo=${repobuild} show --print-metadata-key overrideme ${treeref} > meta.txt
assert_file_has_content meta.txt 'new val'
echo "ok metadata"

ostree --repo=${repobuild} ls -R ${treeref} /usr/lib/ostree-boot > bootls.txt
Expand Down
3 changes: 2 additions & 1 deletion tests/compose-tests/test-basic-unified.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ cat > metadata.json <<EOF
"rev": "97ec21c614689e533d294cdae464df607b526ab9",
"src": "https://gitlab.com/exampleos/custom-atomic-host"
},
"exampleos.tests": ["smoketested", "e2e"]
"exampleos.tests": ["smoketested", "e2e"],
"overrideme": "new val"
}
EOF
runcompose --ex-unified-core --add-metadata-from-json metadata.json
Expand Down
3 changes: 2 additions & 1 deletion tests/compose-tests/test-basic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ cat > metadata.json <<EOF
"rev": "97ec21c614689e533d294cdae464df607b526ab9",
"src": "https://gitlab.com/exampleos/custom-atomic-host"
},
"exampleos.tests": ["smoketested", "e2e"]
"exampleos.tests": ["smoketested", "e2e"],
"overrideme": "new val"
}
EOF
runcompose --add-metadata-from-json metadata.json
Expand Down
6 changes: 5 additions & 1 deletion tests/composedata/fedora-base.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,9 @@
"ignore-removed-users": ["root"],
"ignore-removed-groups": ["root"],
"check-passwd": { "type": "file", "filename": "passwd" },
"check-groups": { "type": "file", "filename": "group" }
"check-groups": { "type": "file", "filename": "group" },
"add-commit-metadata": {
"foobar": "bazboo",
"overrideme": "old var"
}
}

0 comments on commit 9142595

Please sign in to comment.