MigrationActions.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 Reflux from 'reflux';
  15. import Api from '../../components/ApiCaller';
  16. import NotificationActions from '../NotificationActions'
  17. import {servicesUrl, securityGroups} from '../../config';
  18. let MigrationActions = Reflux.createActions({
  19. 'loadMigrations': { children: ['completed', 'failed'], shouldEmit: () => {} },
  20. 'loadMigration': { children: ['completed', 'failed'] }, // TODO: Reload migration action
  21. 'addMigration': { children: ['completed', 'failed'] },
  22. 'deleteMigration': { children: ['completed', 'failed'] },
  23. 'executeReplica': { children: ['completed', 'failed'] },
  24. 'cancelMigration': { children: ['completed', 'failed'] },
  25. 'getReplicaExecutions': { children: ['completed', 'failed'] },
  26. 'getReplicaExecutionDetail': { children: ['completed', 'failed'] },
  27. 'createMigrationFromReplica': { children: ['completed', 'failed'] },
  28. 'deleteReplicaExecution': { children: ['completed', 'failed'] },
  29. 'getMigration': {},
  30. 'setMigration': {},
  31. 'setMigrationProperty': {}
  32. })
  33. MigrationActions.loadMigrations.listen(() => {
  34. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  35. Api.sendAjaxRequest({
  36. url: `${servicesUrl.coriolis}/${projectId}/migrations/detail`,
  37. method: "GET"
  38. })
  39. .then(MigrationActions.loadMigrations.completed, MigrationActions.loadMigrations.failed)
  40. .catch(MigrationActions.loadMigrations.failed);
  41. Api.sendAjaxRequest({
  42. url: `${servicesUrl.coriolis}/${projectId}/replicas/detail`,
  43. method: "GET"
  44. })
  45. .then(MigrationActions.loadMigrations.completed, MigrationActions.loadMigrations.failed)
  46. .catch(MigrationActions.loadMigrations.failed);
  47. })
  48. MigrationActions.loadMigrations.shouldEmit = () => {
  49. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  50. if (typeof projectId === "undefined") {
  51. return false
  52. }
  53. if (Reflux.GlobalState.migrationStore.queryInProgress) {
  54. return false
  55. }
  56. return true
  57. }
  58. MigrationActions.loadMigration.listen((migration) => {
  59. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  60. Api.sendAjaxRequest({
  61. url: `${servicesUrl.coriolis}/${projectId}/migrations/${migration.id}`,
  62. method: "GET"
  63. })
  64. .then(MigrationActions.loadMigration.completed, MigrationActions.loadMigration.failed)
  65. .catch(MigrationActions.loadMigration.failed);
  66. })
  67. MigrationActions.loadMigration.shouldEmit = () => {
  68. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  69. return typeof projectId !== "undefined"
  70. }
  71. MigrationActions.deleteMigration.listen((migration) => {
  72. let migrationType = migration.type === 'replica' ? 'replicas' : 'migrations'
  73. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  74. Api.sendAjaxRequest({
  75. url: `${servicesUrl.coriolis}/${projectId}/${migrationType}/${migration.id}`,
  76. method: "DELETE"
  77. })
  78. .then(MigrationActions.deleteMigration.completed(migration), MigrationActions.deleteMigration.failed)
  79. .catch(MigrationActions.deleteMigration.failed);
  80. })
  81. MigrationActions.executeReplica.listen((replica, callback = null) => {
  82. if (replica.type == 'replica') {
  83. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  84. let payload = {
  85. execution: {
  86. shutdown_instances: false
  87. }
  88. }
  89. Api.sendAjaxRequest({
  90. url: `${servicesUrl.coriolis}/${projectId}/replicas/${replica.id}/executions`,
  91. method: "POST",
  92. data: payload
  93. })
  94. .then((response) => {
  95. MigrationActions.executeReplica.completed(replica, response)
  96. if (callback) {
  97. callback(replica, response)
  98. }
  99. }, MigrationActions.executeReplica.failed)
  100. .catch(MigrationActions.executeReplica.failed);
  101. } else {
  102. NotificationActions.notify("You cannot execute a migration.", "warning")
  103. }
  104. })
  105. MigrationActions.cancelMigration.listen((migration, callback = null) => {
  106. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  107. let url = null
  108. if (migration.type == 'migration') {
  109. url = `${servicesUrl.coriolis}/${projectId}/migrations/${migration.id}/actions`
  110. } else {
  111. if (migration.executions.length) {
  112. let executionId = migration.executions[migration.executions.length - 1].id
  113. url = `${servicesUrl.coriolis}/${projectId}/replicas/${migration.id}/executions/${executionId}/actions`
  114. } else {
  115. NotificationActions.notify("No executions to cancel on this replica")
  116. }
  117. }
  118. let payload = {
  119. cancel: null
  120. }
  121. Api.sendAjaxRequest({
  122. url: url,
  123. method: "POST",
  124. data: payload
  125. })
  126. .then((response) => {
  127. if (callback) {
  128. callback(migration, response)
  129. }
  130. MigrationActions.cancelMigration.completed(migration, response)
  131. }, MigrationActions.cancelMigration.failed
  132. )
  133. .catch(MigrationActions.cancelMigration.failed);
  134. })
  135. MigrationActions.getReplicaExecutions.listen((replica, callback) => {
  136. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  137. Api.sendAjaxRequest({
  138. url: `${servicesUrl.coriolis}/${projectId}/replicas/${replica.id}/executions/detail`,
  139. method: "GET"
  140. })
  141. .then((response) => {
  142. MigrationActions.getReplicaExecutions.completed(replica, response)
  143. if (callback) callback()
  144. }, MigrationActions.getReplicaExecutions.failed)
  145. .catch(MigrationActions.getReplicaExecutions.failed);
  146. })
  147. MigrationActions.getReplicaExecutionDetail.listen((replica, executionId, callback = null) => {
  148. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  149. Api.sendAjaxRequest({
  150. url: `${servicesUrl.coriolis}/${projectId}/replicas/${replica.id}/executions/${executionId}`,
  151. method: "GET"
  152. })
  153. .then((response) => {
  154. MigrationActions.getReplicaExecutionDetail.completed(replica, executionId, response)
  155. if (callback) {
  156. callback(replica, executionId, response)
  157. }
  158. }, MigrationActions.getReplicaExecutionDetail.failed)
  159. .catch(MigrationActions.getReplicaExecutionDetail.failed);
  160. })
  161. MigrationActions.deleteReplicaExecution.listen((replica, executionId, callback = null) => {
  162. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  163. Api.sendAjaxRequest({
  164. url: `${servicesUrl.coriolis}/${projectId}/replicas/${replica.id}/executions/${executionId}`,
  165. method: "DELETE"
  166. })
  167. .then((response) => {
  168. MigrationActions.deleteReplicaExecution.completed(replica, executionId, response)
  169. if (callback) {
  170. callback(replica, executionId, response)
  171. }
  172. }, MigrationActions.deleteReplicaExecution.failed)
  173. .catch(MigrationActions.deleteReplicaExecution.failed);
  174. })
  175. MigrationActions.addMigration.listen((migration) => {
  176. let payload = {}
  177. let instances = []
  178. migration.selectedInstances.forEach(instance => {
  179. if (migration.selectedInstances.indexOf(instance.id)) {
  180. instances.push(instance.instance_name)
  181. }
  182. })
  183. let network_map = {}
  184. migration.networks.forEach(network => {
  185. network_map[network.name] = network.migrateNetwork
  186. })
  187. let destinationEnv = {}
  188. for (var i in migration.destination_environment) {
  189. if (migration.destination_environment[i].label) { // removing label from dropdown if present
  190. destinationEnv[i] = migration.destination_environment[i].value
  191. } else {
  192. destinationEnv[i] = migration.destination_environment[i]
  193. }
  194. }
  195. destinationEnv["network_map"] = network_map
  196. payload[migration.migrationType] = {
  197. "origin_endpoint_id": migration.sourceCloud.credential.id,
  198. "destination_endpoint_id": migration.targetCloud.credential.id,
  199. "destination_environment": destinationEnv,
  200. "instances": instances,
  201. "notes": migration.notes,
  202. "security_groups": securityGroups
  203. }
  204. let migrationType = migration.migrationType === 'replica' ? 'replicas' : 'migrations'
  205. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  206. Api.sendAjaxRequest({
  207. url: `${servicesUrl.coriolis}/${projectId}/${migrationType}`,
  208. method: "POST",
  209. data: payload
  210. })
  211. .then(MigrationActions.addMigration.completed, MigrationActions.addMigration.failed)
  212. .catch(MigrationActions.addMigration.failed);
  213. })
  214. MigrationActions.createMigrationFromReplica.listen((replica) => {
  215. let payload = {
  216. migration: {
  217. replica_id: replica.id,
  218. force: false,
  219. clone_disks: true
  220. }
  221. }
  222. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  223. Api.sendAjaxRequest({
  224. url: `${servicesUrl.coriolis}/${projectId}/migrations`,
  225. method: "POST",
  226. data: payload
  227. })
  228. .then(MigrationActions.createMigrationFromReplica.completed, MigrationActions.createMigrationFromReplica.failed)
  229. .catch(MigrationActions.createMigrationFromReplica.failed);
  230. })
  231. export default MigrationActions;