313 lines
11 KiB
TypeScript
313 lines
11 KiB
TypeScript
import { useState, type FormEvent } from 'react';
|
|
import { ApiError, request, tenantPath, type Session } from './api';
|
|
import { allowed } from './safety';
|
|
import { ErrorBox, errorText, useResource } from './resource';
|
|
import { NavIcon } from './navigation';
|
|
import {
|
|
identityNames,
|
|
interpreterNames,
|
|
latestRevisions,
|
|
loadRevisions,
|
|
parseRevision,
|
|
scriptPlatform,
|
|
type Identity,
|
|
type Interpreter,
|
|
type Revision,
|
|
type Script
|
|
} from './script-catalog';
|
|
import './script-workspace.css';
|
|
|
|
export function ScriptFields({
|
|
script,
|
|
change,
|
|
ids,
|
|
interpreters
|
|
}: {
|
|
script: Script;
|
|
change: (next: Script) => void;
|
|
ids: Identity[];
|
|
interpreters?: Interpreter[];
|
|
}) {
|
|
const possible = ids.filter(
|
|
(id) => id === 'agent_service' || id === (script.interpreter === 'powershell' ? 'system' : 'root')
|
|
);
|
|
return (
|
|
<>
|
|
<div className="work-fields">
|
|
<label>
|
|
Plattform und Interpreter
|
|
<select
|
|
value={script.interpreter}
|
|
onChange={(e) =>
|
|
change({ ...script, interpreter: e.target.value as Interpreter, execution_identity: 'agent_service' })
|
|
}
|
|
>
|
|
{Object.entries(interpreterNames)
|
|
.filter(([key]) => !interpreters || interpreters.includes(key as Interpreter))
|
|
.map(([value, label]) => (
|
|
<option value={value} key={value}>
|
|
{label}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
<label>
|
|
Ausführen als
|
|
<select
|
|
value={script.execution_identity}
|
|
onChange={(e) => change({ ...script, execution_identity: e.target.value as Identity })}
|
|
>
|
|
{possible.map((id) => (
|
|
<option key={id} value={id}>
|
|
{identityNames[id]}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
</div>
|
|
{script.execution_identity !== 'agent_service' && (
|
|
<p className="warning">
|
|
Dieses Skript fordert {identityNames[script.execution_identity]}-Rechte auf den Zielgeräten an.
|
|
</p>
|
|
)}
|
|
<label>
|
|
Skriptinhalt
|
|
<textarea
|
|
className="script-editor"
|
|
spellCheck={false}
|
|
required
|
|
rows={10}
|
|
value={script.script_utf8}
|
|
onChange={(e) => change({ ...script, script_utf8: e.target.value })}
|
|
/>
|
|
</label>
|
|
{script.interpreter === 'powershell' &&
|
|
/[^\x00-\x7f]/.test(script.script_utf8) &&
|
|
!script.script_utf8.startsWith('\ufeff') && (
|
|
<p className="warning">
|
|
Windows PowerShell 5.1 benötigt für diesen Text eine UTF-8-Signatur.{' '}
|
|
<button
|
|
type="button"
|
|
className="link"
|
|
onClick={() => change({ ...script, script_utf8: '\ufeff' + script.script_utf8 })}
|
|
>
|
|
UTF-8-Signatur hinzufügen
|
|
</button>
|
|
</p>
|
|
)}
|
|
</>
|
|
);
|
|
}
|
|
export function ScriptLibrary({ tenantId, auth }: { tenantId: string; auth: Session }) {
|
|
const read = allowed(auth, 'operations:read');
|
|
const canPublish = allowed(auth, 'operations:plan') && allowed(auth, 'scripts:execute');
|
|
const library = useResource(
|
|
(signal) => (read ? loadRevisions(tenantId, signal) : Promise.resolve([])),
|
|
[tenantId, read]
|
|
);
|
|
const [opened, open] = useState('');
|
|
const [draft, setDraft] = useState<Script | null>(null);
|
|
const [base, setBase] = useState(0);
|
|
const [busy, setBusy] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [notice, setNotice] = useState('');
|
|
const [search, setSearch] = useState('');
|
|
const [platform, setPlatform] = useState('');
|
|
const versions = (library.data ?? []).filter((item) => item.name === opened);
|
|
const latest = versions[0];
|
|
const query = search.trim().normalize('NFC').toLocaleLowerCase('de');
|
|
const shown = latestRevisions(library.data ?? []).filter((item) =>
|
|
(!platform || scriptPlatform(item.interpreter) === platform) &&
|
|
item.name.normalize('NFC').toLocaleLowerCase('de').includes(query));
|
|
function edit(existing?: Revision) {
|
|
setBase(existing?.version ?? 0);
|
|
setDraft(
|
|
existing
|
|
? { ...existing }
|
|
: { name: '', interpreter: 'posix_sh', execution_identity: 'agent_service', script_utf8: '' }
|
|
);
|
|
setError(null);
|
|
setNotice('');
|
|
}
|
|
async function publish(e: FormEvent) {
|
|
e.preventDefault();
|
|
if (!draft || busy) return;
|
|
setBusy(true);
|
|
setError(null);
|
|
try {
|
|
const { name, interpreter, execution_identity, script_utf8 } = draft;
|
|
const saved = parseRevision(
|
|
await request(tenantPath(tenantId, '/script-revisions'), {
|
|
csrf: auth.csrf_token,
|
|
body: { name: name.trim(), interpreter, execution_identity, script_utf8, expected_version: base }
|
|
})
|
|
);
|
|
open(saved.name);
|
|
setDraft(null);
|
|
setNotice(`Version ${saved.version} veröffentlicht.`);
|
|
library.reload();
|
|
} catch (err) {
|
|
setError(
|
|
err instanceof ApiError && err.code === 'SCRIPT_REVISION_CONFLICT'
|
|
? 'Unter diesem Namen gibt es bereits eine neuere Version. Öffnen Sie den aktuellen Stand vor dem Veröffentlichen.'
|
|
: errorText(err)
|
|
);
|
|
if (err instanceof ApiError && err.status === 409) library.reload();
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
}
|
|
return (
|
|
<>
|
|
<div className="page-title">
|
|
<h1>Skripte</h1>
|
|
{canPublish && !draft && !opened && (
|
|
<button className="primary" onClick={() => edit()}>
|
|
Neues Skript
|
|
</button>
|
|
)}
|
|
</div>
|
|
<ErrorBox error={error ?? library.error} />
|
|
{notice && <p role="status">{notice}</p>}
|
|
{!read ? (
|
|
<p>Die Berechtigung zum Lesen der Skriptbibliothek fehlt.</p>
|
|
) : draft ? (
|
|
<section>
|
|
<h2>{base ? 'Neue Version' : 'Neues Skript'}</h2>
|
|
<form onSubmit={publish}>
|
|
<fieldset className="work-form" disabled={busy}>
|
|
<label>
|
|
Name
|
|
<input
|
|
autoFocus
|
|
required
|
|
maxLength={120}
|
|
readOnly={base > 0}
|
|
value={draft.name}
|
|
onChange={(e) => setDraft({ ...draft, name: e.target.value })}
|
|
/>
|
|
</label>
|
|
<ScriptFields script={draft} change={setDraft} ids={['agent_service', 'root', 'system']} />
|
|
<div className="work-actions">
|
|
<button
|
|
type="button"
|
|
onClick={() => {
|
|
setError(null);
|
|
setDraft(null);
|
|
}}
|
|
>
|
|
Abbrechen
|
|
</button>
|
|
<button className="primary" type="submit">
|
|
Version veröffentlichen
|
|
</button>
|
|
</div>
|
|
</fieldset>
|
|
</form>
|
|
</section>
|
|
) : opened ? (
|
|
<section>
|
|
<button
|
|
className="link"
|
|
onClick={() => {
|
|
open('');
|
|
setNotice('');
|
|
}}
|
|
>
|
|
Zur Skriptbibliothek
|
|
</button>
|
|
{latest ? (
|
|
<>
|
|
<div className="section-heading">
|
|
<h2>{latest.name}</h2>
|
|
{canPublish && <button onClick={() => edit(latest)}>Neue Version</button>}
|
|
</div>
|
|
<p>
|
|
{interpreterNames[latest.interpreter]} · Version {latest.version} ·{' '}
|
|
{identityNames[latest.execution_identity]}
|
|
</p>
|
|
<pre className="script-body">{latest.script_utf8}</pre>
|
|
{versions.length > 1 && (
|
|
<details>
|
|
<summary>Frühere Versionen ({versions.length - 1})</summary>
|
|
{versions.slice(1).map((item) => (
|
|
<details key={item.revision_id}>
|
|
<summary>
|
|
Version {item.version} · {interpreterNames[item.interpreter]} ·{' '}
|
|
{identityNames[item.execution_identity]}
|
|
</summary>
|
|
<pre className="script-body">{item.script_utf8}</pre>
|
|
</details>
|
|
))}
|
|
</details>
|
|
)}
|
|
</>
|
|
) : (
|
|
<p role="status">Skript wird geladen …</p>
|
|
)}
|
|
</section>
|
|
) : (
|
|
<section aria-label="Skriptbibliothek" className="fleet">
|
|
<div className="filters">
|
|
<label className="search-box">
|
|
<span>Skriptname</span>
|
|
<NavIcon name="suchen"/>
|
|
<input type="search" value={search} onChange={e => setSearch(e.target.value)}
|
|
maxLength={120} placeholder="Skripte suchen …"/>
|
|
</label>
|
|
<label><span>Plattform</span>
|
|
<select aria-label="Plattform" value={platform} onChange={e => setPlatform(e.target.value)}>
|
|
<option value="">Alle Plattformen</option>
|
|
<option value="linux">Linux</option>
|
|
<option value="windows">Windows</option>
|
|
</select>
|
|
</label>
|
|
{(search || platform) && <button className="quiet" type="button" onClick={() => {
|
|
setSearch('');
|
|
setPlatform('');
|
|
}}>Filter zurücksetzen</button>}
|
|
</div>
|
|
{!library.data && !library.error ? (
|
|
<p className="list-message" role="status">Bibliothek wird geladen …</p>
|
|
) : shown.length ? (
|
|
<div className="table fleet-table script-table">
|
|
<table>
|
|
<thead>
|
|
<tr>
|
|
<th>Name</th>
|
|
<th>Plattform</th>
|
|
<th>Version</th>
|
|
<th>Ausführen als</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{shown.map((item) => (
|
|
<tr key={item.name}>
|
|
<td className="hostname">
|
|
<button
|
|
className="host-link"
|
|
onClick={() => {
|
|
open(item.name);
|
|
setNotice('');
|
|
}}
|
|
>
|
|
{item.name}
|
|
</button>
|
|
</td>
|
|
<td data-label="Plattform">{interpreterNames[item.interpreter]}</td>
|
|
<td data-label="Version">{item.version}</td>
|
|
<td data-label="Ausführen als">{identityNames[item.execution_identity]}</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
) : !library.error && <p className="list-message">{library.data?.length
|
|
? 'Keine Skripte passen zu den Filtern.'
|
|
: 'Für diesen Kunden sind noch keine Skripte veröffentlicht.'}</p>}
|
|
</section>
|
|
)}
|
|
</>
|
|
);
|
|
}
|