hostAdminClient.ts 1.8 KB

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