Commit 1bbd11fa authored by YangZhaoJun1's avatar YangZhaoJun1

Merge branch 'master' of http://103.249.252.109:90/lcy/education

parents c42850d4 b1ae207b
......@@ -336,7 +336,12 @@
<version>2.1</version>
</dependency>
<dependency>
<groupId>com.ganymed.ssh2</groupId>
<artifactId>ganymed-ssh2-build</artifactId>
<version>210</version>
</dependency>
</dependencies>
......
......@@ -49,6 +49,9 @@ public class BCodeLxcServiceImpl implements BCodeLxcService {
@Autowired
private SchedulerFactoryBean schedulerFactoryBean;
@Autowired
private LxcCommand lxcCommand;
@Override
public CommandResult startBCodeLxc(BCodeLxc lxc) {
......@@ -83,7 +86,7 @@ public class BCodeLxcServiceImpl implements BCodeLxcService {
if(!result.isSuccess())
return result;
/*2.docker-compose 起容器*/
result = LxcCommand.startOrDownLxc(destPath);
result = lxcCommand.startOrDownLxc(destPath);
if(!result.isSuccess())
return result;
/*3.redis 放置状态*/
......
......@@ -23,6 +23,8 @@ public class StopLxcJob implements Job {
@Autowired
private RcRedisService rcRedisServiceImpl;
@Autowired
private LxcCommand lxcCommand;
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
/*1.容器名称*/
......@@ -41,7 +43,7 @@ public class StopLxcJob implements Job {
/*关闭容器*/
/*2.docker-compose 起容器*/
CommandResult commandResult = null;
commandResult = LxcCommand.startOrDownLxc(containerPath,false);
commandResult = lxcCommand.startOrDownLxc(containerPath,false);
/*3.移除端口*/
}
......
......@@ -3,6 +3,8 @@ package org.rcisoft.core.command;
import lombok.extern.slf4j.Slf4j;
import org.rcisoft.core.result.CommandResult;
import org.rcisoft.core.result.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.File;
......@@ -12,8 +14,12 @@ import java.io.File;
* 开启容器 命令
*/
@Slf4j
@Component
public class LxcCommand {
@Autowired
private RemoteExecuteCommand remoteExecuteCommand;
/**
* 开启容器
*/
......@@ -27,10 +33,13 @@ public class LxcCommand {
*/
public static final String LXC_SETTING_NAME = "docker-compose.yml";
public CommandResult startOrDownLxc(String dest){
return this.startOrDownLxc(dest,true);
}
public static CommandResult startOrDownLxc(String dest){
return LxcCommand.startOrDownLxc(dest,true);
public CommandResult startOrDownLxc(String dest,boolean isStart){
return this.startOrDownLxc(dest,true,true);
}
/**
......@@ -38,7 +47,7 @@ public class LxcCommand {
* @param dest
* @param isStart
*/
public static CommandResult startOrDownLxc(String dest,boolean isStart){
public CommandResult startOrDownLxc(String dest,boolean isStart,boolean isRemote){
CommandResult commandResult = null;
try {
File dirPath = new File(dest);
......@@ -48,9 +57,15 @@ public class LxcCommand {
return commandResult;
}
if (isStart)
if (isRemote)
remoteExecuteCommand.execute(START_LXC_COMMAND,dest);
else
Runtime.getRuntime().exec("cd " + dest + " && " + START_LXC_COMMAND);
else{
Runtime.getRuntime().exec("cd " + dest + " && " + DOWN_LXC_COMMAND);
if (isRemote)
remoteExecuteCommand.execute(START_LXC_COMMAND,dest);
else
Runtime.getRuntime().exec("cd " + dest + " && " + DOWN_LXC_COMMAND);
dirPath.delete();
}
commandResult = new CommandResult(ResultCode.SUCCESS,null,null);
......
package org.rcisoft.core.command;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.*;
/**
* Created by lcy on 18/1/11.
*/
@Data
@NoArgsConstructor
@Slf4j
@Component
public class RemoteExecuteCommand {
//字符编码默认是utf-8
private static String DEFAULT_CHART="UTF-8";
private Connection conn;
@Value("${serverLxc.ip}")
private String ip;
@Value("${serverLxc.username}")
private String userName;
@Value("${serverLxc.password}")
private String userPwd;
/**
* 远程登录linux的主机
* @author Ickes
* @since V0.1
* @return
* 登录成功返回true,否则返回false
*/
private Boolean login(){
boolean flg=false;
try {
conn = new Connection(ip);
conn.connect();//连接
flg=conn.authenticateWithPassword(userName, userPwd);//认证
} catch (IOException e) {
e.printStackTrace();
}
return flg;
}
/**
* @author Ickes
* 远程执行shll脚本或者命令
* @param cmd
* 即将执行的命令
* @return
* 命令执行完后返回的结果值
* @since V0.1
*/
public String execute(String cmd){
log.info("执行:{}",cmd);
String result="";
int exceTime=0;
try {
if(login()){ //这里偶尔会由于网络原因抛出异常
Session session= conn.openSession();//打开一个会话
session.execCommand(cmd);//执行命令
result=processStdout(session.getStdout(),DEFAULT_CHART);
//如果为得到标准输出为空,说明脚本执行出错了
if(StringUtils.isBlank(result)){
result=processStdout(session.getStderr(),DEFAULT_CHART);
}
session.close();
}
} catch (Exception e) {
e.printStackTrace();
if(exceTime<10) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
exceTime++;
return execute(cmd); //异常了就循环执行方法,直到正常了才返回
}else{
//throw new BusinessException(ResultEnums.NETWORK_ERROR);
}
}finally {
conn.close();
}
log.info("返回:{}",result);
return result;
}
/**
* @author Ickes
* 远程执行shll脚本或者命令
* @param cmd
* 即将执行的命令
* @param location
* 即将执行命令的所在位置
* @return
* 命令执行完后返回的结果值
* @since V0.1
*/
public String execute(String cmd,String location){
return execute("cd "+location+" && "+cmd);
}
/**
* @author Ickes
* 远程执行shll脚本或者命令
* @param cmd
* 即将执行的命令
* @return
* 命令执行成功后返回的结果值,如果命令执行失败,返回空字符串,不是null
* @since V0.1
*/
public String executeSuccess(String cmd){
String result="";
try {
if(login()){
Session session= conn.openSession();//打开一个会话
session.execCommand(cmd);//执行命令
result=processStdout(session.getStdout(),DEFAULT_CHART);
session.close();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
conn.close();
}
return result;
}
/**
* 解析脚本执行返回的结果集
* @author Ickes
* @param in 输入流对象
* @param charset 编码
* @since V0.1
* @return
* 以纯文本的格式返回
*/
private String processStdout(InputStream in, String charset){
InputStream stdout = new StreamGobbler(in);
StringBuffer buffer = new StringBuffer();;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(stdout,charset));
String line=null;
while((line=br.readLine()) != null){
buffer.append(line+"\n");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer.toString();
}
public static void main(String[] args) {
//RemoteExecuteCommand remoteExecuteCommand = new RemoteExecuteCommand("106.2.3.134","root","91isoft_xunda");
String branch = "master";
String localUrl = "/working/dockervolume/chedir/project/p_201809004";
/*String branchs = remoteExecuteCommand.execute(GitCommands.GIT_BRANCH,localUrl);
String[] branchArr = branchs.split("\n");
for(String item:branchArr){
if(item.startsWith("*")){
System.out.println(item+" "+branch);
System.out.println(item.replace("* ",""));
System.out.println(item.replace("* ","").equals(branch));
if(!item.replace("* ","").equals(branch)){
break;
}
return;
}
}
System.out.println("aaaa");*/
//remoteExecuteCommand.execute(GitCommands.getGitCheckoutCommand(branch),localUrl);
}
}
......@@ -153,3 +153,8 @@ global:
html_simple: '1001'
html_project: '1002'
serverLxc:
ip: 127.0.0.1
username: root
password: 123456
......@@ -150,4 +150,7 @@ global:
html_simple: '1001'
html_project: '1002'
serverLxc:
ip: 127.0.0.1
username: root
password: 123456
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