Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
O
openXG-WIC-Cnf
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
alex037yang
openXG-WIC-Cnf
Commits
048e08d6
Commit
048e08d6
authored
Oct 20, 2019
by
wutu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
重新构建了utils的服务接口
parent
eadd3ecc
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
58 additions
and
125 deletions
+58
-125
bishe-utils/src/main/java/top/ninwoo/utils/service/DockerService.java
...src/main/java/top/ninwoo/utils/service/DockerService.java
+6
-0
bishe-utils/src/main/java/top/ninwoo/utils/service/LinuxCtlService.java
...c/main/java/top/ninwoo/utils/service/LinuxCtlService.java
+0
-5
bishe-utils/src/main/java/top/ninwoo/utils/service/impl/DockerServiceImpl.java
...java/top/ninwoo/utils/service/impl/DockerServiceImpl.java
+39
-51
bishe-utils/src/main/java/top/ninwoo/utils/service/impl/LinuxCtlServiceImpl.java
...va/top/ninwoo/utils/service/impl/LinuxCtlServiceImpl.java
+0
-56
bishe-utils/src/main/java/top/ninwoo/utils/service/impl/OVSServiceImpl.java
...in/java/top/ninwoo/utils/service/impl/OVSServiceImpl.java
+8
-8
bishe-utils/src/main/java/top/ninwoo/utils/service/impl/OvsDockerServiceImpl.java
...a/top/ninwoo/utils/service/impl/OvsDockerServiceImpl.java
+3
-3
bishe-utils/src/test/java/top/ninwoo/utils/DockerServiceTests.java
...ls/src/test/java/top/ninwoo/utils/DockerServiceTests.java
+2
-2
No files found.
bishe-utils/src/main/java/top/ninwoo/utils/service/DockerService.java
View file @
048e08d6
...
...
@@ -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
);
...
...
bishe-utils/src/main/java/top/ninwoo/utils/service/LinuxCtlService.java
deleted
100644 → 0
View file @
eadd3ecc
package
top.ninwoo.utils.service
;
public
interface
LinuxCtlService
{
String
runCmd
(
String
cmd
);
}
bishe-utils/src/main/java/top/ninwoo/utils/service/impl/DockerServiceImpl.java
View file @
048e08d6
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
LinuxCtl
Service
linuxService
;
private
LinuxCtl
Utils
linuxCtlUtils
;
@Autowired
private
Docker
Client
dockerClient
;
private
Docker
Utils
dockerUtils
;
// 这里考虑要不要区分正在运行中的容器
private
ConcurrentHashMap
<
String
,
DockerContainer
>
containersMap
=
new
ConcurrentHashMap
<>();
...
...
@@ -65,32 +66,26 @@ public class DockerServiceImpl implements DockerService {
}
@Override
public
void
update
Containers
()
{
public
void
get
Containers
()
{
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
;
}
}
bishe-utils/src/main/java/top/ninwoo/utils/service/impl/LinuxCtlServiceImpl.java
deleted
100644 → 0
View file @
eadd3ecc
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
;
}
}
bishe-utils/src/main/java/top/ninwoo/utils/service/impl/OVSServiceImpl.java
View file @
048e08d6
...
...
@@ -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
LinuxCtl
Service
linuxCtlService
;
private
LinuxCtl
Utils
linuxCtlUtils
;
@Override
public
boolean
isInstall
()
{
// 可能会遇到权限问题
String
s
=
linuxCtl
Service
.
runCmd
(
"ovs-vsctl --version"
);
String
s
=
linuxCtl
Utils
.
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
=
linuxCtl
Service
.
runCmd
(
"echo 'Vudo3423' | sudo -S ovs-vsctl show"
);
String
res
=
linuxCtl
Utils
.
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
=
linuxCtl
Service
.
runCmd
(
cmd
);
String
res
=
linuxCtl
Utils
.
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
=
linuxCtl
Service
.
runCmd
(
cmd
);
String
res
=
linuxCtl
Utils
.
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
=
linuxCtl
Service
.
runCmd
(
cmd
);
String
res
=
linuxCtl
Utils
.
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
=
linuxCtl
Service
.
runCmd
(
cmd
);
String
res
=
linuxCtl
Utils
.
runCmd
(
cmd
);
if
(
res
.
contains
(
"Error"
))
{
throw
new
RuntimeException
(
"bridge not found!"
);
}
...
...
bishe-utils/src/main/java/top/ninwoo/utils/service/impl/OvsDockerServiceImpl.java
View file @
048e08d6
...
...
@@ -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
LinuxCtl
Service
linuxCtlService
;
LinuxCtl
Utils
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
=
linuxCtl
Service
.
runCmd
(
cmd
);
String
res
=
linuxCtl
Utils
.
runCmd
(
cmd
);
if
(
res
.
contains
(
"Error"
))
{
throw
new
RuntimeException
(
res
);
}
...
...
bishe-utils/src/test/java/top/ninwoo/utils/DockerServiceTests.java
View file @
048e08d6
...
...
@@ -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
LinuxCtl
Service
linuxCtlService
;
private
LinuxCtl
Utils
linuxCtlService
;
@Autowired
private
DockerService
dockerService
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment