Commit 048e08d6 authored by wutu's avatar wutu

重新构建了utils的服务接口

parent eadd3ecc
......@@ -4,12 +4,18 @@ package top.ninwoo.utils.service;
import top.ninwoo.utils.entity.DockerContainer;
import java.util.List;
import java.util.Map;
public interface DockerService {
List<String> getDockerIds();
List<String> getDockerIds(boolean isAll);
void updateContainers();
void updateContainers(boolean flag);
void getContainers();
Map<String, DockerContainer> getContainers(boolean isfull);
DockerContainer runDocker(DockerContainer container);
DockerContainer getDockerById(String id);
boolean deleteDockerById(String id);
......
package top.ninwoo.utils.service;
public interface LinuxCtlService {
String runCmd(String cmd);
}
package top.ninwoo.utils.service.impl;
import com.spotify.docker.client.DockerClient;
import com.spotify.docker.client.exceptions.DockerException;
import com.spotify.docker.client.messages.Container;
import com.spotify.docker.client.messages.ContainerInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.ninwoo.utils.entity.DockerContainer;
import top.ninwoo.utils.service.DockerService;
import top.ninwoo.utils.service.LinuxCtlService;
import top.ninwoo.utils.util.DockerUtils;
import top.ninwoo.utils.util.LinuxCtlUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Service
public class DockerServiceImpl implements DockerService {
@Autowired
private LinuxCtlService linuxService;
private LinuxCtlUtils linuxCtlUtils;
@Autowired
private DockerClient dockerClient;
private DockerUtils dockerUtils;
// 这里考虑要不要区分正在运行中的容器
private ConcurrentHashMap<String, DockerContainer> containersMap = new ConcurrentHashMap<>();
......@@ -65,32 +66,26 @@ public class DockerServiceImpl implements DockerService {
}
@Override
public void updateContainers() {
public void getContainers() {
updateContainers(true);
}
/**
* 更新容器列表
* @description 更新容器列表
* @param isfull true:全量更新
* @date 2019-10-20
* 修改该服务类为完全无状态的类,容器信息由上层集群服务维护 修改了
* @see DockerServiceImpl#updateContainers(boolean)
* @see DockerServiceImpl#updateContainers()
* @return
*/
@Override
public void updateContainers(boolean isfull) {
if(isfull) {
// 清空
containersMap.clear();
List<Container> containers =
null;
try {
containers = dockerClient.listContainers(DockerClient.ListContainersParam.withStatusRunning());
} catch (DockerException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
List<DockerContainer> dockerContainers = convertDockerResult(containers);
dockerContainers.forEach(dockerContainer -> containersMap.put(dockerContainer.getId(), dockerContainer));
public Map<String, DockerContainer> getContainers(boolean isfull) {
if(containersMap.isEmpty()) {
return dockerUtils.getContainers();
} else {
// TODO: 增量备份
// TODO: 先执行增量更新,在返回结果
return dockerUtils.getContainers();
}
}
......@@ -102,30 +97,13 @@ public class DockerServiceImpl implements DockerService {
*/
@Override
public DockerContainer runDocker(DockerContainer container) {
// TODO: 这里启动Docker容器,需要再研究port如何起作用
// TODO: 这里还需要处理name等属性为空的情况
String cmd = "docker run -itd --name " + container.getName() + " " + container.getImage() + " " + container.getCommand();
String result = linuxService.runCmd(cmd);
if(result.contains("Error")) {
throw new RuntimeException("Run Docker failed!:" + cmd);
}
// TODO:需要从Docker中查询处完整的信息
container.setId(result);
container = dockerUtils.runDocker(container);
return container;
}
@Override
public DockerContainer getDockerById(String id) {
ContainerInfo containerInfo = null;
try {
containerInfo = dockerClient.inspectContainer(id);
} catch (DockerException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return exchangeFromDockerInfo(containerInfo);
return dockerUtils.getDockerById(id);
}
public DockerContainer exchangeFromDockerInfo(ContainerInfo containerInfo) {
......@@ -143,15 +121,25 @@ public class DockerServiceImpl implements DockerService {
@Override
public boolean deleteDockerById(String id) {
try {
dockerClient.stopContainer(id, 0);
dockerClient.removeContainer(id);
return true;
} catch (DockerException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
return dockerUtils.deleteDockerById(id);
}
@Override
public void updateContainers() {
updateContainers(true);
}
@Override
public void updateContainers(boolean isfull) {
if(isfull) {
// 清空
containersMap.clear();
List<Container> containers =
new ArrayList<Container>();
containersMap.putAll(dockerUtils.getContainers());
} else {
// TODO: 增量备份
}
return false;
}
}
package top.ninwoo.utils.service.impl;
import org.springframework.stereotype.Service;
import top.ninwoo.utils.service.LinuxCtlService;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@Service
public class LinuxCtlServiceImpl implements LinuxCtlService {
/**
* 这里存在一个限制,不能执行复合的命令
* @param cmd
* @return
*/
@Override
// TODO: 这个函数中的异常处理是一个问题
public String runCmd(String cmd) {
String result = "";
try {
Process exec = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", cmd});
exec.waitFor();
InputStream in = exec.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String msg;
while((msg = br.readLine())!= null && msg.length() > 0) {
result += msg + "\n";
}
if("".equals(result)) {
// 构建Error
InputStream errorStream = exec.getErrorStream();
BufferedReader ebr = new BufferedReader(new InputStreamReader(errorStream));
String eMsg;
while((eMsg = ebr.readLine())!= null && eMsg.length() > 0) {
result += eMsg + "\n";
}
if(result.equals("")) {
return result;
}
result = "Error: " + result;
}
if(result.length() > 0) {
result = result.substring(0, result.length()-1);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return result;
}
}
......@@ -5,8 +5,8 @@ import org.springframework.stereotype.Service;
import top.ninwoo.utils.entity.BridgePort;
import top.ninwoo.utils.entity.Ovs;
import top.ninwoo.utils.entity.OvsBridge;
import top.ninwoo.utils.service.LinuxCtlService;
import top.ninwoo.utils.service.OVSService;
import top.ninwoo.utils.util.LinuxCtlUtils;
import java.util.ArrayList;
import java.util.List;
......@@ -15,12 +15,12 @@ import java.util.List;
public class OVSServiceImpl implements OVSService {
@Autowired
private LinuxCtlService linuxCtlService;
private LinuxCtlUtils linuxCtlUtils;
@Override
public boolean isInstall() {
// 可能会遇到权限问题
String s = linuxCtlService.runCmd("ovs-vsctl --version");
String s = linuxCtlUtils.runCmd("ovs-vsctl --version");
if(s.contains("Error")) {
return false;
} else {
......@@ -30,7 +30,7 @@ public class OVSServiceImpl implements OVSService {
@Override
public Ovs showDetails() {
String res = linuxCtlService.runCmd("echo 'Vudo3423' | sudo -S ovs-vsctl show");
String res = linuxCtlUtils.runCmd("echo 'Vudo3423' | sudo -S ovs-vsctl show");
Ovs ovs = parseOvsString(res);
return ovs;
......@@ -119,7 +119,7 @@ public class OVSServiceImpl implements OVSService {
@Override
public void addBridge(String name) {
String cmd = "echo 'Vudo3423' | sudo -S ovs-vsctl add-br " + name;
String res = linuxCtlService.runCmd(cmd);
String res = linuxCtlUtils.runCmd(cmd);
System.out.println(res);
if(res.contains("Error")) {
throw new RuntimeException("linux bridge has existed!");
......@@ -129,7 +129,7 @@ public class OVSServiceImpl implements OVSService {
@Override
public void setBridgeProtocol(String bridgeName, String protocol) {
String cmd = "echo 'Vudo3423' | sudo -S ovs-vsctl set bridge " + bridgeName + " protocols=" + protocol;
String res = linuxCtlService.runCmd(cmd);
String res = linuxCtlUtils.runCmd(cmd);
if(res.contains("Error")) {
throw new RuntimeException(res);
}
......@@ -138,7 +138,7 @@ public class OVSServiceImpl implements OVSService {
@Override
public void setController(String bridgeName, String host, int port) {
String cmd = "echo 'Vudo3423' | sudo -S ovs-vsctl set-controller " + bridgeName + " tcp:" + host + ":" + port;
String res = linuxCtlService.runCmd(cmd);
String res = linuxCtlUtils.runCmd(cmd);
if(res.contains("Error")) {
throw new RuntimeException(res);
}
......@@ -147,7 +147,7 @@ public class OVSServiceImpl implements OVSService {
@Override
public void delBridge(String name) {
String cmd = "echo 'Vudo3423' | sudo -S ovs-vsctl del-br " + name;
String res = linuxCtlService.runCmd(cmd);
String res = linuxCtlUtils.runCmd(cmd);
if(res.contains("Error")) {
throw new RuntimeException("bridge not found!");
}
......
......@@ -2,18 +2,18 @@ package top.ninwoo.utils.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.ninwoo.utils.service.LinuxCtlService;
import top.ninwoo.utils.service.OvsDockerService;
import top.ninwoo.utils.util.LinuxCtlUtils;
@Service
public class OvsDockerServiceImpl implements OvsDockerService {
@Autowired
LinuxCtlService linuxCtlService;
LinuxCtlUtils linuxCtlUtils;
@Override
public String addPort(String bridgeName, String devName,String containerId, String ip) {
String cmd = "ovs-docker add-port " + bridgeName + " " + devName + " " + containerId + " --ipaddress=" + ip;
String res = linuxCtlService.runCmd(cmd);
String res = linuxCtlUtils.runCmd(cmd);
if(res.contains("Error")) {
throw new RuntimeException(res);
}
......
......@@ -9,7 +9,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import top.ninwoo.utils.config.DockerConfig;
import top.ninwoo.utils.entity.DockerContainer;
import top.ninwoo.utils.service.DockerService;
import top.ninwoo.utils.service.LinuxCtlService;
import top.ninwoo.utils.util.LinuxCtlUtils;
import java.util.ArrayList;
import java.util.List;
......@@ -18,7 +18,7 @@ import java.util.List;
@ContextConfiguration(classes = DockerConfig.class)
public class DockerServiceTests {
@Autowired
private LinuxCtlService linuxCtlService;
private LinuxCtlUtils linuxCtlService;
@Autowired
private DockerService dockerService;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment