MigrationStore.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. Copyright (C) 2017 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 { observable, action, runInAction } from 'mobx'
  15. import type {
  16. UpdateData, MigrationItem, MigrationItemDetails, MigrationItemOptions, UserScriptData,
  17. } from '../@types/MainItem'
  18. import type { Field } from '../@types/Field'
  19. import type { Endpoint } from '../@types/Endpoint'
  20. import type { InstanceScript } from '../@types/Instance'
  21. import MigrationSource from '../sources/MigrationSource'
  22. import apiCaller from '../utils/ApiCaller'
  23. class MigrationStore {
  24. @observable migrations: MigrationItem[] = []
  25. @observable migrationDetails: MigrationItemDetails | null = null
  26. @observable loading: boolean = true
  27. @observable detailsLoading: boolean = true
  28. migrationsLoaded: boolean = false
  29. @action async getMigrations(options?: { showLoading?: boolean, skipLog?: boolean }) {
  30. if ((options && options.showLoading) || !this.migrationsLoaded) {
  31. this.loading = true
  32. }
  33. try {
  34. const migrations = await MigrationSource.getMigrations(options && options.skipLog)
  35. runInAction(() => {
  36. this.migrations = migrations
  37. this.loading = false
  38. this.migrationsLoaded = true
  39. })
  40. } catch (ex) {
  41. runInAction(() => { this.loading = false })
  42. throw ex
  43. }
  44. }
  45. getDefaultSkipOsMorphing(migration: MigrationItemDetails | null) {
  46. const tasks = migration && migration.tasks
  47. if (tasks && !tasks.find(t => t.task_type === 'OS_MORPHING')) {
  48. return true
  49. }
  50. return null
  51. }
  52. @action async recreateFullCopy(migration: MigrationItemOptions) {
  53. return MigrationSource.recreateFullCopy(migration)
  54. }
  55. @action async recreate(
  56. migration: MigrationItemDetails,
  57. sourceEndpoint: Endpoint,
  58. destEndpoint: Endpoint,
  59. updateData: UpdateData,
  60. defaultStorage: { value: string | null, busType?: string | null },
  61. updatedDefaultStorage: { value: string | null, busType?: string | null } | undefined,
  62. replicationCount: number | null | undefined,
  63. ): Promise<MigrationItemDetails> {
  64. const migrationResult = await MigrationSource.recreate({
  65. sourceEndpoint,
  66. destEndpoint,
  67. migration,
  68. instanceNames: migration.instances,
  69. sourceEnv: migration.source_environment,
  70. updatedSourceEnv: updateData.source,
  71. destEnv: migration.destination_environment,
  72. updatedDestEnv: updateData.destination,
  73. storageMappings: migration.storage_mappings,
  74. updatedStorageMappings: updateData.storage,
  75. defaultStorage,
  76. updatedDefaultStorage,
  77. networkMappings: migration.network_map,
  78. updatedNetworkMappings: updateData.network,
  79. defaultSkipOsMorphing: this.getDefaultSkipOsMorphing(migration),
  80. replicationCount,
  81. uploadedScripts: updateData.uploadedScripts,
  82. removedScripts: updateData.removedScripts,
  83. })
  84. return migrationResult
  85. }
  86. @action async getMigration(
  87. migrationId: string, options?: { showLoading?: boolean, skipLog?: boolean },
  88. ) {
  89. if (options && options.showLoading) {
  90. this.detailsLoading = true
  91. }
  92. try {
  93. const migration = await MigrationSource.getMigration(migrationId, options && options.skipLog)
  94. runInAction(() => {
  95. this.migrationDetails = migration
  96. this.migrations = this.migrations.map(m => (m.id === migration.id ? migration : m))
  97. })
  98. } finally {
  99. runInAction(() => { this.detailsLoading = false })
  100. }
  101. }
  102. @action async cancel(migrationId: string, force?: boolean | null) {
  103. await MigrationSource.cancel(migrationId, force)
  104. }
  105. @action async delete(migrationId: string) {
  106. await MigrationSource.delete(migrationId)
  107. runInAction(() => { this.migrations = this.migrations.filter(r => r.id !== migrationId) })
  108. }
  109. @action async migrateReplica(
  110. replicaId: string,
  111. options: Field[],
  112. uploadedUserScripts: InstanceScript[],
  113. removedUserScripts: InstanceScript[],
  114. userScriptData: UserScriptData | null | undefined,
  115. minionPoolMappings: { [instance: string]: string },
  116. ) {
  117. const migration = await MigrationSource.migrateReplica(
  118. replicaId,
  119. options,
  120. uploadedUserScripts,
  121. removedUserScripts,
  122. userScriptData,
  123. minionPoolMappings,
  124. )
  125. runInAction(() => {
  126. this.migrations = [
  127. migration,
  128. ...this.migrations,
  129. ]
  130. })
  131. return migration
  132. }
  133. @action cancelMigrationDetails() {
  134. if (this.migrationDetails) {
  135. apiCaller.cancelRequests(this.migrationDetails.id)
  136. }
  137. this.detailsLoading = false
  138. }
  139. @action clearDetails() {
  140. this.detailsLoading = true
  141. this.migrationDetails = null
  142. }
  143. }
  144. export default new MigrationStore()