MigrationStore.ts 5.3 KB

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