Skip to content

Commit

Permalink
Merge pull request #6 from RukuLab/dev
Browse files Browse the repository at this point in the history
Feature: Docker deployment
  • Loading branch information
Joker666 authored Aug 5, 2024
2 parents 5b0df81 + 9ffbc66 commit 60b7a45
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
38 changes: 30 additions & 8 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::fs::File;
use std::io::{BufRead, Write};
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
use std::{fs, io};
use std::{env, fs, io};

use cmd_lib::run_cmd;
use cmd_lib::{run_cmd, run_fun};

use crate::logger::Logger;
use crate::misc::sanitize_app_name;
Expand Down Expand Up @@ -105,7 +105,7 @@ cat | RUKU_ROOT="{}" {} git-hook {}
if parts.len() != 3 {
continue;
}
let (_, new_rev, _) = (parts[0], parts[1], parts[2]);
let (_, new_rev, branch) = (parts[0], parts[1], parts[2]);

if !app_path.exists() {
fs::create_dir_all(&app_path).unwrap_or_else(|e| {
Expand All @@ -127,15 +127,37 @@ cat | RUKU_ROOT="{}" {} git-hook {}
});
}

self.checkout_latest(&app_path, new_rev);
self.checkout_latest(&app_path, new_rev, branch);
}
}
fn checkout_latest(&self, app_path: &Path, new_rev: &str, branch: &str) {
unsafe {
env::set_var("GIT_DIR", app_path.join(".git").display().to_string());
env::set_var("GIT_WORK_TREE", app_path.display().to_string());
}

let branch = branch.trim_start_matches("refs/heads/");
self.log
.step(&format!("Checking out the latest code from branch: {}", branch));

// Get the current branch
let current_branch = run_fun!(git rev-parse --abbrev-ref HEAD).unwrap_or_else(|e| {
self.log.error(&format!("Error getting current branch: {}", e));
std::process::exit(1);
});

// Check if the current branch is the same as the target branch
if current_branch.trim() != branch {
run_cmd!(git checkout $branch).unwrap_or_else(|e| {
self.log.error(&format!("Error checking out latest code: {}", e));
std::process::exit(1);
});
}

fn checkout_latest(&self, app_path: &Path, new_rev: &str) {
self.log.step("Checking out the latest code");
// Checkout the latest code
run_cmd!(
git --git-dir=$app_path/.git fetch --quiet;
git --git-dir=$app_path/.git reset --hard $new_rev;
git fetch --quiet;
git reset --hard $new_rev;
)
.unwrap_or_else(|e| {
self.log.error(&format!("Error checking out latest code: {}", e));
Expand Down
18 changes: 18 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ use clap::{Parser, Subcommand};
use logger::Logger;
use server_config::ServerConfig;

use crate::container::Container;
use crate::deploy::Deploy;
use crate::git::Git;
use crate::misc::sanitize_app_name;
use crate::model::Config;

mod container;
mod deploy;
Expand Down Expand Up @@ -110,6 +114,7 @@ async fn main() {
}
Commands::GitHook { repo } => {
git.cmd_git_hook(repo);
deploy(&log, repo, server_config).await;
}
Commands::GitReceivePack { repo } => {
log.section("... RUKU ...");
Expand All @@ -122,6 +127,19 @@ async fn main() {
}
}

async fn deploy(log: &Logger, repo: &str, server_config: ServerConfig) {
log.section("Deploying application");
let config = Config::default();
let docker = get_docker(log).await;

let app = sanitize_app_name(repo);
let app_path = server_config.apps_root.join(&app);

let container = Container::new(log, repo, &docker, &config);
let deploy = Deploy::new(log, repo, app_path.as_path().to_str().unwrap(), &config, &container);
deploy.run().await;
}

async fn get_docker(log: &Logger) -> Docker {
let docker = load_docker(log).await;

Expand Down
3 changes: 1 addition & 2 deletions src/model.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use serde::Deserialize;

#[derive(Deserialize)]
#[derive(Default, Deserialize)]
pub struct Config {
pub port: Option<u16>,
pub name: Option<String>,
pub version: Option<String>,
}

0 comments on commit 60b7a45

Please sign in to comment.