hostAdminClient.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. const err = new Error(error.message)
  56. Object.assign(err, error)
  57. throw err
  58. }
  59. return { stdout, stderr }
  60. }
  61. export async function runScript({
  62. cwd,
  63. script,
  64. args = [],
  65. }: {
  66. cwd: string
  67. script: string
  68. args?: string[]
  69. }) {
  70. return await fetchJSON('http://host-admin/run/script', {
  71. method: 'POST',
  72. body: JSON.stringify({
  73. cwd,
  74. script,
  75. args,
  76. }),
  77. })
  78. }