Skip to content

Commit

Permalink
migrations: add support for bootstrap commands
Browse files Browse the repository at this point in the history
  • Loading branch information
piyush-jena committed Aug 31, 2024
1 parent 39f91d5 commit ba0fe84
Show file tree
Hide file tree
Showing 17 changed files with 140 additions and 1 deletion.
2 changes: 2 additions & 0 deletions sources/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ members = [
"settings-migrations/v1.21.1/public-admin-container-v0-11-10",
"settings-migrations/v1.21.1/aws-control-container-v0-7-14",
"settings-migrations/v1.21.1/public-control-container-v0-7-14",
"settings-migrations/v1.22.0/bootstrap-commands-config-file-v0-1-0",
"settings-migrations/v1.22.0/bootstrap-commands-services-cfg-v0-1-0",

"settings-plugins/aws-dev",
"settings-plugins/aws-ecs-1",
Expand Down
55 changes: 54 additions & 1 deletion sources/api/schnauzer/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,18 @@ mod error {
source: std::io::Error,
},

#[snafu(display(
"Unable to encode input '{}' from template '{}' as toml: {}",
value,
source,
template
))]
TomlEncode {
value: serde_json::Value,
source: serde_json::Error,
template: String,
},

#[snafu(display("Unknown architecture '{}' given to goarch helper", given))]
UnknownArch { given: String },

Expand Down Expand Up @@ -958,7 +970,7 @@ pub fn join_array(
"\"{}\"",
value.as_str().context(error::JoinStringsWrongTypeSnafu {
expected_type: "string",
value: array.to_owned(),
value: value.to_owned(),
template: template_name,
})?
)
Expand All @@ -975,6 +987,47 @@ pub fn join_array(
Ok(())
}

/// `toml_encode` accepts arbitrary input and encodes it as a toml string
///
/// # Example
///
/// Consider an array of values: `[ "a", "b", "c" ]` stored in a setting such as
/// `settings.somewhere.foo-list`. In our template we can write:
/// `{{ toml_encode settings.somewhere.foo-list }}`
///
/// This will render `["a", "b", "c"]`.
pub fn toml_encode(
helper: &Helper<'_, '_>,
_: &Handlebars,
_: &Context,
renderctx: &mut RenderContext<'_, '_>,
out: &mut dyn Output,
) -> Result<(), RenderError> {
trace!("Starting toml_encode helper");
let template_name = template_name(renderctx);
check_param_count(helper, template_name, 1)?;

// get the string
let encode_param = get_param(helper, 0)?;
let toml_value: toml::Value =
serde_json::from_value(encode_param.to_owned()).with_context(|_| {
error::TomlEncodeSnafu {
value: encode_param.to_owned(),
template: template_name,
}
})?;

let result = toml_value.to_string();

// write it to the template
out.write(&result)
.with_context(|_| error::TemplateWriteSnafu {
template: template_name.to_owned(),
})?;

Ok(())
}

/// kube_reserve_memory and kube_reserve_cpu are taken from EKS' calculations.
/// https://github.com/awslabs/amazon-eks-ami/blob/db28da15d2b696bc08ac3aacc9675694f4a69933/files/bootstrap.sh

Expand Down
1 change: 1 addition & 0 deletions sources/api/schnauzer/src/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ pub fn build_template_registry() -> Result<handlebars::Handlebars<'static>> {
template_registry.register_helper("host", Box::new(helpers::host));
template_registry.register_helper("goarch", Box::new(helpers::goarch));
template_registry.register_helper("join_array", Box::new(helpers::join_array));
template_registry.register_helper("toml_encode", Box::new(helpers::toml_encode));
template_registry.register_helper("kube_reserve_cpu", Box::new(helpers::kube_reserve_cpu));
template_registry.register_helper(
"kube_reserve_memory",
Expand Down
1 change: 1 addition & 0 deletions sources/api/schnauzer/src/v2/import/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ fn all_helpers() -> HashMap<ExtensionName, HashMap<HelperName, Box<dyn HelperDef
"base64_decode" => helper!(handlebars_helpers::base64_decode),
"default" => helper!(handlebars_helpers::default),
"join_array" => helper!(handlebars_helpers::join_array),
"toml_encode" => helper!(handlebars_helpers::toml_encode),
"join_map" => helper!(handlebars_helpers::join_map),
"if_not_null" => Box::new(handlebars_helpers::IfNotNullHelper),
"goarch" => helper!(handlebars_helpers::goarch),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "bootstrap-commands-config-file-v0-1-0"
version = "0.1.0"
edition = "2021"
authors = ["Piyush Jena <[email protected]>"]
license = "Apache-2.0 OR MIT"
publish = false
exclude = ["README.md"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
migration-helpers.workspace = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use migration_helpers::common_migrations::AddPrefixesMigration;
use migration_helpers::{migrate, Result};
use std::process;

fn run() -> Result<()> {
migrate(AddPrefixesMigration(vec![
"settings.bootstrap-commands",
"services.bootstrap-commands",
"configuration-files.bootstrap-commands-toml",
]))
}

fn main() {
if let Err(e) = run() {
eprintln!("{}", e);
process::exit(1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "bootstrap-commands-services-cfg-v0-1-0"
version = "0.1.0"
edition = "2021"
authors = ["Piyush Jena <[email protected]>"]
license = "Apache-2.0 OR MIT"
publish = false
exclude = ["README.md"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
migration-helpers.workspace = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use migration_helpers::common_migrations::{ListReplacement, ReplaceListsMigration};
use migration_helpers::{migrate, Result};
use std::process;

fn run() -> Result<()> {
migrate(ReplaceListsMigration(vec![ListReplacement {
setting: "services.bootstrap-commands.configuration-files",
old_vals: &[],
new_vals: &["bootstrap-commands-toml"],
}]))
}

fn main() {
if let Err(e) = run() {
eprintln!("{}", e);
process::exit(1);
}
}
1 change: 1 addition & 0 deletions sources/settings-plugins/aws-dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ struct AwsDevSettings {
motd: bottlerocket_settings_models::MotdV1,
updates: bottlerocket_settings_models::UpdatesSettingsV1,
host_containers: bottlerocket_settings_models::HostContainersSettingsV1,
bootstrap_commands: bottlerocket_settings_models::BootstrapCommandsSettingsV1,
bootstrap_containers: bottlerocket_settings_models::BootstrapContainersSettingsV1,
ntp: bottlerocket_settings_models::NtpSettingsV1,
network: bottlerocket_settings_models::NetworkSettingsV1,
Expand Down
1 change: 1 addition & 0 deletions sources/settings-plugins/aws-ecs-1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ struct AwsEcs1Settings {
motd: bottlerocket_settings_models::MotdV1,
updates: bottlerocket_settings_models::UpdatesSettingsV1,
host_containers: bottlerocket_settings_models::HostContainersSettingsV1,
bootstrap_commands: bottlerocket_settings_models::BootstrapCommandsSettingsV1,
bootstrap_containers: bottlerocket_settings_models::BootstrapContainersSettingsV1,
ntp: bottlerocket_settings_models::NtpSettingsV1,
network: bottlerocket_settings_models::NetworkSettingsV1,
Expand Down
1 change: 1 addition & 0 deletions sources/settings-plugins/aws-ecs-2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ struct AwsEcs2Settings {
motd: bottlerocket_settings_models::MotdV1,
updates: bottlerocket_settings_models::UpdatesSettingsV1,
host_containers: bottlerocket_settings_models::HostContainersSettingsV1,
bootstrap_commands: bottlerocket_settings_models::BootstrapCommandsSettingsV1,
bootstrap_containers: bottlerocket_settings_models::BootstrapContainersSettingsV1,
ntp: bottlerocket_settings_models::NtpSettingsV1,
network: bottlerocket_settings_models::NetworkSettingsV1,
Expand Down
1 change: 1 addition & 0 deletions sources/settings-plugins/aws-k8s/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ struct AwsK8sSettings {
kubernetes: bottlerocket_settings_models::KubernetesSettingsV1,
updates: bottlerocket_settings_models::UpdatesSettingsV1,
host_containers: bottlerocket_settings_models::HostContainersSettingsV1,
bootstrap_commands: bottlerocket_settings_models::BootstrapCommandsSettingsV1,
bootstrap_containers: bottlerocket_settings_models::BootstrapContainersSettingsV1,
ntp: bottlerocket_settings_models::NtpSettingsV1,
network: bottlerocket_settings_models::NetworkSettingsV1,
Expand Down
1 change: 1 addition & 0 deletions sources/settings-plugins/metal-dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ struct MetalDevSettings {
motd: bottlerocket_settings_models::MotdV1,
updates: bottlerocket_settings_models::UpdatesSettingsV1,
host_containers: bottlerocket_settings_models::HostContainersSettingsV1,
bootstrap_commands: bottlerocket_settings_models::BootstrapCommandsSettingsV1,
bootstrap_containers: bottlerocket_settings_models::BootstrapContainersSettingsV1,
ntp: bottlerocket_settings_models::NtpSettingsV1,
network: bottlerocket_settings_models::NetworkSettingsV1,
Expand Down
1 change: 1 addition & 0 deletions sources/settings-plugins/metal-k8s/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ struct MetalK8sSettings {
kubernetes: bottlerocket_settings_models::KubernetesSettingsV1,
updates: bottlerocket_settings_models::UpdatesSettingsV1,
host_containers: bottlerocket_settings_models::HostContainersSettingsV1,
bootstrap_commands: bottlerocket_settings_models::BootstrapCommandsSettingsV1,
bootstrap_containers: bottlerocket_settings_models::BootstrapContainersSettingsV1,
ntp: bottlerocket_settings_models::NtpSettingsV1,
network: bottlerocket_settings_models::NetworkSettingsV1,
Expand Down
1 change: 1 addition & 0 deletions sources/settings-plugins/vmware-dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ struct VmwareDevSettings {
motd: bottlerocket_settings_models::MotdV1,
updates: bottlerocket_settings_models::UpdatesSettingsV1,
host_containers: bottlerocket_settings_models::HostContainersSettingsV1,
bootstrap_commands: bottlerocket_settings_models::BootstrapCommandsSettingsV1,
bootstrap_containers: bottlerocket_settings_models::BootstrapContainersSettingsV1,
ntp: bottlerocket_settings_models::NtpSettingsV1,
network: bottlerocket_settings_models::NetworkSettingsV1,
Expand Down
1 change: 1 addition & 0 deletions sources/settings-plugins/vmware-k8s/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ struct VmwareK8sSettings {
kubernetes: bottlerocket_settings_models::KubernetesSettingsV1,
updates: bottlerocket_settings_models::UpdatesSettingsV1,
host_containers: bottlerocket_settings_models::HostContainersSettingsV1,
bootstrap_commands: bottlerocket_settings_models::BootstrapCommandsSettingsV1,
bootstrap_containers: bottlerocket_settings_models::BootstrapContainersSettingsV1,
ntp: bottlerocket_settings_models::NtpSettingsV1,
network: bottlerocket_settings_models::NetworkSettingsV1,
Expand Down
12 changes: 12 additions & 0 deletions sources/shared-defaults/defaults.toml
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,18 @@ affected-services = ["lockdown"]
path = "/etc/corndog.toml"
template-path = "/usr/share/templates/corndog-toml"

# Bootstrap Commands
[services.bootstrap-commands]
configuration-files = ["bootstrap-commands-toml"]
restart-commands = []

[metadata.settings.bootstrap-commands]
affected-services = ["bootstrap-commands"]

[configuration-files.bootstrap-commands-toml]
path = "/etc/bootstrap-commands/bootstrap-commands.toml"
template-path = "/usr/share/templates/bootstrap-commands-toml"

# Bootstrap Containers

[services.bootstrap-containers]
Expand Down

0 comments on commit ba0fe84

Please sign in to comment.