-
-
Notifications
You must be signed in to change notification settings - Fork 396
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
433 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { DockgeServer } from "../dockge-server"; | ||
import { log } from "../log"; | ||
import { Router } from "../router"; | ||
import express, { Express, Router as ExpressRouter } from "express"; | ||
import { Stack } from "../stack"; | ||
|
||
export class WebhookRouter extends Router { | ||
create(app: Express, server: DockgeServer): ExpressRouter { | ||
const router = express.Router(); | ||
|
||
router.get("/webhook/update/:stackname", async (req, res, _next) => { | ||
try { | ||
const stackname = req.params.stackname; | ||
|
||
log.info("router", `Webhook received for stack: ${stackname}`); | ||
const stack = await Stack.getStack(server, stackname); | ||
if (!stack) { | ||
log.error("router", `Stack not found: ${stackname}`); | ||
res.status(404).json({ message: `Stack not found: ${stackname}` }); | ||
return; | ||
} | ||
await stack.update(undefined); | ||
|
||
// Send a response | ||
res.json({ message: `Updated stack: ${stackname}` }); | ||
|
||
} catch (error) { | ||
_next(error); | ||
} | ||
}); | ||
|
||
return router; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,8 @@ import { | |
import { InteractiveTerminal, Terminal } from "./terminal"; | ||
import childProcessAsync from "promisify-child-process"; | ||
import { Settings } from "./settings"; | ||
import { exec } from "child_process"; | ||
import ini from "ini"; | ||
Check failure on line 23 in backend/stack.ts GitHub Actions / ci (ubuntu-latest, 18.17.1)
Check failure on line 23 in backend/stack.ts GitHub Actions / ci (ARM64, 18.17.1)
|
||
|
||
export class Stack { | ||
|
||
|
@@ -84,6 +86,9 @@ export class Stack { | |
status: this._status, | ||
tags: [], | ||
isManagedByDockge: this.isManagedByDockge, | ||
isGitRepo: this.isGitRepo, | ||
gitUrl: this.gitUrl, | ||
webhook: this.webhook, | ||
composeFileName: this._composeFileName, | ||
endpoint, | ||
}; | ||
|
@@ -107,6 +112,27 @@ export class Stack { | |
return fs.existsSync(this.path) && fs.statSync(this.path).isDirectory(); | ||
} | ||
|
||
get isGitRepo() : boolean { | ||
return fs.existsSync(path.join(this.path, ".git")) && fs.statSync(path.join(this.path, ".git")).isDirectory(); | ||
} | ||
|
||
get gitUrl() : string { | ||
if (this.isGitRepo) { | ||
const gitConfig = ini.parse(fs.readFileSync(path.join(this.path, ".git", "config"), "utf-8")); | ||
return gitConfig["remote \"origin\""]?.url; | ||
} | ||
return ""; | ||
} | ||
|
||
get webhook() : string { | ||
//TODO: refine this. | ||
if (this.server.config.hostname) { | ||
return `http://${this.server.config.hostname}:${this.server.config.port}/webhook/update/${this.name}`; | ||
} else { | ||
return `http://localhost:${this.server.config.port}/webhook/update/${this.name}`; | ||
} | ||
} | ||
|
||
get status() : number { | ||
return this._status; | ||
} | ||
|
@@ -443,8 +469,16 @@ export class Stack { | |
return exitCode; | ||
} | ||
|
||
async update(socket: DockgeSocket) { | ||
const terminalName = getComposeTerminalName(socket.endpoint, this.name); | ||
async update(socket?: DockgeSocket) { | ||
const terminalName = socket ? getComposeTerminalName(socket.endpoint, this.name) : ""; | ||
|
||
if (this.isGitRepo) { | ||
let exitCode = await Terminal.exec(this.server, socket, terminalName, "git", [ "pull", "--strategy-option", "theirs" ], this.path); | ||
if (exitCode !== 0) { | ||
throw new Error("Failed to update, please check the terminal output for more information."); | ||
} | ||
} | ||
|
||
let exitCode = await Terminal.exec(this.server, socket, terminalName, "docker", [ "compose", "pull" ], this.path); | ||
if (exitCode !== 0) { | ||
throw new Error("Failed to pull, please check the terminal output for more information."); | ||
|
@@ -464,6 +498,28 @@ export class Stack { | |
return exitCode; | ||
} | ||
|
||
checkRemoteChanges() { | ||
return new Promise((resolve, reject) => { | ||
if (!this.isGitRepo) { | ||
reject("This stack is not a git repository"); | ||
return; | ||
} | ||
//fetch remote changes and check if the current branch is behind | ||
exec("git fetch origin && git status -uno", { cwd: this.path }, (error, stdout, stderr) => { | ||
if (error) { | ||
log.error("checkRemoteChanges", error); | ||
reject("Failed to check local status"); | ||
return; | ||
} | ||
if (stdout.includes("Your branch is behind")) { | ||
resolve(true); | ||
} else { | ||
resolve(false); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
async joinCombinedTerminal(socket: DockgeSocket) { | ||
const terminalName = getCombinedTerminalName(socket.endpoint, this.name); | ||
const terminal = Terminal.getOrCreateTerminal(this.server, terminalName, "docker", [ "compose", "logs", "-f", "--tail", "100" ], this.path); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<template> | ||
<div> | ||
<form class="my-4" autocomplete="off" @submit.prevent="saveGitOps"> | ||
<!-- Enable Auto Updates --> | ||
<div class="mb-4"> | ||
<label class="form-label" for="gitAutoUpdate"> | ||
{{ $t("gitAutoUpdate") }} | ||
</label> | ||
|
||
<div class="form-check form-switch my-3"> | ||
<input id="git-auto-update" v-model="settings.gitAutoUpdate" class="form-check-input" type="checkbox"> | ||
<label class="form-check-label"> | ||
{{ $t("enableAutoUpdate") }} | ||
</label> | ||
</div> | ||
|
||
<div class="form-text"></div> | ||
</div> | ||
|
||
<!-- Save Button --> | ||
<div> | ||
<button class="btn btn-primary" type="submit"> | ||
{{ $t("Save") }} | ||
</button> | ||
</div> | ||
</form> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
data() { | ||
return {}; | ||
}, | ||
computed: { | ||
settings() { | ||
return this.$parent.$parent.$parent.settings; | ||
}, | ||
saveSettings() { | ||
return this.$parent.$parent.$parent.saveSettings; | ||
} | ||
}, | ||
methods: { | ||
/** Save the settings */ | ||
saveGitOps() { | ||
this.saveSettings(); | ||
}, | ||
}, | ||
}; | ||
</script> | ||
<style scoped lang="scss"> | ||
@import "../../styles/vars.scss"; | ||
</style> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.