Skip to content

Commit

Permalink
Run Containers With Nicer Names
Browse files Browse the repository at this point in the history
Containers created by Lucky will now have names like:

    lucky_app_name_3_d34fkd96
  • Loading branch information
zicklag committed Mar 9, 2020
1 parent e176f65 commit 6dc2246
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/daemon/tools.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::format_err;
use futures::prelude::*;
use rand::{seq::IteratorRandom, thread_rng};
use shiplift::{builder::ExecContainerOptions, PullOptions};
use subprocess::{Exec, ExitStatus, Redirection};

Expand All @@ -14,6 +15,8 @@ use crate::types::{
CharmScript, CharmScriptType, ScriptState, ScriptStatus, LUCKY_EXIT_CODE_HELPER_PREFIX,
};

const CONTAINER_SUFFIX_CHARS: &str = "abcdefghijklmnopqrstuvwxyz0123456789";

use super::*;

/// Load the daemon state from the filesystem
Expand Down Expand Up @@ -642,11 +645,29 @@ fn apply_updates(
}

// Create the container
let docker_options = container_info.config.to_container_options(
let mut docker_options = container_info.config.to_container_options(
&daemon.charm_dir,
&daemon.lucky_data_dir,
&daemon.socket_path,
)?;
let unit_name = std::env::var("JUJU_UNIT_NAME")
.context("Env var JUJU_UNIT_NAME not readable!")?
.replace("/", "_");
docker_options.name = Some(format!("lucky_{}_{}", unit_name, {
// Generate random suffix
let mut rng = thread_rng();
let mut buffer = String::with_capacity(8);
for _ in 0..8 {
buffer.push(
CONTAINER_SUFFIX_CHARS
.chars()
.choose(&mut rng)
.expect("Empty suffix chars"),
);
}
buffer
}));

log::trace!("Creating container with options: {:#?}", docker_options);
let create_info = block_on(containers.create(&docker_options))?;

Expand Down

0 comments on commit 6dc2246

Please sign in to comment.