forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerContextMetaFile.java
More file actions
71 lines (58 loc) · 2.4 KB
/
Copy pathDockerContextMetaFile.java
File metadata and controls
71 lines (58 loc) · 2.4 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
package com.github.dockerjava.core;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
public class DockerContextMetaFile {
private static HashFunction metaHashFunction = Hashing.sha256();
@JsonProperty("Name")
String name;
@JsonProperty("Endpoints")
Endpoints endpoints;
public static class Endpoints {
@JsonProperty("docker")
Docker docker;
public static class Docker {
@JsonProperty("Host")
String host;
@JsonProperty("SkipTLSVerify")
boolean skipTLSVerify;
}
}
public static Optional<DockerContextMetaFile> resolveContextMetaFile(ObjectMapper objectMapper, File dockerConfigPath, String context) {
final File path = dockerConfigPath.toPath()
.resolve("contexts")
.resolve("meta")
.resolve(metaHashFunction.hashString(context, StandardCharsets.UTF_8).toString())
.resolve("meta.json")
.toFile();
return Optional.ofNullable(loadContextMetaFile(objectMapper, path));
}
public static Optional<File> resolveContextTLSFile(File dockerConfigPath, String context) {
final File path = dockerConfigPath.toPath()
.resolve("contexts")
.resolve("tls")
.resolve(metaHashFunction.hashString(context, StandardCharsets.UTF_8).toString())
.resolve("docker")
.toFile();
return Optional.ofNullable(path).filter(File::exists);
}
public static DockerContextMetaFile loadContextMetaFile(ObjectMapper objectMapper, File dockerContextMetaFile) {
try {
return parseContextMetaFile(objectMapper, dockerContextMetaFile);
} catch (Exception exception) {
return null;
}
}
public static DockerContextMetaFile parseContextMetaFile(ObjectMapper objectMapper, File dockerContextMetaFile) throws IOException {
try {
return objectMapper.readValue(dockerContextMetaFile, DockerContextMetaFile.class);
} catch (IOException e) {
throw new IOException("Failed to parse docker context meta file " + dockerContextMetaFile, e);
}
}
}