]> git.proxmox.com Git - proxmox-widget-toolkit.git/blob - src/node/APTRepositories.js
c5c47f4d806e5da7f471276ae752a7f9cb610c23
[proxmox-widget-toolkit.git] / src / node / APTRepositories.js
1 Ext.define('apt-repolist', {
2 extend: 'Ext.data.Model',
3 fields: [
4 'Path',
5 'Index',
6 'Origin',
7 'FileType',
8 'Enabled',
9 'Comment',
10 'Types',
11 'URIs',
12 'Suites',
13 'Components',
14 'Options',
15 ],
16 });
17
18 Ext.define('Proxmox.window.APTRepositoryAdd', {
19 extend: 'Proxmox.window.Edit',
20 alias: 'widget.pmxAPTRepositoryAdd',
21
22 isCreate: true,
23 isAdd: true,
24
25 subject: gettext('Repository'),
26 width: 600,
27
28 initComponent: function() {
29 let me = this;
30
31 if (!me.repoInfo || me.repoInfo.length === 0) {
32 throw "repository information not initialized";
33 }
34
35 let description = Ext.create('Ext.form.field.Display', {
36 fieldLabel: gettext('Description'),
37 name: 'description',
38 });
39
40 let status = Ext.create('Ext.form.field.Display', {
41 fieldLabel: gettext('Status'),
42 name: 'status',
43 renderer: function(value) {
44 let statusText = gettext('Not yet configured');
45 if (value !== '') {
46 statusText = Ext.String.format(
47 '{0}: {1}',
48 gettext('Configured'),
49 value ? gettext('enabled') : gettext('disabled'),
50 );
51 }
52
53 return statusText;
54 },
55 });
56
57 let repoSelector = Ext.create('Proxmox.form.KVComboBox', {
58 fieldLabel: gettext('Repository'),
59 xtype: 'proxmoxKVComboBox',
60 name: 'handle',
61 allowBlank: false,
62 comboItems: me.repoInfo.map(info => [info.handle, info.name]),
63 isValid: function() {
64 const handle = this.value;
65
66 if (!handle) {
67 return false;
68 }
69
70 const info = me.repoInfo.find(elem => elem.handle === handle);
71
72 if (!info) {
73 return false;
74 }
75
76 // not yet configured
77 return info.status === undefined || info.status === null;
78 },
79 listeners: {
80 change: function(f, value) {
81 const info = me.repoInfo.find(elem => elem.handle === value);
82 description.setValue(info.description);
83 status.setValue(info.status);
84 },
85 },
86 });
87
88 repoSelector.setValue(me.repoInfo[0].handle);
89
90 Ext.apply(me, {
91 items: [
92 repoSelector,
93 description,
94 status,
95 ],
96 repoSelector: repoSelector,
97 });
98
99 me.callParent();
100 },
101 });
102
103 Ext.define('Proxmox.node.APTRepositoriesErrors', {
104 extend: 'Ext.grid.GridPanel',
105
106 xtype: 'proxmoxNodeAPTRepositoriesErrors',
107
108 title: gettext('Errors'),
109
110 store: {},
111
112 border: false,
113
114 viewConfig: {
115 stripeRows: false,
116 getRowClass: () => 'proxmox-invalid-row',
117 },
118
119 columns: [
120 {
121 header: gettext('File'),
122 dataIndex: 'path',
123 renderer: value => `<i class='pve-grid-fa fa fa-fw fa-exclamation-triangle'></i>${value}`,
124 width: 350,
125 },
126 {
127 header: gettext('Error'),
128 dataIndex: 'error',
129 flex: 1,
130 },
131 ],
132 });
133
134 Ext.define('Proxmox.node.APTRepositoriesGrid', {
135 extend: 'Ext.grid.GridPanel',
136 xtype: 'proxmoxNodeAPTRepositoriesGrid',
137
138 title: gettext('APT Repositories'),
139
140 cls: 'proxmox-apt-repos', // to allow applying styling to general components with local effect
141
142 border: false,
143
144 tbar: [
145 {
146 text: gettext('Reload'),
147 iconCls: 'fa fa-refresh',
148 handler: function() {
149 let me = this;
150 me.up('proxmoxNodeAPTRepositories').reload();
151 },
152 },
153 {
154 text: gettext('Add'),
155 id: 'addButton',
156 disabled: true,
157 repoInfo: undefined,
158 handler: function(button, event, record) {
159 Proxmox.Utils.checked_command(() => {
160 let me = this;
161 let panel = me.up('proxmoxNodeAPTRepositories');
162
163 let extraParams = {};
164 if (panel.digest !== undefined) {
165 extraParams.digest = panel.digest;
166 }
167
168 Ext.create('Proxmox.window.APTRepositoryAdd', {
169 repoInfo: me.repoInfo,
170 url: `/api2/json/nodes/${panel.nodename}/apt/repositories`,
171 method: 'PUT',
172 extraRequestParams: extraParams,
173 listeners: {
174 destroy: function() {
175 panel.reload();
176 },
177 },
178 }).show();
179 });
180 },
181 },
182 '-',
183 {
184 xtype: 'proxmoxButton',
185 text: gettext('Enable'),
186 defaultText: gettext('Enable'),
187 altText: gettext('Disable'),
188 id: 'repoEnableButton',
189 disabled: true,
190 bind: {
191 text: '{enableButtonText}',
192 },
193 handler: function(button, event, record) {
194 let me = this;
195 let panel = me.up('proxmoxNodeAPTRepositories');
196
197 let params = {
198 path: record.data.Path,
199 index: record.data.Index,
200 enabled: record.data.Enabled ? 0 : 1, // invert
201 };
202
203 if (panel.digest !== undefined) {
204 params.digest = panel.digest;
205 }
206
207 Proxmox.Utils.API2Request({
208 url: `/nodes/${panel.nodename}/apt/repositories`,
209 method: 'POST',
210 params: params,
211 failure: function(response, opts) {
212 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
213 panel.reload();
214 },
215 success: function(response, opts) {
216 panel.reload();
217 },
218 });
219 },
220 listeners: {
221 render: function(btn) {
222 // HACK: calculate the max button width on first render to avoid toolbar glitches
223 let defSize = btn.getSize().width;
224
225 btn.setText(btn.altText);
226 let altSize = btn.getSize().width;
227
228 btn.setText(btn.defaultText);
229 btn.setSize({ width: altSize > defSize ? altSize : defSize });
230 },
231 },
232 },
233 ],
234
235 sortableColumns: false,
236 viewConfig: {
237 stripeRows: false,
238 getRowClass: (record, index) => record.get('Enabled') ? '' : 'proxmox-disabled-row',
239 },
240
241 columns: [
242 {
243 xtype: 'checkcolumn',
244 header: gettext('Enabled'),
245 dataIndex: 'Enabled',
246 listeners: {
247 beforecheckchange: () => false, // veto, we don't want to allow inline change - to subtle
248 },
249 width: 90,
250 },
251 {
252 header: gettext('Types'),
253 dataIndex: 'Types',
254 renderer: function(types, cell, record) {
255 return types.join(' ');
256 },
257 width: 100,
258 },
259 {
260 header: gettext('URIs'),
261 dataIndex: 'URIs',
262 renderer: function(uris, cell, record) {
263 return uris.join(' ');
264 },
265 width: 350,
266 },
267 {
268 header: gettext('Suites'),
269 dataIndex: 'Suites',
270 renderer: function(suites, metaData, record) {
271 let err = '';
272 if (record.data.warnings && record.data.warnings.length > 0) {
273 let txt = [gettext('Warning')];
274 record.data.warnings.forEach((warning) => {
275 if (warning.property === 'Suites') {
276 txt.push(warning.message);
277 }
278 });
279 metaData.tdAttr = `data-qtip="${Ext.htmlEncode(txt.join('<br>'))}"`;
280 if (record.data.Enabled) {
281 metaData.tdCls = 'proxmox-invalid-row';
282 err = '<i class="fa fa-fw critical fa-exclamation-circle"></i> ';
283 } else {
284 metaData.tdCls = 'proxmox-warning-row';
285 err = '<i class="fa fa-fw warning fa-exclamation-circle"></i> ';
286 }
287 }
288 return suites.join(' ') + err;
289 },
290 width: 130,
291 },
292 {
293 header: gettext('Components'),
294 dataIndex: 'Components',
295 renderer: function(components, cell, record) {
296 return components.join(' ');
297 },
298 width: 170,
299 },
300 {
301 header: gettext('Options'),
302 dataIndex: 'Options',
303 renderer: function(options, cell, record) {
304 if (!options) {
305 return '';
306 }
307
308 let filetype = record.data.FileType;
309 let text = '';
310
311 options.forEach(function(option) {
312 let key = option.Key;
313 if (filetype === 'list') {
314 let values = option.Values.join(',');
315 text += `${key}=${values} `;
316 } else if (filetype === 'sources') {
317 let values = option.Values.join(' ');
318 text += `${key}: ${values}<br>`;
319 } else {
320 throw "unkown file type";
321 }
322 });
323 return text;
324 },
325 flex: 1,
326 },
327 {
328 header: gettext('Origin'),
329 dataIndex: 'Origin',
330 width: 120,
331 renderer: (value, meta, rec) => {
332 let cls = 'fa fa-fw fa-question-circle-o';
333 if (value.match(/^\s*Proxmox\s*$/i)) {
334 cls = 'pmx-itype-icon pmx-itype-icon-proxmox-x';
335 } else if (value.match(/^\s*Debian\s*$/i)) {
336 cls = 'pmx-itype-icon pmx-itype-icon-debian-swirl';
337 }
338 return `<i class='${cls}'></i> ${value}`;
339 },
340 },
341 {
342 header: gettext('Comment'),
343 dataIndex: 'Comment',
344 flex: 2,
345 },
346 ],
347
348 initComponent: function() {
349 let me = this;
350
351 if (!me.nodename) {
352 throw "no node name specified";
353 }
354
355 let store = Ext.create('Ext.data.Store', {
356 model: 'apt-repolist',
357 groupField: 'Path',
358 sorters: [
359 {
360 property: 'Index',
361 direction: 'ASC',
362 },
363 ],
364 });
365
366 let groupingFeature = Ext.create('Ext.grid.feature.Grouping', {
367 groupHeaderTpl: '{[ "File: " + values.name ]} ({rows.length} ' +
368 'repositor{[values.rows.length > 1 ? "ies" : "y"]})',
369 enableGroupingMenu: false,
370 });
371
372 Ext.apply(me, {
373 store: store,
374 features: [groupingFeature],
375 });
376
377 me.callParent();
378 },
379 });
380
381 Ext.define('Proxmox.node.APTRepositories', {
382 extend: 'Ext.panel.Panel',
383 xtype: 'proxmoxNodeAPTRepositories',
384 mixins: ['Proxmox.Mixin.CBind'],
385
386 digest: undefined,
387
388 product: 'Proxmox VE', // default
389
390 controller: {
391 xclass: 'Ext.app.ViewController',
392
393 selectionChange: function(grid, selection) {
394 let me = this;
395 if (!selection || selection.length < 1) {
396 return;
397 }
398 let rec = selection[0];
399 let vm = me.getViewModel();
400 vm.set('selectionenabled', rec.get('Enabled'));
401 },
402 },
403
404 viewModel: {
405 data: {
406 product: 'Proxmox VE', // default
407 errorCount: 0,
408 subscriptionActive: '',
409 noSubscriptionRepo: '',
410 enterpriseRepo: '',
411 selectionenabled: false,
412 },
413 formulas: {
414 noErrors: (get) => get('errorCount') === 0,
415 enableButtonText: (get) => get('selectionenabled')
416 ? gettext('Disable') : gettext('Enable'),
417 mainWarning: function(get) {
418 // Not yet initialized
419 if (get('subscriptionActive') === '' ||
420 get('enterpriseRepo') === '') {
421 return '';
422 }
423
424 let icon = `<i class='fa fa-fw fa-exclamation-triangle critical'></i>`;
425 let fmt = (msg) => `<div class="black">${icon}${gettext('Warning')}: ${msg}</div>`;
426
427 if (!get('subscriptionActive') && get('enterpriseRepo')) {
428 return fmt(gettext('The enterprise repository is enabled, but there is no active subscription!'));
429 }
430
431 if (get('noSubscriptionRepo')) {
432 return fmt(gettext('The no-subscription repository is not recommended for production use!'));
433 }
434
435 if (!get('enterpriseRepo') && !get('noSubscriptionRepo')) {
436 let msg = Ext.String.format(gettext('No {0} repository is enabled!'), get('product'));
437 return fmt(msg);
438 }
439
440 return '';
441 },
442 },
443 },
444
445 scrollable: true,
446 layout: {
447 type: 'vbox',
448 align: 'stretch',
449 },
450
451 items: [
452 {
453 xtype: 'header',
454 baseCls: 'x-panel-header',
455 bind: {
456 hidden: '{!mainWarning}',
457 title: '{mainWarning}',
458 },
459 },
460 {
461 xtype: 'box',
462 bind: {
463 hidden: '{!mainWarning}',
464 },
465 height: 5,
466 },
467 {
468 xtype: 'proxmoxNodeAPTRepositoriesErrors',
469 name: 'repositoriesErrors',
470 hidden: true,
471 padding: '0 0 5 0',
472 bind: {
473 hidden: '{noErrors}',
474 },
475 },
476 {
477 xtype: 'proxmoxNodeAPTRepositoriesGrid',
478 name: 'repositoriesGrid',
479 cbind: {
480 nodename: '{nodename}',
481 },
482 majorUpgradeAllowed: false, // TODO get release information from an API call?
483 listeners: {
484 selectionchange: 'selectionChange',
485 },
486 },
487 ],
488
489 check_subscription: function() {
490 let me = this;
491 let vm = me.getViewModel();
492
493 Proxmox.Utils.API2Request({
494 url: `/nodes/${me.nodename}/subscription`,
495 method: 'GET',
496 failure: (response, opts) => Ext.Msg.alert(gettext('Error'), response.htmlStatus),
497 success: function(response, opts) {
498 const res = response.result;
499 const subscription = !(!res || !res.data || res.data.status.toLowerCase() !== 'active');
500 vm.set('subscriptionActive', subscription);
501 },
502 });
503 },
504
505 updateStandardRepos: function(standardRepos) {
506 let me = this;
507 let vm = me.getViewModel();
508
509 let addButton = me.down('#addButton');
510 addButton.repoInfo = [];
511
512 for (const standardRepo of standardRepos) {
513 const handle = standardRepo.handle;
514 const status = standardRepo.status;
515
516 if (handle === "enterprise") {
517 vm.set('enterpriseRepo', status);
518 } else if (handle === "no-subscription") {
519 vm.set('noSubscriptionRepo', status);
520 }
521
522 addButton.repoInfo.push(standardRepo);
523 addButton.digest = me.digest;
524 }
525
526 addButton.setDisabled(false);
527 },
528
529 reload: function() {
530 let me = this;
531 let vm = me.getViewModel();
532 let repoGrid = me.down('proxmoxNodeAPTRepositoriesGrid');
533 let errorGrid = me.down('proxmoxNodeAPTRepositoriesErrors');
534
535 me.store.load(function(records, operation, success) {
536 let gridData = [];
537 let errors = [];
538 let digest;
539
540 if (success && records.length > 0) {
541 let data = records[0].data;
542 let files = data.files;
543 errors = data.errors;
544 digest = data.digest;
545
546 let infos = {};
547 for (const info of data.infos) {
548 let path = info.path;
549 let idx = info.index;
550
551 if (!infos[path]) {
552 infos[path] = {};
553 }
554 if (!infos[path][idx]) {
555 infos[path][idx] = {
556 origin: '',
557 warnings: [],
558 };
559 }
560
561 if (info.kind === 'origin') {
562 infos[path][idx].origin = info.message;
563 } else if (info.kind === 'warning' ||
564 (info.kind === 'ignore-pre-upgrade-warning' && !repoGrid.majorUpgradeAllowed)
565 ) {
566 infos[path][idx].warnings.push(info);
567 } else {
568 throw 'unknown info';
569 }
570 }
571
572
573 files.forEach(function(file) {
574 for (let n = 0; n < file.repositories.length; n++) {
575 let repo = file.repositories[n];
576 repo.Path = file.path;
577 repo.Index = n;
578 if (infos[file.path] && infos[file.path][n]) {
579 repo.Origin = infos[file.path][n].origin || Proxmox.Utils.UnknownText;
580 repo.warnings = infos[file.path][n].warnings || [];
581 }
582 gridData.push(repo);
583 }
584 });
585
586 repoGrid.store.loadData(gridData);
587
588 me.updateStandardRepos(data['standard-repos']);
589 }
590
591 me.digest = digest;
592
593 vm.set('errorCount', errors.length);
594 errorGrid.store.loadData(errors);
595 });
596
597 me.check_subscription();
598 },
599
600 listeners: {
601 activate: function() {
602 let me = this;
603 me.reload();
604 },
605 },
606
607 initComponent: function() {
608 let me = this;
609
610 if (!me.nodename) {
611 throw "no node name specified";
612 }
613
614 let store = Ext.create('Ext.data.Store', {
615 proxy: {
616 type: 'proxmox',
617 url: `/api2/json/nodes/${me.nodename}/apt/repositories`,
618 },
619 });
620
621 Ext.apply(me, { store: store });
622
623 Proxmox.Utils.monStoreErrors(me, me.store, true);
624
625 me.callParent();
626
627 me.getViewModel().set('product', me.product);
628 },
629 });