-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
133abea
commit e2873a7
Showing
12 changed files
with
183 additions
and
95 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,27 @@ | ||
import { chunk } from "../../util/arrays"; | ||
import { ApiV4 } from "./api"; | ||
|
||
const purge = async (crateName, secrets, namedAssets) => { | ||
const { HostUrl } = await import(crateName); | ||
const { zoneId, token } = secrets.CloudflareAuth; | ||
namedAssets.push(""); | ||
/** @const {!Array<!Array<string>>} */ | ||
const batches = chunk(namedAssets, 30); | ||
for (let i = 0; i < batches.length; ++i) { | ||
console.log(`Purging batch ${i + 1} of ${batches.length}:`, batches[i]); | ||
/** @const {string} */ | ||
const body = JSON.stringify({ files: batches[i].map((asset) => `${HostUrl}/${asset}`) }); | ||
const res = await fetch(`${ApiV4}/zones/${zoneId}/purge_cache`, { | ||
method: "POST", | ||
headers: { | ||
"content-type": "application/json", | ||
"authorization": `Bearer ${token}`, | ||
}, | ||
body | ||
}); | ||
const { success } = await res.json(); | ||
console.log(success ? "Success" : "Failed"); | ||
} | ||
} | ||
|
||
export default { purge }; |
This file was deleted.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,33 +1,32 @@ | ||
import { spawn } from "bun"; | ||
import cache from "../cloudflare/cache"; | ||
import { restart } from "./service"; | ||
|
||
const deploy = async (crateName, secrets, namedAssets) => { | ||
const { | ||
host = 'localhost', | ||
username = 'root', | ||
remotePath = '/var/www/crate', | ||
host, | ||
username, | ||
remotePath, | ||
sshKey, | ||
} = secrets.VpsConfig; | ||
|
||
const target = `${username}@${host}:${remotePath}`; | ||
/** @const {string} */ | ||
const target = `[${host}]:${remotePath}/crate`; | ||
|
||
try { | ||
// Deploy files | ||
const rsync = spawn([ | ||
"rsync", | ||
"-azP", | ||
"--delete", | ||
"-e", `ssh -p 22`, | ||
"build/crate/", | ||
"-rP", | ||
"-e", `ssh -6 -i ${sshKey} -l ${username}`, | ||
"./build/crate/", | ||
target | ||
]); | ||
|
||
const deployOutput = await new Response(rsync.stdout).text(); | ||
console.log('Deploy successful:', deployOutput); | ||
|
||
return { success: true }; | ||
console.log("Deploy successful:", deployOutput); | ||
} catch (error) { | ||
console.error('Deploy failed:', error); | ||
console.error("Deploy failed:", error); | ||
throw error; | ||
} | ||
return restart(secrets).then(() => cache.purge(crateName, secrets, namedAssets)); | ||
} | ||
|
||
export { deploy }; |
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,89 @@ | ||
import { spawn } from "bun"; | ||
|
||
const setup = async (secrets) => { | ||
const { | ||
host = "localhost", | ||
username = "root", | ||
sshKey, | ||
remotePath = "/var/www/crate", | ||
} = secrets.VpsConfig; | ||
|
||
// Create systemd service file content | ||
const serviceConfig = `[Unit] | ||
Description=Kastro service | ||
After=network.target | ||
[Service] | ||
Type=simple | ||
User=${username} | ||
WorkingDirectory=${remotePath} | ||
ExecStart=/snap/bin/bun run ./crateServer.js | ||
Restart=always | ||
RestartSec=10 | ||
Environment=NODE_ENV=production | ||
[Install] | ||
WantedBy=multi-user.target`; | ||
|
||
try { | ||
// Write service file remotely | ||
const sshCommand = `echo '${serviceConfig}' | sudo tee /etc/systemd/system/kastro.service`; | ||
const setupCommands = [ | ||
sshCommand, | ||
"sudo chmod 644 /etc/systemd/system/kastro.service", | ||
"sudo systemctl daemon-reload", | ||
"sudo systemctl enable kastro", | ||
"sudo systemctl restart kastro" | ||
].join(" && "); | ||
|
||
const ssh = spawn([ | ||
"ssh", | ||
"-6", | ||
"-i", sshKey, | ||
"-l", username, | ||
host, | ||
setupCommands | ||
]); | ||
|
||
const output = await new Response(ssh.stdout).text(); | ||
console.log("Service setup successful:", output); | ||
|
||
// Check service status | ||
const status = spawn([ | ||
"ssh", | ||
"-6", | ||
"-i", sshKey, | ||
"-l", username, | ||
host, | ||
"sudo systemctl status kastro" | ||
]); | ||
|
||
const statusOutput = await new Response(status.stdout).text(); | ||
console.log("Service status:", statusOutput); | ||
} catch (error) { | ||
console.error("Service setup failed:", error); | ||
throw error; | ||
} | ||
}; | ||
|
||
const restart = async (secrets) => { | ||
const { | ||
host = "localhost", | ||
username = "root", | ||
sshKey, | ||
} = secrets.VpsConfig; | ||
|
||
const ssh = spawn([ | ||
"ssh", | ||
"-6", | ||
"-i", sshKey, | ||
"-l", username, | ||
host, | ||
"sudo systemctl restart kastro" | ||
]); | ||
|
||
const output = await new Response(ssh.stdout).text(); | ||
console.log("Service restart successful:", output); | ||
} | ||
|
||
export { setup, restart }; |
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,25 @@ | ||
import { expect, test } from "bun:test"; | ||
import { chunk } from "../arrays"; | ||
|
||
/** @const {!Array<!Array>} */ | ||
const TestVectors = [ | ||
// [input array, chunk size, expected result] | ||
[[], 2, []], | ||
[[1], 2, [[1]]], | ||
[[1, 2, 3, 4], 2, [[1, 2], [3, 4]]], | ||
[[1, 2, 3, 4, 5], 2, [[1, 2], [3, 4], [5]]], | ||
[[1, 2, 3], 1, [[1], [2], [3]]], | ||
[['a', 'b', 'c', 'd'], 3, [['a', 'b', 'c'], ['d']]], | ||
[[1, 2, 3, 4, 5, 6], 3, [[1, 2, 3], [4, 5, 6]]], | ||
]; | ||
|
||
test("chunk arrays on sample points", () => { | ||
for (const [input, size, expected] of TestVectors) { | ||
expect(chunk(input, size)).toEqual(expected); | ||
} | ||
}); | ||
|
||
test("throws on invalid chunk size", () => { | ||
expect(() => chunk([1, 2, 3], 0)).toThrow(); | ||
expect(() => chunk([1, 2, 3], -1)).toThrow(); | ||
}); |