hostAdminClient.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. const hostAdminURL = Cypress.env('HOST_ADMIN_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. mongoVersion = '',
  17. }): Promise<{ previousConfigServer: string }> {
  18. return await fetchJSON(`${hostAdminURL}/reconfigure`, {
  19. method: 'POST',
  20. body: JSON.stringify({
  21. pro,
  22. version,
  23. vars,
  24. withDataDir,
  25. resetData,
  26. mongoVersion,
  27. }),
  28. })
  29. }
  30. async function fetchJSON<T = { stdout: string; stderr: string }>(
  31. input: RequestInfo,
  32. init?: RequestInit
  33. ): Promise<T> {
  34. if (init?.body) {
  35. init.headers = { 'Content-Type': 'application/json' }
  36. }
  37. let res
  38. for (let attempt = 0; attempt < 5; attempt++) {
  39. try {
  40. res = await fetch(input, init)
  41. break
  42. } catch {
  43. await sleep(3_000)
  44. }
  45. }
  46. if (!res) {
  47. res = await fetch(input, init)
  48. }
  49. const { error, stdout, stderr, ...rest } = await res.json()
  50. if (error) {
  51. console.error(input, init, 'failed:', error)
  52. if (stdout) console.log(stdout)
  53. if (stderr) console.warn(stderr)
  54. const err = new Error(error.message)
  55. Object.assign(err, error)
  56. throw err
  57. }
  58. return { stdout, stderr, ...rest }
  59. }
  60. export async function runScript({
  61. cwd,
  62. script,
  63. args = [],
  64. hasOverleafEnv = true,
  65. }: {
  66. cwd: string
  67. script: string
  68. args?: string[]
  69. hasOverleafEnv?: boolean
  70. }) {
  71. return await fetchJSON(`${hostAdminURL}/run/script`, {
  72. method: 'POST',
  73. body: JSON.stringify({
  74. cwd,
  75. script,
  76. args,
  77. hasOverleafEnv,
  78. }),
  79. })
  80. }
  81. export async function runGruntTask({
  82. task,
  83. args = [],
  84. }: {
  85. task: string
  86. args?: string[]
  87. }) {
  88. return await fetchJSON(`${hostAdminURL}/run/gruntTask`, {
  89. method: 'POST',
  90. body: JSON.stringify({
  91. task,
  92. args,
  93. }),
  94. })
  95. }
  96. export async function getRedisKeys() {
  97. const { stdout } = await fetchJSON(`${hostAdminURL}/redis/keys`, {
  98. method: 'GET',
  99. })
  100. return stdout.split('\n')
  101. }
  102. export async function setMongoFeatureCompatibilityVersion(
  103. mongoVersion: string
  104. ) {
  105. cy.log(`advancing mongo featureCompatibilityVersion to ${mongoVersion}`)
  106. await fetchJSON(`${hostAdminURL}/mongo/setFeatureCompatibilityVersion`, {
  107. method: 'POST',
  108. body: JSON.stringify({
  109. mongoVersion,
  110. }),
  111. })
  112. }
  113. export async function purgeFilestoreData() {
  114. await fetchJSON(`${hostAdminURL}/data/user_files`, {
  115. method: 'DELETE',
  116. })
  117. }
  118. async function sleep(ms: number) {
  119. return new Promise(resolve => {
  120. setTimeout(resolve, ms)
  121. })
  122. }