only update grafana for brand new container (no state)

This commit is contained in:
phact 2020-03-26 22:15:50 -04:00
parent 250a027647
commit a1b97a86f5
2 changed files with 64 additions and 20 deletions

View File

@ -48,27 +48,19 @@ public class DockerHelper {
public String startDocker(String IMG, String tag, String name, List<Integer> ports, List<String> volumeDescList, List<String> envList, List<String> cmdList, String reload) { public String startDocker(String IMG, String tag, String name, List<Integer> ports, List<String> volumeDescList, List<String> envList, List<String> cmdList, String reload) {
logger.debug("Starting docker with img=" + IMG + ", tag=" + tag + ", name=" + name + ", " + logger.debug("Starting docker with img=" + IMG + ", tag=" + tag + ", name=" + name + ", " +
"ports=" + ports + ", volumes=" + volumeDescList + ", env=" + envList + ", cmds=" + cmdList + ", reload=" + reload); "ports=" + ports + ", volumes=" + volumeDescList + ", env=" + envList + ", cmds=" + cmdList + ", reload=" + reload);
ListContainersCmd listContainersCmd = dockerClient.listContainersCmd().withStatusFilter(List.of("exited"));
listContainersCmd.getFilters().put("name", Arrays.asList(name)); boolean existingContainer = removeExitedContainers(name);
List<Container> stoppedContainers = null;
try { /*
stoppedContainers = listContainersCmd.exec(); if(startStoppedContainer(name)){
for (Container stoppedContainer : stoppedContainers) { return null;
String id = stoppedContainer.getId(); };
logger.info("Removing exited container: " + id); */
dockerClient.removeContainerCmd(id).exec();
}
} catch (Exception e) {
e.printStackTrace();
logger.error("Unable to contact docker, make sure docker is up and try again.");
logger.error("If docker is installed make sure this user has access to the docker group.");
logger.error("$ sudo gpasswd -a ${USER} docker && newgrp docker");
System.exit(1);
}
Container containerId = searchContainer(name, reload); Container containerId = searchContainer(name, reload);
if (containerId != null) { if (containerId != null) {
return containerId.getId(); logger.debug("container is already up with the id: "+ containerId.getId());
return null;
} }
Info info = dockerClient.infoCmd().exec(); Info info = dockerClient.infoCmd().exec();
@ -148,10 +140,59 @@ public class DockerHelper {
dockerClient.startContainerCmd(containerResponse.getId()).exec(); dockerClient.startContainerCmd(containerResponse.getId()).exec();
if (existingContainer){
logger.debug("Started existing container");
return null;
}
return containerResponse.getId(); return containerResponse.getId();
} }
private boolean startStoppedContainer(String name) {
ListContainersCmd listContainersCmd = dockerClient.listContainersCmd().withStatusFilter(List.of("stopped"));
listContainersCmd.getFilters().put("name", Arrays.asList(name));
List<Container> stoppedContainers = null;
try {
stoppedContainers = listContainersCmd.exec();
for (Container stoppedContainer : stoppedContainers) {
String id = stoppedContainer.getId();
logger.info("Removing exited container: " + id);
dockerClient.removeContainerCmd(id).exec();
}
} catch (Exception e) {
e.printStackTrace();
logger.error("Unable to contact docker, make sure docker is up and try again.");
logger.error("If docker is installed make sure this user has access to the docker group.");
logger.error("$ sudo gpasswd -a ${USER} docker && newgrp docker");
System.exit(1);
}
return false;
}
private boolean removeExitedContainers(String name) {
ListContainersCmd listContainersCmd = dockerClient.listContainersCmd().withStatusFilter(List.of("exited"));
listContainersCmd.getFilters().put("name", Arrays.asList(name));
List<Container> stoppedContainers = null;
try {
stoppedContainers = listContainersCmd.exec();
for (Container stoppedContainer : stoppedContainers) {
String id = stoppedContainer.getId();
logger.info("Removing exited container: " + id);
dockerClient.removeContainerCmd(id).exec();
return true;
}
} catch (Exception e) {
e.printStackTrace();
logger.error("Unable to contact docker, make sure docker is up and try again.");
logger.error("If docker is installed make sure this user has access to the docker group.");
logger.error("$ sudo gpasswd -a ${USER} docker && newgrp docker");
System.exit(1);
}
return false;
}
public Container searchContainer(String name, String reload) { public Container searchContainer(String name, String reload) {
ListContainersCmd listContainersCmd = dockerClient.listContainersCmd().withStatusFilter(List.of("running")); ListContainersCmd listContainersCmd = dockerClient.listContainersCmd().withStatusFilter(List.of("running"));

View File

@ -45,8 +45,6 @@ public class DockerMetricsManager {
startGrafana(ip); startGrafana(ip);
configureGrafana();
} }
private void startGrafana(String ip) { private void startGrafana(String ip) {
@ -74,10 +72,15 @@ public class DockerMetricsManager {
String reload = null; String reload = null;
String containerId = dh.startDocker(GRAFANA_IMG, tag, name, port, volumeDescList, envList, null, reload); String containerId = dh.startDocker(GRAFANA_IMG, tag, name, port, volumeDescList, envList, null, reload);
if (containerId == null){
return;
}
dh.pollLog(containerId, new LogCallback()); dh.pollLog(containerId, new LogCallback());
logger.info("grafana container started, http listening"); logger.info("grafana container started, http listening");
configureGrafana();
} }
private void startPrometheus(String ip) { private void startPrometheus(String ip) {