From b667a91f8eb7f3192f2c02dce5dc774ab58a2736 Mon Sep 17 00:00:00 2001 From: "Sean P. Kelly" Date: Thu, 3 Aug 2023 07:05:47 +0000 Subject: [PATCH] Add schnauzer-v2 settings generator migrations --- Release.toml | 4 +- sources/Cargo.lock | 8 ++ sources/Cargo.toml | 1 + .../schnauzer-v2-generators/Cargo.toml | 15 ++++ .../v1.15.0/schnauzer-v2-generators/build.rs | 6 ++ .../schnauzer-v2-generators/src/main.rs | 78 +++++++++++++++++++ 6 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 sources/api/migration/migrations/v1.15.0/schnauzer-v2-generators/Cargo.toml create mode 100644 sources/api/migration/migrations/v1.15.0/schnauzer-v2-generators/build.rs create mode 100644 sources/api/migration/migrations/v1.15.0/schnauzer-v2-generators/src/main.rs diff --git a/Release.toml b/Release.toml index 0ff123f00ab..a13fef7ade8 100644 --- a/Release.toml +++ b/Release.toml @@ -216,4 +216,6 @@ version = "1.15.0" "migrate_v1.14.2_ecs-images-cleanup.lz4", ] "(1.14.2, 1.14.3)" = [] -"(1.14.3, 1.15.0)" = [] +"(1.14.3, 1.15.0)" = [ + "migrate_v1.15.0_schnauzer-v2-generators.lz4", +] diff --git a/sources/Cargo.lock b/sources/Cargo.lock index 699aff14b0c..d5bb1d2c326 100644 --- a/sources/Cargo.lock +++ b/sources/Cargo.lock @@ -3388,6 +3388,14 @@ dependencies = [ "url", ] +[[package]] +name = "schnauzer-v2-generators" +version = "0.1.0" +dependencies = [ + "bottlerocket-variant", + "migration-helpers", +] + [[package]] name = "scopeguard" version = "1.1.0" diff --git a/sources/Cargo.toml b/sources/Cargo.toml index 52aff72bffe..97bafde270f 100644 --- a/sources/Cargo.toml +++ b/sources/Cargo.toml @@ -53,6 +53,7 @@ members = [ "api/migration/migrations/v1.14.0/aws-control-container-v0-7-2", "api/migration/migrations/v1.14.0/public-control-container-v0-7-2", "api/migration/migrations/v1.14.2/ecs-images-cleanup", + "api/migration/migrations/v1.15.0/schnauzer-v2-generators", "bloodhound", diff --git a/sources/api/migration/migrations/v1.15.0/schnauzer-v2-generators/Cargo.toml b/sources/api/migration/migrations/v1.15.0/schnauzer-v2-generators/Cargo.toml new file mode 100644 index 00000000000..d5d7c08b138 --- /dev/null +++ b/sources/api/migration/migrations/v1.15.0/schnauzer-v2-generators/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "schnauzer-v2-generators" +version = "0.1.0" +authors = ["Sean P. Kelly "] +license = "Apache-2.0 OR MIT" +edition = "2021" +publish = false +# Don't rebuild crate just because of changes to README. +exclude = ["README.md"] + +[dependencies] +migration-helpers = { path = "../../../migration-helpers", version = "0.1.0" } + +[build-dependencies] +bottlerocket-variant = { version = "0.1", path = "../../../../../bottlerocket-variant" } diff --git a/sources/api/migration/migrations/v1.15.0/schnauzer-v2-generators/build.rs b/sources/api/migration/migrations/v1.15.0/schnauzer-v2-generators/build.rs new file mode 100644 index 00000000000..51d16cf1b4c --- /dev/null +++ b/sources/api/migration/migrations/v1.15.0/schnauzer-v2-generators/build.rs @@ -0,0 +1,6 @@ +use bottlerocket_variant::Variant; + +fn main() { + let variant = Variant::from_env().unwrap(); + variant.emit_cfgs(); +} diff --git a/sources/api/migration/migrations/v1.15.0/schnauzer-v2-generators/src/main.rs b/sources/api/migration/migrations/v1.15.0/schnauzer-v2-generators/src/main.rs new file mode 100644 index 00000000000..04a146d50ef --- /dev/null +++ b/sources/api/migration/migrations/v1.15.0/schnauzer-v2-generators/src/main.rs @@ -0,0 +1,78 @@ +use migration_helpers::common_migrations::{MetadataReplacement, ReplaceMetadataMigration}; +use migration_helpers::{migrate, Result}; +use std::process; + +fn build_metadata_migrations() -> Vec { + let mut migrations = vec![]; + + // On AWS platforms, we use regional ECR repositories. + // Elsewhere, we use ecr-public, which is global. + #[cfg(variant_platform = "aws")] + { + migrations.append(&mut vec![ + MetadataReplacement { + setting: "settings.host-containers.admin.source", + metadata: "setting-generator", + old_val: "schnauzer settings.host-containers.admin.source", + new_val: "schnauzer-v2 render --requires 'aws@v1(helpers[ecr-prefix])' --template '{{ ecr-prefix settings.aws.region }}/bottlerocket-admin:v0.10.1'", + }, + MetadataReplacement { + setting: "settings.host-containers.control.source", + metadata: "setting-generator", + old_val: "schnauzer settings.host-containers.control.source", + new_val: "schnauzer-v2 render --requires 'aws@v1(helpers=[ecr-prefix])' --template '{{ ecr-prefix settings.aws.region }}/bottlerocket-control:v0.7.2'", + }, + MetadataReplacement { + setting: "settings.updates.metadata-base-url", + metadata: "setting-generator", + old_val: "schnauzer settings.updates.metadata-base-url", + new_val: "schnauzer-v2 render --requires 'aws@v1' --requires 'updates@v1(helpers=[metadata-prefix, tuf-prefix])' --template '{{ tuf-prefix settings.aws.region }}{{ metadata-prefix settings.aws.region }}/2020-07-07/{{ os.variant_id }}/{{ os.arch }}/'", + }, + MetadataReplacement { + setting: "settings.updates.targets-base-url", + metadata: "setting-generator", + old_val: "schnauzer settings.updates.targets-base-url", + new_val: "schnauzer-v2 render --requires 'aws@v1' --requires 'updates@v1(helpers=[tuf-prefix])' --template '{{ tuf-prefix settings.aws.region }}/targets/'", + }, + ]); + } + #[cfg(not(variant_platform = "aws"))] + { + migrations.append(&mut vec![ + MetadataReplacement { + setting: "settings.updates.metadata-base-url", + metadata: "setting-generator", + old_val: "schnauzer settings.updates.metadata-base-url", + new_val: "schnauzer-v2 render --template 'https://updates.bottlerocket.aws/2020-07-07/{{ os.variant_id }}/{{ os.arch }}/'", + }, + ]); + } + + #[cfg(variant_family = "aws-k8s")] + { + migrations.append(&mut vec![ + MetadataReplacement { + setting: "settings.kubernetes.pod-infra-container-image", + metadata: "setting-generator", + old_val: "schnauzer settings.kubernetes.pod-infra-container-image", + new_val: "schnauzer-v2 render --requires 'aws@v1' --requires 'kubernetes@v1(helpers=[pause-prefix])' --template '{{ pause-prefix settings.aws.region }}/eks/pause:3.1-eksbuild.1'", + }, + ]); + } + + migrations +} + +fn run() -> Result<()> { + migrate(ReplaceMetadataMigration(build_metadata_migrations())) +} + +// Returning a Result from main makes it print a Debug representation of the error, but with Snafu +// we have nice Display representations of the error, so we wrap "main" (run) and print any error. +// https://github.com/shepmaster/snafu/issues/110 +fn main() { + if let Err(e) = run() { + eprintln!("{}", e); + process::exit(1); + } +}