From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id BFF531FF0B3 for ; Thu, 26 Nov 2026 14:42:36 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id B0D40216FE; Thu, 26 Nov 2026 14:42:06 +0100 (CET) From: Jonas Theisen To: pve-devel@lists.proxmox.com Subject: [PATCH widget-toolkit 06/16] partially fix #3809: node: time: show and edit NTP servers if supported Date: Thu, 26 Nov 2026 14:40:03 +0100 Message-ID: <20261126134128.316681-7-j.theisen@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261126134128.316681-1-j.theisen@proxmox.com> References: <20261126134128.316681-1-j.theisen@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1795700510769 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.069 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: NOU6FA536KNH5PFVQM6SSZXNM3X454XQ X-Message-ID-Hash: NOU6FA536KNH5PFVQM6SSZXNM3X454XQ X-MailFrom: j.theisen@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Add rows for the active NTP service and its custom sources to the time view, and a table to the edit window, where sources can be added and removed, each with a type selector for server or pool. They are only shown if the API reports a supported time synchronization service, as the components are shared with products whose time API does not provide NTP settings. The view shows the custom servers and pools together in one row, with pools marked as such. The API returns neither if the service uses its default sources, so the row gets an empty default value, which is shown as "Default". It needs a separate filter to stay hidden without a supported service, and an explicit sorter to keep the row order, as rows with a default value are added before the first load. The table is submitted as the separate server and pool parameters of the API. The type selector is hidden if the API reports that the service does not support pools, like systemd-timesyncd. Removing all sources resets the service to its default sources. As the edit window is no longer only about the time zone, change its title to "Time settings". Signed-off-by: Jonas Theisen --- src/node/TimeEdit.js | 285 ++++++++++++++++++++++++++++++++++++++++--- src/node/TimeView.js | 64 ++++++++-- 2 files changed, 322 insertions(+), 27 deletions(-) diff --git a/src/node/TimeEdit.js b/src/node/TimeEdit.js index 2238a41..9624973 100644 --- a/src/node/TimeEdit.js +++ b/src/node/TimeEdit.js @@ -1,28 +1,281 @@ +// Edits the custom NTP servers and pools as one list, submitted as the 'ntp-servers' and +// 'ntp-pools' parameters. An empty list resets the service to its default sources. +Ext.define('Proxmox.form.NTPSourceList', { + extend: 'Ext.form.FieldContainer', + alias: 'widget.proxmoxNTPSourceList', + + mixins: ['Ext.form.field.Field'], + + isFormField: true, + + // list of { type: 'server' | 'pool', address } + setValue: function (list) { + let me = this; + + // copy, as the store uses the objects as record data and adds an id to them, which would + // modify the original value used for the dirty tracking + me.lookup('grid') + .getStore() + .setData((list ?? []).map(({ type, address }) => ({ type, address }))); + me.checkChange(); + return me; + }, + + getValue: function () { + let me = this; + + let list = []; + me.lookup('grid') + .getStore() + .each((rec) => { + list.push({ type: rec.data.type, address: rec.data.address }); + }); + return list; + }, + + // the default compares the string representation, which is the same for all lists of objects + // with the same length + isEqual: function (value1, value2) { + return JSON.stringify(value1) === JSON.stringify(value2); + }, + + getErrors: function () { + let me = this; + + let errors = []; + me.lookup('grid') + .getStore() + .each((rec) => { + let address = rec.data.address ?? ''; + if (address === '' || !Ext.form.field.VTypes.DnsOrIp(address)) { + errors.push(Ext.form.field.VTypes.DnsOrIpText); + return false; + } + return true; + }); + return errors; + }, + + getSubmitData: function () { + let me = this; + + if (me.disabled || !me.submitValue) { + return null; + } + + let addresses = { 'ntp-servers': [], 'ntp-pools': [] }; + for (const { type, address } of me.getValue()) { + addresses[type === 'pool' ? 'ntp-pools' : 'ntp-servers'].push(address); + } + + let data = {}; + let deletes = []; + for (const [param, list] of Object.entries(addresses)) { + if (list.length) { + data[param] = list.join(','); + } else { + deletes.push(param); + } + } + if (deletes.length) { + data.delete = deletes; + } + return data; + }, + + // without pool support, all sources are servers, so there is nothing to choose + setPoolsSupported: function (supported) { + let me = this; + me.lookup('typeColumn').setHidden(!supported); + }, + + controller: { + xclass: 'Ext.app.ViewController', + + addSource: function () { + let me = this; + me.lookup('grid').getStore().add({ type: 'server', address: '' }); + me.getView().checkChange(); + me.getView().validate(); + }, + + removeSource: function (button) { + let me = this; + let record = button.getWidgetRecord(); + if (record === undefined) { + // this is sometimes called before a record/column is initialized + return; + } + me.lookup('grid').getStore().remove(record); + me.getView().checkChange(); + me.getView().validate(); + }, + + itemChange: function (field, newValue) { + let me = this; + let record = field.getWidgetRecord(); + if (!record) { + return; + } + record.set(field.getWidgetColumn().dataIndex, newValue); + me.getView().checkChange(); + me.getView().validate(); + }, + + control: { + 'grid button': { + click: 'removeSource', + }, + }, + }, + + initComponent: function () { + let me = this; + + Ext.apply(me, { + items: [ + { + xtype: 'grid', + reference: 'grid', + height: 150, + scrollable: 'vertical', + margin: '0 0 5 0', + viewConfig: { + deferEmptyText: false, + emptyText: gettext('Default sources of the service'), + }, + store: { + fields: ['type', 'address'], + listeners: { + update: function () { + this.commitChanges(); + }, + }, + }, + columns: [ + { + header: gettext('Type'), + reference: 'typeColumn', + dataIndex: 'type', + xtype: 'widgetcolumn', + width: 110, + widget: { + xtype: 'proxmoxKVComboBox', + isFormField: false, + comboItems: [ + ['server', gettext('Server')], + ['pool', gettext('Pool')], + ], + listeners: { + change: 'itemChange', + }, + }, + }, + { + header: gettext('Address'), + dataIndex: 'address', + xtype: 'widgetcolumn', + flex: 1, + widget: { + xtype: 'textfield', + isFormField: false, + allowBlank: false, + vtype: 'DnsOrIp', // matches the 'address' format of the API + emptyText: gettext('Host name or IP address'), + listeners: { + change: 'itemChange', + }, + }, + onWidgetAttach: function (_col, widget) { + widget.isValid(); + }, + }, + { + xtype: 'widgetcolumn', + width: 40, + widget: { + xtype: 'button', + iconCls: 'fa fa-trash-o', + }, + }, + ], + }, + { + xtype: 'button', + text: gettext('Add'), + iconCls: 'fa fa-plus-circle', + handler: 'addSource', + }, + ], + }); + + me.callParent(); + me.initField(); + }, +}); + Ext.define('Proxmox.node.TimeEdit', { extend: 'Proxmox.window.Edit', alias: ['widget.proxmoxNodeTimeEdit'], - subject: gettext('Time zone'), + subject: gettext('Time settings'), - width: 400, + width: 550, autoLoad: true, fieldDefaults: { - labelWidth: 70, - }, - - items: { - xtype: 'combo', - fieldLabel: gettext('Time zone'), - name: 'timezone', - queryMode: 'local', - store: Ext.create('Proxmox.data.TimezoneStore'), - displayField: 'zone', - editable: true, - anyMatch: true, - forceSelection: true, - allowBlank: false, + labelWidth: 90, + }, + + items: [ + { + xtype: 'combo', + fieldLabel: gettext('Time zone'), + name: 'timezone', + queryMode: 'local', + store: Ext.create('Proxmox.data.TimezoneStore'), + displayField: 'zone', + editable: true, + anyMatch: true, + forceSelection: true, + allowBlank: false, + }, + { + // shown once the loaded data says configuring NTP sources is supported + xtype: 'proxmoxNTPSourceList', + fieldLabel: gettext('NTP sources'), + name: 'ntp-sources', + hidden: true, + disabled: true, + }, + ], + + setValues: function (values) { + let me = this; + + let supported = !!values['ntp-service']; + let sourceList = me.down('proxmoxNTPSourceList'); + sourceList.setVisible(supported); + sourceList.setDisabled(!supported); + sourceList.setPoolsSupported(!!values['ntp-pools-supported']); + + // set through the parent, so that the loaded list becomes the original value for the dirty + // tracking and resetting of the form + let toList = (value, type) => + (value ?? '') + .split(',') + .filter((address) => address !== '') + .map((address) => ({ type, address })); + values = { + ...values, + 'ntp-sources': [ + ...toList(values['ntp-servers'], 'server'), + ...toList(values['ntp-pools'], 'pool'), + ], + }; + + me.callParent([values]); }, initComponent: function () { diff --git a/src/node/TimeView.js b/src/node/TimeView.js index 20214eb..35ac72f 100644 --- a/src/node/TimeView.js +++ b/src/node/TimeView.js @@ -14,6 +14,7 @@ Ext.define('Proxmox.node.TimeView', { let servertime = new Date(value * 1000 + tzOffset); return Ext.Date.format(servertime, 'Y-m-d H:i:s'); }; + let splitList = (value) => (value ?? '').split(',').filter((item) => item !== ''); let run_editor = () => { let win = Ext.create('Proxmox.node.TimeEdit', { @@ -23,6 +24,8 @@ Ext.define('Proxmox.node.TimeView', { win.on('destroy', me.reload, me); }; + let get_ntp_service = () => me.rstore.getById('ntp-service')?.data.value; + // the server time is read-only, so its row has no action let run_row_action = () => { let key = me.getSelectionModel().getSelection()[0]?.data.key; @@ -31,22 +34,55 @@ Ext.define('Proxmox.node.TimeView', { } }; + let rows = { + timezone: { + header: gettext('Time zone'), + required: true, + }, + localtime: { + header: gettext('Server time'), + required: true, + renderer: renderLocaltime, + }, + // only returned if configuring NTP servers is supported + 'ntp-service': { + header: gettext('NTP service'), + renderer: Ext.htmlEncode, + }, + // Shows the custom servers and pools together. It is keyed on the servers, as their row + // always exists through the empty default value, while neither is returned if the + // service uses its default sources. The filter below hides it if there is no NTP + // service. + 'ntp-servers': { + header: gettext('NTP sources'), + defaultValue: '', + renderer: function (servers) { + let pools = me.rstore.getById('ntp-pools')?.data.value; + let sources = [ + ...splitList(servers), + ...splitList(pools).map((pool) => `${pool} (${gettext('Pool')})`), + ]; + if (!sources.length) { + return Proxmox.Utils.defaultText; + } + return Ext.htmlEncode(sources.join(', ')); + }, + }, + // the reader drops keys without a row, keep it in the store for the renderer above + 'ntp-pools': { + visible: false, + }, + }; + let rowOrder = Object.keys(rows); + Ext.apply(me, { url: `/api2/json/nodes/${me.nodename}/time`, cwidth1: 150, interval: 1000, run_editor: run_row_action, - rows: { - timezone: { - header: gettext('Time zone'), - required: true, - }, - localtime: { - header: gettext('Server time'), - required: true, - renderer: renderLocaltime, - }, - }, + rows: rows, + // rows with a default value are added before the first load, keep the defined order + sorterFn: (a, b) => rowOrder.indexOf(a.data.key) - rowOrder.indexOf(b.data.key), tbar: [ { text: gettext('Edit'), @@ -60,6 +96,12 @@ Ext.define('Proxmox.node.TimeView', { me.callParent(); + me.store.filters.add( + Ext.create('Ext.util.Filter', { + filterFn: (item) => item.data.key !== 'ntp-servers' || !!get_ntp_service(), + }), + ); + me.on('activate', me.rstore.startUpdate); me.on('deactivate', me.rstore.stopUpdate); me.on('destroy', me.rstore.stopUpdate); -- 2.47.3