forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBind.java
More file actions
155 lines (131 loc) · 5.1 KB
/
Copy pathBind.java
File metadata and controls
155 lines (131 loc) · 5.1 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
package com.github.dockerjava.api.model;
import lombok.EqualsAndHashCode;
import java.io.Serializable;
/**
* Represents a host path being bind mounted as a {@link Volume} in a Docker container.
* The Bind can be in read only or read write access mode.
*/
@EqualsAndHashCode
public class Bind implements Serializable {
private static final long serialVersionUID = 1L;
private String path;
private Volume volume;
private AccessMode accessMode;
/**
* @since {@link com.github.dockerjava.core.RemoteApiVersion#VERSION_1_23}
*/
private Boolean noCopy;
/**
* @since {@link com.github.dockerjava.core.RemoteApiVersion#VERSION_1_17}
*/
private SELContext secMode;
/**
* @since {@link com.github.dockerjava.core.RemoteApiVersion#VERSION_1_22}
*/
private PropagationMode propagationMode;
public Bind(String path, Volume volume) {
this(path, volume, AccessMode.DEFAULT, SELContext.DEFAULT);
}
public Bind(String path, Volume volume, Boolean noCopy) {
this(path, volume, AccessMode.DEFAULT, SELContext.DEFAULT, noCopy);
}
public Bind(String path, Volume volume, AccessMode accessMode) {
this(path, volume, accessMode, SELContext.DEFAULT);
}
public Bind(String path, Volume volume, AccessMode accessMode, SELContext secMode) {
this(path, volume, accessMode, secMode, null);
}
public Bind(String path, Volume volume, AccessMode accessMode, SELContext secMode, Boolean noCopy) {
this(path, volume, accessMode, secMode, noCopy, PropagationMode.DEFAULT_MODE);
}
public Bind(String path, Volume volume, AccessMode accessMode, SELContext secMode, Boolean noCopy, PropagationMode propagationMode) {
this.path = path;
this.volume = volume;
this.accessMode = accessMode;
this.secMode = secMode;
this.noCopy = noCopy;
this.propagationMode = propagationMode;
}
public String getPath() {
return path;
}
public Volume getVolume() {
return volume;
}
public AccessMode getAccessMode() {
return accessMode;
}
public SELContext getSecMode() {
return secMode;
}
public Boolean getNoCopy() {
return noCopy;
}
public PropagationMode getPropagationMode() {
return propagationMode;
}
/**
* Parses a bind mount specification to a {@link Bind}.
*
* @param serialized
* the specification, e.g. <code>/host:/container:ro</code>
* @return a {@link Bind} matching the specification
* @throws IllegalArgumentException
* if the specification cannot be parsed
*/
public static Bind parse(String serialized) {
try {
String[] parts = serialized.split(":");
switch (parts.length) {
case 2: {
return new Bind(parts[0], new Volume(parts[1]));
}
case 3: {
String[] flags = parts[2].split(",");
AccessMode accessMode = AccessMode.DEFAULT;
SELContext seMode = SELContext.DEFAULT;
Boolean nocopy = null;
PropagationMode propagationMode = PropagationMode.DEFAULT_MODE;
for (String p : flags) {
if (p.length() == 2) {
accessMode = AccessMode.valueOf(p.toLowerCase());
} else if ("nocopy".equals(p)) {
nocopy = true;
} else if (PropagationMode.SHARED.toString().equals(p)) {
propagationMode = PropagationMode.SHARED;
} else if (PropagationMode.SLAVE.toString().equals(p)) {
propagationMode = PropagationMode.SLAVE;
} else if (PropagationMode.PRIVATE.toString().equals(p)) {
propagationMode = PropagationMode.PRIVATE;
} else {
seMode = SELContext.fromString(p);
}
}
return new Bind(parts[0], new Volume(parts[1]), accessMode, seMode, nocopy, propagationMode);
}
default: {
throw new IllegalArgumentException();
}
}
} catch (Exception e) {
throw new IllegalArgumentException("Error parsing Bind '" + serialized + "'", e);
}
}
/**
* Returns a string representation of this {@link Bind} suitable for inclusion in a JSON message.
* The format is <code><host path>:<container path>:<access mode></code>,
* like the argument in {@link #parse(String)}.
*
* @return a string representation of this {@link Bind}
*/
@Override
public String toString() {
return String.format("%s:%s:%s%s%s%s",
path,
volume.getPath(),
accessMode.toString(),
secMode != SELContext.none ? "," + secMode.toString() : "",
noCopy != null ? ",nocopy" : "",
propagationMode != PropagationMode.DEFAULT_MODE ? "," + propagationMode.toString() : "");
}
}