mirror of
https://github.com/ctripcorp/zeus.git
synced 2024-09-22 08:36:24 +08:00
update write clean conf
This commit is contained in:
parent
bd7429e5e8
commit
bb45887ead
|
@ -11,6 +11,7 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.List;
|
||||
|
||||
public class NginxOperator {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(NginxOperator.class);
|
||||
|
@ -79,6 +80,42 @@ public class NginxOperator {
|
|||
throw e;
|
||||
}
|
||||
}
|
||||
public NginxResponse cleanConf(List<Long> vsid) throws IOException{
|
||||
try {
|
||||
StringBuilder sb = new StringBuilder(128);
|
||||
sb.append("'");
|
||||
for (Long vs : vsid)
|
||||
{
|
||||
sb.append(vs).append(".conf\\|");
|
||||
}
|
||||
sb.append("'");
|
||||
|
||||
String cleanVhostCommand = "sudo ls "+nginxConfDir+"/vhosts | grep -vw "+sb.toString()+"|sed \"s:^:"+nginxConfDir+"/vhosts/: \" | xargs sudo rm ";
|
||||
String cleanUpstreamCommand = "sudo ls "+nginxConfDir+"/upstreams | grep -vw "+sb.toString()+"|sed \"s:^:"+nginxConfDir+"/upstreams/: \" | xargs sudo rm ";
|
||||
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
|
||||
CommandLine commandline = CommandLine.parse(cleanVhostCommand+";"+cleanUpstreamCommand);
|
||||
|
||||
DefaultExecutor exec = new DefaultExecutor();
|
||||
exec.setExitValues(null);
|
||||
|
||||
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream,errorStream);
|
||||
exec.setStreamHandler(streamHandler);
|
||||
|
||||
int exitVal = exec.execute(commandline);
|
||||
String out = outputStream.toString("UTF-8");
|
||||
String error = errorStream.toString("UTF-8");
|
||||
NginxResponse response = new NginxResponse();
|
||||
response.setOutMsg(out);
|
||||
response.setErrMsg(error);
|
||||
response.setSucceed(0==exitVal);
|
||||
return response;
|
||||
} catch (IOException e) {
|
||||
LOGGER.error("Fail to clean conf",e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
public NginxResponse reloadConf() throws IOException{
|
||||
try {
|
||||
String command = nginxBinDir + "/nginx -s reload";
|
||||
|
|
|
@ -35,10 +35,12 @@ public class UpstreamsConf {
|
|||
|
||||
public static String buildUpstreamConf(Slb slb, VirtualServer vs, Group group, String upstreamName, Set<String> allDownServers, Set<String> allUpGroupServers) throws Exception {
|
||||
StringBuilder b = new StringBuilder(1024);
|
||||
|
||||
String body = buildUpstreamConfBody(slb,vs,group,allDownServers,allUpGroupServers);
|
||||
if (null == body){
|
||||
return "";
|
||||
}
|
||||
b.append("upstream ").append(upstreamName).append(" {").append("\n");
|
||||
|
||||
b.append(buildUpstreamConfBody(slb,vs,group,allDownServers,allUpGroupServers));
|
||||
b.append(body);
|
||||
|
||||
b.append("}").append("\n");
|
||||
|
||||
|
@ -50,14 +52,12 @@ public class UpstreamsConf {
|
|||
//LBMethod
|
||||
b.append(LBConf.generate(slb, vs, group));
|
||||
|
||||
//ToDo:
|
||||
//b.append(" ").append("zone " + upstreamName + " 64K").append(";\n");
|
||||
|
||||
List<GroupServer> groupServers= group.getGroupServers();
|
||||
|
||||
if (groupServers==null)
|
||||
if (groupServers==null||groupServers.size()==0)
|
||||
{
|
||||
groupServers = new ArrayList<>();
|
||||
// groupServers = new ArrayList<>();
|
||||
return null;
|
||||
}
|
||||
|
||||
for (GroupServer as : groupServers) {
|
||||
|
|
|
@ -66,6 +66,7 @@ public class NginxServiceImpl implements NginxService {
|
|||
|
||||
NginxOperator nginxOperator = new NginxOperator(slb.getNginxConf(), slb.getNginxBin());
|
||||
|
||||
cleanConfOnDisk(slbId, version, nginxOperator);
|
||||
writeConfToDisk(slbId, version, nginxOperator);
|
||||
|
||||
NginxResponse response = nginxOperator.reloadConfTest();
|
||||
|
@ -358,4 +359,12 @@ public class NginxServiceImpl implements NginxService {
|
|||
nginxOperator.writeUpstreamsConf(d.getVsId(), d.getContent());
|
||||
}
|
||||
}
|
||||
private void cleanConfOnDisk (Long slbId, int version, NginxOperator nginxOperator) throws Exception {
|
||||
List<NginxConfServerData> nginxConfServerDataList = nginxConfService.getNginxConfServer(slbId, version);
|
||||
List<Long> vslist = new ArrayList<>();
|
||||
for (NginxConfServerData d : nginxConfServerDataList) {
|
||||
vslist.add(d.getVsId());
|
||||
}
|
||||
nginxOperator.cleanConf(vslist);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -139,7 +139,7 @@ public class GroupStatusServiceImpl implements GroupStatusService {
|
|||
boolean flag = "up".equalsIgnoreCase(server.getStatus());
|
||||
if (!(memberUp&&serverUp)&&flag)
|
||||
{
|
||||
LOGGER.error("nginx status api return status while memberUp and serverUp are down! ip:"+ip+" groupId:"+groupId);
|
||||
LOGGER.error("nginx status api return status while memberUp or serverUp is down! ip:"+ip+" groupId:"+groupId);
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue