This repository has been archived by the owner on Jul 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Launch.tsx
203 lines (192 loc) · 5.96 KB
/
Launch.tsx
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import React, { ChangeEvent, ReactElement } from "react";
import {
Button,
CircularProgress,
Container,
Dialog,
DialogActions,
DialogContent,
DialogContentText,
DialogTitle,
FormControl,
IconButton,
InputLabel,
LinearProgress,
Link,
MenuItem,
Select,
Tooltip,
Typography
} from "@material-ui/core";
import logo from "../../assets/logo.png";
import { PlayArrow } from "@material-ui/icons";
import { makeStyles } from "@material-ui/core/styles";
import Docker from "../../docker/Docker";
import Localhost from "../../docker/Localhost";
import Dockerode from "dockerode"; // used for Dockerode.Container class
import StopIcon from "@material-ui/icons/Stop";
import { IpcRenderer } from "electron";
const Dockerode2 = window.require("dockerode"); // used for connecting to docker socket
const useStyles = makeStyles((theme) => ({
logo: {
height: 200,
width: 180
},
start: {
height: 50,
width: 50
},
centered: {
display: "flex",
flexWrap: "wrap",
justifyContent: "center",
alignItems: "center",
paddingTop: 10
},
inline: {
display: "inline-flex"
},
selector: {
width: "25%"
}
}));
const os = window.require("os");
const platform = os.platform();
const socket = {
socketPath: platform.toLowerCase().startsWith("win") ? "//./pipe/docker_engine" : "/var/run/docker.sock"
};
const dockerode = new Dockerode2(socket);
const docker = new Docker(dockerode, socket);
const localhost = new Localhost();
export default function Launch(): ReactElement {
const classes = useStyles();
const [status, setStatus] = React.useState("OFF");
const progress = status === "OFF" ? null : <LinearProgress />;
const [open, setOpen] = React.useState(false);
const [clicked, setClicked] = React.useState(false);
const [activeContainer, setActiveContainer] = React.useState<Dockerode.Container | null>(null);
const [axonVersions, setAxonVersions] = React.useState([""]);
const [requested, setRequested] = React.useState(false);
const [axonVersion, setAxonVersion] = React.useState("edge");
const getVersions = async () => {
const ipcRenderer: IpcRenderer = window.require("electron").ipcRenderer;
ipcRenderer.on("axon-tags", (event, args: string[]) => {
setAxonVersions(args);
setAxonVersion(args[0]);
});
ipcRenderer.send("request-tags");
ipcRenderer.send("request-version");
};
if (!requested) {
getVersions();
setRequested(true);
return <CircularProgress />;
}
const handleClose = () => {
setClicked(true);
};
const startContainer = async () => {
const connected = await docker.isConnected();
if (connected) {
// setPulling(true);
setStatus("Pulling Axon image");
await docker.pullImage(axonVersion);
// setPulling(false);
setStatus("Finished pulling.");
// image downloaded
const containers = await docker.getContainers();
if (containers !== null && containers.length > 0) {
setStatus("Removing old containers");
await docker.reset();
}
setStatus("Creating container");
const container = await docker.createContainer();
// setContainer(container);
// setContainerReady(true);
console.log("Container created.");
setStatus("Running container");
docker.runContainer(container).then(() => {
setStatus("OFF");
setActiveContainer(null);
});
setActiveContainer(container);
localhost.waitForStart();
} else {
setClicked(false);
}
};
docker.isConnected().then((value) => {
setOpen(!value && !clicked);
});
const stopContainer = async () => {
if (activeContainer !== null) {
if ((await activeContainer.inspect()).State.Running) {
console.log("Stopping container");
await activeContainer.stop();
}
}
};
const getButton = (running: boolean) => {
if (running) {
return (
<Tooltip title={<h3>Stop Axon</h3>} placement={"right"}>
<IconButton onClick={stopContainer}>
<StopIcon className={classes.start} />
</IconButton>
</Tooltip>
);
} else {
return (
<Tooltip title={<h3>Start Axon in browser</h3>} className={classes.inline}>
<IconButton onClick={startContainer}>
<PlayArrow className={classes.start} />
</IconButton>
</Tooltip>
);
}
};
return (
<Container>
<Dialog open={open} onClose={handleClose}>
<DialogTitle>{"Issue Connecting to Docker"}</DialogTitle>
<DialogContent>
<DialogContentText>
Axon had trouble connecting to Docker. Please ensure Docker is installed. If this issue persists, file an
issue <Link href="https://github.com/wpilibsuite/Axon/issues">here</Link>. Please include your log file.
</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={handleClose}>Cancel</Button>
</DialogActions>
</Dialog>
<div className={classes.centered}>
<Typography variant="h3" gutterBottom>
Axon Launcher
</Typography>
</div>
<div className={classes.centered}>
<img src={logo} alt={logo} className={classes.logo} />
</div>
<div className={classes.centered}>
<FormControl className={classes.selector}>
<InputLabel>Axon Version</InputLabel>
<Select
value={axonVersion}
onChange={(event: ChangeEvent<{ value: unknown }>) => setAxonVersion(event.target.value as string)}
>
{axonVersions.map((tempversion: string) => {
return (
<MenuItem key={tempversion} value={tempversion}>
{tempversion}
</MenuItem>
);
})}
</Select>
</FormControl>
{getButton(status !== "OFF")}
</div>
<div className={classes.centered}>{status !== "OFF" && <Typography>{status}</Typography>}</div>
{progress}
</Container>
);
}