| 1 |
/* |
| 2 |
* Copyright (C) 2019 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
import cockpit from "cockpit"; |
| 6 |
import React from "react"; |
| 7 |
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; |
| 8 |
import { DatePicker } from "@patternfly/react-core/dist/esm/components/DatePicker/index.js"; |
| 9 |
import { Flex, FlexItem } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js"; |
| 10 |
import { Form, FormGroup } from "@patternfly/react-core/dist/esm/components/Form/index.js"; |
| 11 |
import { Popover } from "@patternfly/react-core/dist/esm/components/Popover/index.js"; |
| 12 |
import { Spinner } from "@patternfly/react-core/dist/esm/components/Spinner/index.js"; |
| 13 |
import { TimePicker } from "@patternfly/react-core/dist/esm/components/TimePicker/index.js"; |
| 14 |
import { TextInput } from "@patternfly/react-core/dist/esm/components/TextInput/index.js"; |
| 15 |
import { Icon } from "@patternfly/react-core/dist/esm/components/Icon/index.js"; |
| 16 |
import { CloseIcon, ExclamationCircleIcon, InfoCircleIcon, PlusIcon } from "@patternfly/react-icons"; |
| 17 |
import { show_modal_dialog } from "cockpit-components-dialog.jsx"; |
| 18 |
import { SimpleSelect } from "cockpit-components-simple-select"; |
| 19 |
import { TypeaheadSelect } from "cockpit-components-typeahead-select"; |
| 20 |
import { useObject, useEvent } from "hooks.js"; |
| 21 |
|
| 22 |
import * as service from "service.js"; |
| 23 |
import * as timeformat from "timeformat"; |
| 24 |
import * as python from "python.js"; |
| 25 |
import get_timesync_backend_py from "./get-timesync-backend.py"; |
| 26 |
|
| 27 |
import { superuser } from "superuser.js"; |
| 28 |
|
| 29 |
import "serverTime.scss"; |
| 30 |
|
| 31 |
const _ = cockpit.gettext; |
| 32 |
|
| 33 |
export function ServerTime() { |
| 34 |
const self = this; |
| 35 |
cockpit.event_target(self); |
| 36 |
|
| 37 |
function emit_changed() { |
| 38 |
self.dispatchEvent("changed"); |
| 39 |
} |
| 40 |
|
| 41 |
let time_offset = null; |
| 42 |
let remote_offset = null; |
| 43 |
|
| 44 |
let client = null; |
| 45 |
let timedate = null; |
| 46 |
|
| 47 |
function connect() { |
| 48 |
if (client) { |
| 49 |
timedate.removeEventListener("changed", emit_changed); |
| 50 |
client.close(); |
| 51 |
} |
| 52 |
client = cockpit.dbus('org.freedesktop.timedate1', { superuser: "try" }); |
| 53 |
timedate = client.proxy(); |
| 54 |
timedate.addEventListener("changed", emit_changed); |
| 55 |
client.subscribe({ |
| 56 |
interface: "org.freedesktop.DBus.Properties", |
| 57 |
member: "PropertiesChanged" |
| 58 |
}, ntp_updated); |
| 59 |
} |
| 60 |
|
| 61 |
const timedate1_service = service.proxy("dbus-org.freedesktop.timedate1.service"); |
| 62 |
const timesyncd_service = service.proxy("systemd-timesyncd.service"); |
| 63 |
const chronyd_service = service.proxy("chronyd.service"); |
| 64 |
|
| 65 |
timesyncd_service.addEventListener("changed", emit_changed); |
| 66 |
chronyd_service.addEventListener("changed", emit_changed); |
| 67 |
|
| 68 |
/* |
| 69 |
* The time we return from here as its UTC time set to the |
| 70 |
* server time. This is the only way to get predictable |
| 71 |
* behavior. |
| 72 |
*/ |
| 73 |
Object.defineProperty(self, 'utc_fake_now', { |
| 74 |
enumerable: true, |
| 75 |
get: function get() { |
| 76 |
const offset = time_offset + remote_offset; |
| 77 |
return new Date(offset + (new Date()).valueOf()); |
| 78 |
} |
| 79 |
}); |
| 80 |
|
| 81 |
Object.defineProperty(self, 'now', { |
| 82 |
enumerable: true, |
| 83 |
get: function get() { |
| 84 |
return new Date(time_offset + (new Date()).valueOf()); |
| 85 |
} |
| 86 |
}); |
| 87 |
|
| 88 |
const updateInterval = window.setInterval(emit_changed, 30000); |
| 89 |
|
| 90 |
self.wait = function wait() { |
| 91 |
if (remote_offset === null) |
| 92 |
return self.update(); |
| 93 |
return Promise.resolve(); |
| 94 |
}; |
| 95 |
|
| 96 |
self.update = function update() { |
| 97 |
return cockpit.spawn(["date", "+%s:%z"], { err: "message" }) |
| 98 |
.then(data => { |
| 99 |
const parts = data.trim().split(":"); |
| 100 |
const timems = parseInt(parts[0], 10) * 1000; |
| 101 |
let tzmin = parseInt(parts[1].slice(-2), 10); |
| 102 |
const tzhour = parseInt(parts[1].slice(0, -2)); |
| 103 |
if (tzhour < 0) |
| 104 |
tzmin = -tzmin; |
| 105 |
const offsetms = (tzhour * 3600000) + tzmin * 60000; |
| 106 |
const now = new Date(); |
| 107 |
time_offset = (timems - now.valueOf()); |
| 108 |
remote_offset = offsetms; |
| 109 |
emit_changed(); |
| 110 |
}) |
| 111 |
.catch(ex => console.log("Couldn't calculate server time offset: " + cockpit.message(ex))); |
| 112 |
}; |
| 113 |
|
| 114 |
/* There is no way to make sense of this date without a round trip to the |
| 115 |
* server, as the timezone is really server specific. */ |
| 116 |
self.change_time = (datestr, timestr) => cockpit.spawn(["date", "--date=" + datestr + " " + timestr, "+%s"]) |
| 117 |
.then(data => { |
| 118 |
const seconds = parseInt(data.trim(), 10); |
| 119 |
return timedate.call('SetTime', [seconds * 1000 * 1000, false, true]) |
| 120 |
.then(self.update); |
| 121 |
}); |
| 122 |
|
| 123 |
self.bump_time = function (millis) { |
| 124 |
return timedate.call('SetTime', [millis, true, true]); |
| 125 |
}; |
| 126 |
|
| 127 |
self.get_time_zone = function () { |
| 128 |
return timedate.Timezone; |
| 129 |
}; |
| 130 |
|
| 131 |
self.set_time_zone = function (tz) { |
| 132 |
return timedate.call('SetTimezone', [tz, true]); |
| 133 |
}; |
| 134 |
|
| 135 |
self.poll_ntp_synchronized = () => client.call( |
| 136 |
timedate.path, "org.freedesktop.DBus.Properties", "Get", ["org.freedesktop.timedate1", "NTPSynchronized"]) |
| 137 |
.then(result => { |
| 138 |
const ifaces = { "org.freedesktop.timedate1": { NTPSynchronized: result[0].v } }; |
| 139 |
const data = { }; |
| 140 |
data[timedate.path] = ifaces; |
| 141 |
client.notify(data); |
| 142 |
}) |
| 143 |
.catch(error => { |
| 144 |
if (error.name != "org.freedesktop.DBus.Error.UnknownProperty" && |
| 145 |
error.problem != "not-found") |
| 146 |
console.log("can't get NTPSynchronized property", error); |
| 147 |
}); |
| 148 |
|
| 149 |
let ntp_waiting_value = null; |
| 150 |
let ntp_waiting_resolve = null; |
| 151 |
|
| 152 |
function ntp_updated(path, iface, member, args) { |
| 153 |
if (!ntp_waiting_resolve || !args[1].NTP) |
| 154 |
return; |
| 155 |
if (ntp_waiting_value !== args[1].NTP.v) |
| 156 |
console.warn("Unexpected value of NTP"); |
| 157 |
ntp_waiting_resolve(); |
| 158 |
ntp_waiting_resolve = null; |
| 159 |
} |
| 160 |
|
| 161 |
self.set_ntp = function set_ntp(val) { |
| 162 |
const promise = new Promise((resolve, reject) => { |
| 163 |
ntp_waiting_resolve = resolve; |
| 164 |
}); |
| 165 |
ntp_waiting_value = val; |
| 166 |
client.call(timedate.path, |
| 167 |
"org.freedesktop.DBus.Properties", "Get", ["org.freedesktop.timedate1", "NTP"]) |
| 168 |
.then(result => { |
| 169 |
// Check if don't want to enable enabled or disable disabled |
| 170 |
if (result[0].v === val) { |
| 171 |
ntp_waiting_resolve(); |
| 172 |
ntp_waiting_resolve = null; |
| 173 |
return; |
| 174 |
} |
| 175 |
timedate.call('SetNTP', [val, true]) |
| 176 |
.catch(e => { |
| 177 |
ntp_waiting_resolve(); |
| 178 |
ntp_waiting_resolve = null; |
| 179 |
console.error("Failed to call SetNTP:", e.message); // not-covered: OS error |
| 180 |
}); |
| 181 |
}); |
| 182 |
return promise; |
| 183 |
}; |
| 184 |
|
| 185 |
self.get_ntp_active = function () { |
| 186 |
return timedate.NTP; |
| 187 |
}; |
| 188 |
|
| 189 |
self.get_ntp_supported = function () { |
| 190 |
return timedate.CanNTP; |
| 191 |
}; |
| 192 |
|
| 193 |
self.get_ntp_status = function () { |
| 194 |
const status = { |
| 195 |
initialized: false, |
| 196 |
active: false, |
| 197 |
synch: false, |
| 198 |
service: null, |
| 199 |
server: null, |
| 200 |
sub_status: null |
| 201 |
}; |
| 202 |
|
| 203 |
// flag for tests that timedated/timesyncd proxies got initialized |
| 204 |
if (timedate.CanNTP !== undefined && |
| 205 |
timedate1_service.unit && timedate1_service.unit.Id && |
| 206 |
timesyncd_service.exists !== null && |
| 207 |
chronyd_service.exists !== null) |
| 208 |
status.initialized = true; |
| 209 |
|
| 210 |
status.active = timedate.NTP; |
| 211 |
status.synch = timedate.NTPSynchronized; |
| 212 |
|
| 213 |
const timesyncd_server_regex = /.*time server (.*)\./i; |
| 214 |
|
| 215 |
const timesyncd_status = (timesyncd_service.state == "running" && |
| 216 |
timesyncd_service.service?.StatusText); |
| 217 |
|
| 218 |
if (timesyncd_service.state == "running") |
| 219 |
status.service = "systemd-timesyncd.service"; |
| 220 |
else if (chronyd_service.state == "running") |
| 221 |
status.service = "chronyd.service"; |
| 222 |
|
| 223 |
if (timesyncd_status) { |
| 224 |
const match = timesyncd_status.match(timesyncd_server_regex); |
| 225 |
if (match) |
| 226 |
status.server = match[1]; |
| 227 |
else if (timesyncd_status != "Idle." && timesyncd_status !== "") |
| 228 |
status.sub_status = timesyncd_status; |
| 229 |
} |
| 230 |
|
| 231 |
return status; |
| 232 |
}; |
| 233 |
|
| 234 |
function get_timesync_backend() { |
| 235 |
return python.spawn(get_timesync_backend_py, [], { superuser: "try", err: "message" }) |
| 236 |
.then(data => { |
| 237 |
const unit = data.trim(); |
| 238 |
if (unit == "systemd-timesyncd.service") |
| 239 |
return "timesyncd"; |
| 240 |
else if (unit == "chrony.service" || unit == "chronyd.service") |
| 241 |
return "chronyd"; |
| 242 |
else |
| 243 |
return null; |
| 244 |
}); |
| 245 |
} |
| 246 |
|
| 247 |
function get_custom_ntp_timesyncd() { |
| 248 |
const custom_ntp_config_file = cockpit.file("/etc/systemd/timesyncd.conf.d/50-cockpit.conf", |
| 249 |
{ superuser: "try" }); |
| 250 |
|
| 251 |
const result = { |
| 252 |
backend: "timesyncd", |
| 253 |
enabled: false, |
| 254 |
servers: [] |
| 255 |
}; |
| 256 |
|
| 257 |
return custom_ntp_config_file.read() |
| 258 |
.then(function(text) { |
| 259 |
let ntp_line = ""; |
| 260 |
if (text) { |
| 261 |
result.enabled = true; |
| 262 |
text.split("\n").forEach(function(line) { |
| 263 |
if (line.indexOf("NTP=") === 0) { |
| 264 |
ntp_line = line.slice(4); |
| 265 |
result.enabled = true; |
| 266 |
} else if (line.indexOf("#NTP=") === 0) { |
| 267 |
ntp_line = line.slice(5); |
| 268 |
result.enabled = false; |
| 269 |
} |
| 270 |
}); |
| 271 |
|
| 272 |
result.servers = ntp_line.split(" ").filter(function(val) { |
| 273 |
return val !== ""; |
| 274 |
}); |
| 275 |
if (result.servers.length === 0) |
| 276 |
result.enabled = false; |
| 277 |
} |
| 278 |
return result; |
| 279 |
}) |
| 280 |
.catch(function(error) { |
| 281 |
console.warn("failed to load time servers", error); |
| 282 |
return result; |
| 283 |
}); |
| 284 |
} |
| 285 |
|
| 286 |
async function set_custom_ntp_timesyncd(config) { |
| 287 |
const conf_path = "/etc/systemd/timesyncd.conf.d/50-cockpit.conf"; |
| 288 |
const custom_ntp_config_file = cockpit.file(conf_path, { superuser: "require" }); |
| 289 |
|
| 290 |
const text = `# This file is automatically generated by Cockpit\n\n[Time]\n${config.enabled ? "" : "#"}NTP=${config.servers.join(" ")}\n`; |
| 291 |
|
| 292 |
// this must be readable with tight umask, timesyncd runs as unprivileged user |
| 293 |
await cockpit.spawn(["mkdir", "-p", "-m755", "/etc/systemd/timesyncd.conf.d"], { superuser: "require" }); |
| 294 |
await custom_ntp_config_file.replace(text); |
| 295 |
await cockpit.spawn(["chmod", "644", conf_path], { superuser: "require" }); |
| 296 |
} |
| 297 |
|
| 298 |
const chronyd_sourcedir = "/etc/chrony/sources.d"; |
| 299 |
const chronyd_sources_enabled = chronyd_sourcedir + "/cockpit.sources"; |
| 300 |
const chronyd_sources_disabled = chronyd_sourcedir + "/cockpit.disabled"; |
| 301 |
|
| 302 |
function get_custom_ntp_chronyd() { |
| 303 |
const enabled_file = cockpit.file(chronyd_sources_enabled, { superuser: "try" }); |
| 304 |
const disabled_file = cockpit.file(chronyd_sources_disabled, { superuser: "try" }); |
| 305 |
|
| 306 |
function parse_servers(data) { |
| 307 |
if (!data) |
| 308 |
return []; |
| 309 |
const servers = []; |
| 310 |
data.split("\n").forEach(function(line) { |
| 311 |
const parts = line.split(" "); |
| 312 |
if (parts[0] == "server") |
| 313 |
servers.push(parts[1]); |
| 314 |
}); |
| 315 |
return servers; |
| 316 |
} |
| 317 |
|
| 318 |
return enabled_file.read() |
| 319 |
.then(data => { |
| 320 |
if (data) { |
| 321 |
return { |
| 322 |
backend: "chronyd", |
| 323 |
enabled: true, |
| 324 |
servers: parse_servers(data) |
| 325 |
}; |
| 326 |
} else { |
| 327 |
return disabled_file.read() |
| 328 |
.then(data => { |
| 329 |
return { |
| 330 |
backend: "chronyd", |
| 331 |
enabled: false, |
| 332 |
servers: parse_servers(data) |
| 333 |
}; |
| 334 |
}); |
| 335 |
} |
| 336 |
}); |
| 337 |
} |
| 338 |
|
| 339 |
function set_custom_ntp_chronyd(config) { |
| 340 |
const enabled_file = cockpit.file(chronyd_sources_enabled, { superuser: "require" }); |
| 341 |
const disabled_file = cockpit.file(chronyd_sources_disabled, { superuser: "require" }); |
| 342 |
|
| 343 |
const text = "# This file is automatically generated by Cockpit\n\n" + config.servers.map(s => `server ${s}\n`).join(""); |
| 344 |
|
| 345 |
// HACK - https://bugzilla.redhat.com/show_bug.cgi?id=2168863 |
| 346 |
function ensure_sourcedir() { |
| 347 |
function add_sourcedir(data) { |
| 348 |
const line = "sourcedir " + chronyd_sourcedir; |
| 349 |
if (data && data.indexOf(line) == -1) |
| 350 |
data += "\n# Added by Cockpit\n" + line + "\n"; |
| 351 |
return data; |
| 352 |
} |
| 353 |
return cockpit.file("/etc/chrony.conf", { superuser: "require" }).modify(add_sourcedir); |
| 354 |
} |
| 355 |
|
| 356 |
return cockpit.spawn(["mkdir", "-p", chronyd_sourcedir], { superuser: "require" }) |
| 357 |
.then(() => { |
| 358 |
if (config.enabled) |
| 359 |
return enabled_file.replace(text).then(() => disabled_file.replace(null)).then(ensure_sourcedir); |
| 360 |
else |
| 361 |
return disabled_file.replace(text).then(() => enabled_file.replace(null)); |
| 362 |
}); |
| 363 |
} |
| 364 |
|
| 365 |
self.get_custom_ntp = function () { |
| 366 |
return get_timesync_backend().then(backend => { |
| 367 |
if (backend == "timesyncd") { |
| 368 |
return get_custom_ntp_timesyncd(); |
| 369 |
} else if (backend == "chronyd") { |
| 370 |
return get_custom_ntp_chronyd(); |
| 371 |
} else { |
| 372 |
return Promise.resolve({ backend: null, servers: [], enabled: false }); |
| 373 |
} |
| 374 |
}); |
| 375 |
}; |
| 376 |
|
| 377 |
self.set_custom_ntp = function (config) { |
| 378 |
if (config.backend == "timesyncd") { |
| 379 |
return set_custom_ntp_timesyncd(config); |
| 380 |
} else if (config.backend == "chronyd") { |
| 381 |
return set_custom_ntp_chronyd(config); |
| 382 |
} else { |
| 383 |
return Promise.resolve(); |
| 384 |
} |
| 385 |
}; |
| 386 |
|
| 387 |
self.get_timezones = function() { |
| 388 |
return cockpit.spawn(["/usr/bin/timedatectl", "list-timezones"]) |
| 389 |
.then(content => content.split('\n').filter(tz => tz != "")); |
| 390 |
}; |
| 391 |
|
| 392 |
/* NTPSynchronized needs to be polled so we just do that |
| 393 |
* always. |
| 394 |
*/ |
| 395 |
|
| 396 |
const ntp_poll_interval = window.setInterval(function() { |
| 397 |
self.poll_ntp_synchronized(); |
| 398 |
}, 5000); |
| 399 |
|
| 400 |
self.close = function close() { |
| 401 |
window.clearInterval(updateInterval); |
| 402 |
window.clearInterval(ntp_poll_interval); |
| 403 |
client.close(); |
| 404 |
}; |
| 405 |
|
| 406 |
connect(); |
| 407 |
superuser.addEventListener("reconnect", connect); |
| 408 |
self.update(); |
| 409 |
} |
| 410 |
|
| 411 |
export function ServerTimeConfig() { |
| 412 |
const server_time = useObject(() => new ServerTime(), |
| 413 |
st => st.close(), |
| 414 |
[]); |
| 415 |
useEvent(server_time, "changed"); |
| 416 |
|
| 417 |
const ntp = server_time.get_ntp_status(); |
| 418 |
|
| 419 |
const tz = server_time.get_time_zone(); |
| 420 |
const systime_button = ( |
| 421 |
<Button variant="link" id="system_information_systime_button" |
| 422 |
onClick={ () => change_systime_dialog(server_time, tz) } |
| 423 |
data-timedated-initialized={ntp?.initialized} |
| 424 |
isInline isDisabled={!superuser.allowed || !tz}> |
| 425 |
{ timeformat.dateTimeUTC(server_time.utc_fake_now) } |
| 426 |
</Button>); |
| 427 |
|
| 428 |
let ntp_status = null; |
| 429 |
if (ntp?.active) { |
| 430 |
let icon; let header; let body = ""; let footer = null; |
| 431 |
if (ntp.synch) { |
| 432 |
icon = <Icon status="info"><InfoCircleIcon className="ct-info-circle" /></Icon>; |
| 433 |
header = _("Synchronized"); |
| 434 |
if (ntp.server) |
| 435 |
body = <div>{cockpit.format(_("Synchronized with $0"), ntp.server)}</div>; |
| 436 |
} else { |
| 437 |
if (ntp.server) { |
| 438 |
icon = <Spinner size="md" />; |
| 439 |
header = _("Synchronizing"); |
| 440 |
body = <div>{cockpit.format(_("Trying to synchronize with $0"), ntp.server)}</div>; |
| 441 |
} else { |
| 442 |
icon = <Icon status="danger"><ExclamationCircleIcon className="ct-exclamation-circle" /></Icon>; |
| 443 |
header = _("Not synchronized"); |
| 444 |
if (ntp.service) { |
| 445 |
footer = ( |
| 446 |
<Button variant="link" |
| 447 |
onClick={() => cockpit.jump("/system/services#/" + |
| 448 |
encodeURIComponent(ntp.service))}> |
| 449 |
{_("Log messages")} |
| 450 |
</Button>); |
| 451 |
} |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
if (ntp.sub_status) { |
| 456 |
body = <>{body}<div>{ntp.sub_status}</div></>; |
| 457 |
} |
| 458 |
|
| 459 |
ntp_status = ( |
| 460 |
<Popover headerContent={header} bodyContent={body} footerContent={footer}> |
| 461 |
{icon} |
| 462 |
</Popover>); |
| 463 |
} |
| 464 |
|
| 465 |
return ( |
| 466 |
<Flex spaceItems={{ default: 'spaceItemsSm' }} alignItems={{ default: 'alignItemsCenter' }}> |
| 467 |
{systime_button} |
| 468 |
{ntp_status} |
| 469 |
</Flex> |
| 470 |
); |
| 471 |
} |
| 472 |
|
| 473 |
function Validated({ errors, error_key, children }) { |
| 474 |
const error = errors?.[error_key]; |
| 475 |
// We need to always render the <div> for the has-error |
| 476 |
// class so that the input field keeps the focus when |
| 477 |
// errors are cleared. Otherwise the DOM changes enough |
| 478 |
// for the Browser to remove focus. |
| 479 |
return ( |
| 480 |
<div className={error ? "ct-validation-wrapper has-error" : "ct-validation-wrapper"}> |
| 481 |
{ children } |
| 482 |
{ error ? <span className="help-block dialog-error">{error}</span> : null } |
| 483 |
</div> |
| 484 |
); |
| 485 |
} |
| 486 |
|
| 487 |
function ValidatedInput({ errors, error_key, children }) { |
| 488 |
const error = errors?.[error_key]; |
| 489 |
return ( |
| 490 |
<span className={error ? "ct-validation-wrapper has-error" : "ct-validation-wrapper"}> |
| 491 |
{ children } |
| 492 |
</span> |
| 493 |
); |
| 494 |
} |
| 495 |
|
| 496 |
function ChangeSystimeBody({ state, errors, change }) { |
| 497 |
const { |
| 498 |
time_zone, time_zones, |
| 499 |
mode, |
| 500 |
manual_date, manual_time, |
| 501 |
ntp_supported, custom_ntp |
| 502 |
} = state; |
| 503 |
|
| 504 |
function add_server(event, index) { |
| 505 |
custom_ntp.servers.splice(index + 1, 0, ""); |
| 506 |
change("custom_ntp", custom_ntp); |
| 507 |
event.stopPropagation(); |
| 508 |
event.preventDefault(); |
| 509 |
return false; |
| 510 |
} |
| 511 |
|
| 512 |
function remove_server(event, index) { |
| 513 |
custom_ntp.servers.splice(index, 1); |
| 514 |
change("custom_ntp", custom_ntp); |
| 515 |
event.stopPropagation(); |
| 516 |
event.preventDefault(); |
| 517 |
return false; |
| 518 |
} |
| 519 |
|
| 520 |
function change_server(event, index, value) { |
| 521 |
custom_ntp.servers[index] = value; |
| 522 |
change("custom_ntp", custom_ntp); |
| 523 |
event.stopPropagation(); |
| 524 |
event.preventDefault(); |
| 525 |
return false; |
| 526 |
} |
| 527 |
|
| 528 |
const ntp_servers = ( |
| 529 |
custom_ntp.servers.map((s, i) => ( |
| 530 |
<Flex className="ntp-server-input-group" spaceItems={{ default: 'spaceItemsSm' }} key={i}> |
| 531 |
<FlexItem grow={{ default: 'grow' }}> |
| 532 |
<TextInput value={s} placeholder={_("NTP server")} aria-label={_("NTP server")} |
| 533 |
onChange={(event, value) => change_server(event, i, value)} /> |
| 534 |
</FlexItem> |
| 535 |
<Button variant="secondary" onClick={event => add_server(event, i)} |
| 536 |
icon={ <PlusIcon /> } /> |
| 537 |
<Button variant="secondary" onClick={event => remove_server(event, i)} |
| 538 |
icon={ <CloseIcon /> } isDisabled={i === custom_ntp.servers.length - 1} /> |
| 539 |
</Flex> |
| 540 |
)) |
| 541 |
); |
| 542 |
|
| 543 |
const mode_options = [ |
| 544 |
{ value: "manual_time", content: _("Manually") }, |
| 545 |
{ value: "ntp_time", content: _("Automatically using NTP"), isDisabled: !ntp_supported }, |
| 546 |
]; |
| 547 |
|
| 548 |
if (custom_ntp.backend) |
| 549 |
mode_options.push( |
| 550 |
{ |
| 551 |
value: "ntp_time_custom", |
| 552 |
isDisabled: !ntp_supported, |
| 553 |
content: (custom_ntp.backend == "chronyd") |
| 554 |
? _("Automatically using additional NTP servers") |
| 555 |
: _("Automatically using specific NTP servers") |
| 556 |
}); |
| 557 |
|
| 558 |
return ( |
| 559 |
<Form isHorizontal> |
| 560 |
<FormGroup fieldId="systime-timezones" label={_("Time zone")}> |
| 561 |
<Validated errors={errors} error_key="time_zone"> |
| 562 |
<TypeaheadSelect toggleProps={ { id: "systime-timezones" } } |
| 563 |
isScrollable |
| 564 |
selected={time_zone} |
| 565 |
onSelect={(event, value) => { change("time_zone", value) }} |
| 566 |
selectOptions={time_zones.map(tz => ( |
| 567 |
{ value: tz, content: tz.replaceAll("_", " ") } |
| 568 |
))} /> |
| 569 |
</Validated> |
| 570 |
</FormGroup> |
| 571 |
<FormGroup fieldId="change_systime_btn" label={_("Set time")} isStack> |
| 572 |
<SimpleSelect |
| 573 |
id="change_systime" |
| 574 |
options={mode_options} |
| 575 |
selected={mode} |
| 576 |
toggleProps={{ id: "change_systime_btn" }} |
| 577 |
onSelect={value => change("mode", value)} /> |
| 578 |
{ mode == "manual_time" && |
| 579 |
<Flex spaceItems={{ default: 'spaceItemsSm' }} id="systime-manual-row"> |
| 580 |
<ValidatedInput errors={errors} error_key="manual_date"> |
| 581 |
<DatePicker id="systime-date-input" |
| 582 |
aria-label={_("Pick date")} |
| 583 |
buttonAriaLabel={_("Toggle date picker")} |
| 584 |
invalidFormatText="" |
| 585 |
locale={timeformat.dateFormatLang()} |
| 586 |
weekStart={timeformat.firstDayOfWeek()} |
| 587 |
onChange={(_, d) => change("manual_date", d)} |
| 588 |
value={manual_date} |
| 589 |
appendTo={() => document.body} /> |
| 590 |
</ValidatedInput> |
| 591 |
<ValidatedInput errors={errors} error_key="manual_time"> |
| 592 |
<TimePicker id="systime-time-input" |
| 593 |
className="ct-serverTime-time-picker" |
| 594 |
time={manual_time} |
| 595 |
is24Hour |
| 596 |
menuAppendTo={() => document.body} |
| 597 |
invalidFormatErrorMessage="" |
| 598 |
onChange={(e, time, h, m, s, valid) => change("manual_time", time, valid) } /> |
| 599 |
</ValidatedInput> |
| 600 |
<Validated errors={errors} error_key="manual_date" /> |
| 601 |
<Validated errors={errors} error_key="manual_time" /> |
| 602 |
</Flex> |
| 603 |
} |
| 604 |
{ mode == "ntp_time_custom" && |
| 605 |
<Validated errors={errors} error_key="ntp_servers"> |
| 606 |
<div id="systime-ntp-servers"> |
| 607 |
{ ntp_servers } |
| 608 |
</div> |
| 609 |
</Validated> |
| 610 |
} |
| 611 |
</FormGroup> |
| 612 |
</Form> |
| 613 |
); |
| 614 |
} |
| 615 |
|
| 616 |
function has_errors(errors) { |
| 617 |
for (const field in errors) { |
| 618 |
if (errors[field]) |
| 619 |
return true; |
| 620 |
} |
| 621 |
return false; |
| 622 |
} |
| 623 |
|
| 624 |
function change_systime_dialog(server_time, timezone) { |
| 625 |
let dlg = null; |
| 626 |
const state = { |
| 627 |
time_zone: timezone, |
| 628 |
time_zones: null, |
| 629 |
mode: null, |
| 630 |
ntp_supported: server_time.get_ntp_supported(), |
| 631 |
custom_ntp: null, |
| 632 |
manual_time_valid: true, |
| 633 |
}; |
| 634 |
let errors = { }; |
| 635 |
|
| 636 |
function get_current_time() { |
| 637 |
state.manual_date = server_time.utc_fake_now.toISOString().split("T")[0]; |
| 638 |
|
| 639 |
const minutes = server_time.utc_fake_now.getUTCMinutes(); |
| 640 |
// normalize to two digits |
| 641 |
const minutes_str = (minutes < 10) ? "0" + minutes.toString() : minutes.toString(); |
| 642 |
state.manual_time = `${server_time.utc_fake_now.getUTCHours()}:${minutes_str}`; |
| 643 |
} |
| 644 |
|
| 645 |
function change(field, value, isValid) { |
| 646 |
state[field] = value; |
| 647 |
errors = { }; |
| 648 |
|
| 649 |
if (field == "mode" && value == "manual_time") |
| 650 |
get_current_time(); |
| 651 |
|
| 652 |
if (field == "manual_time") |
| 653 |
state.manual_time_valid = value && isValid; |
| 654 |
|
| 655 |
update(); |
| 656 |
} |
| 657 |
|
| 658 |
function validate() { |
| 659 |
errors = { }; |
| 660 |
|
| 661 |
if (state.time_zone == "") |
| 662 |
errors.time_zone = _("Invalid timezone"); |
| 663 |
|
| 664 |
if (state.mode == "manual_time") { |
| 665 |
const new_date = new Date(state.manual_date); |
| 666 |
if (isNaN(new_date.getTime()) || new_date.getTime() < 0) |
| 667 |
errors.manual_date = _("Invalid date format"); |
| 668 |
|
| 669 |
if (!state.manual_time_valid) |
| 670 |
errors.manual_time = _("Invalid time format"); |
| 671 |
} |
| 672 |
|
| 673 |
if (state.mode == "ntp_time_custom") { |
| 674 |
if (state.custom_ntp.servers.filter(s => !!s).length == 0) |
| 675 |
errors.ntp_servers = _("Need at least one NTP server"); |
| 676 |
} |
| 677 |
|
| 678 |
return !has_errors(errors); |
| 679 |
} |
| 680 |
|
| 681 |
function apply() { |
| 682 |
return server_time.set_time_zone(state.time_zone) |
| 683 |
.then(() => { |
| 684 |
if (state.mode == "manual_time") { |
| 685 |
return server_time.set_ntp(false) |
| 686 |
.then(() => server_time.change_time(state.manual_date, |
| 687 |
state.manual_time)); |
| 688 |
} else { |
| 689 |
// Switch off NTP, write the config file, and switch NTP back on |
| 690 |
state.custom_ntp.enabled = (state.mode == "ntp_time_custom"); |
| 691 |
state.custom_ntp.servers = state.custom_ntp.servers.filter(s => !!s); |
| 692 |
return server_time.set_ntp(false) |
| 693 |
.then(() => server_time.set_custom_ntp(state.custom_ntp)) |
| 694 |
.then(() => server_time.set_ntp(true)); |
| 695 |
} |
| 696 |
}); |
| 697 |
} |
| 698 |
|
| 699 |
function update() { |
| 700 |
const props = { |
| 701 |
id: "system_information_change_systime", |
| 702 |
title: _("Change system time"), |
| 703 |
body: <ChangeSystimeBody state={state} errors={errors} change={change} /> |
| 704 |
}; |
| 705 |
|
| 706 |
const footer = { |
| 707 |
actions: [ |
| 708 |
{ |
| 709 |
caption: _("Change"), |
| 710 |
style: "primary", |
| 711 |
clicked: () => { |
| 712 |
if (validate()) { |
| 713 |
return apply(); |
| 714 |
} else { |
| 715 |
update(); |
| 716 |
return Promise.reject(); |
| 717 |
} |
| 718 |
} |
| 719 |
} |
| 720 |
] |
| 721 |
}; |
| 722 |
|
| 723 |
if (!dlg) |
| 724 |
dlg = show_modal_dialog(props, footer); |
| 725 |
else { |
| 726 |
dlg.setProps(props); |
| 727 |
dlg.setFooterProps(footer); |
| 728 |
} |
| 729 |
} |
| 730 |
|
| 731 |
Promise.all([server_time.get_custom_ntp(), server_time.get_timezones()]) |
| 732 |
.then(([custom_ntp, time_zones]) => { |
| 733 |
if (custom_ntp.servers.length == 0) |
| 734 |
custom_ntp.servers = [""]; |
| 735 |
state.custom_ntp = custom_ntp; |
| 736 |
state.time_zones = time_zones; |
| 737 |
if (server_time.get_ntp_active()) { |
| 738 |
if (custom_ntp.enabled) |
| 739 |
state.mode = "ntp_time_custom"; |
| 740 |
else |
| 741 |
state.mode = "ntp_time"; |
| 742 |
} else { |
| 743 |
state.mode = "manual_time"; |
| 744 |
get_current_time(); |
| 745 |
} |
| 746 |
update(); |
| 747 |
}); |
| 748 |
} |