Commit 3c8e74e7 authored by joliu's avatar joliu

Merge branch '10-utils-starter' into 'feature-cnf'

Resolve "utils starter化"

See merge request xidiancos/cnf!6
parents 028a177e 318e4a90
Pipeline #108 passed with stage
in 0 seconds
......@@ -32,3 +32,6 @@ bs:
port: 6653
zookeeper:
url: zk.cnf.org:2181
cnf:
passwd: Vudo3423
......@@ -20,9 +20,21 @@
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
......
......@@ -2,6 +2,7 @@ package top.ninwoo.utils.config;
import com.spotify.docker.client.DefaultDockerClient;
import com.spotify.docker.client.DockerClient;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
......@@ -10,7 +11,8 @@ import java.net.URI;
@Configuration
@ComponentScan("top.ninwoo.utils")
public class DockerConfig {
@EnableConfigurationProperties(UtilsProperties.class)
public class CNFUtilsAutoConfiguration {
@Bean(destroyMethod = "close")
public DockerClient dockerClient() {
return DefaultDockerClient.builder()
......
package top.ninwoo.utils.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "cnf.utils")
public class UtilsProperties {
private String passwd = "Vudo3423";
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
}
......@@ -3,6 +3,7 @@ package top.ninwoo.utils.util.impl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import top.ninwoo.utils.util.LinuxCtlUtils;
import top.ninwoo.utils.util.OvsDockerUtils;
import top.ninwoo.utils.util.Utils;
......@@ -15,13 +16,17 @@ import top.ninwoo.utils.util.Utils;
@Utils
public class OvsDockerUtilsImpl implements OvsDockerUtils {
private static Logger LOG = LoggerFactory.getLogger(OvsDockerUtilsImpl.class);
@Value("${cnf.passwd}")
private String passwd;
@Autowired
LinuxCtlUtils linuxCtlUtils;
@Override
public String addPort(String bridgeName, String devName,
String containerId, String ip) {
String cmd = "ovs-docker add-port " + bridgeName
String cmd = "echo '" + passwd + "' | sudo -S ovs-docker add-port " + bridgeName
+ " " + devName + " " + containerId + " --ipaddress=" + ip;
String res = linuxCtlUtils.runCmd(cmd);
if(res.contains("Error")) {
......@@ -37,7 +42,7 @@ public class OvsDockerUtilsImpl implements OvsDockerUtils {
@Override
public String delPort(String bridgeName, String devName, String containerId) {
String cmd = "ovs-docker del-port " + bridgeName + " "
String cmd = "echo '" + passwd + "' | sudo -S ovs-docker del-port " + bridgeName + " "
+ devName + " " + containerId;
return linuxCtlUtils.runCmd(cmd);
......
......@@ -3,6 +3,7 @@ package top.ninwoo.utils.util.impl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import top.ninwoo.utils.entity.BridgePort;
import top.ninwoo.utils.entity.Ovs;
import top.ninwoo.utils.entity.OvsBridge;
......@@ -26,11 +27,13 @@ public class OvsUtilsImpl implements OvsUtils {
private final AtomicInteger atomicInteger = new AtomicInteger(0);
@Autowired
LinuxCtlUtils linuxCtlUtils;
@Value("${cnf.passwd}")
private String passwd;
@Override
public boolean isInstall() {
// 可能会遇到权限问题
String s = linuxCtlUtils.runCmd("ovs-vsctl --version");
String s = linuxCtlUtils.runCmd("echo '" + passwd + "' | sudo -S ovs-vsctl --version");
if(s.contains("Error")) {
return false;
} else {
......@@ -40,7 +43,7 @@ public class OvsUtilsImpl implements OvsUtils {
@Override
public Ovs showDetails() {
String res = linuxCtlUtils.runCmd("ovs-vsctl show");
String res = linuxCtlUtils.runCmd("echo '" + passwd + "' | sudo -S ovs-vsctl show");
Ovs ovs = parseOvsString(res);
return ovs;
......@@ -128,7 +131,7 @@ public class OvsUtilsImpl implements OvsUtils {
@Override
public void addBridge(String name) {
String cmd = "ovs-vsctl add-br " + name;
String cmd = "echo '" + passwd + "' | sudo -S ovs-vsctl add-br " + name;
String res = linuxCtlUtils.runCmd(cmd);
if(res.contains("Error")) {
throw new RuntimeException("linux bridge has existed!");
......@@ -137,7 +140,7 @@ public class OvsUtilsImpl implements OvsUtils {
@Override
public void setBridgeProtocol(String bridgeName, String protocol) {
String cmd = "ovs-vsctl set bridge " + bridgeName + " protocols=" + protocol;
String cmd = "echo '" + passwd + "' | sudo -S ovs-vsctl set bridge " + bridgeName + " protocols=" + protocol;
String res = linuxCtlUtils.runCmd(cmd);
if(res.contains("Error")) {
throw new RuntimeException(res);
......@@ -146,7 +149,7 @@ public class OvsUtilsImpl implements OvsUtils {
@Override
public void setController(String bridgeName, String host, int port) {
String cmd = "ovs-vsctl set-controller " + bridgeName + " tcp:" + host + ":" + port;
String cmd = "echo '" + passwd + "' | sudo -S ovs-vsctl set-controller " + bridgeName + " tcp:" + host + ":" + port;
String res = linuxCtlUtils.runCmd(cmd);
if(res.contains("Error")) {
throw new RuntimeException(res);
......@@ -155,7 +158,7 @@ public class OvsUtilsImpl implements OvsUtils {
@Override
public void delBridge(String name) {
String cmd = "ovs-vsctl del-br " + name;
String cmd = "echo '" + passwd + "' | sudo -S ovs-vsctl del-br " + name;
LOG.info("删除网桥[{}]", name);
String res = linuxCtlUtils.runCmd(cmd);
if(res.contains("Error")) {
......@@ -171,7 +174,7 @@ public class OvsUtilsImpl implements OvsUtils {
*/
@Override
public boolean addBridgePort(String bridgeName, String port) {
String cmd = "ovs-vsctl add-port " + bridgeName + " " + port;
String cmd = "echo '" + passwd + "' | sudo -S ovs-vsctl add-port " + bridgeName + " " + port;
String res = linuxCtlUtils.runCmd(cmd);
LOG.info("为Bridge:{} 添加Port:{}", bridgeName, port);
if(!"".equals(res)) {
......@@ -183,7 +186,7 @@ public class OvsUtilsImpl implements OvsUtils {
@Override
public boolean delBridgePort(String bridgeName, String port) {
String cmd = "ovs-vsctl del-port " + bridgeName + " " + port;
String cmd = "echo '" + passwd + "' | sudo -S ovs-vsctl del-port " + bridgeName + " " + port;
LOG.info("删除Bridge:{} Port:{}", bridgeName, port);
String res = linuxCtlUtils.runCmd(cmd);
if(!"".equals(res)) {
......@@ -203,7 +206,7 @@ public class OvsUtilsImpl implements OvsUtils {
public String[] createVethPair(String br1, String br2) {
String veth1 = br1 + "_" + br2;
String veth2 = br2 + "_" + br1;
String cmd = "ip link add " + veth1 + " type veth peer name " + veth2;
String cmd = "echo '" + passwd + "' | sudo -S ip link add " + veth1 + " type veth peer name " + veth2;
LOG.info("创建虚拟网卡对[{},{}]", veth1, veth2);
String res = linuxCtlUtils.runCmd(cmd);
if(!"".equals(res)) {
......@@ -221,7 +224,7 @@ public class OvsUtilsImpl implements OvsUtils {
public boolean delVethPort(String[] veths) {
String veth1 = veths[0];
String veth2 = veths[1];
String cmd = "ip link del " + veth1 + " type veth peer name " + veth2;
String cmd = "echo '" + passwd + "' | sudo -S ip link del " + veth1 + " type veth peer name " + veth2;
LOG.info("删除虚拟网卡对[{},{}]", veth1, veth2);
String res = linuxCtlUtils.runCmd(cmd);
if(!"".equals(res)) {
......@@ -239,7 +242,7 @@ public class OvsUtilsImpl implements OvsUtils {
*/
@Override
public boolean enableLinuxPort(String port) {
String cmd = "ifconfig " + port + " up";
String cmd = "echo '" + passwd + "' | sudo -S ifconfig " + port + " up";
LOG.info("启动Linux端口:{}",port);
String res = linuxCtlUtils.runCmd(cmd);
if(!"".equals(res)) {
......@@ -275,7 +278,7 @@ public class OvsUtilsImpl implements OvsUtils {
public boolean setVxlan(String bridgeName, String remoteIp) {
// TODO: bug
int id = atomicInteger.getAndIncrement();
String cmd = "ovs-vsctl add-port " + bridgeName
String cmd = "echo '" + passwd + "' | sudo -S ovs-vsctl add-port " + bridgeName
+ " vxlan" + id + " -- set interface vxlan" + id + " type=vxlan options:remote_ip=" + remoteIp;
System.out.println(cmd);
LOG.info("设置VXLAN[bridgeName:{},remoteIP:{},{}]", bridgeName, remoteIp,cmd);
......
......@@ -7,7 +7,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import top.ninwoo.common.entity.DockerContainer;
import top.ninwoo.utils.config.DockerConfig;
import top.ninwoo.utils.config.CNFUtilsAutoConfiguration;
import top.ninwoo.utils.service.DockerService;
import top.ninwoo.utils.util.LinuxCtlUtils;
......@@ -15,7 +15,7 @@ import java.util.ArrayList;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DockerConfig.class)
@ContextConfiguration(classes = CNFUtilsAutoConfiguration.class)
public class DockerServiceTests {
@Autowired
private LinuxCtlUtils linuxCtlService;
......
package top.ninwoo.utils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -7,24 +9,37 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.Assert;
import top.ninwoo.common.entity.DockerContainer;
import top.ninwoo.utils.config.DockerConfig;
import top.ninwoo.utils.config.CNFUtilsAutoConfiguration;
import top.ninwoo.utils.util.DockerUtils;
import java.util.ArrayList;
import java.util.List;
/**
* @Author joliu
* @Description
* @Date Create in 下午5:54 2019/10/25
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DockerConfig.class)
@ContextConfiguration(classes = CNFUtilsAutoConfiguration.class)
public class DockerUtilsTest {
private static String cid = null;
@Autowired
DockerUtils dockerUtils;
@Before
public void prepare() {
DockerContainer container = new DockerContainer();
container.setName("Router_4");
container.setImage("joliu/networktest");
container.setCommand("sh");
cid = dockerUtils.runDocker(container).getId();
}
@Test
public void testGetDockerById() {
DockerContainer dockerById = dockerUtils.getDockerById("123123");
Assert.isNull(dockerById);
DockerContainer dockerById = dockerUtils.getDockerById(cid);
Assert.isTrue(dockerById != null);
}
......@@ -32,6 +47,12 @@ public class DockerUtilsTest {
public void testExecInDocker() {
final String s =
dockerUtils.execInDocker("Router_4", new String[]{"ifconfig"});
System.out.println(s);
Assert.isTrue(!("".equals(s)));
}
@After
public void close() {
boolean result = dockerUtils.deleteDockerById(cid);
Assert.isTrue(result);
}
}
package top.ninwoo.utils;
/**
* @Author joliu
* @Description
* @Date Create in 下午10:50 2019/10/27
*/
public class EntityTests {
}
package top.ninwoo.utils;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import top.ninwoo.utils.config.DockerConfig;
import top.ninwoo.utils.service.IptablesService;
/**
* @Author joliu
* @Description
* @Date Create in 下午3:15 2019/10/30
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DockerConfig.class)
public class IptablesServiceTests {
@Autowired
IptablesService iptablesService;
}
......@@ -10,7 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import top.ninwoo.common.entity.DockerContainer;
import top.ninwoo.utils.config.DockerConfig;
import top.ninwoo.utils.config.CNFUtilsAutoConfiguration;
import top.ninwoo.utils.entity.ChainEntity;
import top.ninwoo.utils.entity.ChainType;
import top.ninwoo.utils.entity.TableType;
......@@ -26,7 +26,7 @@ import java.util.Map;
* @Date Create in 上午11:54 2019/10/29
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DockerConfig.class)
@ContextConfiguration(classes = CNFUtilsAutoConfiguration.class)
public class IptablesUtilsTests {
private static final Logger LOG = LoggerFactory.getLogger(IptablesUtilsTests.class);
......
......@@ -6,11 +6,10 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import top.ninwoo.utils.config.DockerConfig;
import top.ninwoo.utils.config.CNFUtilsAutoConfiguration;
import top.ninwoo.utils.entity.BridgePort;
import top.ninwoo.utils.entity.Ovs;
import top.ninwoo.utils.entity.OvsBridge;
import top.ninwoo.utils.service.OVSService;
import top.ninwoo.utils.util.OvsUtils;
......@@ -18,7 +17,7 @@ import java.util.ArrayList;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DockerConfig.class)
@ContextConfiguration(classes = CNFUtilsAutoConfiguration.class)
public class OVSServiceTests {
@Autowired
private OvsUtils ovsService;
......
......@@ -5,7 +5,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import top.ninwoo.utils.config.DockerConfig;
import top.ninwoo.utils.config.CNFUtilsAutoConfiguration;
import top.ninwoo.utils.util.OSUtils;
/**
......@@ -14,7 +14,7 @@ import top.ninwoo.utils.util.OSUtils;
* @Date Create in 上午10:45 2019/11/4
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DockerConfig.class)
@ContextConfiguration(classes = CNFUtilsAutoConfiguration.class)
public class OsUtilsTests {
@Autowired
......@@ -26,7 +26,7 @@ public class OsUtilsTests {
System.out.println("CPU:" + osUtils.cpuUsage() + "%");
System.out.println("MEM:" + osUtils.memoryUsage() + "%");
int i = 0;
while(i < 10) {
while(i++ < 10) {
System.out.println("NET:" + osUtils.networkUsage() + "%");
}
}
......
package top.ninwoo.utils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import top.ninwoo.utils.config.DockerConfig;
public class SpringMain {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(DockerConfig.class);
}
}
package top.ninwoo.utils;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import top.ninwoo.utils.config.DockerConfig;
import top.ninwoo.utils.util.DockerUtils;
/**
* @Author joliu
* @Description 这个测试迁移到集群服务中
* @Date Create in 下午4:06 2019/10/30
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DockerConfig.class)
public class TcUtilsTests {
@Autowired
DockerUtils dockerUtils;
@Before
public void init() {
// 创建两个容器
// 创建一个ovs网桥
}
}
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