openvidu-server: allow exposing ports in docker containers

pull/255/head
pabloFuente 2019-04-01 15:44:42 +02:00
parent 36047d1ca4
commit d02fd1a989
4 changed files with 77 additions and 10 deletions

View File

@ -162,7 +162,7 @@ public class ComposedRecordingService extends RecordingService {
List<Bind> binds = new ArrayList<>(); List<Bind> binds = new ArrayList<>();
binds.add(bind1); binds.add(bind1);
binds.add(bind2); binds.add(bind2);
containerId = dockerManager.runContainer(container, containerName, volumes, binds, envs); containerId = dockerManager.runContainer(container, containerName, volumes, binds, null, envs);
containers.put(containerId, containerName); containers.put(containerId, containerName);
} catch (Exception e) { } catch (Exception e) {
this.cleanRecordingMaps(recording); this.cleanRecordingMaps(recording);
@ -277,7 +277,7 @@ public class ComposedRecordingService extends RecordingService {
// Gracefully stop ffmpeg process // Gracefully stop ffmpeg process
try { try {
dockerManager.runCommandInContainer(containerId, "echo 'q' > stop"); dockerManager.runCommandInContainer(containerId, "echo 'q' > stop", 0);
} catch (InterruptedException e1) { } catch (InterruptedException e1) {
e1.printStackTrace(); e1.printStackTrace();
} }

View File

@ -637,7 +637,7 @@ public class RpcHandler extends DefaultJsonRpcHandler<JsonObject> {
@Override @Override
public void handleTransportError(Session rpcSession, Throwable exception) throws Exception { public void handleTransportError(Session rpcSession, Throwable exception) throws Exception {
log.error("Transport exception for WebSocket session: {} - Exception: {}", rpcSession.getSessionId(), log.error("Transport exception for WebSocket session: {} - Exception: {}", rpcSession.getSessionId(),
exception); exception.getMessage());
if ("IOException".equals(exception.getClass().getSimpleName()) if ("IOException".equals(exception.getClass().getSimpleName())
&& "Broken pipe".equals(exception.getCause().getMessage())) { && "Broken pipe".equals(exception.getCause().getMessage())) {
log.warn("Parcipant with private id {} unexpectedly closed the websocket", rpcSession.getSessionId()); log.warn("Parcipant with private id {} unexpectedly closed the websocket", rpcSession.getSessionId());

View File

@ -46,6 +46,26 @@ public class CustomFileManager {
} }
} }
public void moveFile(String filePath, String newFilePath, boolean deleteFoldersWhileEmpty) {
try {
FileUtils.moveFile(FileUtils.getFile(filePath), FileUtils.getFile(newFilePath));
} catch (IOException e) {
log.error("Error moving file '{}' to new path '{}': {}", filePath, newFilePath, e.getMessage());
}
if (deleteFoldersWhileEmpty) {
boolean keepDeleting = true;
File folder = new File(filePath).getParentFile();
while (keepDeleting) {
if (folder.exists() && folder.isDirectory() && folder.listFiles().length == 0) {
folder.delete();
folder = folder.getParentFile();
} else {
keepDeleting = false;
}
}
}
}
public boolean createFolderIfNotExists(String path) { public boolean createFolderIfNotExists(String path) {
File folder = new File(path); File folder = new File(path);
if (!folder.exists()) { if (!folder.exists()) {
@ -58,7 +78,7 @@ public class CustomFileManager {
public void deleteFolder(String path) throws IOException { public void deleteFolder(String path) throws IOException {
FileUtils.deleteDirectory(new File(path)); FileUtils.deleteDirectory(new File(path));
} }
public void deleteFile(String path) throws IOException { public void deleteFile(String path) throws IOException {
new File(path).delete(); new File(path).delete();
} }

View File

@ -17,6 +17,8 @@
package io.openvidu.server.utils; package io.openvidu.server.utils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -36,7 +38,11 @@ import com.github.dockerjava.api.exception.InternalServerErrorException;
import com.github.dockerjava.api.exception.NotFoundException; import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.Bind; import com.github.dockerjava.api.model.Bind;
import com.github.dockerjava.api.model.Container; import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.ExposedPort;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.HostConfig; import com.github.dockerjava.api.model.HostConfig;
import com.github.dockerjava.api.model.Ports;
import com.github.dockerjava.api.model.Ports.Binding;
import com.github.dockerjava.api.model.Volume; import com.github.dockerjava.api.model.Volume;
import com.github.dockerjava.core.DefaultDockerClientConfig; import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientBuilder; import com.github.dockerjava.core.DockerClientBuilder;
@ -116,10 +122,30 @@ public class DockerManager {
} }
public String runContainer(String container, String containerName, List<Volume> volumes, List<Bind> binds, public String runContainer(String container, String containerName, List<Volume> volumes, List<Bind> binds,
List<String> envs) throws Exception { List<Integer> exposedPorts, List<String> envs) throws Exception {
HostConfig hostConfig = new HostConfig().withNetworkMode("host").withBinds(binds);
CreateContainerCmd cmd = dockerClient.createContainerCmd(container).withName(containerName).withEnv(envs) CreateContainerCmd cmd = dockerClient.createContainerCmd(container).withName(containerName).withEnv(envs);
.withHostConfig(hostConfig).withVolumes(volumes); HostConfig hostConfig = new HostConfig().withNetworkMode("host");
if (volumes != null) {
cmd.withVolumes(volumes);
}
if (binds != null) {
hostConfig.withBinds(binds);
}
if (exposedPorts != null) {
Ports ps = new Ports();
List<ExposedPort> expPorts = new ArrayList<>();
exposedPorts.forEach(p -> {
ExposedPort port = ExposedPort.tcp(p);
expPorts.add(port);
ps.bind(port, Binding.bindPort(p));
});
hostConfig.withPortBindings(ps);
cmd.withExposedPorts(expPorts);
}
cmd.withHostConfig(hostConfig);
CreateContainerResponse response = null; CreateContainerResponse response = null;
try { try {
response = cmd.exec(); response = cmd.exec();
@ -155,10 +181,21 @@ public class DockerManager {
} }
} }
public void runCommandInContainer(String containerId, String command) throws InterruptedException { public String runCommandInContainer(String containerId, String command, int secondsOfWait)
throws InterruptedException {
ExecCreateCmdResponse execCreateCmdResponse = dockerClient.execCreateCmd(containerId).withAttachStdout(true) ExecCreateCmdResponse execCreateCmdResponse = dockerClient.execCreateCmd(containerId).withAttachStdout(true)
.withAttachStderr(true).withCmd("bash", "-c", command).exec(); .withAttachStderr(true).withCmd("bash", "-c", command).exec();
dockerClient.execStartCmd(execCreateCmdResponse.getId()).exec(new ExecStartResultCallback()).awaitCompletion(); CountDownLatch latch = new CountDownLatch(1);
final String[] stringResponse = new String[1];
dockerClient.execStartCmd(execCreateCmdResponse.getId()).exec(new ExecStartResultCallback() {
@Override
public void onNext(Frame item) {
stringResponse[0] = new String(item.getPayload());
latch.countDown();
}
});
latch.await(secondsOfWait, TimeUnit.SECONDS);
return stringResponse[0];
} }
public void waitForContainerStopped(String containerId, int secondsOfWait) throws Exception { public void waitForContainerStopped(String containerId, int secondsOfWait) throws Exception {
@ -176,4 +213,14 @@ public class DockerManager {
} }
} }
static public String getDokerGatewayIp() {
try {
return CommandExecutor.execCommand("/bin/sh", "-c",
"docker network inspect bridge --format='{{(index .IPAM.Config 0).Gateway}}'");
} catch (IOException | InterruptedException e) {
log.error(e.getMessage());
return null;
}
}
} }