MinionPoolSource.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. Copyright (C) 2020 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import Api from '../utils/ApiCaller'
  15. import DefaultMinionPoolSchemaPlugin from '../plugins/endpoint/default/MinionPoolSchemaPlugin'
  16. import configLoader from '../utils/Config'
  17. import { MinionPool, MinionPoolDetails } from '../@types/MinionPool'
  18. import { ProviderTypes } from '../@types/Providers'
  19. import { Field } from '../@types/Field'
  20. import { providerTypes } from '../constants'
  21. import { SchemaParser } from './Schemas'
  22. import { Endpoint, OptionValues } from '../@types/Endpoint'
  23. import { MinionPoolAction } from '../stores/MinionPoolStore'
  24. import { Execution } from '../@types/Execution'
  25. class MinionPoolSource {
  26. getMinionPoolDefaultSchema(): Field[] {
  27. return [
  28. {
  29. name: 'endpoint_id',
  30. label: 'Endpoint',
  31. type: 'string',
  32. },
  33. {
  34. name: 'platform',
  35. type: 'string',
  36. title: 'Pool Platform',
  37. },
  38. {
  39. name: 'name',
  40. type: 'string',
  41. required: true,
  42. title: 'Pool Name',
  43. },
  44. {
  45. name: 'os_type',
  46. type: 'string',
  47. required: true,
  48. title: 'Pool OS Type',
  49. default: 'linux',
  50. enum: [
  51. {
  52. label: 'Linux',
  53. value: 'linux',
  54. }, {
  55. label: 'Windows',
  56. value: 'windows',
  57. },
  58. ],
  59. },
  60. {
  61. name: 'minimum_minions',
  62. type: 'integer',
  63. minimum: 1,
  64. default: 1,
  65. },
  66. {
  67. name: 'maximum_minions',
  68. type: 'integer',
  69. minimum: 1,
  70. default: 1,
  71. },
  72. {
  73. name: 'minion_max_idle_time',
  74. type: 'integer',
  75. minimum: 0,
  76. default: 3600,
  77. },
  78. {
  79. name: 'minion_retention_strategy',
  80. type: 'string',
  81. default: 'delete',
  82. enum: [
  83. {
  84. value: 'delete',
  85. label: 'Delete',
  86. },
  87. {
  88. value: 'poweroff',
  89. label: 'Power Off',
  90. },
  91. ],
  92. },
  93. {
  94. name: 'skip_allocation',
  95. type: 'boolean',
  96. nullableBoolean: false,
  97. },
  98. {
  99. name: 'notes',
  100. type: 'string',
  101. },
  102. ]
  103. }
  104. async loadMinionPools(options?: { skipLog?: boolean }): Promise<MinionPool[]> {
  105. const response = await Api.send({
  106. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/minion_pools`,
  107. skipLog: options?.skipLog,
  108. })
  109. const minionPools: MinionPool[] = response.data.minion_pools
  110. minionPools.sort((a, b) => new Date(b.updated_at || b.created_at || '').getTime()
  111. - new Date(a.updated_at || a.created_at || '').getTime())
  112. return minionPools
  113. }
  114. async loadMinionPoolDetails(
  115. id: string,
  116. options?: { skipLog?: boolean },
  117. ): Promise<MinionPoolDetails> {
  118. const response = await Api.send({
  119. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/minion_pools/${id}`,
  120. skipLog: options?.skipLog,
  121. })
  122. return response.data.minion_pool
  123. }
  124. async loadOptions(config: {
  125. optionsType: 'source' | 'destination',
  126. endpoint: Endpoint,
  127. envData: { [prop: string]: any } | null | undefined,
  128. useCache?: boolean | null,
  129. }): Promise<OptionValues[]> {
  130. const {
  131. optionsType, endpoint, envData, useCache,
  132. } = config
  133. const envString = SchemaParser.getMinionPoolToOptionsQuery(envData, endpoint.type)
  134. const callName = optionsType === 'source' ? 'source-minion-pool-options' : 'destination-minion-pool-options'
  135. const fieldName = optionsType === 'source' ? 'source_minion_pool_options' : 'destination_minion_pool_options'
  136. const response = await Api.send({
  137. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/endpoints/${endpoint.id}/${callName}${envString}`,
  138. cache: useCache,
  139. cancelId: endpoint.id,
  140. })
  141. return response.data[fieldName]
  142. }
  143. async loadMinionPoolSchema(providerName: ProviderTypes, platform: 'source' | 'destination'): Promise<Field[]> {
  144. const providerType = platform === 'source' ? providerTypes.SOURCE_MINION_POOL : providerTypes.DESTINATION_MINION_POOL
  145. try {
  146. const response = await Api.send({
  147. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/providers/${providerName}/schemas/${providerType}`,
  148. })
  149. const schema = response.data?.schemas?.[`${platform}_minion_pool_environment_schema`]
  150. let fields = []
  151. if (schema) {
  152. fields = SchemaParser.minionPoolOptionsSchemaToFields(providerName, schema, `${providerName}-minion-pool`)
  153. }
  154. return fields
  155. } catch (err) {
  156. console.error(err)
  157. return []
  158. }
  159. }
  160. async add(config: {
  161. endpointId: string,
  162. data: any,
  163. defaultSchema: Field[],
  164. envSchema: Field[],
  165. provider: ProviderTypes
  166. }) {
  167. const {
  168. endpointId, data, defaultSchema, envSchema, provider,
  169. } = config
  170. const payload = {
  171. minion_pool: {
  172. ...DefaultMinionPoolSchemaPlugin.getMinionPoolEnv(defaultSchema, data),
  173. endpoint_id: endpointId,
  174. environment_options: SchemaParser.getMinionPoolEnv(provider, envSchema, data),
  175. },
  176. }
  177. const response = await Api.send({
  178. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/minion_pools`,
  179. method: 'POST',
  180. data: payload,
  181. })
  182. return response.data.minion_pool
  183. }
  184. async update(config: {
  185. data: any,
  186. defaultSchema: Field[],
  187. envSchema: Field[],
  188. provider: ProviderTypes
  189. }) {
  190. const {
  191. data, defaultSchema, envSchema, provider,
  192. } = config
  193. const payload = {
  194. minion_pool: {
  195. ...DefaultMinionPoolSchemaPlugin.getMinionPoolEnv(defaultSchema, data),
  196. environment_options: SchemaParser.getMinionPoolEnv(provider, envSchema, data),
  197. },
  198. }
  199. const response = await Api.send({
  200. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/minion_pools/${data.id}`,
  201. method: 'PUT',
  202. data: payload,
  203. })
  204. return response.data.minion_pool
  205. }
  206. async runAction(
  207. minionPoolId: string,
  208. minionPoolAction: MinionPoolAction,
  209. actionOptions?: any,
  210. ): Promise<Execution> {
  211. const payload: any = {}
  212. if (actionOptions) {
  213. payload[minionPoolAction] = { ...actionOptions }
  214. } else {
  215. payload[minionPoolAction] = null
  216. }
  217. const response = await Api.send({
  218. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/minion_pools/${minionPoolId}/actions`,
  219. method: 'POST',
  220. data: payload,
  221. })
  222. return response.data.execution
  223. }
  224. async deleteMinionPool(minionPoolId: string) {
  225. const response = await Api.send({
  226. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/minion_pools/${minionPoolId}`,
  227. method: 'DELETE',
  228. })
  229. return response.data.execution
  230. }
  231. }
  232. export default new MinionPoolSource()