| 1 |
/* |
| 2 |
* Copyright (C) 2016 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import cockpit from 'cockpit'; |
| 7 |
|
| 8 |
const deprecatedKeys = ["net", "options", "link_delay", "disk_timeout", "debug_mem_level", "blacklist"]; |
| 9 |
const knownKeys = [ |
| 10 |
"raw", "nfs", "ssh", "sshkey", "path", "core_collector", "kdump_post", "kdump_pre", "extra_bins", "extra_modules", |
| 11 |
"default", "force_rebuild", "override_resettable", "dracut_args", "fence_kdump_args", "fence_kdump_nodes" |
| 12 |
]; |
| 13 |
// man kdump.conf suggests this as default configuration |
| 14 |
const defaultCoreCollector = "makedumpfile -l --message-level 7 -d 31"; |
| 15 |
|
| 16 |
/* Parse an ini-style config file |
| 17 |
* and monitor it for changes |
| 18 |
*/ |
| 19 |
export class ConfigFile { |
| 20 |
constructor(filename, superuser = false) { |
| 21 |
this.filename = filename; |
| 22 |
this._rawContent = undefined; |
| 23 |
this._lines = []; |
| 24 |
this._originalSettings = { }; |
| 25 |
this._dataAvailable = new Promise(resolve => { this._dataAvailableResolve = resolve }); |
| 26 |
this.settings = { }; |
| 27 |
|
| 28 |
cockpit.event_target(this); |
| 29 |
|
| 30 |
this._fileHandle = cockpit.file(filename, { superuser }); |
| 31 |
this._fileHandle.watch((rawContent) => { |
| 32 |
this._parseText(rawContent); |
| 33 |
}); |
| 34 |
} |
| 35 |
|
| 36 |
close() { |
| 37 |
if (this._fileHandle) { |
| 38 |
this._fileHandle.close(); |
| 39 |
this._fileHandle = undefined; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
// wait for data to have been read at least once |
| 44 |
wait() { |
| 45 |
return this._dataAvailable; |
| 46 |
} |
| 47 |
|
| 48 |
/* parse lines of the config file |
| 49 |
* if a line has a valid config key, use that as key |
| 50 |
* and also store original line, line index, value and whether the line contains a comment |
| 51 |
* we care about the comment since we don't want to overwrite a user comment when changing a value |
| 52 |
* e.g. for line "someKey foo # comment" |
| 53 |
* outputObject["someKey"] = { index: 0, value: "foo", origLine: "someKey foo # comment", hasComment: true } |
| 54 |
* skipNotify: Don't notify about changes, e.g.to avoid multiple updates when writing a file |
| 55 |
*/ |
| 56 |
_parseText(rawContent, skipNotify = false) { |
| 57 |
this._dataAvailableResolve(); |
| 58 |
|
| 59 |
// if nothing changed, don't bother parsing the content |
| 60 |
// do proceed if the content is empty, it might be our initial read |
| 61 |
if (!rawContent) { |
| 62 |
this._originalSettings = null; |
| 63 |
this.settings = null; |
| 64 |
if (!skipNotify) |
| 65 |
this.dispatchEvent("kdumpConfigChanged", this.settings); |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
if (rawContent == this._rawContent) |
| 70 |
return; |
| 71 |
|
| 72 |
// if (skipNotify === undefined) |
| 73 |
// skipNotify = false; |
| 74 |
|
| 75 |
this._rawContent = rawContent; |
| 76 |
// parse the config file |
| 77 |
this._lines = rawContent.split(/\r?\n/); |
| 78 |
|
| 79 |
// this is the format expected by the UI |
| 80 |
this.settings = { |
| 81 |
_internal: {}, |
| 82 |
targets: {}, |
| 83 |
compression: { enabled: false, allowed: false, }, |
| 84 |
nfs_supports_directory: true, |
| 85 |
}; |
| 86 |
this._lines.forEach((line, index) => { |
| 87 |
const trimmed = line.trim(); |
| 88 |
// if the line is empty or only a comment, skip |
| 89 |
if (trimmed.indexOf("#") === 0 || trimmed.length === 0) |
| 90 |
return; |
| 91 |
|
| 92 |
// we need to have a space between key and value |
| 93 |
const separatorIndex = trimmed.indexOf(" "); |
| 94 |
if (separatorIndex === -1) |
| 95 |
return; |
| 96 |
const key = trimmed.substring(0, separatorIndex); |
| 97 |
let value = trimmed.substring(separatorIndex + 1).trim(); |
| 98 |
|
| 99 |
// value might have a comment at the end |
| 100 |
const commentIndex = value.indexOf("#"); |
| 101 |
let comment; |
| 102 |
if (commentIndex !== -1) { |
| 103 |
comment = value.substring(commentIndex).trim(); |
| 104 |
value = value.substring(0, commentIndex).trim(); |
| 105 |
} |
| 106 |
this.settings._internal[key] = { |
| 107 |
index, |
| 108 |
value, |
| 109 |
origLine: line, |
| 110 |
comment |
| 111 |
}; |
| 112 |
}); |
| 113 |
|
| 114 |
// make sure we copy the original keys so we overwrite the correct lines when saving |
| 115 |
this._originalSettings = { }; |
| 116 |
Object.keys(this.settings._internal).forEach((key) => { |
| 117 |
this._originalSettings[key] = { ...this.settings._internal[key] }; |
| 118 |
}); |
| 119 |
|
| 120 |
this._extractSettings(); |
| 121 |
|
| 122 |
if (!skipNotify) |
| 123 |
this.dispatchEvent("kdumpConfigChanged", this.settings); |
| 124 |
} |
| 125 |
|
| 126 |
/* extract settings managed by cockpit from _internal into platform independent model |
| 127 |
*/ |
| 128 |
_extractSettings() { |
| 129 |
// "path" applies to all targets |
| 130 |
const path = this.settings._internal.path || { value: "" }; |
| 131 |
|
| 132 |
Object.keys(this.settings._internal).forEach((key) => { |
| 133 |
if (key === "nfs") { |
| 134 |
// split nfs line into server and export parts |
| 135 |
const parts = this.settings._internal.nfs.value.match(/^([^[][^:]+|\[[^\]]+\]):(.*)$/); |
| 136 |
if (!parts) |
| 137 |
return; |
| 138 |
this.settings.targets.nfs = { |
| 139 |
type: key, |
| 140 |
path: path.value, |
| 141 |
server: parts[1], |
| 142 |
export: parts[2], |
| 143 |
}; |
| 144 |
} else if (key === "ssh") { |
| 145 |
this.settings.targets.ssh = { |
| 146 |
type: key, |
| 147 |
path: path.value, |
| 148 |
server: this.settings._internal.ssh.value, |
| 149 |
}; |
| 150 |
if ("sshkey" in this.settings._internal) |
| 151 |
this.settings.targets.ssh.sshkey = this.settings._internal.sshkey.value; |
| 152 |
} else if (key === "raw") { |
| 153 |
this.settings.targets.raw = { |
| 154 |
type: key, |
| 155 |
partition: this.settings._internal.raw.value |
| 156 |
}; |
| 157 |
} else { |
| 158 |
// probably local, but we might also have a mount |
| 159 |
// check against known keys, the ones left over may be a mount target |
| 160 |
// if the key is empty or known, we don't care about it here |
| 161 |
if (!key || key in knownKeys || key in deprecatedKeys) |
| 162 |
return; |
| 163 |
// if we have a UUID, LABEL or /dev in the value, we can be pretty sure it's a mount option |
| 164 |
const value = JSON.stringify(this.settings._internal[key]).toLowerCase(); |
| 165 |
if (value.indexOf("uuid") > -1 || value.indexOf("label") > -1 || value.indexOf("/dev") > -1) { |
| 166 |
this.settings.targets.mount = { |
| 167 |
type: "mount", |
| 168 |
path: path.value, |
| 169 |
fsType: key, |
| 170 |
partition: this.settings._internal[key].value, |
| 171 |
}; |
| 172 |
} else { |
| 173 |
// TODO: check for know filesystem types here |
| 174 |
} |
| 175 |
} |
| 176 |
}); |
| 177 |
|
| 178 |
// default to local if no target configured |
| 179 |
if (Object.keys(this.settings.targets).length === 0) |
| 180 |
this.settings.targets.local = { type: "local", path: path.value }; |
| 181 |
|
| 182 |
// only allow compression if there is no core collector set or it's set to makedumpfile |
| 183 |
this.settings.compression.allowed = ( |
| 184 |
!("core_collector" in this.settings._internal) || |
| 185 |
(this.settings._internal.core_collector.value.trim().indexOf("makedumpfile") === 0) |
| 186 |
); |
| 187 |
// compression is enabled if we have a core_collector command with the "-c" parameter |
| 188 |
this.settings.compression.enabled = ( |
| 189 |
("core_collector" in this.settings._internal) && |
| 190 |
this.settings._internal.core_collector.value && |
| 191 |
(this.settings._internal.core_collector.value.split(" ").indexOf("-c") != -1) |
| 192 |
); |
| 193 |
|
| 194 |
this.settings.core_collector = this.settings._internal?.core_collector?.value ?? defaultCoreCollector; |
| 195 |
} |
| 196 |
|
| 197 |
/* update single _internal setting to given value |
| 198 |
* make sure setting exists if value is not empty |
| 199 |
*/ |
| 200 |
_updateSetting(settings, key, value) { |
| 201 |
if (key in settings._internal) { |
| 202 |
if (value) |
| 203 |
settings._internal[key].value = value; |
| 204 |
else |
| 205 |
delete settings._internal[key]; |
| 206 |
} else { |
| 207 |
if (value) |
| 208 |
settings._internal[key] = { value }; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
/* transform settings from model back to _internal format |
| 213 |
* this.settings = current state from file |
| 214 |
* settings = in-memory state from UI |
| 215 |
*/ |
| 216 |
_persistSettings(settings) { |
| 217 |
// target |
| 218 |
if (Object.keys(settings.targets).length > 0) { |
| 219 |
const target = Object.values(settings.targets)[0]; |
| 220 |
this._updateSetting(settings, "path", target.path); |
| 221 |
|
| 222 |
// wipe old target settings |
| 223 |
for (const key in this.settings.targets) { |
| 224 |
const oldTarget = this.settings.targets[key]; |
| 225 |
if (oldTarget.type == "mount") { |
| 226 |
delete settings._internal[oldTarget.fsType]; |
| 227 |
} else if (oldTarget.type == "ssh") { |
| 228 |
delete settings._internal.ssh; |
| 229 |
delete settings._internal.sshkey; |
| 230 |
} else { |
| 231 |
delete settings._internal[key]; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
if (target.type === "nfs") { |
| 236 |
this._updateSetting(settings, "nfs", [target.server, target.export].join(":")); |
| 237 |
} else if (target.type === "ssh") { |
| 238 |
this._updateSetting(settings, "ssh", target.server); |
| 239 |
if ("sshkey" in target) |
| 240 |
this._updateSetting(settings, "sshkey", target.sshkey); |
| 241 |
} else if (target.type === "raw") { |
| 242 |
this._updateSetting(settings, "raw", target.partition); |
| 243 |
} else if (target.type === "mount") { |
| 244 |
this._updateSetting(settings, target.fsType, target.partition); |
| 245 |
} |
| 246 |
|
| 247 |
/* ssh target needs a flattened vmcore for transport */ |
| 248 |
if ("core_collector" in settings._internal && |
| 249 |
settings._internal.core_collector.value.includes("makedumpfile")) { |
| 250 |
if (target.type === "ssh" && !settings._internal.core_collector.value.includes("-F")) |
| 251 |
settings._internal.core_collector.value += " -F"; |
| 252 |
else if (settings._internal.core_collector.value.includes("-F")) |
| 253 |
settings._internal.core_collector.value = |
| 254 |
settings._internal.core_collector.value |
| 255 |
.split(" ") |
| 256 |
.filter(e => e != "-F") |
| 257 |
.join(" "); |
| 258 |
} else { |
| 259 |
settings._internal.core_collector = { value: defaultCoreCollector }; |
| 260 |
if (target.type === "ssh") { |
| 261 |
settings._internal.core_collector.value += " -F"; |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
// compression |
| 266 |
if (this.settings.compression.enabled != settings.compression.enabled) { |
| 267 |
if (settings.compression.enabled) { |
| 268 |
// enable compression |
| 269 |
if ("core_collector" in settings._internal) |
| 270 |
settings._internal.core_collector.value = settings._internal.core_collector.value + " -c"; |
| 271 |
else |
| 272 |
settings._internal.core_collector = { value: defaultCoreCollector }; |
| 273 |
} else { |
| 274 |
// disable compression |
| 275 |
if ("core_collector" in this.settings._internal) { |
| 276 |
// just remove all "-c" parameters |
| 277 |
settings._internal.core_collector.value = |
| 278 |
settings._internal.core_collector.value |
| 279 |
.split(" ") |
| 280 |
.filter((e) => { return (e != "-c") }) |
| 281 |
.join(" "); |
| 282 |
} else { |
| 283 |
// if we don't have anything on this in the original settings, |
| 284 |
// we can get rid of the entry altogether |
| 285 |
delete settings._internal.core_collector; |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
return settings; |
| 290 |
} |
| 291 |
|
| 292 |
/* generate the config file from raw text and settings |
| 293 |
*/ |
| 294 |
generateConfig(settings) { |
| 295 |
settings = this._persistSettings(settings); |
| 296 |
|
| 297 |
const lines = this._lines.slice(0); |
| 298 |
const linesToDelete = []; |
| 299 |
// first find the settings lines that have been disabled/deleted |
| 300 |
Object.keys(this._originalSettings).forEach((key) => { |
| 301 |
if (!(key in settings._internal) || !(key in settings._internal && settings._internal[key].value)) { |
| 302 |
const origEntry = this._originalSettings[key]; |
| 303 |
// if the line had a comment, keep it, otherwise delete |
| 304 |
if (origEntry.comment !== undefined) |
| 305 |
lines[origEntry.index] = "#" + origEntry.origLine; |
| 306 |
else |
| 307 |
linesToDelete.push(origEntry.index); |
| 308 |
} |
| 309 |
}); |
| 310 |
|
| 311 |
// we take the lines from our last read operation and modify them with the new settings |
| 312 |
Object.keys(settings._internal).forEach((key) => { |
| 313 |
const entry = settings._internal[key]; |
| 314 |
let line = key + " " + entry.value; |
| 315 |
if (entry.comment) |
| 316 |
line = line + " " + entry.comment; |
| 317 |
// this might be a new entry |
| 318 |
if (!(key in this._originalSettings)) { |
| 319 |
lines.push(line); |
| 320 |
return; |
| 321 |
} |
| 322 |
// otherwise edit the old line |
| 323 |
const origEntry = this._originalSettings[key]; |
| 324 |
lines[origEntry.index] = line; |
| 325 |
}); |
| 326 |
// now delete the rows we want to delete |
| 327 |
linesToDelete.sort().reverse() |
| 328 |
.forEach((lineIndex) => { |
| 329 |
lines.splice(lineIndex, 1); |
| 330 |
}); |
| 331 |
|
| 332 |
return lines.join("\n") + "\n"; |
| 333 |
} |
| 334 |
|
| 335 |
/* write settings back to file |
| 336 |
* new settings that don't have a corresponding entry already have an undefined or null index |
| 337 |
* returns a promise for the file operation (cockpit File) |
| 338 |
*/ |
| 339 |
write(settings) { |
| 340 |
return this._fileHandle.modify((oldContent) => { |
| 341 |
this._parseText(oldContent, true); |
| 342 |
return this.generateConfig(settings); |
| 343 |
}); |
| 344 |
} |
| 345 |
} |