-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildPythonBase.java
More file actions
44 lines (37 loc) · 1.78 KB
/
Copy pathBuildPythonBase.java
File metadata and controls
44 lines (37 loc) · 1.78 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
import com.wannaverse.ignition.sdk.Ignition;
import com.wannaverse.ignition.sdk.SnapshotHandle;
import com.wannaverse.ignition.sdk.Vm;
import com.wannaverse.ignition.sdk.VmImage;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
/** Run using: ignitionctl create BuildPythonBase.java */
void main() throws Exception {
/** Build the Python image */
Ignition ignition = Ignition.connect();
Duration installBudget = Duration.ofMinutes(10);
Vm builder = ignition.run("python-builder");
builder.execChecked(installBudget, "apt-get", "update");
builder.aptInstall(installBudget, "python3");
String version = builder.exec("python3", "--version").stdout().strip();
builder.kill();
builder.commit("python-base");
builder.remove();
/** Add our application to the new python image */
Path index = Files.writeString(Files.createTempDirectory("python-app").resolve("index.html"),"<h1>served by " + version + " in a microVM</h1>\n");
VmImage app = ignition.deploy("python-hello", index)
.base("python-base")
.entrypoint("python3 -m http.server 8080 --directory /opt/app")
.create();
/** Create a python vm */
Vm warmup = ignition.run("python-hello-warmup", app);
IO.println("Python app booted at http://" + warmup.ipAddress() + ":8080/");
/** Run our "hot" path a few times to warm it up */
for (int i = 0; i < 5; i++) {
warmup.execChecked(Duration.ofSeconds(10), "curl", "-sf", "-o", "/dev/null", "http://127.0.0.1:8080/index.html");
}
/** Snapshot the warm image and remove the running vm */
SnapshotHandle warm = warmup.snapshot("python-hello-warm");
warmup.remove();
// Later we can do: ignitionctl run --snapshot python-hello-warm --name python-hello-N
}