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

chore: add pod name to dc project label #110

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 5 additions & 5 deletions src/cmd/compose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,19 @@ fn runs_docker_compose_on_all_pods() {
assert_ran!(runner, {
["docker-compose",
"-p",
"railshello",
"railshello_db",
"-f",
proj.output_dir().join("pods").join("db.yml"),
"stop"],
["docker-compose",
"-p",
"railshello",
"railshello_frontend",
"-f",
proj.output_dir().join("pods").join("frontend.yml"),
"stop"],
["docker-compose",
"-p",
"railshello",
"railshello_rake",
"-f",
proj.output_dir().join("pods").join("rake.yml"),
"stop"]
Expand All @@ -166,13 +166,13 @@ fn runs_docker_compose_on_named_pods_and_services() {
assert_ran!(runner, {
["docker-compose",
"-p",
"railshello",
"railshello_db",
"-f",
proj.output_dir().join("pods").join("db.yml"),
"stop"],
["docker-compose",
"-p",
"railshello",
"railshello_frontend",
"-f",
proj.output_dir().join("pods").join("frontend.yml"),
"stop",
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ fn invokes_docker_exec() {
[
"docker-compose",
"-p",
"hello",
"hello_frontend",
"-f",
proj.output_dir().join("pods").join("frontend.yml"),
"exec",
Expand All @@ -127,7 +127,7 @@ fn runs_shells() {
[
"docker-compose",
"-p",
"hello",
"hello_frontend",
"-f",
proj.output_dir().join("pods").join("frontend.yml"),
"exec",
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn runs_docker_compose_logs() {
[
"docker-compose",
"-p",
"railshello",
"railshello_frontend",
"-f",
proj.output_dir().join("pods").join("frontend.yml"),
"logs",
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/pull.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn runs_docker_compose_pull_on_all_pods() {
.join("hello.hook")],
["docker-compose",
"-p",
"hello",
"hello_frontend",
"-f",
proj.output_dir().join("pods").join("frontend.yml"),
"pull"]
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ fn runs_a_single_service_pod() {
[
"docker-compose",
"-p",
"railshello",
"railshello_rake",
"-f",
proj.output_dir().join("pods").join("rake.yml"),
"run",
Expand All @@ -163,7 +163,7 @@ fn runs_tests() {
[
"docker-compose",
"-p",
"hellotest",
"hellotest_frontend",
"-f",
proj.output_pods_dir().join("frontend.yml"),
"run",
Expand Down Expand Up @@ -194,7 +194,7 @@ fn runs_tests_with_custom_command() {
[
"docker-compose",
"-p",
"hellotest",
"hellotest_frontend",
"-f",
proj.output_pods_dir().join("frontend.yml"),
"run",
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/run_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn runs_scripts_on_all_services() {
[
"docker-compose",
"-p",
"railshello",
"railshello_rake",
"-f",
proj.output_dir().join("pods").join("rake.yml"),
"run",
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/up.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ fn runs_docker_compose_up_honors_enable_in_targets() {
[
"docker-compose",
"-p",
"railshello",
"railshello_frontend",
"-f",
proj.output_dir().join("pods").join("frontend.yml"),
"up",
Expand Down
2 changes: 1 addition & 1 deletion src/pod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ impl Pod {
pub fn compose_args(&self, proj: &Project) -> Result<Vec<OsString>> {
Ok(vec![
"-p".into(),
proj.compose_name().into(),
proj.compose_name(Some(&self)).into(),
"-f".into(),
proj.output_pods_dir().join(self.rel_path()).into(),
])
Expand Down
4 changes: 2 additions & 2 deletions src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,8 @@ impl Project {
}

/// Get that name that `docker_compose` would use for this project.
pub fn compose_name(&self) -> String {
self.current_target.compose_project_name(self)
pub fn compose_name(&self, pod: Option<&Pod>) -> String {
self.current_target.compose_project_name(self, pod)
}

/// The root directory of this project.
Expand Down
30 changes: 20 additions & 10 deletions src/runtime_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl RuntimeState {
.enable_all()
.build()?;

let name = project.compose_name();
let name = project.compose_name(None);
let target = project.current_target().name().to_owned();
let docker = boondock::Docker::connect_with_defaults()?;

Expand All @@ -51,17 +51,27 @@ impl RuntimeState {
.chain_err(|| {
format!("error looking up container {:?}", container.Id)
})?;

let labels = &info.Config.Labels;
if labels.get("com.docker.compose.project") == Some(&name)
&& labels.get("io.fdy.cage.target") == Some(&target)
{
if let Some(service) = labels.get("com.docker.compose.service") {
let our_info = ContainerInfo::new(&info)?;
services
.entry(service.to_owned())
.or_insert_with(Vec::new)
.push(our_info);

if let Some(project_name) = labels.get("com.docker.compose.project") {
if !project_name.starts_with(&name) {
break;
}
} else {
break;
}

if labels.get("io.fdy.cage.target") != Some(&target) {
break;
}

if let Some(service) = labels.get("com.docker.compose.service") {
let our_info = ContainerInfo::new(&info)?;
services
.entry(service.to_owned())
.or_insert_with(Vec::new)
.push(our_info);
}
}
Ok(RuntimeState { services })
Expand Down
19 changes: 16 additions & 3 deletions src/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use regex::Regex;

use crate::pod::Pod;
use crate::project::Project;

/// An `Target` provides collection of extensions to a project's basic
Expand Down Expand Up @@ -63,20 +64,32 @@ impl Target {
}

/// Get a value for `docker-compose`'s `-p` argument for a given project.
pub fn compose_project_name(&self, project: &Project) -> String {
pub fn compose_project_name(
&self,
project: &Project,
pod: Option<&Pod>,
) -> String {
let base_name: String = if self.name == "test" {
format!("{}test", project.name())
} else {
project.name().to_owned()
};

let pod_name = match pod {
Some(p) => p.name(),
None => "",
};

// We strip out non-alphabetic characters and convert everything to
// lowercase, which is what the `docker-compose` source code does.
lazy_static! {
static ref NON_ALNUM: Regex = Regex::new(r#"[^a-z0-9]"#).unwrap();
}
NON_ALNUM

let base_name = NON_ALNUM
.replace_all(&base_name.to_lowercase(), "")
.into_owned()
.into_owned();

format!("{}_{}", base_name, pod_name).to_owned()
}
}