MigrationStore.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /* eslint-disable */
  2. /*
  3. Copyright (C) 2017 Cloudbase Solutions SRL
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Affero General Public License as
  6. published by the Free Software Foundation, either version 3 of the
  7. License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. import Reflux from 'reflux';
  16. import MigrationActions from '../../actions/MigrationActions';
  17. import {networkMock, targetNetworkMock} from '../../config';
  18. import moment from 'moment';
  19. class MigrationStore extends Reflux.Store
  20. {
  21. constructor() {
  22. super()
  23. this.listenables = MigrationActions
  24. this.state = {
  25. migrations: null,
  26. replicas: null,
  27. migration: null,
  28. replica: null
  29. }
  30. }
  31. onLoadMigrations() {
  32. //this.setState({ migrations: null })
  33. }
  34. onLoadMigrationsCompleted(response) {
  35. let connections = Reflux.GlobalState.connectionStore.connections
  36. this.setState({ migrations: [] })
  37. let migrations = response.data.migrations
  38. migrations.forEach(migration => {
  39. let connection = connections.filter(connection => connection.id == migration.destination_endpoint_id)[0]
  40. if (connection) {
  41. migration.destination_endpoint_type = connection.type
  42. }
  43. connection = connections.filter(connection => connection.id == migration.origin_endpoint_id)[0]
  44. if (connection) {
  45. migration.origin_endpoint_type = connection.type
  46. }
  47. migration.name = migration.instances.join(", ");
  48. })
  49. migrations.sort((a, b) => {
  50. return moment(b.created_at).isAfter(moment(a.created_at))
  51. })
  52. this.setState({
  53. migrations: migrations
  54. })
  55. }
  56. onLoadReplicas() {
  57. //this.setState({ replicas: null })
  58. }
  59. onLoadReplicasCompleted(response) {
  60. let connections = Reflux.GlobalState.connectionStore.connections
  61. let replicas = response.data.replicas
  62. replicas.forEach(replica => {
  63. let connection = connections.filter(connection => connection.id == replica.destination_endpoint_id)[0]
  64. if (connection) {
  65. replica.destination_endpoint_type = connection.type
  66. }
  67. connection = connections.filter(connection => connection.id == replica.origin_endpoint_id)[0]
  68. if (connection) {
  69. replica.origin_endpoint_type = connection.type
  70. }
  71. replica.name = replica.instances.join(", ");
  72. if (replica.executions.length) {
  73. MigrationActions.getReplicaExecutions(replica)
  74. }
  75. })
  76. replicas.sort((a, b) => {
  77. return moment(b.created_at).isAfter(moment(a.created_at))
  78. })
  79. this.setState({
  80. replicas: replicas,
  81. })
  82. }
  83. onLoadMigrationCompleted(response) {
  84. let migrations = this.state.migrations
  85. let connections = Reflux.GlobalState.connectionStore.connections
  86. migrations.forEach((migration, index) => {
  87. let updatedMigration = response.data.migration
  88. if (migration.id === updatedMigration.id) {
  89. let connection = connections.filter(connection => connection.id === updatedMigration.destination_endpoint_id)[0]
  90. if (connection) {
  91. updatedMigration.destination_endpoint_type = connection.type
  92. }
  93. connection = connections.filter(connection => connection.id === updatedMigration.origin_endpoint_id)[0]
  94. if (connection) {
  95. updatedMigration.origin_endpoint_type = connection.type
  96. }
  97. updatedMigration.name = migration.instances.join(", ");
  98. migrations[index] = updatedMigration
  99. }
  100. })
  101. this.setState({ migrations: migrations })
  102. }
  103. onGetReplicaExecutionsCompleted(replica, response) {
  104. let replicas = this.state.replicas
  105. replicas.forEach((item) => {
  106. if (item.id == replica.id) {
  107. item.executions = response.data.executions.sort((a, b) => a.number - b.number)
  108. item.tasks = item.executions[item.executions.length - 1].tasks
  109. item.status = item.executions[item.executions.length - 1].status
  110. item.executions.forEach(execution => {
  111. //MigrationActions.getReplicaExecutionDetail(replica, execution.id)
  112. })
  113. }
  114. })
  115. this.setState({ replicas: replicas })
  116. }
  117. onGetReplicaExecutionDetailCompleted(replica, executionId, response) {
  118. let replicas = this.state.replicas
  119. let index = replicas.indexOf(replica)
  120. replicas[index].executions.forEach((execution, execIndex) => {
  121. if (execution.id == executionId) {
  122. replicas[index].executions[execIndex] = response.data.execution
  123. }
  124. replicas[index].tasks = replica.executions[replica.executions.length - 1].tasks
  125. replicas[index].status = replica.executions[replica.executions.length - 1].status
  126. })
  127. this.setState({ replicas: replicas })
  128. }
  129. onDeleteReplicaExecutionCompleted(replica, executionId) {
  130. let replicas = this.state.replicas
  131. let index = replicas.indexOf(replica)
  132. let execIndex = -1
  133. replicas[index].executions.forEach((execution, i) => {
  134. if (execution.id == executionId) {
  135. execIndex = i
  136. }
  137. })
  138. replicas[index].executions.splice(execIndex, 1)
  139. if (replicas[index].executions[replicas[index].executions]) {
  140. replicas[index].tasks = replicas[index].executions[replicas[index].executions.length - 1].tasks
  141. replicas[index].status = replicas[index].executions[replicas[index].executions.length - 1].status
  142. } else {
  143. replicas[index].tasks = []
  144. replicas[index].status = null
  145. }
  146. this.setState({ replicas: replicas })
  147. }
  148. onGetReplicaExecutionDetailFailed(response) {
  149. }
  150. onLoadMigrationsFailed(response) {
  151. }
  152. onAddMigrationCompleted(response)
  153. {
  154. let migrations = this.state.migrations
  155. let migration = response.data.migration ? response.data.migration : response.data.replica
  156. let migrationType = !!response.data.migration ? "migration" : "replica"
  157. let connections = Reflux.GlobalState.connectionStore.connections
  158. migration.selected = false
  159. let connection = connections.filter(connection => connection.id == migration.destination_endpoint_id)[0]
  160. if (connection) {
  161. migration.destination_endpoint_type = connection.type
  162. }
  163. connection = connections.filter(connection => connection.id == migration.origin_endpoint_id)[0]
  164. if (connection) {
  165. migration.origin_endpoint_type = connection.type
  166. }
  167. migration.name = migration.instances.join(", ");
  168. migrations.push(migration)
  169. this.setState({migrations: migrations})
  170. if (migrationType == "replica") {
  171. MigrationActions.executeReplica(migration)
  172. }
  173. }
  174. onDeleteMigrationCompleted(migration) {
  175. let migrations = this.state.migrations
  176. let index = migrations.indexOf(migration)
  177. migrations.splice(index, 1)
  178. this.setState({migrations: migrations})
  179. }
  180. onDeleteReplicaCompleted(migration) {
  181. let replicas = this.state.replicas
  182. let index = replicas.indexOf(migration)
  183. replicas.splice(index, 1)
  184. this.setState({replicas: replicas})
  185. }
  186. onExecuteReplicaCompleted(replica, response) {
  187. let replicas = this.state.replicas
  188. replicas.forEach((item, index) => {
  189. if (item.id == replica.id) {
  190. replicas[index].executions.push(response.data.execution)
  191. replicas[index].tasks = item.executions[item.executions.length - 1].tasks
  192. replicas[index].status = item.executions[item.executions.length - 1].status
  193. }
  194. })
  195. this.setState({ migrations: replicas })
  196. }
  197. onCancelMigrationCompleted(migration, response, callback = null) {
  198. if (callback) {
  199. callback(migration, response)
  200. }
  201. }
  202. onSetMigration(migrationId) {
  203. let migration = this.onGetMigration(migrationId)
  204. this.setState({migration: migration})
  205. if (migration && migration.type === "migration") {
  206. MigrationActions.loadMigration(migration)
  207. }
  208. }
  209. onSetReplica(replicaId) {
  210. let replica = null
  211. if (this.state.replicas) {
  212. this.state.replicas.forEach(function(item) {
  213. if (item.id == replicaId) {
  214. replica = item
  215. }
  216. })
  217. }
  218. if (replica) {
  219. this.setState({replica: replica})
  220. }
  221. }
  222. onGetMigration(migrationId)
  223. {
  224. let migration = null
  225. if (this.state.migrations) {
  226. this.state.migrations.forEach(function(item) {
  227. if (item.id == migrationId) {
  228. migration = item
  229. }
  230. })
  231. }
  232. return migration
  233. }
  234. onSetMigrationProperty(migrationId, property, value) {
  235. this.state.migrations.forEach(function(item) {
  236. if (item.id == migrationId) {
  237. item[property] = value
  238. }
  239. })
  240. }
  241. onCreateMigrationFromReplicaCompleted(response) {
  242. if (response.data.migration) {
  243. let migrations = this.state.migrations
  244. let migration = response.data.migration
  245. let connections = Reflux.GlobalState.connectionStore.connections
  246. let connection = connections.filter(connection => connection.id == migration.destination_endpoint_id)[0]
  247. if (connection) {
  248. migration.destination_endpoint_type = connection.type
  249. }
  250. connection = connections.filter(connection => connection.id == migration.origin_endpoint_id)[0]
  251. if (connection) {
  252. migration.origin_endpoint_type = connection.type
  253. }
  254. migration.name = migration.instances.join(", ");
  255. migrations.push(migration)
  256. this.setState({migrations: migrations})
  257. }
  258. }
  259. processTasks(tasksRaw) {
  260. let lines = tasksRaw.split("\n")
  261. let tasks = []
  262. let currentTask = {}
  263. let newTask = false
  264. let progressUpdates = []
  265. for (let i in lines) {
  266. if (newTask) {
  267. currentTask["progress_updates"] = progressUpdates
  268. tasks.push(currentTask)
  269. currentTask = {}
  270. newTask = false
  271. progressUpdates = []
  272. }
  273. let line = lines[i].trim()
  274. if (line == "") {
  275. newTask = true
  276. } else {
  277. let sep = line.split(": ")
  278. if (sep[0] == "progress_updates:") { // progress updates section starts
  279. currentTask["progress_updates"] = null
  280. } else if (isNaN(sep[0][0])) {
  281. currentTask[sep[0]] = sep[1]
  282. } else { // adds progress messages
  283. let date = line.substring(0, line.indexOf(' '))
  284. let msg = line.substring(line.indexOf(' ')+1)
  285. progressUpdates.push([date, msg])
  286. }
  287. }
  288. }
  289. currentTask["progress_updates"] = progressUpdates
  290. tasks.push(currentTask)
  291. console.log(tasks)
  292. return tasks
  293. }
  294. }
  295. MigrationStore.id = "migrationStore"
  296. export default MigrationStore;