Skip to content

Commit

Permalink
Refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
oestradiol committed Aug 16, 2024
1 parent 967c192 commit 4522ca4
Show file tree
Hide file tree
Showing 66 changed files with 99 additions and 166 deletions.
1 change: 1 addition & 0 deletions .gitignore
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
target/
Logs/
Deployments/

# Local environment
.env
Empty file modified Traefik/.env.example
100644 → 100755
Empty file.
Empty file modified bacon.toml
100644 → 100755
Empty file.
Empty file modified guides/cloudflare/CLOUDFLARE.md
100644 → 100755
Empty file.
Empty file modified guides/cloudflare/images/IMG_1.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified guides/cloudflare/images/IMG_2.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified guides/cloudflare/images/IMG_3.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified guides/cloudflare/images/IMG_4.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified guides/cloudflare/images/IMG_5.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified guides/cloudflare/images/IMG_6.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified rustfmt.toml
100644 → 100755
Empty file.
Empty file modified src/business/mod.rs
100644 → 100755
Empty file.
10 changes: 5 additions & 5 deletions src/business/repositories/deployments/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ use axum::http::StatusCode;
use mongodb::bson::{doc, oid::ObjectId};
use tracing::{event, Level};

pub async fn delete(id: String) -> Result<(), VoyagerError> {
pub async fn delete(id: &str) -> Result<(), VoyagerError> {
event!(
Level::DEBUG,
"Deleting deployment of id {id} from database."
);

let oid = ObjectId::from_str(id.as_str())
.map_err(|e| VoyagerError::invalid_delete_id(Box::new(e), &id))?;
let oid = ObjectId::from_str(id)
.map_err(|e| VoyagerError::invalid_delete_id(Box::new(e), id))?;

let result = REPOSITORIES_RUNTIME
.spawn_handled(
Expand All @@ -28,10 +28,10 @@ pub async fn delete(id: String) -> Result<(), VoyagerError> {
.await?;

let result = result.map_or_else(
|e| Err(VoyagerError::delete_mongo(Box::new(e), &id)),
|e| Err(VoyagerError::delete_mongo(Box::new(e), id)),
|r| {
if r.deleted_count == 0 {
Err(VoyagerError::delete(&id))
Err(VoyagerError::delete(id))
} else {
Ok(())
}
Expand Down
8 changes: 4 additions & 4 deletions src/business/repositories/deployments/find_by_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ use axum::http::StatusCode;
use mongodb::bson::doc;
use tracing::{event, Level};

pub async fn find_by_name(name: String) -> Result<Option<Deployment>, VoyagerError> {
pub async fn find_by_name(name: &str) -> Result<Option<Deployment>, VoyagerError> {
event!(
Level::DEBUG,
"Finding deployment with hostname {} in database",
&name
name
);

let result = REPOSITORIES_RUNTIME
.spawn_handled(
"repositories::deployments::find_by_name",
DB_CONTEXT.deployments.find_one(doc! { "container_name": &name }, None),
DB_CONTEXT.deployments.find_one(doc! { "container_name": name }, None),
)
.await?;

let result = result.map_or_else(
|e| Err(VoyagerError::find_mongo_name(Box::new(e), &name)),
|e| Err(VoyagerError::find_mongo_name(Box::new(e), name)),
|r| Ok(r),
)?;

Expand Down
10 changes: 5 additions & 5 deletions src/business/repositories/deployments/find_by_repo_branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ use axum::http::StatusCode;
use mongodb::bson::doc;
use tracing::{event, Level};

pub async fn find_by_repo_branch(repo_url: String, branch: String) -> Result<Option<Deployment>, VoyagerError> {
pub async fn find_by_repo_branch(repo_url: &str, branch: &str) -> Result<Option<Deployment>, VoyagerError> {
event!(
Level::DEBUG,
"Finding deployment with repo url {} and branch {} in database",
&repo_url,
&branch
repo_url,
branch
);

let result = REPOSITORIES_RUNTIME
.spawn_handled(
"repositories::deployments::find_by_repo_branch",
DB_CONTEXT.deployments.find_one(doc! { "repo_url": &repo_url, "branch": &branch }, None),
DB_CONTEXT.deployments.find_one(doc! { "repo_url": repo_url, "branch": branch }, None),
)
.await?;

let result = result.map_or_else(
|e| Err(VoyagerError::find_mongo_repo_branch(Box::new(e), &repo_url, &branch)),
|e| Err(VoyagerError::find_mongo_repo_branch(Box::new(e), repo_url, branch)),
|r| Ok(r),
)?;

Expand Down
Empty file modified src/business/repositories/deployments/mod.rs
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion src/business/repositories/deployments/retrieve_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub async fn retrieve_all(
let document = repo_url
.map_or_else(|| doc! { }, |repo_url|
branch.map_or_else(|| doc! {"repo_url": &repo_url}, |branch|
doc! {"repo_url": &repo_url, "branch": branch}
doc! {"repo_url": &repo_url, "branch": &branch}
)
);

Expand Down
Empty file modified src/business/repositories/mod.rs
100644 → 100755
Empty file.
10 changes: 5 additions & 5 deletions src/business/services/deployments/check.rs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ pub async fn check(
) -> Result<(), VoyagerError> {
let final_branch: String;
let mut log = format!("Checking if deployment exists. Host {host}, mode {mode}, repo_url {repo_url}");
if let Some(branch) = branch.as_ref() {
final_branch = branch.clone();
if let Some(branch) = branch {
log = format!("{log}, branch {branch}");
final_branch = branch;
} else {
final_branch = "default".to_string();
log = format!("{log}, branch default");
final_branch = "default".to_string();
}
event!(Level::INFO, log);

let host = host.replace('.', "-");
let future = async move {
let result = repositories::deployments::find_by_name(host).await?;
let result = repositories::deployments::find_by_name(&host).await?;
match result {
Some(_) => Err(VoyagerError::new(
format!("Deployment at this subdomain already exists!"),
Expand All @@ -39,7 +39,7 @@ pub async fn check(


if let Mode::Production = mode {
let result = repositories::deployments::find_by_repo_branch(repo_url, final_branch).await?;
let result = repositories::deployments::find_by_repo_branch(&repo_url, &final_branch).await?;
match result {
Some(_) => Err(VoyagerError::new(
format!("A Production deployment for this repository and branch already exists!"),
Expand Down
4 changes: 2 additions & 2 deletions src/business/services/deployments/delete.rs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ pub async fn delete(deployment_id: String) -> Result<(), VoyagerError> {
delete_dns_record(&deployment.dns_record_id).await?;

delete_container(name.clone()).await?;
delete_image(deployment.image_name).await?;
delete_image(deployment.image_id).await?;

repositories::deployments::delete(deployment_id).await?;
repositories::deployments::delete(&deployment_id).await?;

// TODO: notify user via email

Expand Down
2 changes: 1 addition & 1 deletion src/business/services/deployments/get.rs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub async fn get(id: String) -> Result<Deployment, VoyagerError> {
let result = SERVICES_RUNTIME
.spawn_handled(
"services::deployments::get",
repositories::deployments::find_by_id(id),
async move { repositories::deployments::find_by_id(&id).await },
)
.await?;

Expand Down
2 changes: 1 addition & 1 deletion src/business/services/deployments/get_logs.rs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub async fn get_logs(id: String) -> Result<Vec<String>, VoyagerError> {
event!(Level::INFO, "Retrieving deployment logs. Id: {id}");

let future = async move {
let deployment = repositories::deployments::find_by_id(id).await?;
let deployment = repositories::deployments::find_by_id(&id).await?;

docker::get_logs(&deployment.container_name).await
};
Expand Down
Empty file modified src/business/services/deployments/list.rs
100644 → 100755
Empty file.
Empty file modified src/business/services/deployments/mod.rs
100644 → 100755
Empty file.
37 changes: 18 additions & 19 deletions src/business/services/deployments/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::modules::discord::send_deployment_message;
use crate::modules::{cloudflare, git};
use crate::types::model::deployment;
use crate::types::other::voyager_error::VoyagerError;
use crate::utils::{self, get_free_port};
use crate::utils::{self};
use crate::utils::runtime_helpers::RuntimeSpawnHandled;
use crate::modules::docker;
use async_trait::async_trait;
Expand Down Expand Up @@ -64,9 +64,9 @@ pub async fn new(
dir_as_path: None,
tar_path: None,
container_id: None,
port: None,
internal_port: None,
image_name: None,
// port: None,
// internal_port: None,
image_id: None,
dns_record_id: None,
final_id: None,
};
Expand Down Expand Up @@ -104,10 +104,10 @@ struct TransactionManager {
container_id: Option<String>,
host: Option<String>,
mode: Option<Mode>,
port: Option<u16>,
internal_port: Option<u16>,
// port: Option<u16>,
// internal_port: Option<u16>,
container_name: Option<String>,
image_name: Option<String>,
image_id: Option<String>,
dns_record_id: Option<String>,

final_id: Option<String>,
Expand Down Expand Up @@ -209,21 +209,20 @@ impl Command for CreateImage {
fs::read_to_string(&dockerfile).map_err(|e| VoyagerError::dockerfile_read(Box::new(e)))?;

let internal_port = docker::find_internal_port(dockerfile_contents.as_str())?;

let traefik_labels = utils::gen_traefik_labels(manager.container_name.as_ref().unwrap(), manager.host.as_ref().unwrap(), internal_port);

let image_name = docker::build_image(manager.tar_path.as_ref().unwrap(), &traefik_labels, None).await?;
let image_id = docker::build_image(manager.tar_path.as_ref().unwrap(), &traefik_labels, None).await?;

manager.internal_port = Some(internal_port);
manager.image_name = Some(image_name);
// manager.internal_port = Some(internal_port);
manager.image_id = Some(image_id);

manager.next = Some(Box::new(CreateContainer));

Ok(())
}
async fn undo(&self, manager: &TransactionManager) {
let image_name = manager.image_name.clone().unwrap();
let _ = docker::delete_image(image_name).await;
let image_id = manager.image_id.clone().unwrap();
let _ = docker::delete_image(image_id).await;
}
}

Expand All @@ -237,11 +236,11 @@ impl Command for CreateContainer {
tokio::fs::remove_file(manager.tar_path.as_ref().unwrap()).await
.map_err(|e| VoyagerError::delete_file_or_dir(Box::new(e)))?;

let port = get_free_port()?;
// let port = get_free_port()?;
let container_id =
docker::create_container(manager.container_name.clone().unwrap(), port, manager.internal_port.unwrap(), manager.image_name.as_ref().unwrap()).await?;
docker::create_container(manager.container_name.clone().unwrap(),/* port, manager.internal_port.unwrap(), */manager.image_id.as_ref().unwrap()).await?;

manager.port = Some(port);
// manager.port = Some(port);
manager.container_id = Some(container_id);

manager.next = Some(Box::new(StartContainer));
Expand Down Expand Up @@ -297,9 +296,9 @@ impl Command for SaveDeployment {
_id: ObjectId::new(),
container_id: manager.container_id.take().unwrap(),
dns_record_id: manager.dns_record_id.clone().unwrap(),
image_name: manager.image_name.clone().unwrap(),
image_id: manager.image_id.clone().unwrap(),
container_name: manager.container_name.clone().unwrap(),
port: manager.port.take().unwrap(),
// port: manager.port.take().unwrap(),
mode: manager.mode.take().unwrap(),
host: manager.host.take().unwrap(),
repo_url: manager.repo_url.take().unwrap(),
Expand All @@ -315,7 +314,7 @@ impl Command for SaveDeployment {
}

async fn undo(&self, manager: &TransactionManager) {
let deployment_id = manager.final_id.clone().unwrap();
let deployment_id = manager.final_id.as_ref().unwrap();
let _ = repositories::deployments::delete(deployment_id).await;
}
}
Expand Down
Empty file modified src/business/services/mod.rs
100644 → 100755
Empty file.
Empty file modified src/configs/environment.rs
100644 → 100755
Empty file.
Empty file modified src/configs/mod.rs
100644 → 100755
Empty file.
12 changes: 6 additions & 6 deletions src/controllers/deployments/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ use crate::{
};

pub async fn create(Query(queries): Query<HashMap<String, String>>) -> impl IntoResponse {
let mode = queries.get("mode").cloned();
let repo_url = queries.get("repoUrl").cloned();
let subdomain = queries.get("subdomain").cloned();
let mode = queries.get("mode");
let repo_url = queries.get("repoUrl");
let subdomain = queries.get("subdomain");

// Validations
let mode = match mode.as_deref() {
let mode = match mode.map(|s| s.as_str()) {
Some("production") => Mode::Production,
Some("preview") => Mode::Preview,
Some(_) => {
Expand Down Expand Up @@ -98,8 +98,8 @@ pub async fn create(Query(queries): Query<HashMap<String, String>>) -> impl Into
}
}

fn resolve_host(subdomain: Option<String>, mode: &Mode) -> Option<String> {
let subdomain = subdomain.unwrap_or_default();
fn resolve_host(subdomain: Option<&String>, mode: &Mode) -> Option<String> {
let subdomain = subdomain.map(|s| s.as_ref()).unwrap_or_default();

// Validates the subdomain
#[allow(clippy::unwrap_used)] // We know that the unwrap will always succeed because it is a valid Regex
Expand Down
Empty file modified src/controllers/deployments/mod.rs
100644 → 100755
Empty file.
Empty file modified src/modules/cloudflare/mod.rs
100644 → 100755
Empty file.
Empty file modified src/modules/cloudflare/types/add_dns_record.rs
100644 → 100755
Empty file.
Empty file modified src/modules/cloudflare/types/cloudflare_responses.rs
100644 → 100755
Empty file.
Empty file modified src/modules/cloudflare/types/delete_dns_record.rs
100644 → 100755
Empty file.
Empty file modified src/modules/cloudflare/types/dns_record.rs
100644 → 100755
Empty file.
Empty file modified src/modules/cloudflare/types/mod.rs
100644 → 100755
Empty file.
Empty file modified src/modules/discord/mod.rs
100644 → 100755
Empty file.
7 changes: 3 additions & 4 deletions src/modules/docker/create_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ use super::{DOCKER, DOCKER_RUNTIME};

pub async fn create_container(
name: String,
port: u16,
#[allow(unused)]
internal_port: u16,
// port: u16,
// internal_port: u16,
docker_image: &str,
) -> Result<String, VoyagerError> {
event!(
Level::INFO,
"Creating a new container {name} at port {port}. Docker Image: {docker_image}"
"Creating a new container {name}. Docker Image: {docker_image}"
);

// let host_config = HostConfig {
Expand Down
6 changes: 3 additions & 3 deletions src/modules/docker/delete_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use axum::http::StatusCode;
use bollard::image::RemoveImageOptions;
use tracing::{event, Level};

pub async fn delete_image(image_name: String) -> Result<(), VoyagerError> {
event!(Level::INFO, "Deleting image {image_name}");
pub async fn delete_image(image_id: String) -> Result<(), VoyagerError> {
event!(Level::INFO, "Deleting image {image_id}");

let options = Some(RemoveImageOptions {
force: true,
Expand All @@ -16,7 +16,7 @@ pub async fn delete_image(image_name: String) -> Result<(), VoyagerError> {

DOCKER_RUNTIME
.spawn_handled("modules::docker::delete_image", async move {
DOCKER.remove_image(&image_name, options, None).await
DOCKER.remove_image(&image_id, options, None).await
})
.await?
.map_err(|e| VoyagerError::delete_image(Box::new(e)))?;
Expand Down
23 changes: 7 additions & 16 deletions src/modules/docker/get_internal_port.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::num::ParseIntError;

use axum::http::StatusCode;
use regex::Regex;
use tracing::{event, Level};
Expand All @@ -13,21 +11,14 @@ pub fn find_internal_port(docker_file_content: &str) -> Result<u16, VoyagerError
);

#[allow(clippy::unwrap_used)] // Should never fail since valid Regex
let re = Regex::new(r"EXPOSE (\d+)")
.unwrap();

let re = re
Regex::new(r"EXPOSE (\d+)")
.unwrap()
.captures_iter(docker_file_content)
.map(|c| c.extract());

let mut results: Vec<Result<u16, ParseIntError>> = vec![];
for (_, [port]) in re {
results.push(port.parse::<u16>());
}

results.first()
.ok_or_else(|| VoyagerError::parse_port(None))
.and_then(|r| r.clone().map_err(|e| VoyagerError::parse_port(Some(Box::new(e)))))
.map(|c| c[1].to_string())
.next()
.ok_or_else(|| VoyagerError::parse_port(None))?
.parse::<u16>()
.map_err(|e| VoyagerError::parse_port(Some(Box::new(e))))
}

impl VoyagerError {
Expand Down
Empty file modified src/modules/docker/get_logs.rs
100644 → 100755
Empty file.
Empty file modified src/modules/docker/mod.rs
100644 → 100755
Empty file.
4 changes: 2 additions & 2 deletions src/modules/git/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ pub fn clone(
) -> Result<Repository, VoyagerError> {
event!(Level::INFO, "Cloning repository: {}", repo_url);

let username = GIT_USERNAME.as_ref();
let username = &*GIT_USERNAME;

let pat = GIT_PAT.as_ref();
let pat = &*GIT_PAT;
let repo_url = format!("https://git.lunarlabs.cc/{repo_url}.git");

// Configure authentication
Expand Down
Empty file modified src/modules/git/mod.rs
100644 → 100755
Empty file.
Empty file modified src/modules/tar/mod.rs
100644 → 100755
Empty file.
4 changes: 2 additions & 2 deletions src/types/model/deployment.rs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ pub struct Deployment {
pub container_id: String,
pub dns_record_id: String,
pub container_name: String,
pub image_name: String,
pub port: u16,
pub image_id: String,
// pub port: u16,
pub mode: Mode,
pub host: String,
pub repo_url: String,
Expand Down
Empty file modified src/types/model/mod.rs
100644 → 100755
Empty file.
Empty file modified src/types/other/mod.rs
100644 → 100755
Empty file.
Loading

0 comments on commit 4522ca4

Please sign in to comment.