|
| 1 | +package com.github.dockerjava.client; |
| 2 | + |
| 3 | +import static org.apache.commons.io.IOUtils.closeQuietly; |
| 4 | + |
| 5 | +import java.io.File; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.InputStream; |
| 8 | +import java.io.StringWriter; |
| 9 | +import java.net.URI; |
| 10 | + |
| 11 | +import org.apache.commons.io.IOUtils; |
| 12 | +import org.apache.commons.io.LineIterator; |
| 13 | +import org.apache.http.client.HttpClient; |
| 14 | +import org.apache.http.conn.scheme.PlainSocketFactory; |
| 15 | +import org.apache.http.conn.scheme.Scheme; |
| 16 | +import org.apache.http.conn.scheme.SchemeRegistry; |
| 17 | +import org.apache.http.conn.ssl.SSLSocketFactory; |
| 18 | +import org.apache.http.impl.client.DefaultHttpClient; |
| 19 | +import org.apache.http.impl.conn.PoolingClientConnectionManager; |
| 20 | +import org.slf4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; |
| 22 | + |
| 23 | +import com.github.dockerjava.client.command.AbstrDockerCmd; |
| 24 | +import com.github.dockerjava.client.command.AuthCmd; |
| 25 | +import com.github.dockerjava.client.command.BuildImgCmd; |
| 26 | +import com.github.dockerjava.client.command.CommitCmd; |
| 27 | +import com.github.dockerjava.client.command.ContainerDiffCmd; |
| 28 | +import com.github.dockerjava.client.command.CopyFileFromContainerCmd; |
| 29 | +import com.github.dockerjava.client.command.CreateContainerCmd; |
| 30 | +import com.github.dockerjava.client.command.ImportImageCmd; |
| 31 | +import com.github.dockerjava.client.command.InfoCmd; |
| 32 | +import com.github.dockerjava.client.command.InspectContainerCmd; |
| 33 | +import com.github.dockerjava.client.command.InspectImageCmd; |
| 34 | +import com.github.dockerjava.client.command.KillContainerCmd; |
| 35 | +import com.github.dockerjava.client.command.ListContainersCmd; |
| 36 | +import com.github.dockerjava.client.command.ListImagesCmd; |
| 37 | +import com.github.dockerjava.client.command.LogContainerCmd; |
| 38 | +import com.github.dockerjava.client.command.PullImageCmd; |
| 39 | +import com.github.dockerjava.client.command.PushImageCmd; |
| 40 | +import com.github.dockerjava.client.command.RemoveContainerCmd; |
| 41 | +import com.github.dockerjava.client.command.RemoveImageCmd; |
| 42 | +import com.github.dockerjava.client.command.RestartContainerCmd; |
| 43 | +import com.github.dockerjava.client.command.SearchImagesCmd; |
| 44 | +import com.github.dockerjava.client.command.StartContainerCmd; |
| 45 | +import com.github.dockerjava.client.command.StopContainerCmd; |
| 46 | +import com.github.dockerjava.client.command.TagImageCmd; |
| 47 | +import com.github.dockerjava.client.command.TopContainerCmd; |
| 48 | +import com.github.dockerjava.client.command.VersionCmd; |
| 49 | +import com.github.dockerjava.client.command.WaitContainerCmd; |
| 50 | +import com.github.dockerjava.client.model.AuthConfig; |
| 51 | +import com.github.dockerjava.client.model.CreateContainerConfig; |
| 52 | +import com.github.dockerjava.client.utils.JsonClientFilter; |
| 53 | +import com.sun.jersey.api.client.Client; |
| 54 | +import com.sun.jersey.api.client.ClientResponse; |
| 55 | +import com.sun.jersey.api.client.WebResource; |
| 56 | +import com.sun.jersey.api.client.config.ClientConfig; |
| 57 | +import com.sun.jersey.api.client.config.DefaultClientConfig; |
| 58 | +import com.sun.jersey.api.client.filter.LoggingFilter; |
| 59 | +import com.sun.jersey.client.apache4.ApacheHttpClient4; |
| 60 | +import com.sun.jersey.client.apache4.ApacheHttpClient4Handler; |
| 61 | + |
| 62 | +/** |
| 63 | + * @author Konstantin Pelykh (kpelykh@gmail.com) |
| 64 | + */ |
| 65 | +public class DockerClient { |
| 66 | + |
| 67 | + private Client client; |
| 68 | + private WebResource baseResource; |
| 69 | + private AuthConfig authConfig; |
| 70 | + |
| 71 | + public DockerClient() throws DockerException { |
| 72 | + this(Config.createConfig()); |
| 73 | + } |
| 74 | + |
| 75 | + public DockerClient(String serverUrl) throws DockerException { |
| 76 | + this(configWithServerUrl(serverUrl)); |
| 77 | + } |
| 78 | + |
| 79 | + private static Config configWithServerUrl(String serverUrl) |
| 80 | + throws DockerException { |
| 81 | + final Config c = Config.createConfig(); |
| 82 | + c.url = URI.create(serverUrl); |
| 83 | + return c; |
| 84 | + } |
| 85 | + |
| 86 | + private DockerClient(Config config) { |
| 87 | + // restEndpointUrl = config.url + "/v" + config.version; |
| 88 | + ClientConfig clientConfig = new DefaultClientConfig(); |
| 89 | + // clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, |
| 90 | + // Boolean.TRUE); |
| 91 | + |
| 92 | + SchemeRegistry schemeRegistry = new SchemeRegistry(); |
| 93 | + schemeRegistry.register(new Scheme("http", config.url.getPort(), |
| 94 | + PlainSocketFactory.getSocketFactory())); |
| 95 | + schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory |
| 96 | + .getSocketFactory())); |
| 97 | + |
| 98 | + PoolingClientConnectionManager cm = new PoolingClientConnectionManager( |
| 99 | + schemeRegistry); |
| 100 | + // Increase max total connection |
| 101 | + cm.setMaxTotal(1000); |
| 102 | + // Increase default max connection per route |
| 103 | + cm.setDefaultMaxPerRoute(1000); |
| 104 | + |
| 105 | + HttpClient httpClient = new DefaultHttpClient(cm); |
| 106 | + client = new ApacheHttpClient4(new ApacheHttpClient4Handler(httpClient, |
| 107 | + null, false), clientConfig); |
| 108 | + |
| 109 | + client.setReadTimeout(10000); |
| 110 | + // Experimental support for unix sockets: |
| 111 | + // client = new UnixSocketClient(clientConfig); |
| 112 | + |
| 113 | + client.addFilter(new JsonClientFilter()); |
| 114 | + client.addFilter(new LoggingFilter()); |
| 115 | + |
| 116 | + baseResource = client.resource(config.url + "/v" + config.version); |
| 117 | + } |
| 118 | + |
| 119 | + public void setCredentials(String username, String password, String email) { |
| 120 | + if (username == null) { |
| 121 | + throw new IllegalArgumentException("username is null"); |
| 122 | + } |
| 123 | + if (password == null) { |
| 124 | + throw new IllegalArgumentException("password is null"); |
| 125 | + } |
| 126 | + if (email == null) { |
| 127 | + throw new IllegalArgumentException("email is null"); |
| 128 | + } |
| 129 | + authConfig = new AuthConfig(); |
| 130 | + authConfig.setUsername(username); |
| 131 | + authConfig.setPassword(password); |
| 132 | + authConfig.setEmail(email); |
| 133 | + } |
| 134 | + |
| 135 | + public <RES_T> RES_T execute(AbstrDockerCmd<?, RES_T> command) |
| 136 | + throws DockerException { |
| 137 | + return command.withBaseResource(baseResource).exec(); |
| 138 | + } |
| 139 | + |
| 140 | + public AuthConfig authConfig() throws DockerException { |
| 141 | + return authConfig != null ? authConfig : authConfigFromProperties(); |
| 142 | + } |
| 143 | + |
| 144 | + private static AuthConfig authConfigFromProperties() throws DockerException { |
| 145 | + final AuthConfig a = new AuthConfig(); |
| 146 | + |
| 147 | + a.setUsername(Config.createConfig().username); |
| 148 | + a.setPassword(Config.createConfig().password); |
| 149 | + a.setEmail(Config.createConfig().email); |
| 150 | + |
| 151 | + if (a.getUsername() == null) { |
| 152 | + throw new IllegalStateException("username is null"); |
| 153 | + } |
| 154 | + if (a.getPassword() == null) { |
| 155 | + throw new IllegalStateException("password is null"); |
| 156 | + } |
| 157 | + if (a.getEmail() == null) { |
| 158 | + throw new IllegalStateException("email is null"); |
| 159 | + } |
| 160 | + |
| 161 | + return a; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * * MISC API * |
| 166 | + */ |
| 167 | + |
| 168 | + /** |
| 169 | + * Authenticate with the server, useful for checking authentication. |
| 170 | + */ |
| 171 | + public AuthCmd authCmd() { |
| 172 | + return new AuthCmd(authConfig()).withBaseResource(baseResource); |
| 173 | + } |
| 174 | + |
| 175 | + public InfoCmd infoCmd() throws DockerException { |
| 176 | + return new InfoCmd().withBaseResource(baseResource); |
| 177 | + } |
| 178 | + |
| 179 | + public VersionCmd versionCmd() throws DockerException { |
| 180 | + return new VersionCmd().withBaseResource(baseResource); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * * IMAGE API * |
| 185 | + */ |
| 186 | + |
| 187 | + public PullImageCmd pullImageCmd(String repository) { |
| 188 | + return new PullImageCmd(repository).withBaseResource(baseResource); |
| 189 | + } |
| 190 | + |
| 191 | + public PushImageCmd pushImageCmd(String name) { |
| 192 | + return new PushImageCmd(name).withAuthConfig(authConfig()) |
| 193 | + .withBaseResource(baseResource); |
| 194 | + } |
| 195 | + |
| 196 | +// public ClientResponse pushImage(String name) { |
| 197 | +// return execute(pushImageCmd(name)); |
| 198 | +// } |
| 199 | + |
| 200 | + public ImportImageCmd importImageCmd(String repository, |
| 201 | + InputStream imageStream) { |
| 202 | + return new ImportImageCmd(repository, imageStream) |
| 203 | + .withBaseResource(baseResource); |
| 204 | + } |
| 205 | + |
| 206 | +// public ImageCreateResponse importImage(String repository, |
| 207 | +// InputStream imageStream) { |
| 208 | +// return execute(importImageCmd(repository, imageStream)); |
| 209 | +// } |
| 210 | + |
| 211 | + public SearchImagesCmd searchImagesCmd(String term) { |
| 212 | + return new SearchImagesCmd(term).withBaseResource(baseResource); |
| 213 | + } |
| 214 | + |
| 215 | +// public List<SearchItem> searchImages(String term) { |
| 216 | +// return execute(searchImagesCmd(term)); |
| 217 | +// } |
| 218 | + |
| 219 | + public RemoveImageCmd removeImageCmd(String imageId) { |
| 220 | + return new RemoveImageCmd(imageId).withBaseResource(baseResource); |
| 221 | + } |
| 222 | + |
| 223 | +// /** |
| 224 | +// * Remove an image, deleting any tags it might have. |
| 225 | +// */ |
| 226 | +// public void removeImage(String imageId) { |
| 227 | +// execute(removeImageCmd(imageId)); |
| 228 | +// } |
| 229 | +// |
| 230 | +// public void removeImages(List<String> images) { |
| 231 | +// Preconditions.checkNotNull(images, "List of images can't be null"); |
| 232 | +// |
| 233 | +// for (String imageId : images) { |
| 234 | +// removeImage(imageId); |
| 235 | +// } |
| 236 | +// } |
| 237 | + |
| 238 | + public ListImagesCmd listImagesCmd() { |
| 239 | + return new ListImagesCmd().withBaseResource(baseResource); |
| 240 | + } |
| 241 | + |
| 242 | +// public List<Image> listImages() { |
| 243 | +// return execute(listImagesCmd()); |
| 244 | +// } |
| 245 | + |
| 246 | + public InspectImageCmd inspectImageCmd(String imageId) { |
| 247 | + return new InspectImageCmd(imageId).withBaseResource(baseResource); |
| 248 | + } |
| 249 | + |
| 250 | +// public ImageInspectResponse inspectImage(String imageId) { |
| 251 | +// return execute(inspectImageCmd(imageId)); |
| 252 | +// } |
| 253 | + |
| 254 | + /** |
| 255 | + * * CONTAINER API * |
| 256 | + */ |
| 257 | + |
| 258 | + public ListContainersCmd listContainersCmd() { |
| 259 | + return new ListContainersCmd().withBaseResource(baseResource); |
| 260 | + } |
| 261 | + |
| 262 | +// public List<Container> listContainers() { |
| 263 | +// return execute(listContainersCmd()); |
| 264 | +// } |
| 265 | + |
| 266 | + public CreateContainerCmd createContainerCmd(String image) { |
| 267 | + return new CreateContainerCmd(new CreateContainerConfig()).withImage( |
| 268 | + image).withBaseResource(baseResource); |
| 269 | + } |
| 270 | + |
| 271 | + public StartContainerCmd startContainerCmd(String containerId) { |
| 272 | + return new StartContainerCmd(containerId) |
| 273 | + .withBaseResource(baseResource); |
| 274 | + } |
| 275 | + |
| 276 | + public InspectContainerCmd inspectContainerCmd(String containerId) { |
| 277 | + return new InspectContainerCmd(containerId) |
| 278 | + .withBaseResource(baseResource); |
| 279 | + } |
| 280 | + |
| 281 | + public RemoveContainerCmd removeContainerCmd(String containerId) { |
| 282 | + return new RemoveContainerCmd(containerId) |
| 283 | + .withBaseResource(baseResource); |
| 284 | + } |
| 285 | + |
| 286 | + public WaitContainerCmd waitContainerCmd(String containerId) { |
| 287 | + return new WaitContainerCmd(containerId).withBaseResource(baseResource); |
| 288 | + } |
| 289 | + |
| 290 | + public LogContainerCmd logContainerCmd(String containerId) { |
| 291 | + return new LogContainerCmd(containerId).withBaseResource(baseResource); |
| 292 | + } |
| 293 | + |
| 294 | + public CopyFileFromContainerCmd copyFileFromContainerCmd( |
| 295 | + String containerId, String resource) { |
| 296 | + return new CopyFileFromContainerCmd(containerId, resource) |
| 297 | + .withBaseResource(baseResource); |
| 298 | + } |
| 299 | + |
| 300 | + public ContainerDiffCmd containerDiffCmd(String containerId) { |
| 301 | + return new ContainerDiffCmd(containerId).withBaseResource(baseResource); |
| 302 | + } |
| 303 | + |
| 304 | + public StopContainerCmd stopContainerCmd(String containerId) { |
| 305 | + return new StopContainerCmd(containerId).withBaseResource(baseResource); |
| 306 | + } |
| 307 | + |
| 308 | + public KillContainerCmd killContainerCmd(String containerId) { |
| 309 | + return new KillContainerCmd(containerId).withBaseResource(baseResource); |
| 310 | + } |
| 311 | + |
| 312 | + public RestartContainerCmd restartContainerCmd(String containerId) { |
| 313 | + return new RestartContainerCmd(containerId) |
| 314 | + .withBaseResource(baseResource); |
| 315 | + } |
| 316 | + |
| 317 | + public CommitCmd commitCmd(String containerId) { |
| 318 | + return new CommitCmd(containerId).withBaseResource(baseResource); |
| 319 | + } |
| 320 | + |
| 321 | + public BuildImgCmd buildImageCmd(File dockerFolder) { |
| 322 | + return new BuildImgCmd(dockerFolder).withBaseResource(baseResource); |
| 323 | + } |
| 324 | + |
| 325 | + public TopContainerCmd topContainerCmd(String containerId) { |
| 326 | + return new TopContainerCmd(containerId).withBaseResource(baseResource); |
| 327 | + } |
| 328 | + |
| 329 | + public TagImageCmd tagImageCmd(String imageId, String repository, String tag) { |
| 330 | + return new TagImageCmd(imageId, repository, tag).withBaseResource(baseResource); |
| 331 | + } |
| 332 | + |
| 333 | + |
| 334 | + /** |
| 335 | + * @return The output slurped into a string. |
| 336 | + */ |
| 337 | + public static String asString(ClientResponse response) throws IOException { |
| 338 | + |
| 339 | + StringWriter out = new StringWriter(); |
| 340 | + try { |
| 341 | + LineIterator itr = IOUtils.lineIterator( |
| 342 | + response.getEntityInputStream(), "UTF-8"); |
| 343 | + while (itr.hasNext()) { |
| 344 | + String line = itr.next(); |
| 345 | + out.write(line + (itr.hasNext() ? "\n" : "")); |
| 346 | + } |
| 347 | + } finally { |
| 348 | + closeQuietly(response.getEntityInputStream()); |
| 349 | + } |
| 350 | + return out.toString(); |
| 351 | + } |
| 352 | +} |
0 commit comments