MigrationSource.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 moment from 'moment'
  16. import { OptionsSchemaPlugin } from '../plugins/endpoint'
  17. import { sortTasks } from './ReplicaSource'
  18. import Api from '../utils/ApiCaller'
  19. import type { MainItem } from '../types/MainItem'
  20. import type { InstanceScript } from '../types/Instance'
  21. import type { Field } from '../types/Field'
  22. import type { NetworkMap } from '../types/Network'
  23. import type { Endpoint, StorageMap } from '../types/Endpoint'
  24. import { servicesUrl } from '../constants'
  25. class MigrationSourceUtils {
  26. static sortTaskUpdates(updates) {
  27. if (!updates) {
  28. return
  29. }
  30. updates.sort((a, b) => {
  31. let sortNull = !a && b ? 1 : a && !b ? -1 : !a && !b ? 0 : false
  32. if (sortNull !== false) {
  33. return sortNull
  34. }
  35. return moment(a.created_at).toDate().getTime() - moment(b.created_at).toDate().getTime()
  36. })
  37. }
  38. static sortMigrations(migrations) {
  39. migrations.sort((a, b) => moment(b.created_at).diff(moment(a.created_at)))
  40. migrations.forEach(migration => {
  41. sortTasks(migration.tasks, MigrationSourceUtils.sortTaskUpdates)
  42. })
  43. }
  44. }
  45. class MigrationSource {
  46. async getMigrations(skipLog?: boolean): Promise<MainItem[]> {
  47. let response = await Api.send({
  48. url: `${servicesUrl.coriolis}/${Api.projectId}/migrations/detail`,
  49. skipLog,
  50. })
  51. let migrations = response.data.migrations
  52. MigrationSourceUtils.sortMigrations(migrations)
  53. return migrations
  54. }
  55. async getMigration(migrationId: string, skipLog?: boolean): Promise<MainItem> {
  56. let response = await Api.send({
  57. url: `${servicesUrl.coriolis}/${Api.projectId}/migrations/${migrationId}`,
  58. skipLog,
  59. })
  60. let migration = response.data.migration
  61. sortTasks(migration.tasks, MigrationSourceUtils.sortTaskUpdates)
  62. return migration
  63. }
  64. async recreate(opts: {
  65. sourceEndpoint: Endpoint,
  66. destEndpoint: Endpoint,
  67. instanceNames: string[],
  68. destEnv: ?{ [string]: any },
  69. updatedDestEnv: ?{ [string]: any },
  70. sourceEnv?: ?{ [string]: any },
  71. updatedSourceEnv?: ?{ [string]: any },
  72. storageMappings: ?{ [string]: any },
  73. updatedStorageMappings: ?StorageMap[],
  74. defaultStorage: ?string,
  75. updatedDefaultStorage: ?string,
  76. networkMappings: ?{ [string]: any },
  77. updatedNetworkMappings: ?NetworkMap[],
  78. }): Promise<MainItem> {
  79. const getValue = (fieldName: string): ?string => {
  80. return (opts.updatedDestEnv && opts.updatedDestEnv[fieldName]) ||
  81. (opts.destEnv && opts.destEnv[fieldName])
  82. }
  83. const sourceParser = OptionsSchemaPlugin[opts.sourceEndpoint.type] || OptionsSchemaPlugin.default
  84. const destParser = OptionsSchemaPlugin[opts.destEndpoint.type] || OptionsSchemaPlugin.default
  85. let payload: any = {}
  86. payload.migration = {
  87. origin_endpoint_id: opts.sourceEndpoint.id,
  88. destination_endpoint_id: opts.destEndpoint.id,
  89. destination_environment: {
  90. ...opts.destEnv,
  91. ...destParser.getDestinationEnv(opts.updatedDestEnv),
  92. },
  93. shutdown_instances: Boolean(opts.updatedDestEnv && opts.updatedDestEnv.shutdown_instances),
  94. replication_count: (opts.updatedDestEnv && opts.updatedDestEnv.replication_count) || 2,
  95. instances: opts.instanceNames,
  96. notes: getValue('description') || '',
  97. }
  98. if (getValue('skip_os_morphing') != null) {
  99. payload.migration.skip_os_morphing = getValue('skip_os_morphing')
  100. }
  101. if (opts.networkMappings || (opts.updatedNetworkMappings && opts.updatedNetworkMappings.length)) {
  102. payload.migration.network_map = {
  103. ...opts.networkMappings,
  104. ...destParser.getNetworkMap(opts.updatedNetworkMappings),
  105. }
  106. }
  107. if ((opts.storageMappings && Object.keys(opts.storageMappings).length)
  108. || (opts.updatedStorageMappings && opts.updatedStorageMappings.length)) {
  109. payload.migration.storage_mappings = {
  110. ...opts.storageMappings,
  111. ...destParser.getStorageMap(opts.updatedDefaultStorage || opts.defaultStorage, opts.updatedStorageMappings),
  112. }
  113. }
  114. if (opts.sourceEnv || opts.updatedSourceEnv) {
  115. payload.migration.source_environment = {
  116. ...opts.sourceEnv,
  117. ...sourceParser.getDestinationEnv(opts.updatedSourceEnv),
  118. }
  119. }
  120. let response = await Api.send({
  121. url: `${servicesUrl.coriolis}/${Api.projectId}/migrations`,
  122. method: 'POST',
  123. data: payload,
  124. })
  125. return response.data.migration
  126. }
  127. async cancel(migrationId: string): Promise<string> {
  128. await Api.send({
  129. url: `${servicesUrl.coriolis}/${Api.projectId}/migrations/${migrationId}/actions`,
  130. method: 'POST',
  131. data: { cancel: null },
  132. })
  133. return migrationId
  134. }
  135. async delete(migrationId: string): Promise<string> {
  136. await Api.send({
  137. url: `${servicesUrl.coriolis}/${Api.projectId}/migrations/${migrationId}`,
  138. method: 'DELETE',
  139. })
  140. return migrationId
  141. }
  142. async migrateReplica(replicaId: string, options: Field[], userScripts: InstanceScript[]): Promise<MainItem> {
  143. let payload: any = {
  144. migration: {
  145. replica_id: replicaId,
  146. },
  147. }
  148. options.forEach(o => {
  149. payload.migration[o.name] = o.value || o.default || false
  150. })
  151. if (userScripts.length) {
  152. payload.migration.user_scripts = OptionsSchemaPlugin.default.getUserScripts(userScripts)
  153. }
  154. let response = await Api.send({
  155. url: `${servicesUrl.coriolis}/${Api.projectId}/migrations`,
  156. method: 'POST',
  157. data: payload,
  158. })
  159. return response.data.migration
  160. }
  161. }
  162. export default new MigrationSource()