Skip to content

Commit

Permalink
Add support for bootstrap commands
Browse files Browse the repository at this point in the history
  • Loading branch information
piyush-jena committed Aug 16, 2024
1 parent 4d43022 commit b919242
Show file tree
Hide file tree
Showing 17 changed files with 163 additions and 1 deletion.
81 changes: 80 additions & 1 deletion sources/api/schnauzer/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,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 @@ -972,6 +972,85 @@ pub fn join_array(
Ok(())
}

/// `join_nested_array` is used to join a nested array of scalar strings into a
/// nested array of quoted, comma delimited strings.
///
/// # Example
///
/// Consider an array of values: `[[ "a", "b", "c" ], [ "d", "e" ]]` stored in a
/// setting such as `settings.somewhere.foo-list`. In our template we can write:
/// `{{ join_nested_array settings.somewhere.foo-list }}`
///
/// This will render `["a", "b", "c"], ["d", "e"]`.
pub fn join_nested_array(
helper: &Helper<'_, '_>,
_: &Handlebars,
_: &Context,
renderctx: &mut RenderContext<'_, '_>,
out: &mut dyn Output,
) -> Result<(), RenderError> {
trace!("Starting join_nested_array helper");
let template_name = template_name(renderctx);
check_param_count(helper, template_name, 1)?;

let nested_array_param = get_param(helper, 0)?;
let nested_array =
nested_array_param
.as_array()
.with_context(|| error::JoinStringsWrongTypeSnafu {
expected_type: "array",
value: nested_array_param.to_owned(),
template: template_name,
})?;

let mut result = String::new();

for (i, array) in nested_array.iter().enumerate() {
if i > 0 {
result.push_str(", ");
}

result.push('[');

for (j, value) in array
.as_array()
.with_context(|| error::JoinStringsWrongTypeSnafu {
expected_type: "array",
value: array.to_owned(),
template: template_name,
})?
.iter()
.enumerate()
{
if j > 0 {
result.push_str(", ");
}

result.push_str(
format!(
"\"{}\"",
value.as_str().context(error::JoinStringsWrongTypeSnafu {
expected_type: "string",
value: value.to_owned(),
template: template_name,
})?
)
.as_str(),
);
}

result.push(']');
}

// 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("join_nested_array", Box::new(helpers::join_nested_array));
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),
"join_nested_array" => helper!(handlebars_helpers::join_nested_array),
"join_map" => helper!(handlebars_helpers::join_map),
"if_not_null" => Box::new(handlebars_helpers::IfNotNullHelper),
"goarch" => helper!(handlebars_helpers::goarch),
Expand Down
1 change: 1 addition & 0 deletions sources/deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,5 @@ allow-git = [
# We will allow it as an exception until the following is resolved:
# https://github.com/bottlerocket-os/bottlerocket-settings-sdk/issues/18
"https://github.com/bottlerocket-os/bottlerocket-settings-sdk",
"https://github.com/piyush-jena/bottlerocket-settings-sdk",
]
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 <jepiyush@amazon.com>"]
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 = { path = "../../../api/migration/migration-helpers", version = "0.1.0" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use migration_helpers::common_migrations::AddPrefixesMigration;
use migration_helpers::{migrate, Result};
use std::process;

fn run() -> Result<()> {
migrate(AddPrefixesMigration(vec![
"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 <jepiyush@amazon.com>"]
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 = { path = "../../../api/migration/migration-helpers", version = "0.1.0" }
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 b919242

Please sign in to comment.