Skip to content
This repository has been archived by the owner on Dec 14, 2023. It is now read-only.

Commit

Permalink
Merge pull request #20 from Tolfix/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Tolfx authored Jul 6, 2021
2 parents fca3b54 + 0029a80 commit 412e54b
Show file tree
Hide file tree
Showing 20 changed files with 531 additions and 63 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"scripts": {
"build": "tsc",
"start": "npm run build && node ./build/Server.js",
"nodemon": "nodemon ./build/Server",
"setup": "npm run build && node ./build/Setup.js"
},
"repository": {
Expand Down
3 changes: 3 additions & 0 deletions public/Scripts/Bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$(document).ready(function(){
$('.toast').toast('show');
});
1 change: 1 addition & 0 deletions public/Scripts/Socket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const socket = io();
22 changes: 16 additions & 6 deletions src/Docker/DockerCompose.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import { stripIndent } from "common-tags"
import { ICD } from "../Interfaces/CD";
import docker from "docker-compose";
import cp from "child_process";
import log from "../Lib/Logger";
import AW from "../Lib/Async";
import { ICreateDockerCompose } from "../Interfaces/Docker";

export function DockerCompose(dir: string, service: string = ""): void
export function DockerCompose(dir: string): Promise<string>
{

docker.upOne(service, {
cwd: dir
return new Promise(async (resolve, reject) => {
const [DS, D_Error] = await AW(docker.upAll({
cwd: dir
}));

if(D_Error)
{
log.error(`${DS?.err.trim()}`);
reject(`Unable to build docker`);
}

resolve(`Build and finished`);
});
}

export function CreateDockerCompose(options: ICD): string
export function CreateDockerCompose(options: ICreateDockerCompose): string
{

let envs = stripIndent`
Expand Down
20 changes: 20 additions & 0 deletions src/Docker/DockerDown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import docker from "docker-compose";
import AW from "../Lib/Async";
import log from "../Lib/Logger";

export default function DockerRemove(dir: string): Promise<Boolean>
{
return new Promise(async (resolve, reject) => {
const [DS, D_Error] = await AW(docker.down({
cwd: dir
}));

if(D_Error)
{
log.error(`${DS?.err.trim()}`);
resolve(false);
}

resolve(true);
});
}
10 changes: 6 additions & 4 deletions src/Docker/Pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ import docker from "docker-compose";
import AW from "../Lib/Async";
import log from "../Lib/Logger";

export default function PullImage(image: string): Promise<Boolean>
export default function PullImage(dir: string): Promise<Boolean>
{
return new Promise(async (resolve, reject) => {
const [Image, I_Error] = await AW(docker.pullOne(image));
const [Image, I_Error] = await AW(docker.pullAll({
cwd: dir
}));

if(I_Error)
{
log.error(`Unable to pull image: ${I_Error}`);
log.error(`Unable to pull image`);
return resolve(false);
}
log.info(Image);

return resolve(true);
});
}
38 changes: 29 additions & 9 deletions src/Docker/Setup.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import fs from "fs";
import cp from "child_process";
import { Active, Failed } from "../Types/Statues";
import { DockerDir } from "../Config";
import { ISetupDocker } from "../Interfaces/SetupDocker";
import { CreateDockerCompose, DockerCompose } from "./DockerCompose";
import AW from "../Lib/Async";
import PullImage from "./Pull";
import { io } from "../Server";
import { getCDSocketActive, getCDSocketBuild, getCDSocketFail } from "../Lib/CDSocket";

export default function SetupDocker(options: ISetupDocker): Promise<string>
export default function SetupDocker(options: ISetupDocker, mong: any): Promise<string>
{
return new Promise(async (resolve, reject) => {
const [Image, I_Error] = await AW(PullImage(options.image));

if(!Image)
return reject("Failed to pull image " + options.image);

if (!fs.existsSync(DockerDir))
{
fs.mkdirSync(DockerDir);
}

io.emit(getCDSocketBuild(options.name), `Building ${options.name}`)

// Create docker-compose.yml
const file = CreateDockerCompose(options);
Expand All @@ -31,8 +30,29 @@ export default function SetupDocker(options: ISetupDocker): Promise<string>

const [] = await AW(fs.appendFileSync(fileDir, file));

DockerCompose(Dir);
const [Image, I_Error] = await AW(PullImage(Dir));

if(!Image)
{
mong.status = Failed;
await mong.save();
io.emit(getCDSocketFail(options.name), `Failed to pull image`)
return reject("Failed to pull image " + options.image);
}

const [D, D_Error] = await AW(DockerCompose(Dir));

if(D_Error)
{
mong.status = Failed;
await mong.save();
io.emit(getCDSocketFail(options.name), `Failed to build docker`)
return reject(`Failed to build ${options.name}`);
}

resolve("Finished")
mong.status = Active;
await mong.save();
io.emit(getCDSocketActive(options.name), `Docker built and finished`);
return resolve("Finished");
});
}
2 changes: 2 additions & 0 deletions src/Interfaces/CD.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Document, model, Schema } from "mongoose"

export interface IDCD extends ICD, Document {};
export interface ICD
{
name: string;
image: string;
env?: Array<ENV>;
ports?: Array<PORTS>;
webhookUrl: string;
status: string;
restartPolicy: "always" | "never" | "on_failure" | "unless_stopped";
}

Expand Down
10 changes: 10 additions & 0 deletions src/Interfaces/Docker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ENV, PORTS } from "./CD";

export interface ICreateDockerCompose
{
name: string;
image: string;
restartPolicy: "always" | "never" | "on_failure" | "unless_stopped";
env?: ENV[];
ports?: PORTS[];
}
14 changes: 14 additions & 0 deletions src/Lib/CDSocket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function getCDSocketFail(name: string): string
{
return `cd-${name}-fail`;
}

export function getCDSocketBuild(name: string): string
{
return `cd-${name}-building`;
}

export function getCDSocketActive(name: string): string
{
return `cd-${name}-active`;
}
2 changes: 1 addition & 1 deletion src/Middlewares/EnsureAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export default function EnsureAuth(req: Request, res: Response, next: NextFuncti
return next();
}

req.flash("error_msg", "Please login");
req.flash("error", "Please login");
return res.redirect("/login");
}
11 changes: 7 additions & 4 deletions src/Models/CD.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { model, Schema, Document } from "mongoose"
import { ICD } from "../Interfaces/CD";
import { ICD, IDCD } from "../Interfaces/CD";

const CDSchema = new Schema
(
Expand All @@ -21,6 +21,11 @@ const CDSchema = new Schema
required: false
},

ports: {
type: Array,
required: false
},

restartPolicy: {
type: String,
default: "always"
Expand All @@ -39,8 +44,6 @@ const CDSchema = new Schema
}
);

interface b extends ICD, Document {};

const CDModel = model<b>("cd", CDSchema);
const CDModel = model<IDCD>("cd", CDSchema);

export default CDModel;
Loading

0 comments on commit 412e54b

Please sign in to comment.