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
|
package io.github.jshipit;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class ContainerManager {
private String containerName;
private String containerRepo;
private String containerImage;
private String containerTag;
private String containerApiRepo;
private OCIDataStore dataStore;
public ContainerManager(String containerName, String containerImage, String containerTag, String containerApiRepo, String containerRepo, OCIDataStore dataStore) {
this.containerName = containerName;
this.containerImage = containerImage;
this.containerTag = containerTag;
this.containerApiRepo = containerApiRepo;
this.containerRepo = containerRepo;
this.dataStore = dataStore;
}
public ContainerManager(String containerName, OCIDataStore dataStore) {
this.containerName = containerName;
this.containerImage = dataStore.getContainerImage(containerName);
this.containerTag = dataStore.getContainerTag(containerName);
this.containerApiRepo = dataStore.getContainerApiRepo(containerName);
this.containerRepo = dataStore.getContainerRepo(containerName);
this.dataStore = dataStore;
}
public void createContainer() {
System.out.println("Creating container");
if (!Files.isDirectory(Paths.get(dataStore.getPath() + "/" + this.containerImage + "/" + this.containerTag))) {
System.out.println("Image does not exist");
return;
}
String containerDirectory = dataStore.createContainerDirectory(this.containerImage, this.containerTag, this.containerName, this.containerApiRepo, this.containerRepo);
new File(containerDirectory + "/containerOverlay").mkdirs();
new File(containerDirectory + "/root").mkdirs();
new File(containerDirectory+"/work").mkdirs();
}
public void startContainer() {
String containerDirectory = dataStore.getContainerPath(this.containerName);
List<String> content = null;
try {
content = Files.readAllLines(Paths.get(this.dataStore.getPath() + "/" + this.containerImage + "/" + this.containerTag + "/layers"));
} catch (IOException e) {
throw new RuntimeException(e);
}
assert content != null;
List<String> layers = new ArrayList<>();
for (String line : content) {
layers.add(this.dataStore.getPath() + "/blobs/" + line.replace("sha256:", ""));
}
assert layers.size() > 0;
SysUtils sysUtils = new SysUtils();
String[] diffs = layers.toArray(new String[0]);
sysUtils.overlayMount(diffs, containerDirectory + "/containerOverlay", containerDirectory + "/root", containerDirectory + "/work");
}
public void runCommand(String command) {
String containerDirectory = dataStore.getContainerPath(this.containerName);
String dataStorePath = dataStore.getPath();
startContainer();
File configPath = new File(dataStorePath + "/" + this.containerImage + "/" + this.containerTag + "/config");
String content = null;
try {
content = Files.readAllLines(Paths.get(configPath.getAbsolutePath())).toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
ObjectMapper mapper = new ObjectMapper();
List<String> env = new ArrayList<>();
String cmd = null;
String hostname = null;
try {
JsonNode config = mapper.readTree(content.toString()).get(0).get("config");
config.get("Env").forEach((JsonNode envVar) -> {
env.add(envVar.asText());
});
cmd = config.get("Cmd").get(0).asText();
hostname = config.get("Hostname").asText();
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
// why would these be null? I don't know, but I won't bother handling this better
assert hostname != null;
assert cmd != null;
List<String> bwrapCommand = new ArrayList<>();
for(String envVar : env) {
bwrapCommand.add("--setenv "+envVar.split("=")[0]+" "+envVar.split("=")[1]);
}
bwrapCommand.add("--ro-bind "+containerDirectory+"/root / --chdir /");
bwrapCommand.add("--share-net");
bwrapCommand.add("--hostname "+ (!hostname.isBlank() ? hostname : this.containerName+"-"+this.containerImage));
bwrapCommand.add(cmd);
SysUtils sysUtils = new SysUtils();
sysUtils.execInBwrap(bwrapCommand.toArray(new String[0]));
}
public String genContainerID() {
return UUID.randomUUID().toString();
}
}
|