MigrationStore.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. replicationCount: ?number
  64. ): Promise<MainItem> {
  65. let migrationResult = await MigrationSource.recreate({
  66. sourceEndpoint,
  67. destEndpoint,
  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. })
  82. return migrationResult
  83. }
  84. @action async getMigration(migrationId: string, options?: { showLoading?: boolean, skipLog?: boolean }) {
  85. if (options && options.showLoading) {
  86. this.detailsLoading = true
  87. }
  88. try {
  89. let migration = await MigrationSource.getMigration(migrationId, options && options.skipLog)
  90. runInAction(() => {
  91. this.migrationDetails = migration
  92. this.migrations = this.migrations.map(m => m.id === migration.id ? migration : m)
  93. })
  94. } finally {
  95. runInAction(() => { this.detailsLoading = false })
  96. }
  97. }
  98. @action async cancel(migrationId: string, force: ?boolean) {
  99. await MigrationSource.cancel(migrationId, force)
  100. }
  101. @action async delete(migrationId: string) {
  102. await MigrationSource.delete(migrationId)
  103. runInAction(() => { this.migrations = this.migrations.filter(r => r.id !== migrationId) })
  104. }
  105. @action async migrateReplica(replicaId: string, options: Field[], userScripts: InstanceScript[]) {
  106. let migration = await MigrationSource.migrateReplica(replicaId, options, userScripts)
  107. runInAction(() => {
  108. this.migrations = [
  109. migration,
  110. ...this.migrations,
  111. ]
  112. })
  113. return migration
  114. }
  115. @action clearDetails() {
  116. this.detailsLoading = true
  117. this.migrationDetails = null
  118. }
  119. }
  120. export default new MigrationStore()