BindPhysicalCpu.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{action}}</div>
  4. <div slot="body">
  5. <dialog-selected-tips :name="$t('dictionary.server')" :count="params.data.length" :action="action" />
  6. <dialog-table :data="params.data" :columns="columns" />
  7. <a-spin :spinning="cpuCoresInfoLoading">
  8. <a-form :form="form.fc" hideRequiredMark v-bind="formItemLayout">
  9. <a-form-item :label="$t('compute.bind_physical_cpu')">
  10. <a-switch
  11. v-decorator="decorators.bindCpuCores"
  12. :checkedChildren="$t('compute.text_115')"
  13. :unCheckedChildren="$t('compute.text_116')" />
  14. </a-form-item>
  15. <a-form-item :label="$t('compute.text_1058')" v-show="form.fd.bindCpuCores">
  16. <a-checkbox-group
  17. v-decorator="decorators.cpuCores"
  18. style="width: 100%; margin-top: 10px;">
  19. <a-row v-for="v in hostCores" :key="v">
  20. <a-col :span="4" v-for="c in v" :key="c">
  21. <a-tooltip v-if="hostUsedCores.includes(c)">
  22. <template slot="title">
  23. {{ $t('compute.host_use_core_tips') }}
  24. </template>
  25. <a-checkbox disabled :value="c">{{ c }}</a-checkbox>
  26. </a-tooltip>
  27. <a-checkbox v-else :value="c">{{ c }}</a-checkbox>
  28. </a-col>
  29. </a-row>
  30. </a-checkbox-group>
  31. </a-form-item>
  32. </a-form>
  33. </a-spin>
  34. </div>
  35. <div slot="footer">
  36. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  37. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  38. </div>
  39. </base-dialog>
  40. </template>
  41. <script>
  42. import chunk from 'lodash/chunk'
  43. import DialogMixin from '@/mixins/dialog'
  44. import WindowsMixin from '@/mixins/windows'
  45. export default {
  46. name: 'BindPhysicalCpuDialog',
  47. mixins: [DialogMixin, WindowsMixin],
  48. data () {
  49. return {
  50. loading: false,
  51. action: this.$t('compute.bind_physical_cpu'),
  52. cpuCoresInfo: {},
  53. cpuCoresInfoLoading: false,
  54. form: {
  55. fc: this.$form.createForm(this, { onValuesChange: this.onValuesChange }),
  56. fd: {
  57. bindCpuCores: false,
  58. },
  59. },
  60. decorators: {
  61. bindCpuCores: [
  62. 'bindCpuCores',
  63. {
  64. valuePropName: 'checked',
  65. initialValue: false,
  66. },
  67. ],
  68. cpuCores: [
  69. 'cpuCores',
  70. ],
  71. },
  72. formItemLayout: {
  73. wrapperCol: {
  74. span: 20,
  75. },
  76. labelCol: {
  77. span: 4,
  78. },
  79. },
  80. }
  81. },
  82. computed: {
  83. columns () {
  84. return this.params.columns.slice(0, 3)
  85. },
  86. hostCores () {
  87. const hostCores = (this.cpuCoresInfo.host_cores || []).sort((a, b) => a - b)
  88. return chunk(hostCores, 6)
  89. },
  90. hostUsedCores () {
  91. const hostUsedCores = this.cpuCoresInfo?.host_used_cores || []
  92. return hostUsedCores.filter(v => {
  93. const pinnedCores = this.cpuCoresInfo?.pinned_cores || []
  94. return !pinnedCores.includes(v)
  95. })
  96. },
  97. },
  98. created () {
  99. this.loadCpusetCores()
  100. },
  101. methods: {
  102. async loadCpusetCores () {
  103. try {
  104. const id = this.params.data[0].id
  105. const manager = new this.$Manager('servers')
  106. this.cpuCoresInfoLoading = true
  107. const { data } = await manager.getSpecific({ id, spec: 'cpuset-cores' })
  108. this.cpuCoresInfo = data
  109. this.form.fc.setFieldsValue({
  110. cpuCores: data.pinned_cores,
  111. bindCpuCores: data.pinned_cores?.length > 0,
  112. })
  113. } catch (error) {
  114. throw error
  115. } finally {
  116. this.cpuCoresInfoLoading = false
  117. }
  118. },
  119. doBindPhysicalSubmit (values) {
  120. const { bindCpuCores, cpuCores } = values
  121. const ids = this.params.data.map(item => item.id)
  122. const params = { cpus: cpuCores }
  123. if (bindCpuCores) {
  124. return this.params.onManager('performAction', {
  125. id: ids[0],
  126. steadyStatus: ['ready'],
  127. managerArgs: {
  128. action: 'cpuset',
  129. data: params,
  130. },
  131. })
  132. } else {
  133. return this.params.onManager('performAction', {
  134. id: ids[0],
  135. steadyStatus: ['ready'],
  136. managerArgs: {
  137. action: 'cpuset-remove',
  138. data: {},
  139. },
  140. })
  141. }
  142. },
  143. async handleConfirm () {
  144. this.loading = true
  145. try {
  146. const values = await this.form.fc.validateFields()
  147. await this.doBindPhysicalSubmit(values)
  148. this.params.refresh && this.params.refresh()
  149. this.cancelDialog()
  150. } catch (error) {
  151. throw error
  152. } finally {
  153. this.loading = false
  154. }
  155. },
  156. onValuesChange (props, values) {
  157. Object.keys(values).forEach((key) => {
  158. this.form.fd[key] = values[key]
  159. })
  160. },
  161. },
  162. }
  163. </script>