hostAdminClient.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. const hostAdminUrl = Cypress.env('ADMIN_CLIENT_URL') || 'http://host-admin'
  2. export async function dockerCompose(cmd: string, ...args: string[]) {
  3. return await fetchJSON(`${hostAdminUrl}/docker/compose/${cmd}`, {
  4. method: 'POST',
  5. body: JSON.stringify({
  6. args,
  7. }),
  8. })
  9. }
  10. export async function reconfigure({
  11. pro = false,
  12. version = 'latest',
  13. vars = {},
  14. withDataDir = false,
  15. resetData = false,
  16. }): Promise<{ previousConfigServer: string }> {
  17. return await fetchJSON(`${hostAdminUrl}/reconfigure`, {
  18. method: 'POST',
  19. body: JSON.stringify({
  20. pro,
  21. version,
  22. vars,
  23. withDataDir,
  24. resetData,
  25. }),
  26. })
  27. }
  28. async function fetchJSON<T = { stdout: string; stderr: string }>(
  29. input: RequestInfo,
  30. init?: RequestInit
  31. ): Promise<T> {
  32. if (init?.body) {
  33. init.headers = { 'Content-Type': 'application/json' }
  34. }
  35. const res = await fetch(input, init)
  36. const { error, stdout, stderr, ...rest } = await res.json()
  37. if (error) {
  38. console.error(input, init, 'failed:', error)
  39. if (stdout) console.log(stdout)
  40. if (stderr) console.warn(stderr)
  41. const err = new Error(error.message)
  42. Object.assign(err, error)
  43. throw err
  44. }
  45. return { stdout, stderr, ...rest }
  46. }
  47. export async function runScript({
  48. cwd,
  49. script,
  50. args = [],
  51. }: {
  52. cwd: string
  53. script: string
  54. args?: string[]
  55. }) {
  56. return await fetchJSON(`${hostAdminUrl}/run/script`, {
  57. method: 'POST',
  58. body: JSON.stringify({
  59. cwd,
  60. script,
  61. args,
  62. }),
  63. })
  64. }
  65. export async function getRedisKeys() {
  66. const { stdout } = await fetchJSON(`${hostAdminUrl}/redis/keys`, {
  67. method: 'GET',
  68. })
  69. return stdout.split('\n')
  70. }