forked from ceph/pulpito-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
157 lines (145 loc) · 5.27 KB
/
index.tsx
File metadata and controls
157 lines (145 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { useState } from "react";
import type { UseMutationResult, UseQueryResult } from "@tanstack/react-query";
import Button from "@mui/material/Button";
import Box from "@mui/material/Box";
import CircularProgress from "@mui/material/CircularProgress";
import DialogTitle from '@mui/material/DialogTitle';
import DialogContent from '@mui/material/DialogContent';
import Dialog from '@mui/material/Dialog';
import Paper from "@mui/material/Paper";
import Typography from "@mui/material/Typography";
import Tooltip from '@mui/material/Tooltip';
import CodeBlock from "../CodeBlock";
import type { Run as RunResponse } from "../../lib/paddles.d";
import { KillRunPayload } from "../../lib/teuthologyAPI.d";
import { useSession, useRunKill } from "../../lib/teuthologyAPI";
import Alert from "../Alert";
type KillButtonProps = {
query: UseQueryResult<RunResponse>;
};
type KillButtonDialogProps = {
mutation: UseMutationResult;
payload: KillRunPayload;
open: boolean;
handleClose: () => void;
};
export default function KillButton({query: runQuery}: KillButtonProps) {
const killMutation = useRunKill();
const [open, setOpen] = useState(false);
const sessionQuery = useSession();
const data: RunResponse | undefined = runQuery.data;
const run_owner = data?.jobs[0].owner || "";
const killPayload: KillRunPayload = {
"--run": data?.name || "",
"--owner": run_owner,
"--machine-type": data?.machine_type || "",
"--preserve-queue": true,
}
const loggedUser = sessionQuery.data?.session?.username;
const isUserAdmin = sessionQuery.data?.session?.isUserAdmin;
const owner = killPayload["--owner"].toLowerCase()
const isOwner = (loggedUser?.toLowerCase() == owner) || (`scheduled_${loggedUser?.toLowerCase()}@teuthology` == owner)
const isButtonDisabled = (!isOwner && !isUserAdmin)
const getHelperMessage = () => {
if (isButtonDisabled) {
return `User (${loggedUser}) does not have admin privileges to kill runs owned by another user (${owner}). `;
} else {
if (!isOwner && isUserAdmin) return `Use admin privileges to kill run owned by '${owner}'. `;
return "Terminate all jobs in this run";
}
}
const toggleDialog = () => {
setOpen(!open);
};
const refreshAndtoggle = () => {
if (open && !killMutation.isIdle) { // on closing confirmation dialog after kill-run
runQuery.refetch();
}
toggleDialog();
killMutation.reset();
}
if ((data?.status.includes("finished")) || !(sessionQuery.data?.session?.username)) {
// run finished or user logged out
return null;
}
return (
<div>
<div style={{ display: "flex" }}>
<Tooltip arrow title={getHelperMessage()}>
<span>
<Button
variant="contained"
color="error"
size="large"
onClick={refreshAndtoggle}
disabled={isButtonDisabled}
sx={{ marginBottom: "12px" }}
>
{(isOwner) ? "Kill Run" : "Kill Run As Admin"}
</Button>
</span>
</Tooltip>
<KillButtonDialog
mutation={killMutation}
payload={killPayload}
open={open}
handleClose={refreshAndtoggle}
/>
</div>
{ (killMutation.isError) ? <Alert severity="error" message="Unable to kill run" /> : null }
{ (killMutation.isSuccess) ? <Alert severity="success" message={`Run killed successfully! \n`} /> : null }
</div>
);
};
function KillButtonDialog({mutation, open, handleClose, payload}: KillButtonDialogProps) {
return (
<div>
<Dialog onClose={handleClose} open={open} scroll="paper" fullWidth={true} maxWidth="md">
<DialogTitle variant="h6">KILL CONFIRMATION</DialogTitle>
<DialogContent dividers>
{ (mutation.isSuccess && mutation.data ) ?
<div>
<Typography variant="h6" display="block" color="green" gutterBottom>
Successful!
</Typography>
<Paper>
<CodeBlock value={mutation.data?.data?.logs || ""} language="python" />
</Paper>
</div> :
(mutation.isLoading) ? (
<div style={{display: "flex", alignItems: "center"}}>
<CircularProgress size={20} color="inherit" style={{marginRight: "12px"}} />
<Typography variant="subtitle1" display="block">
Killing run...
</Typography>
</div>
) :
(mutation.isError) ? (
<div>
<Typography variant="h6" display="block" color="red" gutterBottom>
Failed!
</Typography>
<Paper>
<CodeBlock value={(mutation.error?.response?.data?.detail) || ""} language="python" />
</Paper>
</div>
) :
<div>
<Typography variant="overline" display="block" gutterBottom>
Are you sure you want to kill this run/job?
</Typography>
<Button
variant="contained"
color="error"
size="large"
onClick={() => mutation.mutate(payload)}
>
Yes, I'm sure
</Button>
</div>
}
</DialogContent>
</Dialog>
</div>
)
}