Repository: bottlerocket-os/bottlerocket
File: sources/dogtag/src/lib.rs (Rust)

Code before:
/*!
dogtag is a set of tools that detect the hostname of a bottlerocket server/instance and prints it to stdout.
if the tool is called in an environment it cannot resolve the hostname it will error out.

Currently the following hostname tools are implemented:

* 01-imds - Fetches hostname from the Instance Metadata via IMDS
* 00-reverse-dns - Uses reverse dns lookup to resolve the hostname
 */
use argh::FromArgs;
use log::debug;
use snafu::ResultExt;
use std::{fs, process};

const DOGTAG_BIN_PATH: &str = "/var/bottlerocket/dogtag";

/// CLi defines the standard cmdline interface for all hostname handlers
#[derive(FromArgs)]
#[argh(description = "hostname resolution tool")]
pub struct Cli {
    #[argh(option)]
    #[argh(description = "ip_address of the host")]
    pub ip_address: String,
}

pub type Result<T> = std::result::Result<T, error::Error>;

/// find_hostname will utilize the helpers located in /var/bottlerocket/dogtag/ to try and discover the hostname
pub async fn find_hostname() -> Result<String> {
    debug!(
        "attempting to discover hostname helpers in {}",
        DOGTAG_BIN_PATH
    );
    let walker = fs::read_dir(DOGTAG_BIN_PATH).context(error::IoSnafu)?;
    let mut hostname_helpers = Vec::new();
    for entry in walker.flatten() {
        let path = entry.path();
        if path.is_file() {
            hostname_helpers.push(path.clone());
        }
    }
    // We want to do reverse sort as we want to prioritize higher numbers first

Review comment (suggestion):
```suggestion
/// Cli defines the standard cmdline interface for all hostname handlers
```

Code after:
/*!
dogtag resolves the hostname of a bottlerocket server/instance. It's used to generate settings.network.hostname. To accomplish this, it uses a set of standalone binaries in /var/bottlerocket/dogtag that resolve the hostname via different methods.

Currently, bottlerocket ships with two hostname resolver binaries:

20-imds - Fetches hostname from EC2 Instance Metadata Service
10-reverse-dns - Uses reverse DNS lookup to resolve the hostname

dogtag runs the resolvers in /var/bottlerocket/dogtag in reverse alphanumerical order until one of them returns a hostname, at which point it will exit early and print the returned hostname to stdout.
 */
use argh::FromArgs;
use log::debug;
use snafu::ResultExt;
use std::{path::PathBuf, process};
use walkdir::WalkDir;

const DOGTAG_BIN_PATH: &str = "/var/bottlerocket/dogtag";

/// Cli defines the standard cmdline interface for all hostname handlers
#[derive(FromArgs)]
#[argh(description = "hostname resolution tool")]
pub struct Cli {
    #[argh(option)]
    #[argh(description = "ip_address of the host")]
    pub ip_address: String,
}

pub type Result<T> = std::result::Result<T, error::Error>;

/// find_hostname will utilize the helpers located in /var/bottlerocket/dogtag/ to try and discover the hostname
pub async fn find_hostname() -> Result<String> {
    debug!(
        "attempting to discover hostname helpers in {}",
        DOGTAG_BIN_PATH
    );
    // We want to do reverse sort as we want to prioritize higher numbers first
    // this is because it makes it easier to add more of these and not have to worry about
    // bumping the binary name for existing ones
    let mut hostname_helpers: Vec<PathBuf> = WalkDir::new(DOGTAG_BIN_PATH)
        .max_depth(1)
        .min_depth(1)
        .sort_by_file_name()