]> git.proxmox.com Git - proxmox-widget-toolkit.git/blob - src/node/APTRepositories.js
tree-wide typo fixes
[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 validator: function(renderedValue) {
64 let handle = this.value;
65 // we cannot use this.callParent in instantiations
66 let valid = Proxmox.form.KVComboBox.prototype.validator.call(this, renderedValue);
67
68 if (!valid || !handle) {
69 return false;
70 }
71
72 const info = me.repoInfo.find(elem => elem.handle === handle);
73 if (!info) {
74 return false;
75 }
76
77 if (info.status) {
78 return Ext.String.format(gettext('{0} is already configured'), renderedValue);
79 }
80 return valid;
81 },
82 listeners: {
83 change: function(f, value) {
84 const info = me.repoInfo.find(elem => elem.handle === value);
85 description.setValue(info.description);
86 status.setValue(info.status);
87 },
88 },
89 });
90
91 repoSelector.setValue(me.repoInfo[0].handle);
92
93 Ext.apply(me, {
94 items: [
95 repoSelector,
96 description,
97 status,
98 ],
99 repoSelector: repoSelector,
100 });
101
102 me.callParent();
103 },
104 });
105
106 Ext.define('Proxmox.node.APTRepositoriesErrors', {
107 extend: 'Ext.grid.GridPanel',
108
109 xtype: 'proxmoxNodeAPTRepositoriesErrors',
110
111 store: {},
112
113 scrollable: true,
114
115 viewConfig: {
116 stripeRows: false,
117 getRowClass: (record) => {
118 switch (record.data.status) {
119 case 'warning': return 'proxmox-warning-row';
120 case 'critical': return 'proxmox-invalid-row';
121 default: return '';
122 }
123 },
124 },
125
126 hideHeaders: true,
127
128 columns: [
129 {
130 dataIndex: 'status',
131 renderer: (value) => `<i class="fa fa-fw ${Proxmox.Utils.get_health_icon(value, true)}"></i>`,
132 width: 50,
133 },
134 {
135 dataIndex: 'message',
136 flex: 1,
137 },
138 ],
139 });
140
141 Ext.define('Proxmox.node.APTRepositoriesGrid', {
142 extend: 'Ext.grid.GridPanel',
143 xtype: 'proxmoxNodeAPTRepositoriesGrid',
144 mixins: ['Proxmox.Mixin.CBind'],
145
146 title: gettext('APT Repositories'),
147
148 cls: 'proxmox-apt-repos', // to allow applying styling to general components with local effect
149
150 border: false,
151
152 tbar: [
153 {
154 text: gettext('Reload'),
155 iconCls: 'fa fa-refresh',
156 handler: function() {
157 let me = this;
158 me.up('proxmoxNodeAPTRepositories').reload();
159 },
160 },
161 {
162 text: gettext('Add'),
163 name: 'addRepo',
164 disabled: true,
165 repoInfo: undefined,
166 cbind: {
167 onlineHelp: '{onlineHelp}',
168 },
169 handler: function(button, event, record) {
170 Proxmox.Utils.checked_command(() => {
171 let me = this;
172 let panel = me.up('proxmoxNodeAPTRepositories');
173
174 let extraParams = {};
175 if (panel.digest !== undefined) {
176 extraParams.digest = panel.digest;
177 }
178
179 Ext.create('Proxmox.window.APTRepositoryAdd', {
180 repoInfo: me.repoInfo,
181 url: `/api2/extjs/nodes/${panel.nodename}/apt/repositories`,
182 method: 'PUT',
183 extraRequestParams: extraParams,
184 onlineHelp: me.onlineHelp,
185 listeners: {
186 destroy: function() {
187 panel.reload();
188 },
189 },
190 }).show();
191 });
192 },
193 },
194 '-',
195 {
196 xtype: 'proxmoxButton',
197 text: gettext('Enable'),
198 defaultText: gettext('Enable'),
199 altText: gettext('Disable'),
200 name: 'repoEnable',
201 disabled: true,
202 bind: {
203 text: '{enableButtonText}',
204 },
205 handler: function(button, event, record) {
206 let me = this;
207 let panel = me.up('proxmoxNodeAPTRepositories');
208
209 let params = {
210 path: record.data.Path,
211 index: record.data.Index,
212 enabled: record.data.Enabled ? 0 : 1, // invert
213 };
214
215 if (panel.digest !== undefined) {
216 params.digest = panel.digest;
217 }
218
219 Proxmox.Utils.API2Request({
220 url: `/nodes/${panel.nodename}/apt/repositories`,
221 method: 'POST',
222 params: params,
223 failure: function(response, opts) {
224 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
225 panel.reload();
226 },
227 success: function(response, opts) {
228 panel.reload();
229 },
230 });
231 },
232 listeners: {
233 render: function(btn) {
234 // HACK: calculate the max button width on first render to avoid toolbar glitches
235 let defSize = btn.getSize().width;
236
237 btn.setText(btn.altText);
238 let altSize = btn.getSize().width;
239
240 btn.setText(btn.defaultText);
241 btn.setSize({ width: altSize > defSize ? altSize : defSize });
242 },
243 },
244 },
245 ],
246
247 sortableColumns: false,
248 viewConfig: {
249 stripeRows: false,
250 getRowClass: (record, index) => record.get('Enabled') ? '' : 'proxmox-disabled-row',
251 },
252
253 columns: [
254 {
255 xtype: 'checkcolumn',
256 header: gettext('Enabled'),
257 dataIndex: 'Enabled',
258 listeners: {
259 beforecheckchange: () => false, // veto, we don't want to allow inline change - to subtle
260 },
261 width: 90,
262 },
263 {
264 header: gettext('Types'),
265 dataIndex: 'Types',
266 renderer: function(types, cell, record) {
267 return types.join(' ');
268 },
269 width: 100,
270 },
271 {
272 header: gettext('URIs'),
273 dataIndex: 'URIs',
274 renderer: function(uris, cell, record) {
275 return uris.join(' ');
276 },
277 width: 350,
278 },
279 {
280 header: gettext('Suites'),
281 dataIndex: 'Suites',
282 renderer: function(suites, metaData, record) {
283 let err = '';
284 if (record.data.warnings && record.data.warnings.length > 0) {
285 let txt = [gettext('Warning')];
286 record.data.warnings.forEach((warning) => {
287 if (warning.property === 'Suites') {
288 txt.push(warning.message);
289 }
290 });
291 metaData.tdAttr = `data-qtip="${Ext.htmlEncode(txt.join('<br>'))}"`;
292 if (record.data.Enabled) {
293 metaData.tdCls = 'proxmox-invalid-row';
294 err = '<i class="fa fa-fw critical fa-exclamation-circle"></i> ';
295 } else {
296 metaData.tdCls = 'proxmox-warning-row';
297 err = '<i class="fa fa-fw warning fa-exclamation-circle"></i> ';
298 }
299 }
300 return suites.join(' ') + err;
301 },
302 width: 130,
303 },
304 {
305 header: gettext('Components'),
306 dataIndex: 'Components',
307 renderer: function(components, metaData, record) {
308 if (components === undefined) {
309 return '';
310 }
311 let err = '';
312 if (components.length === 1) {
313 // FIXME: this should be a flag set to the actual repsotiories, i.e., a tristate
314 // like production-ready = <yes|no|other> (Option<bool>)
315 if (components[0].match(/\w+(-no-subscription|test)\s*$/i)) {
316 metaData.tdCls = 'proxmox-warning-row';
317 err = '<i class="fa fa-fw warning fa-exclamation-circle"></i> ';
318
319 let qtip = components[0].match(/no-subscription/)
320 ? gettext('The no-subscription repository is NOT production-ready')
321 : gettext('The test repository may contain unstable updates')
322 ;
323 metaData.tdAttr = `data-qtip="${Ext.htmlEncode(qtip)}"`;
324 }
325 }
326 return components.join(' ') + err;
327 },
328 width: 170,
329 },
330 {
331 header: gettext('Options'),
332 dataIndex: 'Options',
333 renderer: function(options, cell, record) {
334 if (!options) {
335 return '';
336 }
337
338 let filetype = record.data.FileType;
339 let text = '';
340
341 options.forEach(function(option) {
342 let key = option.Key;
343 if (filetype === 'list') {
344 let values = option.Values.join(',');
345 text += `${key}=${values} `;
346 } else if (filetype === 'sources') {
347 let values = option.Values.join(' ');
348 text += `${key}: ${values}<br>`;
349 } else {
350 throw "unknown file type";
351 }
352 });
353 return text;
354 },
355 flex: 1,
356 },
357 {
358 header: gettext('Origin'),
359 dataIndex: 'Origin',
360 width: 120,
361 renderer: (value, meta, rec) => {
362 if (typeof value !== 'string' || value.length === 0) {
363 value = gettext('Other');
364 }
365 let cls = 'fa fa-fw fa-question-circle-o';
366 if (value.match(/^\s*Proxmox\s*$/i)) {
367 cls = 'pmx-itype-icon pmx-itype-icon-proxmox-x';
368 } else if (value.match(/^\s*Debian\s*(:?Backports)?$/i)) {
369 cls = 'pmx-itype-icon pmx-itype-icon-debian-swirl';
370 }
371 return `<i class='${cls}'></i> ${value}`;
372 },
373 },
374 {
375 header: gettext('Comment'),
376 dataIndex: 'Comment',
377 flex: 2,
378 },
379 ],
380
381 features: [
382 {
383 ftype: 'grouping',
384 groupHeaderTpl: '{[ "File: " + values.name ]} ({rows.length} repositor{[values.rows.length > 1 ? "ies" : "y"]})',
385 enableGroupingMenu: false,
386 },
387 ],
388
389 store: {
390 model: 'apt-repolist',
391 groupField: 'Path',
392 sorters: [
393 {
394 property: 'Index',
395 direction: 'ASC',
396 },
397 ],
398 },
399
400 initComponent: function() {
401 let me = this;
402
403 if (!me.nodename) {
404 throw "no node name specified";
405 }
406
407 me.callParent();
408 },
409 });
410
411 Ext.define('Proxmox.node.APTRepositories', {
412 extend: 'Ext.panel.Panel',
413 xtype: 'proxmoxNodeAPTRepositories',
414 mixins: ['Proxmox.Mixin.CBind'],
415
416 digest: undefined,
417
418 onlineHelp: undefined,
419
420 product: 'Proxmox VE', // default
421
422 controller: {
423 xclass: 'Ext.app.ViewController',
424
425 selectionChange: function(grid, selection) {
426 let me = this;
427 if (!selection || selection.length < 1) {
428 return;
429 }
430 let rec = selection[0];
431 let vm = me.getViewModel();
432 vm.set('selectionenabled', rec.get('Enabled'));
433 vm.notify();
434 },
435
436 updateState: function() {
437 let me = this;
438 let vm = me.getViewModel();
439
440 let store = vm.get('errorstore');
441 store.removeAll();
442
443 let status = 'good'; // start with best, the helper below will downgrade if needed
444 let text = gettext('All OK, you have production-ready repositories configured!');
445
446 let addGood = message => store.add({ status: 'good', message });
447 let addWarn = (message, important) => {
448 if (status !== 'critical') {
449 status = 'warning';
450 text = important ? message : gettext('Warning');
451 }
452 store.add({ status: 'warning', message });
453 };
454 let addCritical = (message, important) => {
455 status = 'critical';
456 text = important ? message : gettext('Error');
457 store.add({ status: 'critical', message });
458 };
459
460 let errors = vm.get('errors');
461 errors.forEach(error => addCritical(`${error.path} - ${error.error}`));
462
463 let activeSubscription = vm.get('subscriptionActive');
464 let enterprise = vm.get('enterpriseRepo');
465 let nosubscription = vm.get('noSubscriptionRepo');
466 let test = vm.get('testRepo');
467 let wrongSuites = vm.get('suitesWarning');
468
469 if (!enterprise && !nosubscription && !test) {
470 addCritical(
471 Ext.String.format(gettext('No {0} repository is enabled, you do not get any updates!'), vm.get('product')),
472 );
473 } else if (errors.length > 0) {
474 // nothing extra, just avoid that we show "get updates"
475 } else if (enterprise && !nosubscription && !test && activeSubscription) {
476 addGood(Ext.String.format(gettext('You get supported updates for {0}'), vm.get('product')));
477 } else if (nosubscription || test) {
478 addGood(Ext.String.format(gettext('You get updates for {0}'), vm.get('product')));
479 }
480
481 if (wrongSuites) {
482 addWarn(gettext('Some suites are misconfigured'));
483 }
484
485 if (!activeSubscription && enterprise) {
486 addWarn(gettext('The enterprise repository is enabled, but there is no active subscription!'));
487 }
488
489 if (nosubscription) {
490 addWarn(gettext('The no-subscription repository is not recommended for production use!'));
491 }
492
493 if (test) {
494 addWarn(gettext('The test repository may pull in unstable updates and is not recommended for production use!'));
495 }
496
497 if (errors.length > 0) {
498 text = gettext('Fatal parsing error for at least one repository');
499 }
500
501 let iconCls = Proxmox.Utils.get_health_icon(status, true);
502
503 vm.set('state', {
504 iconCls,
505 text,
506 });
507 },
508 },
509
510 viewModel: {
511 data: {
512 product: 'Proxmox VE', // default
513 errors: [],
514 suitesWarning: false,
515 subscriptionActive: '',
516 noSubscriptionRepo: '',
517 enterpriseRepo: '',
518 testRepo: '',
519 selectionenabled: false,
520 state: {},
521 },
522 formulas: {
523 enableButtonText: (get) => get('selectionenabled')
524 ? gettext('Disable') : gettext('Enable'),
525 },
526 stores: {
527 errorstore: {
528 fields: ['status', 'message'],
529 },
530 },
531 },
532
533 scrollable: true,
534 layout: {
535 type: 'vbox',
536 align: 'stretch',
537 },
538
539 items: [
540 {
541 xtype: 'panel',
542 border: false,
543 layout: {
544 type: 'hbox',
545 align: 'stretch',
546 },
547 height: 200,
548 title: gettext('Status'),
549 items: [
550 {
551 xtype: 'box',
552 flex: 2,
553 margin: 10,
554 data: {
555 iconCls: Proxmox.Utils.get_health_icon(undefined, true),
556 text: '',
557 },
558 bind: {
559 data: '{state}',
560 },
561 tpl: [
562 '<center class="centered-flex-column" style="font-size:15px;line-height: 25px;">',
563 '<i class="fa fa-4x {iconCls}"></i>',
564 '{text}',
565 '</center>',
566 ],
567 },
568 {
569 xtype: 'proxmoxNodeAPTRepositoriesErrors',
570 name: 'repositoriesErrors',
571 flex: 7,
572 margin: 10,
573 bind: {
574 store: '{errorstore}',
575 },
576 },
577 ],
578 },
579 {
580 xtype: 'proxmoxNodeAPTRepositoriesGrid',
581 name: 'repositoriesGrid',
582 flex: 1,
583 cbind: {
584 nodename: '{nodename}',
585 onlineHelp: '{onlineHelp}',
586 },
587 majorUpgradeAllowed: false, // TODO get release information from an API call?
588 listeners: {
589 selectionchange: 'selectionChange',
590 },
591 },
592 ],
593
594 check_subscription: function() {
595 let me = this;
596 let vm = me.getViewModel();
597
598 Proxmox.Utils.API2Request({
599 url: `/nodes/${me.nodename}/subscription`,
600 method: 'GET',
601 failure: (response, opts) => Ext.Msg.alert(gettext('Error'), response.htmlStatus),
602 success: function(response, opts) {
603 const res = response.result;
604 const subscription = !(!res || !res.data || res.data.status.toLowerCase() !== 'active');
605 vm.set('subscriptionActive', subscription);
606 me.getController().updateState();
607 },
608 });
609 },
610
611 updateStandardRepos: function(standardRepos) {
612 let me = this;
613 let vm = me.getViewModel();
614
615 let addButton = me.down('button[name=addRepo]');
616
617 addButton.repoInfo = [];
618 for (const standardRepo of standardRepos) {
619 const handle = standardRepo.handle;
620 const status = standardRepo.status;
621
622 if (handle === "enterprise") {
623 vm.set('enterpriseRepo', status);
624 } else if (handle === "no-subscription") {
625 vm.set('noSubscriptionRepo', status);
626 } else if (handle === 'test') {
627 vm.set('testRepo', status);
628 }
629 me.getController().updateState();
630
631 addButton.repoInfo.push(standardRepo);
632 addButton.digest = me.digest;
633 }
634
635 addButton.setDisabled(false);
636 },
637
638 reload: function() {
639 let me = this;
640 let vm = me.getViewModel();
641 let repoGrid = me.down('proxmoxNodeAPTRepositoriesGrid');
642
643 me.store.load(function(records, operation, success) {
644 let gridData = [];
645 let errors = [];
646 let digest;
647 let suitesWarning = false;
648
649 if (success && records.length > 0) {
650 let data = records[0].data;
651 let files = data.files;
652 errors = data.errors;
653 digest = data.digest;
654
655 let infos = {};
656 for (const info of data.infos) {
657 let path = info.path;
658 let idx = info.index;
659
660 if (!infos[path]) {
661 infos[path] = {};
662 }
663 if (!infos[path][idx]) {
664 infos[path][idx] = {
665 origin: '',
666 warnings: [],
667 };
668 }
669
670 if (info.kind === 'origin') {
671 infos[path][idx].origin = info.message;
672 } else if (info.kind === 'warning' ||
673 (info.kind === 'ignore-pre-upgrade-warning' && !repoGrid.majorUpgradeAllowed)
674 ) {
675 infos[path][idx].warnings.push(info);
676 } else {
677 throw 'unknown info';
678 }
679 }
680
681
682 files.forEach(function(file) {
683 for (let n = 0; n < file.repositories.length; n++) {
684 let repo = file.repositories[n];
685 repo.Path = file.path;
686 repo.Index = n;
687 if (infos[file.path] && infos[file.path][n]) {
688 repo.Origin = infos[file.path][n].origin || Proxmox.Utils.UnknownText;
689 repo.warnings = infos[file.path][n].warnings || [];
690
691 if (repo.Enabled && repo.warnings.some(w => w.property === 'Suites')) {
692 suitesWarning = true;
693 }
694 }
695 gridData.push(repo);
696 }
697 });
698
699 repoGrid.store.loadData(gridData);
700
701 me.updateStandardRepos(data['standard-repos']);
702 }
703
704 me.digest = digest;
705
706 vm.set('errors', errors);
707 vm.set('suitesWarning', suitesWarning);
708 me.getController().updateState();
709 });
710
711 me.check_subscription();
712 },
713
714 listeners: {
715 activate: function() {
716 let me = this;
717 me.reload();
718 },
719 },
720
721 initComponent: function() {
722 let me = this;
723
724 if (!me.nodename) {
725 throw "no node name specified";
726 }
727
728 let store = Ext.create('Ext.data.Store', {
729 proxy: {
730 type: 'proxmox',
731 url: `/api2/json/nodes/${me.nodename}/apt/repositories`,
732 },
733 });
734
735 Ext.apply(me, { store: store });
736
737 Proxmox.Utils.monStoreErrors(me, me.store, true);
738
739 me.callParent();
740
741 me.getViewModel().set('product', me.product);
742 },
743 });