MigrationActions.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. loadReplicas: { children: ['completed', 'failed'], shouldEmit: () => {} },
  21. loadMigration: { children: ['completed', 'failed'] }, // TODO: Reload migration action
  22. addMigration: { children: ['completed', 'failed'] },
  23. deleteMigration: { children: ['completed', 'failed'] },
  24. deleteReplica: { children: ['completed', 'failed'] },
  25. executeReplica: { children: ['completed', 'failed'] },
  26. cancelMigration: { children: ['completed', 'failed'] },
  27. getReplicaExecutions: { children: ['completed', 'failed'] },
  28. getReplicaExecutionDetail: { children: ['completed', 'failed'] },
  29. createMigrationFromReplica: { children: ['completed', 'failed'] },
  30. deleteReplicaExecution: { children: ['completed', 'failed'] },
  31. getMigration: {},
  32. setMigration: {},
  33. setReplica: {},
  34. setMigrationProperty: {}
  35. })
  36. MigrationActions.loadMigrations.listen(() => {
  37. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  38. Api.sendAjaxRequest({
  39. url: `${servicesUrl.coriolis}/${projectId}/migrations/detail`,
  40. method: "GET"
  41. })
  42. .then(MigrationActions.loadMigrations.completed, MigrationActions.loadMigrations.failed)
  43. .catch(MigrationActions.loadMigrations.failed);
  44. })
  45. MigrationActions.loadMigrations.shouldEmit = () => {
  46. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  47. return typeof projectId !== "undefined";
  48. }
  49. MigrationActions.loadReplicas.listen(() => {
  50. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  51. Api.sendAjaxRequest({
  52. url: `${servicesUrl.coriolis}/${projectId}/replicas/detail`,
  53. method: "GET"
  54. })
  55. .then(MigrationActions.loadReplicas.completed, MigrationActions.loadReplicas.failed)
  56. .catch(MigrationActions.loadReplicas.failed);
  57. })
  58. MigrationActions.loadReplicas.shouldEmit = () => {
  59. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  60. return typeof projectId !== "undefined";
  61. }
  62. MigrationActions.loadMigration.listen((migration) => {
  63. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  64. Api.sendAjaxRequest({
  65. url: `${servicesUrl.coriolis}/${projectId}/migrations/${migration.id}`,
  66. method: "GET"
  67. })
  68. .then(MigrationActions.loadMigration.completed, MigrationActions.loadMigration.failed)
  69. .catch(MigrationActions.loadMigration.failed);
  70. })
  71. MigrationActions.loadMigration.shouldEmit = () => {
  72. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  73. return typeof projectId !== "undefined"
  74. }
  75. MigrationActions.deleteMigration.listen((migration, callback = null) => {
  76. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  77. Api.sendAjaxRequest({
  78. url: `${servicesUrl.coriolis}/${projectId}/migrations/${migration.id}`,
  79. method: "DELETE"
  80. })
  81. .then(() => {
  82. MigrationActions.deleteMigration.completed(migration)
  83. if (callback) {
  84. callback(migration)
  85. }
  86. }, MigrationActions.deleteMigration.failed)
  87. .catch(MigrationActions.deleteMigration.failed);
  88. })
  89. MigrationActions.deleteReplica.listen((replica, callback = null) => {
  90. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  91. Api.sendAjaxRequest({
  92. url: `${servicesUrl.coriolis}/${projectId}/replicas/${replica.id}`,
  93. method: "DELETE"
  94. })
  95. .then(() => {
  96. MigrationActions.deleteReplica.completed(replica)
  97. if (callback) {
  98. callback(replica)
  99. }
  100. }, MigrationActions.deleteReplica.failed)
  101. .catch(MigrationActions.deleteReplica.failed);
  102. })
  103. MigrationActions.executeReplica.listen((replica, callback = null) => {
  104. if (replica.type == 'replica') {
  105. // check if endpoints exists
  106. let connections = Reflux.GlobalState.connectionStore.connections
  107. let originEndpoint = false
  108. let destinationEndpoint = false
  109. connections.forEach(connection => {
  110. if (replica.origin_endpoint_id === connection.id) {
  111. originEndpoint = true
  112. }
  113. if (replica.destination_endpoint_id === connection.id) {
  114. destinationEndpoint = true
  115. }
  116. })
  117. if (!originEndpoint) {
  118. NotificationActions.notify("Origin endpoint is missing. Cannot execute this replica.", "error")
  119. return
  120. }
  121. if (!destinationEndpoint) {
  122. NotificationActions.notify("Destination endpoint is missing. Cannot execute this replica.", "error")
  123. return
  124. }
  125. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  126. let payload = {
  127. execution: {
  128. shutdown_instances: false
  129. }
  130. }
  131. Api.sendAjaxRequest({
  132. url: `${servicesUrl.coriolis}/${projectId}/replicas/${replica.id}/executions`,
  133. method: "POST",
  134. data: payload
  135. })
  136. .then((response) => {
  137. MigrationActions.executeReplica.completed(replica, response)
  138. if (callback) {
  139. callback(replica, response)
  140. }
  141. }, MigrationActions.executeReplica.failed)
  142. .catch(MigrationActions.executeReplica.failed);
  143. } else {
  144. NotificationActions.notify("You cannot execute a migration.", "warning")
  145. }
  146. })
  147. MigrationActions.cancelMigration.listen((migration, callback = null) => {
  148. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  149. let url = null
  150. if (migration.type == 'migration') {
  151. url = `${servicesUrl.coriolis}/${projectId}/migrations/${migration.id}/actions`
  152. } else {
  153. if (migration.executions.length) {
  154. let executionId = migration.executions[migration.executions.length - 1].id
  155. url = `${servicesUrl.coriolis}/${projectId}/replicas/${migration.id}/executions/${executionId}/actions`
  156. } else {
  157. NotificationActions.notify("No executions to cancel on this replica")
  158. }
  159. }
  160. let payload = {
  161. cancel: null
  162. }
  163. Api.sendAjaxRequest({
  164. url: url,
  165. method: "POST",
  166. data: payload
  167. })
  168. .then((response) => {
  169. if (callback) {
  170. callback(migration, response)
  171. }
  172. MigrationActions.cancelMigration.completed(migration, response)
  173. }, MigrationActions.cancelMigration.failed)
  174. .catch(MigrationActions.cancelMigration.failed);
  175. })
  176. MigrationActions.getReplicaExecutions.listen((replica, callback) => {
  177. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  178. Api.sendAjaxRequest({
  179. url: `${servicesUrl.coriolis}/${projectId}/replicas/${replica.id}/executions/detail`,
  180. method: "GET"
  181. })
  182. .then((response) => {
  183. MigrationActions.getReplicaExecutions.completed(replica, response)
  184. if (callback) callback()
  185. }, MigrationActions.getReplicaExecutions.failed)
  186. .catch(MigrationActions.getReplicaExecutions.failed);
  187. })
  188. MigrationActions.getReplicaExecutionDetail.listen((replica, executionId, callback = null) => {
  189. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  190. Api.sendAjaxRequest({
  191. url: `${servicesUrl.coriolis}/${projectId}/replicas/${replica.id}/executions/${executionId}`,
  192. method: "GET"
  193. })
  194. .then((response) => {
  195. MigrationActions.getReplicaExecutionDetail.completed(replica, executionId, response)
  196. if (callback) {
  197. callback(replica, executionId, response)
  198. }
  199. }, MigrationActions.getReplicaExecutionDetail.failed)
  200. .catch(MigrationActions.getReplicaExecutionDetail.failed);
  201. })
  202. MigrationActions.deleteReplicaExecution.listen((replica, executionId, callback = null) => {
  203. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  204. Api.sendAjaxRequest({
  205. url: `${servicesUrl.coriolis}/${projectId}/replicas/${replica.id}/executions/${executionId}`,
  206. method: "DELETE"
  207. })
  208. .then((response) => {
  209. MigrationActions.deleteReplicaExecution.completed(replica, executionId)
  210. if (callback) {
  211. callback(replica, executionId, response)
  212. }
  213. }, MigrationActions.deleteReplicaExecution.failed)
  214. .catch(MigrationActions.deleteReplicaExecution.failed);
  215. })
  216. MigrationActions.addMigration.listen((migration, callback = null, errorCallback = null) => {
  217. let payload = {}
  218. let instances = []
  219. migration.selectedInstances.forEach(instance => {
  220. if (migration.selectedInstances.indexOf(instance.id)) {
  221. instances.push(instance.instance_name)
  222. }
  223. })
  224. let networkMap = {}
  225. migration.networks.forEach(network => {
  226. networkMap[network.network_name] = network.migrateNetwork
  227. })
  228. let destinationEnv = {}
  229. for (let i in migration.destination_environment) {
  230. if (migration.destination_environment[i].label) { // removing label from dropdown if present
  231. destinationEnv[i] = migration.destination_environment[i].value
  232. } else {
  233. destinationEnv[i] = migration.destination_environment[i]
  234. }
  235. }
  236. destinationEnv["network_map"] = networkMap // eslint-disable-line dot-notation
  237. payload[migration.migrationType] = {
  238. origin_endpoint_id: migration.sourceCloud.credential.id,
  239. destination_endpoint_id: migration.targetCloud.credential.id,
  240. destination_environment: destinationEnv,
  241. instances: instances,
  242. notes: migration.notes,
  243. security_groups: securityGroups
  244. }
  245. let migrationType = migration.migrationType === 'replica' ? 'replicas' : 'migrations'
  246. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  247. Api.sendAjaxRequest({
  248. url: `${servicesUrl.coriolis}/${projectId}/${migrationType}`,
  249. method: "POST",
  250. data: payload
  251. })
  252. .then((response) => {
  253. MigrationActions.addMigration.completed(response);
  254. if (callback) {
  255. callback(migration);
  256. }
  257. }, (response) => {
  258. MigrationActions.addMigration.failed(response)
  259. if (errorCallback) {
  260. errorCallback(migration);
  261. }
  262. })
  263. .catch(MigrationActions.addMigration.failed);
  264. })
  265. MigrationActions.createMigrationFromReplica.listen((replica) => {
  266. let payload = {
  267. migration: {
  268. replica_id: replica.id,
  269. force: false,
  270. clone_disks: true
  271. }
  272. }
  273. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  274. Api.sendAjaxRequest({
  275. url: `${servicesUrl.coriolis}/${projectId}/migrations`,
  276. method: "POST",
  277. data: payload
  278. })
  279. .then(MigrationActions.createMigrationFromReplica.completed, MigrationActions.createMigrationFromReplica.failed)
  280. .catch(MigrationActions.createMigrationFromReplica.failed);
  281. })
  282. export default MigrationActions;