This commit is contained in:
Rizky 2024-11-14 18:44:16 +07:00
parent 0801109562
commit ca8ba09889
2 changed files with 46 additions and 28 deletions

View File

@ -22,18 +22,24 @@ export const _ = {
| { type: "domain-add"; domain: string } | { type: "domain-add"; domain: string }
| { type: "domain-del"; domain: string } | { type: "domain-del"; domain: string }
| { type: "deploy-del"; ts: string } | { type: "deploy-del"; ts: string }
| { type: "deploy"; dlurl: string } | { type: "deploy"; load_from?: string }
| { type: "deploy-status" } | { type: "deploy-status" }
| { type: "redeploy"; ts: string } | { type: "redeploy"; ts: string }
) & { ) & {
id_site: string; id_site: string;
} }
) { ) {
const { res } = apiContext(this); const { res, req } = apiContext(this);
const path = dir(`app/web/`); const path = dir(`app/web/`);
await dirAsync(path); await dirAsync(path);
if (typeof req.query_parameters["export"] !== "undefined") {
return new Response(
Bun.file(dir(`app/web/deploy/${deploy.config.deploy.ts}.gz`))
);
}
switch (action.type) { switch (action.type) {
case "check": case "check":
const deploys = fs.readdirSync(dir(`/app/web/deploy`)); const deploys = fs.readdirSync(dir(`/app/web/deploy`));
@ -164,7 +170,7 @@ export const _ = {
await deploy.saveConfig(); await deploy.saveConfig();
deploy.config.deploy.ts = Date.now() + ""; deploy.config.deploy.ts = Date.now() + "";
await deploy.init(); await deploy.init(action.load_from);
const deploys = fs.readdirSync(dir(`/app/web/deploy`)); const deploys = fs.readdirSync(dir(`/app/web/deploy`));
if (g.mode === "prod") { if (g.mode === "prod") {

View File

@ -15,7 +15,7 @@ import { gunzipAsync } from "./gzip";
const decoder = new TextDecoder(); const decoder = new TextDecoder();
export const deploy = { export const deploy = {
async init() { async init(load_from?: string) {
await dirAsync(dir(`app/web/deploy`)); await dirAsync(dir(`app/web/deploy`));
if (!(await this.has_gz())) { if (!(await this.has_gz())) {
@ -32,19 +32,21 @@ export const deploy = {
g.deploy.content = decode( g.deploy.content = decode(
await gunzipAsync( await gunzipAsync(
new Uint8Array( new Uint8Array(
await Bun.file(dir(`app/web/deploy/${ts}.gz`)).arrayBuffer(), await Bun.file(dir(`app/web/deploy/${ts}.gz`)).arrayBuffer()
), )
), )
); );
} else { } else {
g.deploy.content = JSON.parse( g.deploy.content = JSON.parse(
decoder.decode( decoder.decode(
await gunzipAsync( new Uint8Array(
new Uint8Array( await gunzipAsync(
await Bun.file(dir(`app/web/deploy/${ts}.gz`)).arrayBuffer(), new Uint8Array(
), await Bun.file(dir(`app/web/deploy/${ts}.gz`)).arrayBuffer()
), )
), )
)
)
); );
} }
@ -98,7 +100,7 @@ export const deploy = {
await removeAsync(dir(`app/web/server`)); await removeAsync(dir(`app/web/server`));
await dirAsync(dir(`app/web/server`)); await dirAsync(dir(`app/web/server`));
for (const [k, v] of Object.entries( for (const [k, v] of Object.entries(
g.deploy.content.code.server, g.deploy.content.code.server
)) { )) {
await writeAsync(dir(`app/web/server/${k}`), v); await writeAsync(dir(`app/web/server/${k}`), v);
} }
@ -130,27 +132,37 @@ export const deploy = {
console.error(e.message, `[app/web/deploy/${ts}.gz]`); console.error(e.message, `[app/web/deploy/${ts}.gz]`);
} }
}, },
async run() { async run(load_from?: string) {
if (!this.config.site_id) { if (!this.config.site_id) {
console.log("site_id is not found on app/web/config.json"); console.log("site_id is not found on app/web/config.json");
return; return;
} }
let buf: ArrayBuffer | null = null;
if (!load_from) {
let base_url = "https://prasi.avolut.com";
if (g.mode === "dev") {
base_url = "http://localhost:4550";
}
let base_url = "https://prasi.avolut.com"; console.log(
if (g.mode === "dev") { `Downloading site deploy: ${this.config.site_id} [ts: ${this.config.deploy.ts}] ${base_url}`
base_url = "http://localhost:4550"; );
const res = await fetch(
`${base_url}/prod-zip/${this.config.site_id}?ts=${Date.now()}&msgpack=1`
);
buf = await res.arrayBuffer();
} else {
const res = await fetch(load_from);
buf = await res.arrayBuffer();
} }
console.log( if (!buf) {
`Downloading site deploy: ${this.config.site_id} [ts: ${this.config.deploy.ts}] ${base_url}`, console.log("Failed to download site deploy");
); return;
const res = await fetch( }
`${base_url}/prod-zip/${this.config.site_id}?ts=${Date.now()}&msgpack=1`,
);
const ts = Date.now(); const ts = Date.now();
const file = Bun.file(dir(`app/web/deploy/${ts}.gz`)); const file = Bun.file(dir(`app/web/deploy/${ts}.gz`));
await Bun.write(file, await res.arrayBuffer()); await Bun.write(file, buf);
await Bun.write(dir(`app/web/deploy/${ts}.mpack`), "ok"); await Bun.write(dir(`app/web/deploy/${ts}.mpack`), "ok");
this.config.deploy.ts = ts + ""; this.config.deploy.ts = ts + "";
@ -187,13 +199,13 @@ export const deploy = {
saveConfig() { saveConfig() {
return Bun.write( return Bun.write(
Bun.file(dir(`app/web/config.json`)), Bun.file(dir(`app/web/config.json`)),
JSON.stringify(this.config, null, 2), JSON.stringify(this.config, null, 2)
); );
}, },
has_gz() { has_gz() {
if (this.config.deploy.ts) { if (this.config.deploy.ts) {
return Bun.file( return Bun.file(
dir(`app/web/deploy/${this.config.deploy.ts}.gz`), dir(`app/web/deploy/${this.config.deploy.ts}.gz`)
).exists(); ).exists();
} }