MinionPoolSource.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 "@src/utils/ApiCaller";
  15. import DefaultMinionPoolSchemaPlugin from "@src/plugins/default/MinionPoolSchemaPlugin";
  16. import configLoader from "@src/utils/Config";
  17. import { MinionPool, MinionPoolDetails } from "@src/@types/MinionPool";
  18. import { ProviderTypes } from "@src/@types/Providers";
  19. import { Field } from "@src/@types/Field";
  20. import { providerTypes } from "@src/constants";
  21. import { Endpoint, OptionValues } from "@src/@types/Endpoint";
  22. import { MinionPoolAction } from "@src/stores/MinionPoolStore";
  23. import { Execution } from "@src/@types/Execution";
  24. import { SchemaParser } from "./Schemas";
  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. {
  56. label: "Windows",
  57. value: "windows",
  58. },
  59. ],
  60. },
  61. {
  62. name: "minimum_minions",
  63. type: "integer",
  64. minimum: 1,
  65. default: 1,
  66. },
  67. {
  68. name: "maximum_minions",
  69. type: "integer",
  70. minimum: 1,
  71. default: 1,
  72. },
  73. {
  74. name: "minion_max_idle_time",
  75. type: "integer",
  76. minimum: 0,
  77. default: 3600,
  78. },
  79. {
  80. name: "minion_retention_strategy",
  81. type: "string",
  82. default: "delete",
  83. enum: [
  84. {
  85. value: "delete",
  86. label: "Delete",
  87. },
  88. {
  89. value: "poweroff",
  90. label: "Power Off",
  91. },
  92. ],
  93. },
  94. {
  95. name: "skip_allocation",
  96. type: "boolean",
  97. nullableBoolean: false,
  98. },
  99. {
  100. name: "notes",
  101. type: "string",
  102. },
  103. ];
  104. }
  105. async loadMinionPools(options?: {
  106. skipLog?: boolean;
  107. }): Promise<MinionPool[]> {
  108. const response = await Api.send({
  109. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/minion_pools`,
  110. skipLog: options?.skipLog,
  111. });
  112. const minionPools: MinionPool[] = response.data.minion_pools;
  113. minionPools.sort(
  114. (a, b) =>
  115. new Date(b.updated_at || b.created_at || "").getTime() -
  116. new Date(a.updated_at || a.created_at || "").getTime()
  117. );
  118. return minionPools;
  119. }
  120. async loadMinionPoolDetails(
  121. id: string,
  122. options?: { skipLog?: boolean }
  123. ): Promise<MinionPoolDetails> {
  124. const response = await Api.send({
  125. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/minion_pools/${id}`,
  126. skipLog: options?.skipLog,
  127. });
  128. return response.data.minion_pool;
  129. }
  130. async loadOptions(config: {
  131. optionsType: "source" | "destination";
  132. endpoint: Endpoint;
  133. envData: { [prop: string]: any } | null | undefined;
  134. useCache?: boolean | null;
  135. }): Promise<OptionValues[]> {
  136. const { optionsType, endpoint, envData, useCache } = config;
  137. const envString = SchemaParser.getMinionPoolToOptionsQuery(
  138. envData,
  139. endpoint.type
  140. );
  141. const callName =
  142. optionsType === "source"
  143. ? "source-minion-pool-options"
  144. : "destination-minion-pool-options";
  145. const fieldName =
  146. optionsType === "source"
  147. ? "source_minion_pool_options"
  148. : "destination_minion_pool_options";
  149. const response = await Api.send({
  150. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/endpoints/${endpoint.id}/${callName}${envString}`,
  151. cache: useCache,
  152. cancelId: endpoint.id,
  153. });
  154. return response.data[fieldName];
  155. }
  156. async loadMinionPoolSchema(
  157. providerName: ProviderTypes,
  158. platform: "source" | "destination"
  159. ): Promise<Field[]> {
  160. const providerType =
  161. platform === "source"
  162. ? providerTypes.SOURCE_MINION_POOL
  163. : providerTypes.DESTINATION_MINION_POOL;
  164. try {
  165. const response = await Api.send({
  166. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/providers/${providerName}/schemas/${providerType}`,
  167. });
  168. const schema =
  169. response.data?.schemas?.[`${platform}_minion_pool_environment_schema`];
  170. let fields = [];
  171. if (schema) {
  172. fields = SchemaParser.minionPoolOptionsSchemaToFields(
  173. providerName,
  174. schema,
  175. `${providerName}-minion-pool`
  176. );
  177. }
  178. return fields;
  179. } catch (err) {
  180. console.error(err);
  181. return [];
  182. }
  183. }
  184. async add(config: {
  185. endpointId: string;
  186. data: any;
  187. defaultSchema: Field[];
  188. envSchema: Field[];
  189. provider: ProviderTypes;
  190. }) {
  191. const { endpointId, data, defaultSchema, envSchema, provider } = config;
  192. const payload = {
  193. minion_pool: {
  194. ...new DefaultMinionPoolSchemaPlugin().getMinionPoolEnv(
  195. defaultSchema,
  196. data
  197. ),
  198. endpoint_id: endpointId,
  199. environment_options: SchemaParser.getMinionPoolEnv(
  200. provider,
  201. envSchema,
  202. data
  203. ),
  204. },
  205. };
  206. const response = await Api.send({
  207. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/minion_pools`,
  208. method: "POST",
  209. data: payload,
  210. });
  211. return response.data.minion_pool;
  212. }
  213. async update(config: {
  214. data: any;
  215. defaultSchema: Field[];
  216. envSchema: Field[];
  217. provider: ProviderTypes;
  218. }) {
  219. const { data, defaultSchema, envSchema, provider } = config;
  220. const payload = {
  221. minion_pool: {
  222. ...new DefaultMinionPoolSchemaPlugin().getMinionPoolEnv(
  223. defaultSchema,
  224. data
  225. ),
  226. environment_options: SchemaParser.getMinionPoolEnv(
  227. provider,
  228. envSchema,
  229. data
  230. ),
  231. },
  232. };
  233. const response = await Api.send({
  234. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/minion_pools/${data.id}`,
  235. method: "PUT",
  236. data: payload,
  237. });
  238. return response.data.minion_pool;
  239. }
  240. async runAction(
  241. minionPoolId: string,
  242. minionPoolAction: MinionPoolAction,
  243. actionOptions?: any
  244. ): Promise<Execution> {
  245. const payload: any = {};
  246. if (actionOptions) {
  247. payload[minionPoolAction] = { ...actionOptions };
  248. } else {
  249. payload[minionPoolAction] = null;
  250. }
  251. const response = await Api.send({
  252. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/minion_pools/${minionPoolId}/actions`,
  253. method: "POST",
  254. data: payload,
  255. });
  256. return response.data.execution;
  257. }
  258. async deleteMinionPool(minionPoolId: string) {
  259. const response = await Api.send({
  260. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/minion_pools/${minionPoolId}`,
  261. method: "DELETE",
  262. });
  263. return response.data.execution;
  264. }
  265. }
  266. export default new MinionPoolSource();