Commit a9800d93 authored by wutu's avatar wutu

starter化改造

parent 028a177e
Pipeline #106 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 = "admin";
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);
......
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
top.ninwoo.utils.config.CNFUtilsAutoConfiguration
\ No newline at end of file
......@@ -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;
......
......@@ -7,7 +7,7 @@ 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;
/**
......@@ -16,7 +16,7 @@ import top.ninwoo.utils.util.DockerUtils;
* @Date Create in 下午5:54 2019/10/25
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DockerConfig.class)
@ContextConfiguration(classes = CNFUtilsAutoConfiguration.class)
public class DockerUtilsTest {
@Autowired
DockerUtils dockerUtils;
......
......@@ -4,7 +4,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.service.IptablesService;
/**
......@@ -14,7 +14,7 @@ import top.ninwoo.utils.service.IptablesService;
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DockerConfig.class)
@ContextConfiguration(classes = CNFUtilsAutoConfiguration.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
......
package top.ninwoo.utils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import top.ninwoo.utils.config.DockerConfig;
import top.ninwoo.utils.config.CNFUtilsAutoConfiguration;
public class SpringMain {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(DockerConfig.class);
new AnnotationConfigApplicationContext(CNFUtilsAutoConfiguration.class);
}
}
......@@ -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.DockerUtils;
/**
......@@ -14,7 +14,7 @@ import top.ninwoo.utils.util.DockerUtils;
* @Date Create in 下午4:06 2019/10/30
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DockerConfig.class)
@ContextConfiguration(classes = CNFUtilsAutoConfiguration.class)
public class TcUtilsTests {
@Autowired
DockerUtils dockerUtils;
......
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