|
|
|
@@ -0,0 +1,72 @@ |
|
|
|
package com.imitate; |
|
|
|
|
|
|
|
|
|
|
|
import com.imitate.common.bean.ShellResult; |
|
|
|
import com.imitate.common.util.ShellUtil; |
|
|
|
import com.imitate.web.WebApplication; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.runner.RunWith; |
|
|
|
import org.springframework.boot.test.context.SpringBootTest; |
|
|
|
import org.springframework.test.context.junit4.SpringRunner; |
|
|
|
import org.yaml.snakeyaml.Yaml; |
|
|
|
|
|
|
|
import java.util.LinkedList; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Set; |
|
|
|
import java.util.regex.Matcher; |
|
|
|
import java.util.regex.Pattern; |
|
|
|
|
|
|
|
|
|
|
|
@Slf4j |
|
|
|
//@SpringBootTest(classes = {WebApplication.class}) |
|
|
|
//@RunWith(SpringRunner.class) |
|
|
|
public class Test1 { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取pod详情 解析 docker容器id列表 |
|
|
|
@Test |
|
|
|
public void Test22(){ |
|
|
|
String command = "kubectl describe pod pod1"; |
|
|
|
//(docker://[A-Za-z0-9]+) |
|
|
|
ShellResult result = ShellUtil.executeAndGetExitStatus(command); |
|
|
|
//docker容器 允许存在 1个Pod 多个容器情况 |
|
|
|
List<String> dockerContainerIds = new LinkedList<>(); |
|
|
|
String reg = "docker://[A-Za-z0-9]+"; |
|
|
|
Matcher m; |
|
|
|
Pattern r; |
|
|
|
r = Pattern.compile(reg); |
|
|
|
m = r.matcher(result.getOut()); |
|
|
|
while (m.find()){ |
|
|
|
dockerContainerIds.add(m.group().replace("docker://","")); |
|
|
|
} |
|
|
|
log.info("dockerContainerIds:{}",dockerContainerIds); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
public void test33(){ |
|
|
|
String imageName = "ubuntu-vnc"; |
|
|
|
String tag = "v1.0"; |
|
|
|
String containerId = "abcdefg"; |
|
|
|
String author = "yanchao"; |
|
|
|
String comment = "ubuntu is 11"; |
|
|
|
|
|
|
|
//将容器commit成镜像 |
|
|
|
String command = String.format("docker commit -a='%s' -m='%s' %s %s:%s",author,comment,containerId,imageName,tag); |
|
|
|
ShellUtil.executeAndGetExitStatus(command); |
|
|
|
|
|
|
|
//检查镜像是否存在 |
|
|
|
String checkVersion = String.format("docker images |grep %s",imageName); |
|
|
|
ShellResult r1 = ShellUtil.executeAndGetExitStatus(checkVersion); |
|
|
|
if(r1.getOut().contains(tag)){ |
|
|
|
log.info("镜像创建成功!"); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |