Commit 048e08d6 authored by wutu's avatar wutu

重新构建了utils的服务接口

parent eadd3ecc
...@@ -4,12 +4,18 @@ package top.ninwoo.utils.service; ...@@ -4,12 +4,18 @@ package top.ninwoo.utils.service;
import top.ninwoo.utils.entity.DockerContainer; import top.ninwoo.utils.entity.DockerContainer;
import java.util.List; import java.util.List;
import java.util.Map;
public interface DockerService { public interface DockerService {
List<String> getDockerIds(); List<String> getDockerIds();
List<String> getDockerIds(boolean isAll); List<String> getDockerIds(boolean isAll);
void updateContainers(); void updateContainers();
void updateContainers(boolean flag); void updateContainers(boolean flag);
void getContainers();
Map<String, DockerContainer> getContainers(boolean isfull);
DockerContainer runDocker(DockerContainer container); DockerContainer runDocker(DockerContainer container);
DockerContainer getDockerById(String id); DockerContainer getDockerById(String id);
boolean deleteDockerById(String id); boolean deleteDockerById(String id);
......
package top.ninwoo.utils.service;
public interface LinuxCtlService {
String runCmd(String cmd);
}
package top.ninwoo.utils.service.impl; 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.Container;
import com.spotify.docker.client.messages.ContainerInfo; import com.spotify.docker.client.messages.ContainerInfo;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import top.ninwoo.utils.entity.DockerContainer; import top.ninwoo.utils.entity.DockerContainer;
import top.ninwoo.utils.service.DockerService; 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.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@Service @Service
public class DockerServiceImpl implements DockerService { public class DockerServiceImpl implements DockerService {
@Autowired @Autowired
private LinuxCtlService linuxService; private LinuxCtlUtils linuxCtlUtils;
@Autowired @Autowired
private DockerClient dockerClient; private DockerUtils dockerUtils;
// 这里考虑要不要区分正在运行中的容器 // 这里考虑要不要区分正在运行中的容器
private ConcurrentHashMap<String, DockerContainer> containersMap = new ConcurrentHashMap<>(); private ConcurrentHashMap<String, DockerContainer> containersMap = new ConcurrentHashMap<>();
...@@ -65,32 +66,26 @@ public class DockerServiceImpl implements DockerService { ...@@ -65,32 +66,26 @@ public class DockerServiceImpl implements DockerService {
} }
@Override @Override
public void updateContainers() { public void getContainers() {
updateContainers(true); updateContainers(true);
} }
/** /**
* 更新容器列表 * @description 更新容器列表
* @param isfull true:全量更新 * @param isfull true:全量更新
* @date 2019-10-20
* 修改该服务类为完全无状态的类,容器信息由上层集群服务维护 修改了
* @see DockerServiceImpl#updateContainers(boolean)
* @see DockerServiceImpl#updateContainers()
* @return
*/ */
@Override @Override
public void updateContainers(boolean isfull) { public Map<String, DockerContainer> getContainers(boolean isfull) {
if(isfull) { if(containersMap.isEmpty()) {
// 清空 return dockerUtils.getContainers();
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));
} else { } else {
// TODO: 增量备份 // TODO: 先执行增量更新,在返回结果
return dockerUtils.getContainers();
} }
} }
...@@ -102,30 +97,13 @@ public class DockerServiceImpl implements DockerService { ...@@ -102,30 +97,13 @@ public class DockerServiceImpl implements DockerService {
*/ */
@Override @Override
public DockerContainer runDocker(DockerContainer container) { public DockerContainer runDocker(DockerContainer container) {
// TODO: 这里启动Docker容器,需要再研究port如何起作用 container = dockerUtils.runDocker(container);
// 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);
return container; return container;
} }
@Override @Override
public DockerContainer getDockerById(String id) { public DockerContainer getDockerById(String id) {
ContainerInfo containerInfo = null; return dockerUtils.getDockerById(id);
try {
containerInfo = dockerClient.inspectContainer(id);
} catch (DockerException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return exchangeFromDockerInfo(containerInfo);
} }
public DockerContainer exchangeFromDockerInfo(ContainerInfo containerInfo) { public DockerContainer exchangeFromDockerInfo(ContainerInfo containerInfo) {
...@@ -143,15 +121,25 @@ public class DockerServiceImpl implements DockerService { ...@@ -143,15 +121,25 @@ public class DockerServiceImpl implements DockerService {
@Override @Override
public boolean deleteDockerById(String id) { public boolean deleteDockerById(String id) {
try { return dockerUtils.deleteDockerById(id);
dockerClient.stopContainer(id, 0); }
dockerClient.removeContainer(id);
return true; @Override
} catch (DockerException e) { public void updateContainers() {
e.printStackTrace(); updateContainers(true);
} catch (InterruptedException e) { }
e.printStackTrace();
@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; ...@@ -5,8 +5,8 @@ import org.springframework.stereotype.Service;
import top.ninwoo.utils.entity.BridgePort; import top.ninwoo.utils.entity.BridgePort;
import top.ninwoo.utils.entity.Ovs; import top.ninwoo.utils.entity.Ovs;
import top.ninwoo.utils.entity.OvsBridge; import top.ninwoo.utils.entity.OvsBridge;
import top.ninwoo.utils.service.LinuxCtlService;
import top.ninwoo.utils.service.OVSService; import top.ninwoo.utils.service.OVSService;
import top.ninwoo.utils.util.LinuxCtlUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -15,12 +15,12 @@ import java.util.List; ...@@ -15,12 +15,12 @@ import java.util.List;
public class OVSServiceImpl implements OVSService { public class OVSServiceImpl implements OVSService {
@Autowired @Autowired
private LinuxCtlService linuxCtlService; private LinuxCtlUtils linuxCtlUtils;
@Override @Override
public boolean isInstall() { public boolean isInstall() {
// 可能会遇到权限问题 // 可能会遇到权限问题
String s = linuxCtlService.runCmd("ovs-vsctl --version"); String s = linuxCtlUtils.runCmd("ovs-vsctl --version");
if(s.contains("Error")) { if(s.contains("Error")) {
return false; return false;
} else { } else {
...@@ -30,7 +30,7 @@ public class OVSServiceImpl implements OVSService { ...@@ -30,7 +30,7 @@ public class OVSServiceImpl implements OVSService {
@Override @Override
public Ovs showDetails() { 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); Ovs ovs = parseOvsString(res);
return ovs; return ovs;
...@@ -119,7 +119,7 @@ public class OVSServiceImpl implements OVSService { ...@@ -119,7 +119,7 @@ public class OVSServiceImpl implements OVSService {
@Override @Override
public void addBridge(String name) { public void addBridge(String name) {
String cmd = "echo 'Vudo3423' | sudo -S ovs-vsctl add-br " + 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); System.out.println(res);
if(res.contains("Error")) { if(res.contains("Error")) {
throw new RuntimeException("linux bridge has existed!"); throw new RuntimeException("linux bridge has existed!");
...@@ -129,7 +129,7 @@ public class OVSServiceImpl implements OVSService { ...@@ -129,7 +129,7 @@ public class OVSServiceImpl implements OVSService {
@Override @Override
public void setBridgeProtocol(String bridgeName, String protocol) { public void setBridgeProtocol(String bridgeName, String protocol) {
String cmd = "echo 'Vudo3423' | sudo -S ovs-vsctl set bridge " + bridgeName + " protocols=" + 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")) { if(res.contains("Error")) {
throw new RuntimeException(res); throw new RuntimeException(res);
} }
...@@ -138,7 +138,7 @@ public class OVSServiceImpl implements OVSService { ...@@ -138,7 +138,7 @@ public class OVSServiceImpl implements OVSService {
@Override @Override
public void setController(String bridgeName, String host, int port) { public void setController(String bridgeName, String host, int port) {
String cmd = "echo 'Vudo3423' | sudo -S ovs-vsctl set-controller " + bridgeName + " tcp:" + host + ":" + 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")) { if(res.contains("Error")) {
throw new RuntimeException(res); throw new RuntimeException(res);
} }
...@@ -147,7 +147,7 @@ public class OVSServiceImpl implements OVSService { ...@@ -147,7 +147,7 @@ public class OVSServiceImpl implements OVSService {
@Override @Override
public void delBridge(String name) { public void delBridge(String name) {
String cmd = "echo 'Vudo3423' | sudo -S ovs-vsctl del-br " + 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")) { if(res.contains("Error")) {
throw new RuntimeException("bridge not found!"); throw new RuntimeException("bridge not found!");
} }
......
...@@ -2,18 +2,18 @@ package top.ninwoo.utils.service.impl; ...@@ -2,18 +2,18 @@ package top.ninwoo.utils.service.impl;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import top.ninwoo.utils.service.LinuxCtlService;
import top.ninwoo.utils.service.OvsDockerService; import top.ninwoo.utils.service.OvsDockerService;
import top.ninwoo.utils.util.LinuxCtlUtils;
@Service @Service
public class OvsDockerServiceImpl implements OvsDockerService { public class OvsDockerServiceImpl implements OvsDockerService {
@Autowired @Autowired
LinuxCtlService linuxCtlService; LinuxCtlUtils linuxCtlUtils;
@Override @Override
public String addPort(String bridgeName, String devName,String containerId, String ip) { public String addPort(String bridgeName, String devName,String containerId, String ip) {
String cmd = "ovs-docker add-port " + bridgeName + " " + devName + " " + containerId + " --ipaddress=" + 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")) { if(res.contains("Error")) {
throw new RuntimeException(res); throw new RuntimeException(res);
} }
......
...@@ -9,7 +9,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; ...@@ -9,7 +9,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import top.ninwoo.utils.config.DockerConfig; import top.ninwoo.utils.config.DockerConfig;
import top.ninwoo.utils.entity.DockerContainer; import top.ninwoo.utils.entity.DockerContainer;
import top.ninwoo.utils.service.DockerService; 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.ArrayList;
import java.util.List; import java.util.List;
...@@ -18,7 +18,7 @@ import java.util.List; ...@@ -18,7 +18,7 @@ import java.util.List;
@ContextConfiguration(classes = DockerConfig.class) @ContextConfiguration(classes = DockerConfig.class)
public class DockerServiceTests { public class DockerServiceTests {
@Autowired @Autowired
private LinuxCtlService linuxCtlService; private LinuxCtlUtils linuxCtlService;
@Autowired @Autowired
private DockerService dockerService; 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