Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

early-boot-config: Simplify conditional compilation and make local_file available to all platforms #1576

Merged
merged 2 commits into from
May 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 2 additions & 29 deletions sources/api/early-boot-config/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::{env, process};
mod compression;
mod provider;
mod settings;
use crate::provider::PlatformDataProvider;
use crate::provider::{Platform, PlatformDataProvider};

// TODO
// Tests!
Expand All @@ -40,30 +40,6 @@ const TRANSACTION: &str = "bottlerocket-launch";
// We create it after running successfully.
const MARKER_FILE: &str = "/var/lib/bottlerocket/early-boot-config.ran";

/// This function returns the appropriate data provider for this variant. It exists primarily to
/// keep the ugly bits of conditional compilation out of the main function.
fn create_provider() -> Result<Box<dyn PlatformDataProvider>> {
#[cfg(bottlerocket_platform = "aws")]
{
Ok(Box::new(provider::aws::AwsDataProvider))
}

#[cfg(bottlerocket_platform = "aws-dev")]
{
use std::path::Path;
if Path::new(provider::local_file::LocalFileDataProvider::USER_DATA_FILE).exists() {
Ok(Box::new(provider::local_file::LocalFileDataProvider))
} else {
Ok(Box::new(provider::aws::AwsDataProvider))
}
}

#[cfg(bottlerocket_platform = "vmware")]
{
Ok(Box::new(provider::vmware::VmwareDataProvider))
}
}

/// Store the args we receive on the command line
#[derive(Debug)]
struct Args {
Expand Down Expand Up @@ -134,13 +110,10 @@ async fn run() -> Result<()> {

info!("early-boot-config started");

// Figure out the current provider
let data_provider = create_provider()?;

info!("Retrieving platform-specific data");
let uri = &format!("{}?tx={}", API_SETTINGS_URI, TRANSACTION);
let method = "PATCH";
for settings_json in data_provider
for settings_json in Platform
.platform_data()
.await
.context(error::ProviderError)?
Expand Down
14 changes: 9 additions & 5 deletions sources/api/early-boot-config/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
use crate::settings::SettingsJson;
use async_trait::async_trait;

#[cfg(any(bottlerocket_platform = "aws", bottlerocket_platform = "aws-dev"))]
pub(crate) mod aws;

#[cfg(bottlerocket_platform = "aws-dev")]
pub(crate) mod local_file;
mod local_file;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused as to why local_file still has the cfg directive, since the PR says "It also makes local_file a common function that all platforms can use" - wouldn't we remove all conditionals on it, and let the data providers / platforms decide whether to call the function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could for sure do that, but when compiling variants that don't use the code in the local_file module, we're going to get "unused" warnings like:

warning: constant is never used: `USER_DATA_FILE`
...
warning: function is never used: `local_file_user_data`

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really sure how we get around it other than hiding the whole module behind a cfg directive like I've done here... perhaps someone knows a trick I don't know?

Copy link
Contributor

@webern webern May 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could just silence the warning with #[allow(dead_code)] on the function that is sometimes unused. (need to look up the correct string there)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm - I suppose we could do that but I like it a bit less. Are those really the only two options here? 🤔

Copy link
Contributor Author

@zmrow zmrow May 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This (very good) article suggests the pattern I'm using: putting cfg directives on module imports to keep the actual code clean and not ignore any valid warnings.

IMHO let's keep the code as is unless we come up with something better.


#[cfg(any(bottlerocket_platform = "aws", bottlerocket_platform = "aws-dev"))]
mod aws;
#[cfg(any(bottlerocket_platform = "aws", bottlerocket_platform = "aws-dev"))]
pub(crate) use aws::AwsDataProvider as Platform;

#[cfg(bottlerocket_platform = "vmware")]
pub(crate) mod vmware;
mod vmware;
#[cfg(bottlerocket_platform = "vmware")]
pub(crate) use vmware::VmwareDataProvider as Platform;

/// Support for new platforms can be added by implementing this trait.
#[async_trait]
Expand Down
14 changes: 13 additions & 1 deletion sources/api/early-boot-config/src/provider/aws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use snafu::{OptionExt, ResultExt};
use std::fs;
use std::path::Path;

#[cfg(bottlerocket_platform = "aws-dev")]
use crate::provider::local_file::{local_file_user_data, USER_DATA_FILE};

/// Unit struct for AWS so we can implement the PlatformDataProvider trait.
pub(crate) struct AwsDataProvider;

Expand Down Expand Up @@ -82,7 +85,16 @@ impl PlatformDataProvider for AwsDataProvider {

let mut client = ImdsClient::new().await.context(error::ImdsClient)?;

// Instance identity doc first, so the user has a chance to override
// Attempt to read from local file first on the `aws-dev` variant
#[cfg(bottlerocket_platform = "aws-dev")]
{
match local_file_user_data()? {
None => warn!("No user data found via local file: {}", USER_DATA_FILE),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think not having a user data file is the normal case - is it worth a warning-level message?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for variants that have the ability to use a local file it's worth having the message so users know that nothing was found there, similar to what we do for other sources.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have it as a info level message? I guess that would mean the message won't go to the console?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll keep it as warn! for now to stay consistent with the rest of the program and ensure it outputs to the console as expected.

Some(s) => output.push(s),
}
}

// Instance identity doc next, so the user has a chance to override
match Self::identity_document(&mut client).await? {
None => warn!("No instance identity document found."),
Some(s) => output.push(s),
Expand Down
57 changes: 25 additions & 32 deletions sources/api/early-boot-config/src/provider/local_file.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,35 @@
//! The local_file module implements the `PlatformDataProvider` trait for gathering userdata from
//! local file
//! The local_file module provides a method for gathering userdata from local file

use super::{PlatformDataProvider, SettingsJson};
use super::SettingsJson;
use crate::compression::expand_file_maybe;
use async_trait::async_trait;
use snafu::ResultExt;
use std::path::Path;

pub(crate) struct LocalFileDataProvider;
pub(crate) const USER_DATA_FILE: &'static str = "/etc/early-boot-config/user-data";

impl LocalFileDataProvider {
pub(crate) const USER_DATA_FILE: &'static str = "/etc/early-boot-config/user-data";
}
pub(crate) fn local_file_user_data(
) -> std::result::Result<Option<SettingsJson>, Box<dyn std::error::Error>> {
if !Path::new(USER_DATA_FILE).exists() {
return Ok(None);
}
info!("'{}' exists, using it", USER_DATA_FILE);

// Read the file, decompressing it if compressed.
let user_data_str = expand_file_maybe(USER_DATA_FILE).context(error::InputFileRead {
path: USER_DATA_FILE,
})?;

#[async_trait]
impl PlatformDataProvider for LocalFileDataProvider {
async fn platform_data(&self) -> std::result::Result<Vec<SettingsJson>, Box<dyn std::error::Error>> {
let mut output = Vec::new();
info!("'{}' exists, using it", Self::USER_DATA_FILE);

// Read the file, decompressing it if compressed.
let user_data_str =
expand_file_maybe(Self::USER_DATA_FILE).context(error::InputFileRead {
path: Self::USER_DATA_FILE,
})?;

if user_data_str.is_empty() {
return Ok(output);
}

let json = SettingsJson::from_toml_str(&user_data_str, "user data").context(
error::SettingsToJSON {
from: Self::USER_DATA_FILE,
},
)?;
output.push(json);

Ok(output)
if user_data_str.is_empty() {
return Ok(None);
}

let json = SettingsJson::from_toml_str(&user_data_str, "user data").context(
error::SettingsToJSON {
from: USER_DATA_FILE,
},
)?;

Ok(Some(json))
}

mod error {
Expand Down