ReplicaSource.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 Api from '../utils/ApiCaller'
  17. import { servicesUrl } from '../config'
  18. import type { MainItem } from '../types/MainItem'
  19. import type { Execution } from '../types/Execution'
  20. import type { Field } from '../types/Field'
  21. class ReplicaSourceUtils {
  22. static filterDeletedExecutionsInReplicas(replicas) {
  23. return replicas.map(replica => {
  24. replica.executions = ReplicaSourceUtils.filterDeletedExecutions(replica.executions)
  25. return replica
  26. })
  27. }
  28. static filterDeletedExecutions(executions) {
  29. if (!executions || !executions.length) {
  30. return executions
  31. }
  32. return executions.filter(execution => execution.deleted_at == null)
  33. }
  34. static sortReplicas(replicas) {
  35. if (replicas.length === 1) {
  36. ReplicaSourceUtils.sortExecutions(replicas[0].executions)
  37. return
  38. }
  39. replicas.sort((a, b) => {
  40. ReplicaSourceUtils.sortExecutions(a.executions)
  41. ReplicaSourceUtils.sortExecutions(b.executions)
  42. let aLastExecution = a.executions && a.executions.length ? a.executions[a.executions.length - 1] : null
  43. let bLastExecution = b.executions && b.executions.length ? b.executions[b.executions.length - 1] : null
  44. let aLastTime = aLastExecution ? aLastExecution.updated_at || aLastExecution.created_at : null
  45. let bLastTime = bLastExecution ? bLastExecution.updated_at || bLastExecution.created_at : null
  46. let aTime = aLastTime || a.updated_at || a.created_at
  47. let bTime = bLastTime || b.updated_at || b.created_at
  48. return moment(bTime).diff(moment(aTime))
  49. })
  50. }
  51. static sortExecutions(executions) {
  52. if (executions) {
  53. executions.sort((a, b) => a.number - b.number)
  54. }
  55. }
  56. static sortExecutionsAndTaskUpdates(executions) {
  57. this.sortExecutions(executions)
  58. executions.forEach(execution => {
  59. this.sortTaskUpdates(execution)
  60. })
  61. }
  62. static sortTaskUpdates(execution) {
  63. if (execution.tasks) {
  64. execution.tasks.forEach(task => {
  65. if (task.progress_updates) {
  66. task.progress_updates.sort((a, b) => moment(b.created_at).isBefore(moment(a.created_at)))
  67. }
  68. })
  69. }
  70. }
  71. }
  72. class ReplicaSource {
  73. static getReplicas(): Promise<MainItem[]> {
  74. return Api.get(`${servicesUrl.coriolis}/${Api.projectId}/replicas/detail`).then(response => {
  75. let replicas = response.data.replicas
  76. replicas = ReplicaSourceUtils.filterDeletedExecutionsInReplicas(replicas)
  77. ReplicaSourceUtils.sortReplicas(replicas)
  78. return replicas
  79. })
  80. }
  81. static getReplicaExecutions(replicaId: string): Promise<Execution[]> {
  82. return Api.get(`${servicesUrl.coriolis}/${Api.projectId}/replicas/${replicaId}/executions/detail`).then((response) => {
  83. let executions = response.data.executions
  84. ReplicaSourceUtils.sortExecutionsAndTaskUpdates(executions)
  85. return executions
  86. })
  87. }
  88. static getReplica(replicaId: string): Promise<MainItem> {
  89. return Api.get(`${servicesUrl.coriolis}/${Api.projectId}/replicas/${replicaId}`).then(response => {
  90. let replica = response.data.replica
  91. replica.executions = ReplicaSourceUtils.filterDeletedExecutions(replica.executions)
  92. ReplicaSourceUtils.sortExecutions(replica.executions)
  93. return replica
  94. })
  95. }
  96. static execute(replicaId: string, fields?: Field[]): Promise<Execution> {
  97. let payload = { execution: { shutdown_instances: false } }
  98. if (fields) {
  99. fields.forEach(f => {
  100. payload.execution[f.name] = f.value || false
  101. })
  102. }
  103. return Api.send({
  104. url: `${servicesUrl.coriolis}/${Api.projectId}/replicas/${replicaId}/executions`,
  105. method: 'POST',
  106. data: payload,
  107. }).then((response) => {
  108. let execution = response.data.execution
  109. ReplicaSourceUtils.sortTaskUpdates(execution)
  110. return execution
  111. })
  112. }
  113. static cancelExecution(replicaId: string, executionId: string): Promise<string> {
  114. return Api.send({
  115. url: `${servicesUrl.coriolis}/${Api.projectId}/replicas/${replicaId}/executions/${executionId}/actions`,
  116. method: 'POST',
  117. data: { cancel: null },
  118. }).then(() => replicaId)
  119. }
  120. static deleteExecution(replicaId: string, executionId: string): Promise<string> {
  121. return Api.send({
  122. url: `${servicesUrl.coriolis}/${Api.projectId}/replicas/${replicaId}/executions/${executionId}`,
  123. method: 'DELETE',
  124. }).then(() => replicaId)
  125. }
  126. static delete(replicaId: string): Promise<string> {
  127. return Api.send({
  128. url: `${servicesUrl.coriolis}/${Api.projectId}/replicas/${replicaId}`,
  129. method: 'DELETE',
  130. }).then(() => replicaId)
  131. }
  132. static deleteDisks(replicaId: string): Promise<Execution> {
  133. return Api.send({
  134. url: `${servicesUrl.coriolis}/${Api.projectId}/replicas/${replicaId}/actions`,
  135. method: 'POST',
  136. data: { 'delete-disks': null },
  137. }).then(response => response.data.execution)
  138. }
  139. }
  140. export default ReplicaSource