From 86b9515c4e1c65b83d9a0a74f8bdb97c11d9e78c Mon Sep 17 00:00:00 2001 From: Mengyi Zhou Date: Thu, 12 Nov 2015 13:50:29 +0800 Subject: [PATCH] abandon certificate command field --- .../restful/resource/OperationResource.java | 3 +- .../impl/VirtualServerRepositoryImpl.java | 3 +- .../service/nginx/CertificateService.java | 8 +--- .../nginx/impl/CertificateServiceImpl.java | 47 ++++++------------- .../service/nginx/CertificateTestService.java | 12 +++-- 5 files changed, 28 insertions(+), 45 deletions(-) diff --git a/src/main/java/com/ctrip/zeus/restful/resource/OperationResource.java b/src/main/java/com/ctrip/zeus/restful/resource/OperationResource.java index c3d0bfe2..3e45edf9 100644 --- a/src/main/java/com/ctrip/zeus/restful/resource/OperationResource.java +++ b/src/main/java/com/ctrip/zeus/restful/resource/OperationResource.java @@ -352,8 +352,7 @@ public class OperationResource { throw new ValidationException("certId, vsId and ips are required."); } ips = configureIps(vsId, ips); - certificateService.command(vsId, ips, certId); - certificateService.install(vsId); + certificateService.install(vsId, ips, certId); return responseHandler.handle("Certificates uploaded. Re-activate the virtual server to take effect.", hh.getMediaType()); } diff --git a/src/main/java/com/ctrip/zeus/service/model/impl/VirtualServerRepositoryImpl.java b/src/main/java/com/ctrip/zeus/service/model/impl/VirtualServerRepositoryImpl.java index 5382a196..abebde3a 100644 --- a/src/main/java/com/ctrip/zeus/service/model/impl/VirtualServerRepositoryImpl.java +++ b/src/main/java/com/ctrip/zeus/service/model/impl/VirtualServerRepositoryImpl.java @@ -133,8 +133,7 @@ public class VirtualServerRepositoryImpl implements VirtualServerRepository { domains[i] = vsDomains.get(i).getName(); } Long certId = certificateService.getCertificateOnBoard(domains); - certificateService.command(virtualServer.getId(), ips, certId); - certificateService.install(virtualServer.getId()); + certificateService.install(virtualServer.getId(), ips, certId); } private VirtualServer createVirtualServer(SlbVirtualServerDo d) throws DalException { diff --git a/src/main/java/com/ctrip/zeus/service/nginx/CertificateService.java b/src/main/java/com/ctrip/zeus/service/nginx/CertificateService.java index e6813ac1..b7982ccb 100644 --- a/src/main/java/com/ctrip/zeus/service/nginx/CertificateService.java +++ b/src/main/java/com/ctrip/zeus/service/nginx/CertificateService.java @@ -15,11 +15,7 @@ public interface CertificateService { Long upgrade(InputStream cert, InputStream key, String domain, boolean state) throws Exception; - void command(Long vsId, List ips, Long certId) throws Exception; + void install(Long vsId, List ips, Long certId) throws Exception; - void recall(Long vsId, List ips) throws Exception; - - void install(Long vsId) throws Exception; - - void uninstallIfRecalled(Long vsId) throws Exception; + void uninstallIfRecalled(Long vsId, List ips) throws Exception; } \ No newline at end of file diff --git a/src/main/java/com/ctrip/zeus/service/nginx/impl/CertificateServiceImpl.java b/src/main/java/com/ctrip/zeus/service/nginx/impl/CertificateServiceImpl.java index feec805a..e301a8db 100644 --- a/src/main/java/com/ctrip/zeus/service/nginx/impl/CertificateServiceImpl.java +++ b/src/main/java/com/ctrip/zeus/service/nginx/impl/CertificateServiceImpl.java @@ -82,44 +82,29 @@ public class CertificateServiceImpl implements CertificateService { } @Override - public void command(Long vsId, List ips, Long certId) throws Exception { - CertificateDo cert = certificateDao.findByPK(certId, CertificateEntity.READSET_FULL); - if (cert == null) - throw new ValidationException("Certificate cannot be found."); - for (String ip : ips) { - rCertificateSlbServerDao.insertOrUpdateCommand( - new RelCertSlbServerDo().setIp(ip).setCommand(cert.getId()).setVsId(vsId)); - } - } - - @Override - public void recall(Long vsId, List ips) throws Exception { - for (String ip : ips) { - rCertificateSlbServerDao.insertOrUpdateCommand( - new RelCertSlbServerDo().setIp(ip).setCommand(0L).setVsId(vsId)); - } - } - - @Override - public void install(Long vsId) throws Exception { + public void install(Long vsId, List ips, Long certId) throws Exception { List dos = rCertificateSlbServerDao.findByVs(vsId, RCertificateSlbServerEntity.READSET_FULL); + Set check = new HashSet<>(); + for (RelCertSlbServerDo d : dos) { + check.add(d.getIp() + "#" + vsId + "#" + d.getCertId()); + } boolean success = true; String errMsg = ""; - for (RelCertSlbServerDo d : dos) { - if (d.getCertId() == d.getCommand()) + for (String ip : ips) { + if (check.contains(ip + "#" + vsId + "#" + certId)) continue; - CertSyncClient c = new CertSyncClient("http://" + d.getIp() + ":8099"); - Response res = c.requestInstall(vsId, d.getCommand()); + CertSyncClient c = new CertSyncClient("http://" + ip + ":8099"); + Response res = c.requestInstall(vsId, certId); // retry if (res.getStatus() / 100 > 2) - res = c.requestInstall(vsId, d.getCommand()); + res = c.requestInstall(vsId, certId); // still failed after retry if (res.getStatus() / 100 > 2) { success &= false; try { - errMsg += d.getIp() + ":" + IOUtils.inputStreamStringify((InputStream) res.getEntity()) + "\n"; + errMsg += ip + ":" + IOUtils.inputStreamStringify((InputStream) res.getEntity()) + "\n"; } catch (IOException e) { - errMsg += d.getIp() + ":" + "Unable to parse the response entity.\n"; + errMsg += ip + ":" + "Unable to parse the response entity.\n"; } } if (!success) @@ -128,13 +113,11 @@ public class CertificateServiceImpl implements CertificateService { } @Override - public void uninstallIfRecalled(Long vsId) throws Exception { - List dos = rCertificateSlbServerDao.findByVs(vsId, RCertificateSlbServerEntity.READSET_FULL); + public void uninstallIfRecalled(Long vsId, List ips) throws Exception { Map abandoned = new HashMap<>(); - for (RelCertSlbServerDo d : dos) { - if (d.getCommand() == 0L) { + for (RelCertSlbServerDo d : rCertificateSlbServerDao.findByVs(vsId, RCertificateSlbServerEntity.READSET_FULL)) { + if (ips.contains(d.getIp())) abandoned.put(d.getIp(), d); - } } boolean success = true; String errMsg = ""; diff --git a/src/test/java/com/ctrip/zeus/service/nginx/CertificateTestService.java b/src/test/java/com/ctrip/zeus/service/nginx/CertificateTestService.java index 4da78db2..72a1b107 100644 --- a/src/test/java/com/ctrip/zeus/service/nginx/CertificateTestService.java +++ b/src/test/java/com/ctrip/zeus/service/nginx/CertificateTestService.java @@ -6,7 +6,9 @@ import com.ctrip.zeus.dal.core.RelCertSlbServerDo; import com.ctrip.zeus.service.nginx.impl.CertificateServiceImpl; import javax.annotation.Resource; +import java.util.HashSet; import java.util.List; +import java.util.Set; /** * Created by zhoumy on 2015/11/5. @@ -18,12 +20,16 @@ public class CertificateTestService extends CertificateServiceImpl { private CertificateInstaller certificateInstaller; @Override - public void install(Long vsId) throws Exception { + public void install(Long vsId, List ips, Long certId) throws Exception { List dos = rCertificateSlbServerDao.findByVs(vsId, RCertificateSlbServerEntity.READSET_FULL); + Set check = new HashSet<>(); for (RelCertSlbServerDo d : dos) { - if (d.getCertId() == d.getCommand()) + check.add(d.getIp() + "#" + vsId + "#" + d.getCertId()); + } + for (String ip : ips) { + if (check.contains(ip + "#" + vsId + "#" + certId)) continue; - certificateInstaller.localInstall(vsId, d.getCommand()); + certificateInstaller.localInstall(vsId, certId); } } } \ No newline at end of file