MigrationStore.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. // @flow
  15. import { observable, action, runInAction } from 'mobx'
  16. import type { MainItem, UpdateData } from '../types/MainItem'
  17. import type { Field } from '../types/Field'
  18. import type { Endpoint } from '../types/Endpoint'
  19. import type { InstanceScript } from '../types/Instance'
  20. import MigrationSource from '../sources/MigrationSource'
  21. class MigrationStore {
  22. @observable migrations: MainItem[] = []
  23. @observable migrationDetails: ?MainItem = null
  24. @observable loading: boolean = true
  25. @observable detailsLoading: boolean = true
  26. migrationsLoaded: boolean = false
  27. @action async getMigrations(options?: { showLoading?: boolean, skipLog?: boolean }) {
  28. if ((options && options.showLoading) || !this.migrationsLoaded) {
  29. this.loading = true
  30. }
  31. try {
  32. let migrations = await MigrationSource.getMigrations(options && options.skipLog)
  33. runInAction(() => {
  34. this.migrations = migrations.map(migration => {
  35. let oldMigration = this.migrations.find(r => r.id === migration.id)
  36. if (oldMigration) {
  37. migration.executions = oldMigration.executions
  38. }
  39. return migration
  40. })
  41. this.loading = false
  42. this.migrationsLoaded = true
  43. })
  44. } catch (ex) {
  45. runInAction(() => { this.loading = false })
  46. throw ex
  47. }
  48. }
  49. getDefaultSkipOsMorphing(migration: ?MainItem) {
  50. let tasks = migration && migration.tasks
  51. if (tasks && !tasks.find(t => t.task_type === 'OS_MORPHING')) {
  52. return true
  53. }
  54. return null
  55. }
  56. @action async recreate(
  57. migration: MainItem,
  58. sourceEndpoint: Endpoint,
  59. destEndpoint: Endpoint,
  60. updateData: UpdateData,
  61. defaultStorage: ?string,
  62. updatedDefaultStorage: ?string
  63. ): Promise<MainItem> {
  64. let migrationResult = await MigrationSource.recreate({
  65. sourceEndpoint,
  66. destEndpoint,
  67. instanceNames: migration.instances,
  68. sourceEnv: migration.source_environment,
  69. updatedSourceEnv: updateData.source,
  70. destEnv: migration.destination_environment,
  71. updatedDestEnv: updateData.destination,
  72. storageMappings: migration.storage_mappings,
  73. updatedStorageMappings: updateData.storage,
  74. defaultStorage,
  75. updatedDefaultStorage,
  76. networkMappings: migration.network_map,
  77. updatedNetworkMappings: updateData.network,
  78. defaultSkipOsMorphing: this.getDefaultSkipOsMorphing(migration),
  79. })
  80. return migrationResult
  81. }
  82. @action async getMigration(migrationId: string, options?: { showLoading?: boolean, skipLog?: boolean }) {
  83. if (options && options.showLoading) {
  84. this.detailsLoading = true
  85. }
  86. try {
  87. let migration = await MigrationSource.getMigration(migrationId, options && options.skipLog)
  88. runInAction(() => {
  89. this.migrationDetails = migration
  90. this.migrations = this.migrations.map(m => m.id === migration.id ? migration : m)
  91. })
  92. } finally {
  93. runInAction(() => { this.detailsLoading = false })
  94. }
  95. }
  96. @action async cancel(migrationId: string, force: ?boolean) {
  97. await MigrationSource.cancel(migrationId, force)
  98. }
  99. @action async delete(migrationId: string) {
  100. await MigrationSource.delete(migrationId)
  101. runInAction(() => { this.migrations = this.migrations.filter(r => r.id !== migrationId) })
  102. }
  103. @action async migrateReplica(replicaId: string, options: Field[], userScripts: InstanceScript[]) {
  104. let migration = await MigrationSource.migrateReplica(replicaId, options, userScripts)
  105. runInAction(() => {
  106. this.migrations = [
  107. migration,
  108. ...this.migrations,
  109. ]
  110. })
  111. return migration
  112. }
  113. @action clearDetails() {
  114. this.detailsLoading = true
  115. this.migrationDetails = null
  116. }
  117. }
  118. export default new MigrationStore()