2024-06-17 07:05:06 -04:00
|
|
|
const hostAdminUrl = Cypress.env('ADMIN_CLIENT_URL') || 'http://host-admin'
|
|
|
|
|
2024-05-30 03:12:56 -04:00
|
|
|
export async function dockerCompose(cmd: string, ...args: string[]) {
|
2024-06-17 07:05:06 -04:00
|
|
|
return await fetchJSON(`${hostAdminUrl}/docker/compose/${cmd}`, {
|
2024-05-30 03:12:56 -04:00
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({
|
|
|
|
args,
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function reconfigure({
|
|
|
|
pro = false,
|
|
|
|
version = 'latest',
|
|
|
|
vars = {},
|
2024-06-11 07:01:21 -04:00
|
|
|
withDataDir = false,
|
2024-07-10 10:00:41 -04:00
|
|
|
resetData = false,
|
2024-08-06 05:10:03 -04:00
|
|
|
}): Promise<{ previousConfigServer: string }> {
|
2024-06-17 07:05:06 -04:00
|
|
|
return await fetchJSON(`${hostAdminUrl}/reconfigure`, {
|
2024-05-30 03:12:56 -04:00
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({
|
|
|
|
pro,
|
|
|
|
version,
|
|
|
|
vars,
|
2024-06-11 07:01:21 -04:00
|
|
|
withDataDir,
|
2024-07-10 10:00:41 -04:00
|
|
|
resetData,
|
2024-05-30 03:12:56 -04:00
|
|
|
}),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-08-06 05:10:03 -04:00
|
|
|
async function fetchJSON<T = { stdout: string; stderr: string }>(
|
2024-05-30 03:12:56 -04:00
|
|
|
input: RequestInfo,
|
|
|
|
init?: RequestInit
|
2024-08-06 05:10:03 -04:00
|
|
|
): Promise<T> {
|
2024-05-30 03:12:56 -04:00
|
|
|
if (init?.body) {
|
|
|
|
init.headers = { 'Content-Type': 'application/json' }
|
|
|
|
}
|
2024-08-06 05:10:24 -04:00
|
|
|
let res
|
|
|
|
for (let attempt = 0; attempt < 5; attempt++) {
|
|
|
|
try {
|
|
|
|
res = await fetch(input, init)
|
|
|
|
break
|
|
|
|
} catch {
|
|
|
|
await sleep(3_000)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!res) {
|
|
|
|
res = await fetch(input, init)
|
|
|
|
}
|
2024-08-06 05:10:03 -04:00
|
|
|
const { error, stdout, stderr, ...rest } = await res.json()
|
2024-05-30 03:12:56 -04:00
|
|
|
if (error) {
|
|
|
|
console.error(input, init, 'failed:', error)
|
2024-05-30 03:13:24 -04:00
|
|
|
if (stdout) console.log(stdout)
|
|
|
|
if (stderr) console.warn(stderr)
|
2024-05-30 03:12:56 -04:00
|
|
|
const err = new Error(error.message)
|
|
|
|
Object.assign(err, error)
|
|
|
|
throw err
|
|
|
|
}
|
2024-08-06 05:10:03 -04:00
|
|
|
return { stdout, stderr, ...rest }
|
2024-05-30 03:12:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function runScript({
|
|
|
|
cwd,
|
|
|
|
script,
|
|
|
|
args = [],
|
|
|
|
}: {
|
|
|
|
cwd: string
|
|
|
|
script: string
|
|
|
|
args?: string[]
|
|
|
|
}) {
|
2024-06-17 07:05:06 -04:00
|
|
|
return await fetchJSON(`${hostAdminUrl}/run/script`, {
|
2024-05-30 03:12:56 -04:00
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({
|
|
|
|
cwd,
|
|
|
|
script,
|
|
|
|
args,
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
}
|
2024-06-25 07:03:05 -04:00
|
|
|
|
|
|
|
export async function getRedisKeys() {
|
|
|
|
const { stdout } = await fetchJSON(`${hostAdminUrl}/redis/keys`, {
|
|
|
|
method: 'GET',
|
|
|
|
})
|
|
|
|
return stdout.split('\n')
|
|
|
|
}
|
2024-08-06 05:10:24 -04:00
|
|
|
|
|
|
|
async function sleep(ms: number) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
setTimeout(resolve, ms)
|
|
|
|
})
|
|
|
|
}
|