AttachGpu.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{action}}</div>
  4. <div slot="body">
  5. <a-alert class="mb-2" type="warning">
  6. <div slot="message" v-if="params.data.length === 1">{{$t('compute.text_1167')}}</div>
  7. <div slot="message" v-else>
  8. <p>{{$t('compute.text_1168')}}</p>
  9. <p>{{$t('compute.text_1169')}}</p>
  10. </div>
  11. </a-alert>
  12. <dialog-selected-tips :name="$t('dictionary.server')" :count="params.data.length" :action="action" />
  13. <dialog-table :data="params.data" :columns="columns" />
  14. <a-form
  15. :form="form.fc"
  16. v-bind="formItemLayout">
  17. <a-form-item :label="$t('compute.text_1170')">
  18. <a-radio-group name="radioGroup" :defaultValue="true" v-if="isGroupAction" v-model="isOpenGpu">
  19. <a-radio :value="true">{{$t('compute.text_902')}}</a-radio>
  20. <a-radio :value="false">{{$t('compute.text_723')}}</a-radio>
  21. </a-radio-group>
  22. <a-switch :checkedChildren="$t('compute.text_115')" :unCheckedChildren="$t('compute.text_116')" v-model="isOpenGpu" v-else />
  23. </a-form-item>
  24. <a-form-item v-if="isOpenGpu" :extra="$t('compute.text_1171')">
  25. <span slot="label">
  26. {{ $t('compute.text_607') }}&nbsp;
  27. <a-tooltip :title="$t('compute.vgpu_check.tooltip')">
  28. <a-icon type="question-circle-o" />
  29. </a-tooltip>
  30. </span>
  31. <!-- 批量设置 -->
  32. <base-select
  33. v-if="isGroupAction"
  34. v-decorator="decorators.device"
  35. :params="gpuParams"
  36. :need-params="false"
  37. :labelFormat="labelFormat"
  38. :disabled-items="disabledItems"
  39. filterable
  40. :resList.sync="gpuOpt"
  41. :mapper="mapper"
  42. resource="isolated_devices"
  43. :select-props="{ allowClear: true, placeholder: $t('compute.text_1172'), mode: 'default' }"
  44. @change="gpuChangeHandle">
  45. <template v-slot:optionTemplate>
  46. <a-select-option v-for="item in gpuOpt" :key="item.id" :value="item.id" :disabled="item.__disabled">
  47. <div class="d-flex">
  48. <span class="text-truncate flex-fill mr-2" :title="item.model">{{ item.model }}</span>
  49. <span style="color: #8492a6; font-size: 13px" v-show="item.totalCount > item.usedCount">{{$t('compute.text_1173', [ item.totalCount - item.usedCount , item.totalCount ])}}</span>
  50. <span style="color: #8492a6; font-size: 13px" v-show="item.totalCount === item.usedCount">{{$t('compute.text_1174')}}</span>
  51. </div>
  52. </a-select-option>
  53. </template>
  54. </base-select>
  55. <!-- 单条操作 -->
  56. <base-select
  57. v-else
  58. v-decorator="decorators.device"
  59. :params="gpuParams"
  60. :need-params="false"
  61. :labelFormat="labelFormat"
  62. :disabled-items="disabledItems"
  63. filterable
  64. :resList.sync="gpuOpt"
  65. resource="isolated_devices"
  66. :select-props="{ allowClear: true, placeholder: $t('compute.text_1172'), mode: 'multiple' }"
  67. @change="gpuChangeHandle" />
  68. </a-form-item>
  69. <a-form-item :label="$t('compute.text_294')" v-if="isOpenGpu && isGroupAction" :extra="$t('compute.text_1175')">
  70. <a-input-number :min="1" v-decorator="decorators.number" />
  71. </a-form-item>
  72. <a-form-item :label="$t('compute.text_494')" :extra="$t('compute.text_495')" v-if="isOpenAutoStart">
  73. <a-switch :checkedChildren="$t('compute.text_115')" :unCheckedChildren="$t('compute.text_116')" v-decorator="decorators.autoStart" />
  74. </a-form-item>
  75. </a-form>
  76. </div>
  77. <div slot="footer">
  78. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  79. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  80. </div>
  81. </base-dialog>
  82. </template>
  83. <script>
  84. import * as R from 'ramda'
  85. import {
  86. getIpsTableColumn,
  87. } from '@/utils/common/tableColumn'
  88. import DialogMixin from '@/mixins/dialog'
  89. import WindowsMixin from '@/mixins/windows'
  90. import { GPU_DEV_TYPE_OPTION_MAP } from '@Compute/constants'
  91. export default {
  92. name: 'VmAttachGpuDialog',
  93. mixins: [DialogMixin, WindowsMixin],
  94. data () {
  95. return {
  96. loading: false,
  97. action: this.$t('compute.text_1112'),
  98. form: {
  99. fc: this.$form.createForm(this, { onValuesChange: this.onValuesChange }),
  100. fd: {
  101. device: [],
  102. },
  103. },
  104. decorators: {
  105. device: [
  106. 'device',
  107. {
  108. rules: [
  109. { required: true, type: 'any', message: this.$t('compute.text_1172'), trigger: 'change' },
  110. ],
  111. },
  112. ],
  113. autoStart: [
  114. 'autoStart',
  115. {
  116. valuePropName: 'checked',
  117. initialValue: false,
  118. },
  119. ],
  120. number: [
  121. 'number',
  122. {
  123. initialValue: 1,
  124. },
  125. ],
  126. },
  127. formItemLayout: {
  128. wrapperCol: {
  129. span: 20,
  130. },
  131. labelCol: {
  132. span: 4,
  133. },
  134. },
  135. gpuOpt: [],
  136. isOpenGpu: false,
  137. bindGpus: [],
  138. columns: [
  139. {
  140. field: 'name',
  141. title: this.$t('compute.text_228'),
  142. },
  143. getIpsTableColumn({ field: 'ip', title: 'IP' }),
  144. {
  145. field: 'isolated_devices',
  146. title: this.$t('compute.text_113'),
  147. slots: {
  148. default: ({ row }) => {
  149. const ret = []
  150. if (row.isolated_devices) {
  151. row.isolated_devices.map(item => {
  152. ret.push(<list-body-cell-wrap row={{ showName: `${item.addr || ''} ${item.model || ''}` }} field="showName" />)
  153. })
  154. }
  155. return ret
  156. },
  157. },
  158. },
  159. ],
  160. }
  161. },
  162. computed: {
  163. selectedItems () {
  164. return this.params.data
  165. },
  166. gpuParams () {
  167. if (this.selectedItems && this.selectedItems.length > 0) {
  168. let host = ''
  169. this.selectedItems.map(item => {
  170. host += item.host_id + ','
  171. })
  172. host = host.substring(0, host.lastIndexOf(','))
  173. return {
  174. 'filter.0': `host_id.in(${host})`,
  175. limit: 0,
  176. 'filter.1': 'dev_type.notin(USB,NIC,NVME-PT)',
  177. scope: this.$store.getters.scope,
  178. }
  179. }
  180. return {}
  181. },
  182. attchGpu () {
  183. return this.form.fd.device.filter((id) => { return !this.bindGpus.includes(id) })
  184. },
  185. detachGpu () {
  186. return this.bindGpus.filter((id) => { return !this.form.fd.device.includes(id) })
  187. },
  188. disabledItems () {
  189. if (this.isGroupAction) {
  190. return this.gpuOpt.filter(val => {
  191. if (val.usedCount === val.totalCount) {
  192. return true
  193. } else if (val.totalCount - val.usedCount < this.params.data.length) {
  194. return true
  195. }
  196. return false
  197. }).map(item => { return item.id })
  198. } else {
  199. return this.gpuOpt.filter(val => { return val.guest_id && val.guest_id !== this.selectedItems[0].id }).map(item => { return item.id })
  200. }
  201. },
  202. isGroupAction () { // 是否是批量操作
  203. if (this.params.data.length > 1) return true
  204. return false
  205. },
  206. isOpenAutoStart () {
  207. return this.selectedItems.every(item => item.status === 'ready')
  208. },
  209. },
  210. watch: {
  211. gpuOpt () {
  212. if (!this.isGroupAction) {
  213. this.bindGpus = this.gpuOpt.filter(item => item.guest_id === this.params.data[0].id).map(item => { return item.id })
  214. if (this.bindGpus.length > 0) {
  215. this.form.fc.setFieldsValue({ device: this.bindGpus })
  216. this.isOpenGpu = true
  217. }
  218. }
  219. },
  220. disabledItems () {
  221. if (this.disabledItems && this.disabledItems.length && this.isGroupAction) { // 禁用某些选项
  222. this.disabledItems.forEach(disabledId => {
  223. this.gpuOpt.forEach(item => {
  224. if (disabledId === item.id) {
  225. item.__disabled = true
  226. }
  227. })
  228. })
  229. }
  230. },
  231. },
  232. created () {
  233. this.init()
  234. },
  235. methods: {
  236. init () {
  237. if (!this.isGroupAction) {
  238. const bindDevices = this.selectedItems?.[0]?.isolated_devices?.map(item => item.id) || []
  239. if (bindDevices?.length > 0) {
  240. this.isOpenGpu = true
  241. this.form.fc.setFieldsValue({ device: bindDevices })
  242. }
  243. }
  244. },
  245. async doAttachSubmit (data) {
  246. const params = {
  247. add_devices: this.attchGpu,
  248. del_devices: this.detachGpu,
  249. auto_start: data.autoStart,
  250. }
  251. const ids = this.params.data.map(item => item.id)
  252. if (ids.length > 1) {
  253. const selectedNum = this.params.data.length
  254. const { number: count } = data
  255. const gpuItem = this.gpuOpt.filter(item => { return item.id === this.attchGpu[0] })
  256. const model = gpuItem[0].model
  257. const remain = gpuItem[0].totalCount - gpuItem[0].usedCount
  258. if (selectedNum * count > remain) {
  259. this.$message.warning(this.$t('compute.text_1177'))
  260. throw new Error(this.$t('compute.text_1178'))
  261. }
  262. return this.params.onManager('batchPerformAction', {
  263. id: ids,
  264. steadyStatus: ['running', 'ready'],
  265. managerArgs: {
  266. action: 'attach-isolated-device',
  267. data: {
  268. model,
  269. count,
  270. auto_start: data.autoStart,
  271. },
  272. },
  273. })
  274. }
  275. return this.params.onManager('batchPerformAction', {
  276. id: ids,
  277. steadyStatus: ['running', 'ready'],
  278. managerArgs: {
  279. action: 'set-isolated-device',
  280. data: params,
  281. },
  282. })
  283. },
  284. async doDetachSubmit (data) {
  285. // 批量解除绑定
  286. let params = {}
  287. if (this.isGroupAction) {
  288. params = {
  289. detach_all: true,
  290. auto_start: data.autoStart,
  291. }
  292. } else {
  293. params = {
  294. add_devices: [],
  295. del_devices: this.bindGpus,
  296. auto_start: data.autoStart,
  297. }
  298. }
  299. const ids = this.params.data.map(item => item.id)
  300. return this.params.onManager('batchPerformAction', {
  301. id: ids,
  302. steadyStatus: ['running', 'ready'],
  303. managerArgs: {
  304. action: this.isGroupAction ? 'detach-isolated-device' : 'set-isolated-device',
  305. data: params,
  306. },
  307. })
  308. },
  309. async handleConfirm () {
  310. this.loading = true
  311. try {
  312. if (this.isOpenGpu) {
  313. const values = await this.form.fc.validateFields()
  314. await this.doAttachSubmit(values)
  315. } else {
  316. const values = await this.form.fc.getFieldsValue()
  317. await this.doDetachSubmit(values)
  318. }
  319. this.loading = false
  320. this.cancelDialog()
  321. } catch (error) {
  322. this.loading = false
  323. throw error
  324. }
  325. },
  326. mapper (data) {
  327. let newData = []
  328. newData = this.grpupMapper(data)
  329. return newData
  330. },
  331. // 格式化基本数据
  332. genResourceData (data) {
  333. const obj = {}
  334. for (let i = 0; i < data.length; i++) {
  335. const item = data[i]
  336. if (!obj[item.model]) {
  337. obj[item.model] = {
  338. usedCount: item.guest ? 1 : 0,
  339. totalCount: 1,
  340. }
  341. if (!item.guest) {
  342. obj[item.model] = {
  343. ...obj[item.model],
  344. ...item,
  345. }
  346. }
  347. // 兼容如果某个型号第一个就是已使用情况
  348. if (item.guest && !obj[item.model].id) {
  349. obj[item.model] = {
  350. ...obj[item.model],
  351. ...item,
  352. }
  353. }
  354. } else {
  355. obj[item.model].totalCount += 1
  356. if (item.guest) {
  357. obj[item.model].usedCount += 1
  358. } else {
  359. obj[item.model] = {
  360. ...obj[item.model],
  361. ...item,
  362. }
  363. }
  364. }
  365. }
  366. const newData = []
  367. R.forEachObjIndexed((value, key) => {
  368. newData.push(value)
  369. }, obj)
  370. return newData
  371. },
  372. grpupMapper (data) {
  373. const obj = {}
  374. for (var i = 0; i < data.length; i++) {
  375. const item = data[i]
  376. if (!obj[item.host_id]) {
  377. obj[item.host_id] = [
  378. item,
  379. ]
  380. } else {
  381. obj[item.host_id].push(item)
  382. }
  383. }
  384. for (const key in obj) {
  385. obj[key] = this.genResourceData(obj[key])
  386. }
  387. return this.filterSameModel(obj)
  388. },
  389. // 获取多个宿主机下共有的GPU型号数据
  390. filterSameModel (obj) {
  391. // 转成二维数组
  392. const arrs = []
  393. Object.values(obj).map(item => arrs.push(item))
  394. let arr = arrs.shift()
  395. for (let i = arrs.length; i--;) {
  396. const obj = {}
  397. arr = arr.concat(arrs[i]).filter((item, key) => {
  398. const objItem = obj[item.model]
  399. if (!objItem) {
  400. obj[item.model] = item
  401. obj[item.model].inx = key
  402. }
  403. if (objItem && objItem.inx !== key) {
  404. const readyRemain = objItem.totalCount - objItem.usedCount
  405. const targetRemain = item.totalCount - item.usedCount
  406. if (readyRemain > targetRemain) {
  407. objItem.totalCount = item.totalCount
  408. objItem.usedCount = item.usedCount
  409. } else {
  410. item.totalCount = objItem.totalCount
  411. item.usedCount = objItem.usedCount
  412. }
  413. return true
  414. }
  415. return false
  416. })
  417. }
  418. this.selectedItems.map(item => {
  419. if (!obj[item.host_id]) {
  420. arr = []
  421. }
  422. })
  423. return arr
  424. },
  425. labelFormat (val) {
  426. const vgpuType = GPU_DEV_TYPE_OPTION_MAP.VGPU.value
  427. if (val.dev_type.endsWith(vgpuType)) {
  428. return `${vgpuType}:${val.model || ''}`
  429. }
  430. return `${val.dev_type}:${val.addr || ''}:${val.model || ''}`
  431. },
  432. onValuesChange (props, values) {
  433. Object.keys(values).forEach((key) => {
  434. if (key === 'device') {
  435. if (this.isGroupAction) {
  436. this.form.fd[key] = [values[key]]
  437. } else {
  438. this.form.fd[key] = values[key]
  439. }
  440. } else {
  441. this.form.fd[key] = values[key]
  442. }
  443. })
  444. },
  445. gpuChangeHandle (val = []) {
  446. if (val && val.length > 0) {
  447. const vgpuType = GPU_DEV_TYPE_OPTION_MAP.VGPU.value
  448. const bindDevices = this.gpuOpt.filter(o => val.includes(o.id))
  449. const gpuTypesSet = new Set()
  450. const lastGpu = this.gpuOpt.find(o => o.id === val[val.length - 1])
  451. bindDevices.forEach(o => {
  452. gpuTypesSet.add(o.dev_type.endsWith(vgpuType) ? vgpuType : 'GPU')
  453. })
  454. if (lastGpu.dev_type === 'LEGACY-VGPU' || gpuTypesSet.size > 1) {
  455. this.$nextTick(() => {
  456. this.form.fc.setFieldsValue({ device: [lastGpu.id] })
  457. })
  458. }
  459. }
  460. },
  461. },
  462. }
  463. </script>