diff --git a/src/cron/functions/check-services/git.ts b/src/cron/functions/check-services/git.ts index 89785b3..4ee4bb3 100644 --- a/src/cron/functions/check-services/git.ts +++ b/src/cron/functions/check-services/git.ts @@ -1,38 +1,112 @@ import { - NormalizedServerObject, ParsedDeploymentServiceConfig, + ResponseObject, TCIGlobalConfig, } from "@/src/types/turboci"; -import execSSH from "@/src/utils/exec-ssh"; +import bunGrabBulkSyncScripts from "@/src/utils/bun-grab-bulk-sync-script"; import serviceFlight from "@/src/utils/flight"; +import grabGitRepoName from "@/src/utils/grab-git-repo-name"; +import relayExecSSH from "@/src/utils/relay-exec-ssh"; +import turboCIPkgrabDirNames from "@/src/utils/turboci-pkg-grab-dir-names"; +import _ from "lodash"; +import path from "path"; + +const { relayServerRsyncDir } = turboCIPkgrabDirNames(); type Params = { service: ParsedDeploymentServiceConfig; - config: TCIGlobalConfig; + deployment: TCIGlobalConfig; }; -export default async function cronCheckServicesGit({ - config, - service, -}: Params) { - if (service.git) { - const service_git_array = Array.isArray(service.git) - ? service.git - : [service.git]; +export default async function cronCheckServicesGit( + params: Params, +): Promise { + const { service, deployment } = params; - for (let i = 0; i < service_git_array.length; i++) { - const service_git = service_git_array[i]; - - if (!service_git.keep_updated) { - continue; - } - - const work_dir = service_git.work_dir || "/turboci/app"; - - let cmd = ``; - - cmd += `set -e\n`; - cmd += `\n`; - } + if (!service.git || !service.servers?.[0]) { + return { success: true }; } + + const servers_private_ips = service.servers + .map((srv) => srv.private_ip) + .filter((ip) => Boolean(ip)) as string[]; + + const git_array = Array.isArray(service.git) ? service.git : [service.git]; + + for (let i = 0; i < git_array.length; i++) { + const service_git = git_array[i]; + + if (!service_git) continue; + + const git_url = service_git.repo_url; + const repo_name = grabGitRepoName({ git_url }); + + if (!repo_name) continue; + + const relay_dst = path.join( + relayServerRsyncDir, + service.service_name, + "git", + repo_name, + ); + + let git_pull_cmd = ``; + + git_pull_cmd += `cd ${relay_dst}\n`; + git_pull_cmd += `git pull\n`; + + const git_pull_check = await relayExecSSH({ + cmd: git_pull_cmd, + log_error: true, + }); + + if (git_pull_check?.match(/Already up to date./i)) { + continue; + } + + let cmd = ``; + + const src = relay_dst + "/"; + const dst = (service_git.work_dir || "/turboci/app") + "/"; + + const sync_cmd = await relayExecSSH({ + cmd: bunGrabBulkSyncScripts({ + dst, + src, + private_server_ips: servers_private_ips, + parrallel: true, + relay_ignore: [".git"], + }), + bun: true, + return_cmd_only: true, + }); + + cmd += `${sync_cmd}\n`; + + cmd += `echo "Git Setup Success!"\n`; + + const res = await relayExecSSH({ + cmd, + log_error: true, + debug: true, + }); + + if (!res?.match(/Git Setup Success/)) { + console.error( + `\`${service.service_name}\` service git prep failed!`, + ); + + continue; + } + + await serviceFlight({ + deployment, + servers: service.servers, + service, + }); + } + + return { + success: true, + }; } diff --git a/src/cron/functions/check-services/index.ts b/src/cron/functions/check-services/index.ts index b60e1f0..1304932 100644 --- a/src/cron/functions/check-services/index.ts +++ b/src/cron/functions/check-services/index.ts @@ -1,6 +1,6 @@ import grabTurboCiConfig from "@/src/utils/grab-turboci-config"; import cronCheckServicesHealtcheck from "./healthcheck"; -import serviceGitCheck from "../../utils/service-git-check"; +import cronCheckServicesGit from "./git"; export default async function cronCheckServices() { const config = grabTurboCiConfig(); @@ -12,7 +12,7 @@ export default async function cronCheckServices() { continue; } - await serviceGitCheck({ deployment: config, service }); + await cronCheckServicesGit({ deployment: config, service }); for (let srv = 0; srv < service.servers.length; srv++) { const server = service.servers[srv]; diff --git a/src/cron/utils/service-git-check.ts b/src/cron/utils/service-git-check.ts deleted file mode 100644 index e4ede70..0000000 --- a/src/cron/utils/service-git-check.ts +++ /dev/null @@ -1,112 +0,0 @@ -import { - ParsedDeploymentServiceConfig, - ResponseObject, - TCIGlobalConfig, -} from "@/src/types/turboci"; -import bunGrabBulkSyncScripts from "@/src/utils/bun-grab-bulk-sync-script"; -import serviceFlight from "@/src/utils/flight"; -import grabGitRepoName from "@/src/utils/grab-git-repo-name"; -import relayExecSSH from "@/src/utils/relay-exec-ssh"; -import turboCIPkgrabDirNames from "@/src/utils/turboci-pkg-grab-dir-names"; -import _ from "lodash"; -import path from "path"; - -const { relayServerRsyncDir } = turboCIPkgrabDirNames(); - -type Params = { - service: ParsedDeploymentServiceConfig; - deployment: TCIGlobalConfig; -}; - -export default async function serviceGitCheck( - params: Params, -): Promise { - const { service, deployment } = params; - - if (!service.git || !service.servers?.[0]) { - return { success: true }; - } - - const servers_private_ips = service.servers - .map((srv) => srv.private_ip) - .filter((ip) => Boolean(ip)) as string[]; - - const git_array = Array.isArray(service.git) ? service.git : [service.git]; - - for (let i = 0; i < git_array.length; i++) { - const service_git = git_array[i]; - - if (!service_git) continue; - - const git_url = service_git.repo_url; - const repo_name = grabGitRepoName({ git_url }); - - if (!repo_name) continue; - - const relay_dst = path.join( - relayServerRsyncDir, - service.service_name, - "git", - repo_name, - ); - - let git_pull_cmd = ``; - - git_pull_cmd += `cd ${relay_dst}\n`; - git_pull_cmd += `git pull\n`; - - const git_pull_check = await relayExecSSH({ - cmd: git_pull_cmd, - log_error: true, - }); - - if (git_pull_check?.match(/Already up to date./i)) { - continue; - } - - let cmd = ``; - - const src = relay_dst + "/"; - const dst = (service_git.work_dir || "/turboci/app") + "/"; - - const sync_cmd = await relayExecSSH({ - cmd: bunGrabBulkSyncScripts({ - dst, - src, - private_server_ips: servers_private_ips, - parrallel: true, - relay_ignore: [".git"], - }), - bun: true, - return_cmd_only: true, - }); - - cmd += `${sync_cmd}\n`; - - cmd += `echo "Git Setup Success!"\n`; - - const res = await relayExecSSH({ - cmd, - log_error: true, - debug: true, - }); - - if (!res?.match(/Git Setup Success/)) { - console.error( - `\`${service.service_name}\` service git prep failed!`, - ); - - continue; - } - - await serviceFlight({ - deployment, - servers: service.servers, - service, - }); - } - - return { - success: true, - }; -}