-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to start and stop a database docker container for use whi… (
#36)
- Loading branch information
Showing
6 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* This Source Code Form is subject to the terms of the Mozilla Public License, | ||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under | ||
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license. | ||
* | ||
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS | ||
* graphic logo is a trademark of OpenMRS Inc. | ||
*/ | ||
package org.pih.petl; | ||
|
||
import com.github.dockerjava.api.DockerClient; | ||
import com.github.dockerjava.api.model.Container; | ||
import com.github.dockerjava.core.DefaultDockerClientConfig; | ||
import com.github.dockerjava.core.DockerClientConfig; | ||
import com.github.dockerjava.core.DockerClientImpl; | ||
import com.github.dockerjava.httpclient5.ApacheDockerHttpClient; | ||
import com.github.dockerjava.transport.DockerHttpClient; | ||
import org.apache.commons.io.IOUtils; | ||
|
||
import java.io.Closeable; | ||
import java.time.Duration; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* Utility methods useful for manipulating SQL statements | ||
*/ | ||
public class DockerConnector implements Closeable { | ||
|
||
private final DockerClientConfig dockerClientConfig; | ||
private final DockerHttpClient dockerHttpClient; | ||
private final DockerClient dockerClient; | ||
|
||
private DockerConnector() { | ||
dockerClientConfig = DefaultDockerClientConfig.createDefaultConfigBuilder().build(); | ||
dockerHttpClient = new ApacheDockerHttpClient.Builder() | ||
.dockerHost(dockerClientConfig.getDockerHost()) | ||
.sslConfig(dockerClientConfig.getSSLConfig()) | ||
.maxConnections(100) | ||
.connectionTimeout(Duration.ofSeconds(30)) | ||
.responseTimeout(Duration.ofSeconds(45)) | ||
.build(); | ||
dockerClient = DockerClientImpl.getInstance(dockerClientConfig, dockerHttpClient); | ||
} | ||
|
||
public static DockerConnector open() { | ||
return new DockerConnector(); | ||
} | ||
|
||
public void close() { | ||
IOUtils.closeQuietly(dockerClient); | ||
} | ||
|
||
public List<Container> getContainers() { | ||
return dockerClient.listContainersCmd().withShowAll(true).exec(); | ||
} | ||
|
||
public Container getContainer(String containerName) { | ||
for (Container container : getContainers()) { | ||
List<String> names = Arrays.asList(container.getNames()); | ||
if (names.contains(containerName) || names.contains("/" + containerName)) { | ||
return container; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public boolean containerExists(String containerName) { | ||
return getContainer(containerName) != null; | ||
} | ||
|
||
public boolean isContainerRunning(Container container) { | ||
return "running".equalsIgnoreCase(container.getState()); | ||
} | ||
|
||
public void startContainer(Container container) { | ||
dockerClient.startContainerCmd(container.getId()).exec(); | ||
} | ||
|
||
public void stopContainer(Container container) { | ||
dockerClient.stopContainerCmd(container.getId()).exec(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.pih.petl; | ||
|
||
import com.github.dockerjava.api.model.Container; | ||
|
||
import java.util.Arrays; | ||
|
||
public class DockerConnectorTest { | ||
|
||
public void testIsRunning() { | ||
try (DockerConnector docker = DockerConnector.open()) { | ||
for (Container container : docker.getContainers()) { | ||
System.out.println("Container: " + Arrays.asList(container.getNames())); | ||
System.out.println("Is running: " + docker.isContainerRunning(container)); | ||
} | ||
} | ||
} | ||
|
||
} |