blob: 5460eb4655e87452d9ff77019d04882979f693f5 [file] [log] [blame]
Mateusz Gapski75100462020-07-30 11:01:29 +02001<template>
2 <b-container fluid="xl">
3 <page-title />
4 <b-row class="mb-4">
5 <b-col md="12">
6 <page-section
7 :section-title="$t('pageVirtualMedia.virtualMediaSubTitleFirst')"
8 >
9 <b-row>
10 <b-col v-for="(dev, $index) in proxyDevices" :key="$index" md="6">
11 <b-form-group
12 :label="dev.id"
13 :label-for="dev.id"
14 label-class="bold"
15 >
16 <b-form-file
17 v-show="!dev.isActive"
18 :id="dev.id"
19 v-model="dev.file"
20 />
21 <p v-if="dev.isActive">{{ dev.file.name }}</p>
22 </b-form-group>
23 <b-button
24 v-if="!dev.isActive"
25 variant="primary"
26 :disabled="!dev.file"
27 @click="startVM(dev)"
28 >
Mateusz Gapski2224ece2020-09-02 17:00:06 +020029 {{ $t('pageVirtualMedia.start') }}
Mateusz Gapski75100462020-07-30 11:01:29 +020030 </b-button>
31 <b-button
32 v-if="dev.isActive"
33 variant="primary"
34 :disabled="!dev.file"
35 @click="stopVM(dev)"
36 >
Mateusz Gapski2224ece2020-09-02 17:00:06 +020037 {{ $t('pageVirtualMedia.stop') }}
Mateusz Gapski75100462020-07-30 11:01:29 +020038 </b-button>
39 </b-col>
40 </b-row>
41 </page-section>
42 </b-col>
43 </b-row>
44 <b-row v-if="loadImageFromExternalServer" class="mb-4">
45 <b-col md="12">
46 <page-section
47 :section-title="$t('pageVirtualMedia.virtualMediaSubTitleSecond')"
48 >
49 <b-row>
50 <b-col
51 v-for="(device, $index) in legacyDevices"
52 :key="$index"
53 md="6"
54 >
55 <b-form-group
56 :label="device.id"
57 :label-for="device.id"
58 label-class="bold"
59 >
Mateusz Gapski2224ece2020-09-02 17:00:06 +020060 <b-button
61 variant="primary"
62 :disabled="device.isActive"
63 @click="configureConnection(device)"
64 >
Mateusz Gapski75100462020-07-30 11:01:29 +020065 {{ $t('pageVirtualMedia.configureConnection') }}
66 </b-button>
67
68 <b-button
Mateusz Gapski2224ece2020-09-02 17:00:06 +020069 v-if="!device.isActive"
Mateusz Gapski75100462020-07-30 11:01:29 +020070 variant="primary"
71 class="float-right"
Mateusz Gapski2224ece2020-09-02 17:00:06 +020072 :disabled="!device.serverUri"
Mateusz Gapski75100462020-07-30 11:01:29 +020073 @click="startLegacy(device)"
74 >
Mateusz Gapski2224ece2020-09-02 17:00:06 +020075 {{ $t('pageVirtualMedia.start') }}
76 </b-button>
77 <b-button
78 v-if="device.isActive"
79 variant="primary"
80 class="float-right"
81 @click="stopLegacy(device)"
82 >
83 {{ $t('pageVirtualMedia.stop') }}
Mateusz Gapski75100462020-07-30 11:01:29 +020084 </b-button>
85 </b-form-group>
86 </b-col>
87 </b-row>
88 </page-section>
89 </b-col>
90 </b-row>
Mateusz Gapski2224ece2020-09-02 17:00:06 +020091 <modal-configure-connection
92 :connection="modalConfigureConnection"
93 @ok="saveConnection"
94 />
Mateusz Gapski75100462020-07-30 11:01:29 +020095 </b-container>
96</template>
97
98<script>
99import PageTitle from '@/components/Global/PageTitle';
100import PageSection from '@/components/Global/PageSection';
101import BVToastMixin from '@/components/Mixins/BVToastMixin';
102import LoadingBarMixin from '@/components/Mixins/LoadingBarMixin';
Mateusz Gapski2224ece2020-09-02 17:00:06 +0200103import ModalConfigureConnection from './ModalConfigureConnection';
Mateusz Gapski75100462020-07-30 11:01:29 +0200104import NbdServer from '@/utilities/NBDServer';
105
106export default {
107 name: 'VirtualMedia',
Mateusz Gapski2224ece2020-09-02 17:00:06 +0200108 components: { PageTitle, PageSection, ModalConfigureConnection },
Mateusz Gapski75100462020-07-30 11:01:29 +0200109 mixins: [BVToastMixin, LoadingBarMixin],
110 data() {
111 return {
Mateusz Gapski2224ece2020-09-02 17:00:06 +0200112 modalConfigureConnection: null,
Mateusz Gapski75100462020-07-30 11:01:29 +0200113 loadImageFromExternalServer:
114 process.env.VUE_APP_VIRTUAL_MEDIA_LIST_ENABLED === 'true' ? true : false
115 };
116 },
117 computed: {
118 proxyDevices() {
119 return this.$store.getters['virtualMedia/proxyDevices'];
120 },
121 legacyDevices() {
122 return this.$store.getters['virtualMedia/legacyDevices'];
123 }
124 },
125 created() {
126 if (this.proxyDevices.length > 0 || this.legacyDevices.length > 0) return;
127 this.startLoader();
128 this.$store
129 .dispatch('virtualMedia/getData')
130 .finally(() => this.endLoader());
131 },
132 methods: {
133 startVM(device) {
134 const token = this.$store.getters['authentication/token'];
135 device.nbd = new NbdServer(
136 `wss://${window.location.host}${device.websocket}`,
137 device.file,
138 device.id,
139 token
140 );
141 device.nbd.socketStarted = () =>
142 this.successToast(this.$t('pageVirtualMedia.toast.serverRunning'));
143 device.nbd.errorReadingFile = () =>
144 this.errorToast(this.$t('pageVirtualMedia.toast.errorReadingFile'));
145 device.nbd.socketClosed = code => {
146 if (code === 1000)
147 this.successToast(
148 this.$t('pageVirtualMedia.toast.serverClosedSuccessfully')
149 );
150 else
151 this.errorToast(
152 this.$t('pageVirtualMedia.toast.serverClosedWithErrors')
153 );
154 device.file = null;
155 device.isActive = false;
156 };
157
158 device.nbd.start();
159 device.isActive = true;
160 },
161 stopVM(device) {
162 device.nbd.stop();
163 },
Mateusz Gapski2224ece2020-09-02 17:00:06 +0200164 startLegacy(connectionData) {
165 var data = {};
166 data.Image = connectionData.serverUri;
167 data.UserName = connectionData.username;
168 data.Password = connectionData.password;
169 data.WriteProtected = connectionData.isRW;
170 this.startLoader();
171 this.$store
172 .dispatch('virtualMedia/mountImage', {
173 id: connectionData.id,
174 data: data
175 })
176 .then(() => {
177 this.successToast(
178 this.$t('pageVirtualMedia.toast.serverClosedSuccessfully')
179 );
180 connectionData.isActive = true;
181 })
182 .catch(() => {
183 this.errorToast(this.$t('pageVirtualMedia.toast.errorMounting'));
184 this.isActive = false;
185 })
186 .finally(() => this.endLoader());
Mateusz Gapski75100462020-07-30 11:01:29 +0200187 },
Mateusz Gapski2224ece2020-09-02 17:00:06 +0200188 stopLegacy(connectionData) {
189 this.$store
190 .dispatch('virtualMedia/unmountImage', connectionData.id)
191 .then(() => {
192 this.successToast(
193 this.$t('pageVirtualMedia.toast.serverClosedSuccessfully')
194 );
195 connectionData.isActive = false;
196 })
197 .catch(() =>
198 this.errorToast(this.$t('pageVirtualMedia.toast.errorUnmounting'))
199 )
200 .finally(() => this.endLoader());
201 },
202 saveConnection(connectionData) {
203 this.modalConfigureConnection.serverUri = connectionData.serverUri;
204 this.modalConfigureConnection.username = connectionData.username;
205 this.modalConfigureConnection.password = connectionData.password;
206 this.modalConfigureConnection.isRW = connectionData.isRW;
207 },
208 configureConnection(connectionData) {
209 this.modalConfigureConnection = connectionData;
210 this.$bvModal.show('configure-connection');
Mateusz Gapski75100462020-07-30 11:01:29 +0200211 }
212 }
213};
214</script>