Skip to content

Commit

Permalink
refactor: get project name from label (#646)
Browse files Browse the repository at this point in the history
  • Loading branch information
oddgrd authored Feb 24, 2023
1 parent e8536e8 commit 6ee5a66
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 38 deletions.
8 changes: 8 additions & 0 deletions auth/prepare.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env sh

###############################################################################
# This file is used by our common Containerfile incase the container for this #
# service might need some extra preparation steps for its final image #
###############################################################################

# Nothing to prepare in container image here
34 changes: 17 additions & 17 deletions gateway/src/api/latest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,23 @@ async fn get_projects_list(
Ok(AxumJson(projects))
}

async fn get_projects_list_with_filter(
State(RouterState { service, .. }): State<RouterState>,
User { name, .. }: User,
Path(project_status): Path<String>,
) -> Result<AxumJson<Vec<project::Response>>, Error> {
let projects = service
.iter_user_projects_detailed_filtered(name.clone(), project_status)
.await?
.into_iter()
.map(|project| project::Response {
name: project.0.to_string(),
state: project.1.into(),
})
.collect();

Ok(AxumJson(projects))
}
// async fn get_projects_list_with_filter(
// State(RouterState { service, .. }): State<RouterState>,
// User { name, .. }: User,
// Path(project_status): Path<String>,
// ) -> Result<AxumJson<Vec<project::Response>>, Error> {
// let projects = service
// .iter_user_projects_detailed_filtered(name.clone(), project_status)
// .await?
// .into_iter()
// .map(|project| project::Response {
// name: project.0.to_string(),
// state: project.1.into(),
// })
// .collect();

// Ok(AxumJson(projects))
// }

#[instrument(skip_all, fields(%project))]
async fn post_project(
Expand Down
32 changes: 11 additions & 21 deletions gateway/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,11 @@ where
pub trait ContainerInspectResponseExt {
fn container(&self) -> &ContainerInspectResponse;

fn project_name(&self, prefix: &str) -> Result<ProjectName, ProjectError> {
// This version can't be enabled while there are active
// deployers before v0.8.0 since the don't have this label
// TODO: switch to this version when you notice all deployers
// are greater than v0.8.0
// let name = safe_unwrap!(container.config.labels.get("project.name")).to_string();

fn project_name(&self) -> Result<ProjectName, ProjectError> {
let container = self.container();
let container_name = safe_unwrap!(container.name.strip_prefix("/")).to_string();
safe_unwrap!(container_name.strip_prefix(prefix).strip_suffix("_run"))

safe_unwrap!(container.config.labels.get("shuttle.project"))
.to_string()
.parse::<ProjectName>()
.map_err(|_| ProjectError::internal("invalid project name"))
}
Expand Down Expand Up @@ -471,7 +466,7 @@ where
}) => {
// container not found, let's try to recreate it
// with the same image
Self::Creating(ProjectCreating::from_container(container, ctx, 0)?)
Self::Creating(ProjectCreating::from_container(container, 0)?)
}
Err(err) => return Err(err.into()),
},
Expand Down Expand Up @@ -500,7 +495,7 @@ where
}) => {
// container not found, let's try to recreate it
// with the same image
Self::Creating(ProjectCreating::from_container(container, ctx, 0)?)
Self::Creating(ProjectCreating::from_container(container, 0)?)
}
Err(err) => return Err(err.into()),
},
Expand Down Expand Up @@ -565,12 +560,11 @@ impl ProjectCreating {
}
}

pub fn from_container<Ctx: DockerContext>(
pub fn from_container(
container: ContainerInspectResponse,
ctx: &Ctx,
recreate_count: usize,
) -> Result<Self, ProjectError> {
let project_name = container.project_name(&ctx.container_settings().prefix)?;
let project_name = container.project_name()?;
let initial_key = container.initial_key()?;

Ok(Self {
Expand Down Expand Up @@ -871,7 +865,6 @@ where
sleep(Duration::from_secs(5)).await;
Ok(ProjectCreating::from_container(
container,
ctx,
recreate_count + 1,
)?)
} else {
Expand Down Expand Up @@ -997,7 +990,7 @@ where
let container = self.container.refresh(ctx).await?;
let mut service = match self.service {
Some(service) => service,
None => Service::from_container(ctx, container.clone())?,
None => Service::from_container(container.clone())?,
};

if service.is_healthy().await {
Expand Down Expand Up @@ -1080,11 +1073,8 @@ pub struct Service {
}

impl Service {
pub fn from_container<Ctx: DockerContext>(
ctx: &Ctx,
container: ContainerInspectResponse,
) -> Result<Self, ProjectError> {
let resource_name = container.project_name(&ctx.container_settings().prefix)?;
pub fn from_container(container: ContainerInspectResponse) -> Result<Self, ProjectError> {
let resource_name = container.project_name()?;

let network = safe_unwrap!(container.network_settings.networks)
.values()
Expand Down

0 comments on commit 6ee5a66

Please sign in to comment.