下面列出了org.apache.maven.plugin.PluginParameterExpressionEvaluator#com.spotify.docker.client.DockerClient 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public SupervisorFactory(final AgentModel model, final DockerClient dockerClient,
final Map<String, String> envVars,
final ServiceRegistrar registrar,
final List<ContainerDecorator> containerDecorators,
final DockerHost dockerHost,
final String host,
final SupervisorMetrics supervisorMetrics,
final String namespace,
final String defaultRegistrationDomain,
final List<String> dns) {
this.dockerClient = dockerClient;
this.namespace = namespace;
this.model = checkNotNull(model, "model");
this.envVars = checkNotNull(envVars, "envVars");
this.registrar = registrar;
this.containerDecorators = containerDecorators;
this.dockerHost = dockerHost;
this.host = host;
this.metrics = supervisorMetrics;
this.defaultRegistrationDomain = checkNotNull(defaultRegistrationDomain,
"defaultRegistrationDomain");
this.dns = checkNotNull(dns, "dns");
this.agentRunningInContainer = checkIfAgentRunningInContainer();
}
protected DockerClient buildDockerClient() throws MojoExecutionException {
final DefaultDockerClient.Builder builder;
try {
builder = getBuilder();
final String dockerHost = rawDockerHost();
if (!isNullOrEmpty(dockerHost)) {
builder.uri(dockerHost);
}
final Optional<DockerCertificatesStore> certs = dockerCertificates();
if (certs.isPresent()) {
builder.dockerCertificates(certs.get());
}
} catch (DockerCertificateException ex) {
throw new MojoExecutionException("Cannot build DockerClient due to certificate problem", ex);
}
builder.registryAuthSupplier(authSupplier());
return builder.build();
}
@Test
public void testDnsParam() throws Exception {
final String server1 = "127.0.0.1";
final String server2 = "127.0.0.2";
startDefaultMaster();
startDefaultAgent(testHost(), "--dns", server1, "--dns", server2);
awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
final JobId jobId = createJob(testJobName, testJobVersion, BUSYBOX,
asList("cat", "/etc/resolv.conf"));
deployJob(jobId, testHost());
final TaskStatus taskStatus = awaitTaskState(jobId, testHost(), EXITED);
try (final DockerClient dockerClient = getNewDockerClient()) {
final LogStream logs = dockerClient.logs(taskStatus.getContainerId(), stdout(), stderr());
final String log = logs.readFully();
assertThat(log, containsString(server1));
assertThat(log, containsString(server2));
}
}
@Test
public void testPushHubPublicImageWithAuth() throws Exception {
// Push an image to a public repo on Docker Hub and check it succeeds
final String dockerDirectory = Resources.getResource("dockerDirectory").getPath();
final RegistryAuth registryAuth = RegistryAuth.builder()
.username(HUB_AUTH_USERNAME)
.password(HUB_AUTH_PASSWORD)
.build();
final DockerClient client = DefaultDockerClient
.fromEnv()
.registryAuthSupplier(new FixedRegistryAuthSupplier(
registryAuth, RegistryConfigs.create(singletonMap(HUB_NAME, registryAuth))))
.build();
client.build(Paths.get(dockerDirectory), HUB_PUBLIC_IMAGE);
client.push(HUB_PUBLIC_IMAGE);
}
@Test
public void test() throws Exception {
final CreateJobResponse created = client.createJob(job).get();
assertEquals(CreateJobResponse.Status.OK, created.getStatus());
final JobId jobId = job.getId();
// Wait for agent to come up
awaitHostRegistered(client, testHost(), LONG_WAIT_SECONDS, SECONDS);
awaitHostStatus(client, testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
// Deploy the job on the agent
final Deployment deployment = Deployment.of(jobId, START);
final JobDeployResponse deployed = client.deploy(deployment, testHost()).get();
assertEquals(JobDeployResponse.Status.OK, deployed.getStatus());
// Wait for the job to run
final TaskStatus taskStatus = awaitJobState(
client, testHost(), jobId, RUNNING, LONG_WAIT_SECONDS, SECONDS);
try (final DockerClient docker = getNewDockerClient()) {
final HostConfig hostConfig =
docker.inspectContainer(taskStatus.getContainerId()).hostConfig();
assertEquals(NETWORK_MODE, hostConfig.networkMode());
}
}
public void testTag2() throws Exception {
final File pom = getPom("/pom-tag2.xml");
final TagMojo mojo = (TagMojo) lookupMojo("tag", pom);
assertNotNull(mojo);
final DockerClient docker = mock(DockerClient.class);
final ArgumentCaptor<String> image = ArgumentCaptor.forClass(String.class);
final ArgumentCaptor<String> name = ArgumentCaptor.forClass(String.class);
mojo.execute(docker);
verify(docker).tag(image.capture(), name.capture(), eq(false));
assertEquals("wrong image", "imageToTag", image.getValue());
final String[] split = name.getValue().split(":");
assertEquals("wrong name", "newRepo", split[0]);
assertTrue(String.format("tag '%s' should be git commit ID at least 7 characters long ",
split[1]),
split[1].length() >= 7);
}
@Test
public void testPushHubPrivateImageWithAuth() throws Exception {
// Push an image to a private repo on Docker Hub and check it succeeds
final String dockerDirectory = Resources.getResource("dockerDirectory").getPath();
final RegistryAuth registryAuth = RegistryAuth.builder()
.username(HUB_AUTH_USERNAME)
.password(HUB_AUTH_PASSWORD)
.build();
final DockerClient client = DefaultDockerClient
.fromEnv()
.registryAuthSupplier(new FixedRegistryAuthSupplier(
registryAuth, RegistryConfigs.create(singletonMap(HUB_NAME, registryAuth))))
.build();
client.build(Paths.get(dockerDirectory), HUB_PRIVATE_IMAGE);
client.push(HUB_PRIVATE_IMAGE);
}
@Test
public void testPortEnvVars() throws Exception {
startDefaultMaster();
startDefaultAgent(testHost());
awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
final Map<String, PortMapping> ports =
ImmutableMap.of("bar", staticMapping1);
try (final DockerClient dockerClient = getNewDockerClient()) {
final JobId jobId = createJob(testJobName + 1, testJobVersion, BUSYBOX,
asList("sh", "-c", "echo $HELIOS_PORT_bar"), EMPTY_ENV, ports);
deployJob(jobId, testHost());
final TaskStatus taskStatus = awaitTaskState(jobId, testHost(), EXITED);
final String log;
try (final LogStream logs = dockerClient.logs(taskStatus.getContainerId(),
stdout(), stderr())) {
log = logs.readFully();
}
assertEquals(testHost() + ":" + externalPort1, log.trim());
}
}
public void testBuildWithMultipleCompositePushTag() throws Exception {
final File pom = getPom("/pom-build-push-tags-composite.xml");
assertNotNull("Null pom.xml", pom);
assertTrue("pom.xml does not exist", pom.exists());
final BuildMojo mojo = setupMojo(pom);
final DockerClient docker = mock(DockerClient.class);
mojo.execute(docker);
verify(docker).build(eq(Paths.get("target/docker")), eq("busybox:compositeTag"),
any(AnsiProgressHandler.class));
verify(docker).push(eq("busybox:compositeTag"), any(AnsiProgressHandler.class));
verify(docker).push(eq("busybox:late"), any(AnsiProgressHandler.class));
verify(docker).push(eq("busybox:later"), any(AnsiProgressHandler.class));
verify(docker).push(eq("busybox:latest"), any(AnsiProgressHandler.class));
}
public static String findDockerImage(String imageName, DockerClient docker) {
if (docker == null) {
throw new RuntimeException("Docker client has not been initialized. No docker image can be found.");
}
try {
List<Image> allImages = docker.listImages(DockerClient.ListImagesParam.allImages());
String imageId = null;
for (Image image : allImages) {
if (image.repoTags() != null && image.repoTags().contains(imageName)) {
imageId = image.id();
break;
}
}
if (imageId == null) {
throw new RuntimeException("There was a problem when looking for the docker image with argument \""+imageName+"\": no image has been found.");
}
return imageId;
} catch (InterruptedException|DockerException e) {
throw new RuntimeException("Error while looking for the docker image",e);
}
}
/**
* In case of timeout, we kill the container.
* @param remove if true, it will remove both the container and the volumes
*/
public void killDockerContainer(DockerClient docker, boolean remove) {
if (this.containerId == null) {
LOGGER.error("Error while trying to kill docker container: the container id is not available. Maybe the container is not started yet.");
} else {
LOGGER.info("Killing the docker container with id "+containerId+". Forced killing date: "+this.limitDateBeforeKilling);
try {
docker.killContainer(containerId);
if (remove) {
docker.removeContainer(containerId);
this.removeVolumes(docker);
}
this.poolManager.removeSubmittedRunnablePipelineContainer(this);
serialize("INTERRUPTED");
} catch (DockerException|InterruptedException e) {
LOGGER.error("Error while killing docker container "+containerId, e);
}
}
}
@Override
public ValidationResult validate(Map<String, String> elasticProfile) {
final ValidationResult validationResult = new ValidationResult();
try {
final DockerSecrets dockerSecrets = DockerSecrets.fromString(elasticProfile.get("Secrets"));
if (!dockerSecrets.isEmpty()) {
DockerClient dockerClient = dockerClientFactory.docker(createAgentRequest.getClusterProfileProperties());
if (!dockerApiVersionAtLeast(dockerClient, "1.26")) {
throw new RuntimeException("Docker secret requires api version 1.26 or higher.");
}
dockerSecrets.toSecretBind(dockerClient.listSecrets());
}
} catch (Exception e) {
validationResult.addError("Secrets", e.getMessage());
}
return validationResult;
}
public void testRemoveMissingImage() throws Exception {
final File pom = getTestFile("src/test/resources/pom-removeImage.xml");
assertNotNull("Null pom.xml", pom);
assertTrue("pom.xml does not exist", pom.exists());
final RemoveImageMojo mojo = (RemoveImageMojo) lookupMojo("removeImage", pom);
assertNotNull(mojo);
final DockerClient docker = mock(DockerClient.class);
Mockito.when(docker.removeImage("imageToRemove", true, false))
.thenThrow(new ImageNotFoundException("imageToRemove"));
try {
mojo.execute(docker);
verify(docker).removeImage("imageToRemove", true, false);
} catch (DockerException e){
assertFalse("image to remove was missing", e instanceof ImageNotFoundException);
}
}
public void testBuildGeneratedDockerFile_CopiesEntireDirectory() throws Exception {
final File pom = getPom("/pom-build-copy-entire-directory.xml");
final BuildMojo mojo = setupMojo(pom);
final DockerClient docker = mock(DockerClient.class);
mojo.execute(docker);
verify(docker).build(eq(Paths.get("target/docker")), eq("test-copied-directory"),
any(AnsiProgressHandler.class));
final List<String> expectedDockerFileContents = ImmutableList.of(
"FROM busybox",
"ADD /data /data",
"ENTRYPOINT echo"
);
assertEquals("wrong dockerfile contents", expectedDockerFileContents,
Files.readAllLines(Paths.get("target/docker/Dockerfile"), UTF_8));
assertFileExists("target/docker/data/file.txt");
assertFileExists("target/docker/data/nested/file2");
}
protected List<Container> listContainers(final DockerClient dockerClient, final String needle)
throws DockerException, InterruptedException {
final List<Container> containers = dockerClient.listContainers();
final List<Container> matches = Lists.newArrayList();
for (final Container container : containers) {
if (container.names() != null) {
for (final String name : container.names()) {
if (name.contains(needle)) {
matches.add(container);
break;
}
}
}
}
return matches;
}
public GoPluginApiResponse execute() {
String elasticAgentId = request.getElasticAgentId();
JobIdentifier jobIdentifier = request.getJobIdentifier();
LOG.info(String.format("[status-report] Generating status report for agent: %s with job: %s", elasticAgentId, jobIdentifier));
try {
final DockerClient dockerClient = dockerClientFactory.docker(request.getClusterProfileProperties());
Service dockerService = findService(elasticAgentId, jobIdentifier, dockerClient);
DockerServiceElasticAgent elasticAgent = DockerServiceElasticAgent.fromService(dockerService, dockerClient);
final String statusReportView = builder.build(builder.getTemplate("agent-status-report.template.ftlh"), elasticAgent);
JsonObject responseJSON = new JsonObject();
responseJSON.addProperty("view", statusReportView);
return DefaultGoPluginApiResponse.success(responseJSON.toString());
} catch (Exception e) {
return StatusReportGenerationErrorHandler.handle(builder, e);
}
}
void stop() {
if (stopped) {
return;
}
try {
stopped = true;
System.err.println("Killing redis container with ID: " + containerId);
LogStream logs = docker.logs(containerId, DockerClient.LogsParam.stdout(), DockerClient.LogsParam.stderr());
System.err.println("Killed container logs:\n");
logs.attach(System.err, System.err);
docker.stopContainer(containerId, 5);
docker.removeContainer(containerId);
} catch (DockerException | InterruptedException | IOException e) {
System.err.println("Could not shutdown " + containerId);
e.printStackTrace();
}
}
public void testPullOnBuild() throws Exception {
final BuildMojo mojo = setupMojo(getPom("/pom-build-pull-on-build.xml"));
final DockerClient docker = mock(DockerClient.class);
mojo.execute(docker);
verify(docker).build(any(Path.class),
anyString(),
any(ProgressHandler.class),
eq(BuildParam.pullNewerImage()));
}
@Test
public void testValidHostname() throws Exception {
startDefaultMaster();
startDefaultAgent(testHost());
awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
try (final DockerClient dockerClient = getNewDockerClient()) {
final List<String> command = asList("hostname", "-f");
// Create job
final JobId jobId = createJob(Job.newBuilder()
.setName(testJobName)
.setVersion(testJobVersion)
.setImage(BUSYBOX)
.setHostname(testHost())
.setCommand(command)
.build());
// deploy
deployJob(jobId, testHost());
final TaskStatus taskStatus = awaitTaskState(jobId, testHost(), EXITED);
final String log;
try (final LogStream logs = dockerClient.logs(
taskStatus.getContainerId(), stdout(), stderr())) {
log = logs.readFully();
}
assertThat(log, containsString(testHost()));
}
}
public static synchronized DockerClient docker(PluginSettings pluginSettings) throws Exception {
if (pluginSettings.equals(DockerClientFactory.pluginSettings) && DockerClientFactory.client != null) {
return DockerClientFactory.client;
}
DockerClientFactory.pluginSettings = pluginSettings;
DockerClientFactory.client = createClient(pluginSettings);
return DockerClientFactory.client;
}
private void buildImage(final DockerClient docker, final String buildDir,
final DockerClient.BuildParam... buildParams)
throws MojoExecutionException, DockerException, IOException, InterruptedException {
getLog().info("Building image " + imageName);
docker.build(Paths.get(buildDir), imageName, new AnsiProgressHandler(), buildParams);
getLog().info("Built " + imageName);
}
@Before
public void setUp() throws Exception {
final ExecHealthCheck healthCheck = ExecHealthCheck.of("exit 0");
final Info info = mock(Info.class);
when(info.executionDriver()).thenReturn("native-0.2");
final Version version = mock(Version.class);
when(version.apiVersion()).thenReturn("1.18");
final ExecState execState = mock(ExecState.class);
when(execState.exitCode()).thenReturn(0L);
final LogStream log = mock(LogStream.class);
when(log.readFully()).thenReturn("");
docker = mock(DockerClient.class);
when(docker.info()).thenReturn(info);
when(docker.version()).thenReturn(version);
when(docker.execCreate(eq(CONTAINER_ID), any(String[].class),
(DockerClient.ExecCreateParam) anyVararg()))
.thenReturn(ExecCreation.create(EXEC_ID, emptyList()));
when(docker.execStart(eq(EXEC_ID), (ExecStartParameter) anyVararg())).thenReturn(log);
when(docker.execInspect(EXEC_ID)).thenReturn(execState);
checker = new ExecHealthChecker(healthCheck, docker);
}
private static void awaitRunning(final DockerClient client, final String containerId)
throws Exception {
Polling.await(LONG_WAIT_SECONDS, SECONDS, new Callable<Object>() {
@Override
public Object call() throws Exception {
final ContainerInfo containerInfo = client.inspectContainer(containerId);
return containerInfo.state().running() ? true : null;
}
});
}
@Bean(name = "dockerClient")
DockerClient dockerClient() {
return DefaultDockerClient.builder()
.uri(URI.create(serverUrl))
// .dockerCertificates(new DockerCertificates(Paths.get("D:/")))
.build();
}
public void testTagSkipDocker() throws Exception {
final TagMojo mojo = (TagMojo) lookupMojo("tag",
getPom("/pom-tag-skip-docker.xml"));
assertThat(mojo.isSkipDocker()).isTrue();
final TagMojo mojoSpy = spy(mojo);
mojo.execute();
verify(mojoSpy, never()).execute(any(DockerClient.class));
}
public StatusReport getStatusReport(PluginSettings pluginSettings) throws Exception {
DockerClient dockerClient = DockerClientFactory.docker(pluginSettings);
Info info = dockerClient.info();
return new StatusReport(info.osType(), info.architecture(), info.serverVersion(),
info.cpus(), readableSize(info.memTotal()), getContainerStatus(dockerClient));
}
public void testBuildWithMultiplePushTag() throws Exception {
final File pom = getPom("/pom-build-push-tags.xml");
final BuildMojo mojo = setupMojo(pom);
final DockerClient docker = mock(DockerClient.class);
mojo.execute(docker);
verify(docker).build(eq(Paths.get("target/docker")), eq("busybox"),
any(AnsiProgressHandler.class));
verify(docker).push(eq("busybox:late"), any(AnsiProgressHandler.class));
verify(docker).push(eq("busybox:later"), any(AnsiProgressHandler.class));
verify(docker).push(eq("busybox:latest"), any(AnsiProgressHandler.class));
}
@Override
protected void execute(DockerClient docker)
throws MojoExecutionException, DockerException,
IOException, InterruptedException, GitAPIException {
if (skipDockerTag) {
getLog().info("Skipping docker tag");
return;
}
final String[] repoTag = parseImageName(newName);
final String repo = repoTag[0];
String tag = repoTag[1];
if (useGitCommitId) {
if (tag != null) {
getLog().warn("Ignoring useGitCommitId flag because tag is explicitly set in image name ");
} else {
tag = new Git().getCommitId();
}
}
final String normalizedName = isNullOrEmpty(tag) ? repo : String.format("%s:%s", repo, tag);
getLog().info(String.format("Creating tag %s from %s", normalizedName, image));
docker.tag(image, normalizedName, forceTags);
final DockerBuildInformation buildInfo = new DockerBuildInformation(normalizedName, getLog());
if (pushImage) {
pushImage(docker, newName, null, getLog(), buildInfo, getRetryPushCount(),
getRetryPushTimeout(), isSkipDockerPush());
}
writeImageInfoFile(buildInfo, tagInfoFile);
}
public void testPush() throws Exception {
final File pom = getPom("/pom-push.xml");
final PushMojo mojo = (PushMojo) lookupMojo("push", pom);
assertNotNull(mojo);
final DockerClient docker = mock(DockerClient.class);
mojo.execute(docker);
verify(docker).push(eq("busybox"), any(AnsiProgressHandler.class));
}
@Test
public void testAgent() throws Exception {
startDefaultMaster("--zk-cluster-id=" + zkClusterId);
startDefaultAgent(testHost(), "--zk-cluster-id=" + zkClusterId);
awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
// Create job and deploy it
final JobId jobId = createJob(testJobName, testJobVersion, BUSYBOX, IDLE_COMMAND);
deployJob(jobId, testHost());
final TaskStatus runningStatus = awaitTaskState(jobId, testHost(), RUNNING);
final String containerId = runningStatus.getContainerId();
// Delete the config node which contains the cluster ID and all the job definitions
zk().curatorWithSuperAuth().delete().deletingChildrenIfNeeded().forPath("/config");
// Sleep for a second so agent has a chance to react to deletion
Thread.sleep(1000);
// Make sure the agent didn't stop the job
try (final DockerClient docker = getNewDockerClient()) {
final List<Container> containers = docker.listContainers();
final CustomTypeSafeMatcher<Container> containerIdMatcher =
new CustomTypeSafeMatcher<Container>("Container with id " + containerId) {
@Override
protected boolean matchesSafely(Container container) {
return container.id().equals(containerId);
}
};
assertContainersMatch(containers, containerIdMatcher);
}
}