use of com.spotify.docker.client.DockerException in project kie-wb-common by kiegroup.
the class DockerRuntimeExecExecutor method create.
private Optional<DockerRuntime> create(final DockerRuntimeConfig runtimeConfig) throws ProvisioningException {
if (runtimeConfig.isPull()) {
try {
LOG.info("Pulling Docker Image: " + runtimeConfig.getImage());
docker.getDockerClient(runtimeConfig.getProviderId()).pull(runtimeConfig.getImage());
} catch (DockerException | InterruptedException ex) {
LOG.error(ex.getMessage(), ex);
throw new ProvisioningException("Error Pulling Docker Image: " + runtimeConfig.getImage() + "with error: " + ex.getMessage());
}
}
final String[] ports = { runtimeConfig.getPort() };
final Map<String, List<PortBinding>> portBindings = new HashMap<>();
final Optional<DockerProvider> _dockerProvider = runtimeRegistry.getProvider(runtimeConfig.getProviderId(), DockerProvider.class);
if (!_dockerProvider.isPresent()) {
return Optional.empty();
}
final DockerProvider dockerProvider = _dockerProvider.get();
final List<PortBinding> randomPort = new ArrayList<>();
final PortBinding randomPortBinding = PortBinding.randomPort(dockerProvider.getConfig().getHostIp());
randomPort.add(randomPortBinding);
portBindings.put(runtimeConfig.getPort(), randomPort);
final HostConfig hostConfig = HostConfig.builder().portBindings(portBindings).build();
final ContainerConfig containerConfig = ContainerConfig.builder().hostConfig(hostConfig).image(runtimeConfig.getImage()).exposedPorts(ports).build();
final ContainerCreation creation;
try {
creation = docker.getDockerClient(runtimeConfig.getProviderId()).createContainer(containerConfig);
docker.getDockerClient(runtimeConfig.getProviderId()).startContainer(creation.id());
} catch (DockerException | InterruptedException ex) {
LOG.error(ex.getMessage(), ex);
throw new ProvisioningException("Error Creating Docker Container with image: " + runtimeConfig.getImage() + "with error: " + ex.getMessage(), ex);
}
final String id = creation.id();
String shortId = id.substring(0, 12);
String host = "";
try {
docker.getDockerClient(runtimeConfig.getProviderId()).inspectContainer(id);
host = docker.getDockerClient(runtimeConfig.getProviderId()).getHost();
} catch (DockerException | InterruptedException ex) {
throw new ProvisioningException("Error Getting Docker Container info: " + id + "with error: " + ex.getMessage(), ex);
}
DockerRuntimeEndpoint dockerRuntimeEndpoint = new DockerRuntimeEndpoint();
dockerRuntimeEndpoint.setHost(host);
dockerRuntimeEndpoint.setPort(Integer.valueOf(runtimeConfig.getPort()));
dockerRuntimeEndpoint.setContext("");
return Optional.of(new DockerRuntime(shortId, buildRuntimeName(runtimeConfig, shortId), runtimeConfig, dockerProvider, dockerRuntimeEndpoint, new DockerRuntimeInfo(), new DockerRuntimeState(RUNNING, new Date().toString())));
}
use of com.spotify.docker.client.DockerException in project kie-wb-common by kiegroup.
the class DockerRuntimeManager method pause.
@Override
public void pause(RuntimeId runtimeId) throws RuntimeOperationException {
DockerRuntime runtime = (DockerRuntime) runtimeRegistry.getRuntimeById(runtimeId.getId());
try {
docker.getDockerClient(runtime.getProviderId()).pauseContainer(runtime.getId());
refresh(runtimeId);
} catch (DockerException | InterruptedException ex) {
LOG.error("Error Pausing container: " + runtimeId.getId(), ex);
throw new RuntimeOperationException("Error Pausing container: " + runtimeId.getId(), ex);
}
}
use of com.spotify.docker.client.DockerException in project kie-wb-common by kiegroup.
the class DockerRuntimeManager method refresh.
@Override
public void refresh(RuntimeId runtimeId) throws RuntimeOperationException {
DockerRuntime runtime = (DockerRuntime) runtimeRegistry.getRuntimeById(runtimeId.getId());
try {
ContainerInfo containerInfo = docker.getDockerClient(runtime.getProviderId()).inspectContainer(runtime.getId());
ContainerState state = containerInfo.state();
String stateString = STOPPED;
if (state.running() && !state.paused()) {
stateString = RUNNING;
} else if (state.paused()) {
stateString = "Paused";
} else if (state.restarting()) {
stateString = "Restarting";
} else if (state.oomKilled()) {
stateString = "Killed";
}
DockerRuntime newRuntime = new DockerRuntime(runtime.getId(), runtime.getName(), runtime.getConfig(), runtime.getProviderId(), runtime.getEndpoint(), runtime.getInfo(), new DockerRuntimeState(stateString, state.startedAt().toString()));
runtimeRegistry.registerRuntime(newRuntime);
} catch (DockerException | InterruptedException ex) {
LOG.error("Error Refreshing container: " + runtimeId.getId(), ex);
throw new RuntimeOperationException("Error Refreshing container: " + runtimeId.getId(), ex);
}
}
use of com.spotify.docker.client.DockerException in project kie-wb-common by kiegroup.
the class DockerRuntimeManager method stop.
@Override
public void stop(RuntimeId runtimeId) throws RuntimeOperationException {
DockerRuntime runtime = (DockerRuntime) runtimeRegistry.getRuntimeById(runtimeId.getId());
try {
LOG.info("Stopping container: " + runtimeId.getId());
docker.getDockerClient(runtime.getProviderId()).stopContainer(runtime.getId(), 1);
refresh(runtimeId);
} catch (DockerException | InterruptedException ex) {
LOG.error("Error Stopping container: " + runtimeId.getId(), ex);
throw new RuntimeOperationException("Error Stopping container: " + runtimeId.getId(), ex);
}
}
use of com.spotify.docker.client.DockerException in project kie-wb-common by kiegroup.
the class DockerAccessInterfaceImpl method buildClient.
private DockerClient buildClient(final ProviderId providerId) throws DockerException, InterruptedException {
DefaultDockerClient dockerClient;
if (providerId.getId().equals("local")) {
try {
if (System.getProperty("os.name").toLowerCase().contains("mac")) {
dockerClient = DefaultDockerClient.builder().uri(DefaultDockerClient.DEFAULT_UNIX_ENDPOINT).build();
// This test the docker client connection to see if the client was built properly
Info info = dockerClient.info();
LOG.info("Connected to Docker Client Info: " + info);
return dockerClient;
}
try {
dockerClient = DefaultDockerClient.fromEnv().build();
Info info = dockerClient.info();
LOG.info("Connected to Docker Client Info: " + info);
return dockerClient;
} catch (DockerCertificateException ex) {
throw new RuntimeException(ex);
}
} catch (DockerException | InterruptedException ex) {
try {
dockerClient = DefaultDockerClient.fromEnv().build();
Info info = dockerClient.info();
LOG.info("Connected to Docker Client Info: " + info);
return dockerClient;
} catch (DockerCertificateException e) {
throw new RuntimeException(e);
}
}
}
throw new RuntimeException("Couldn't create Docker Client, for providerId = " + providerId.getId());
}
Aggregations