Java tutorial
/* * Copyright 2014 by SCSK Corporation. * * This file is part of PrimeCloud Controller(TM). * * PrimeCloud Controller(TM) is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * PrimeCloud Controller(TM) is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with PrimeCloud Controller(TM). If not, see <http://www.gnu.org/licenses/>. */ package jp.primecloud.auto.process; import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import org.apache.commons.lang.BooleanUtils; import org.apache.commons.lang.StringUtils; import jp.primecloud.auto.common.component.DnsStrategy; import jp.primecloud.auto.common.constant.PCCConstant; import jp.primecloud.auto.entity.crud.AwsInstance; import jp.primecloud.auto.entity.crud.AzureInstance; import jp.primecloud.auto.entity.crud.CloudstackInstance; import jp.primecloud.auto.entity.crud.Image; import jp.primecloud.auto.entity.crud.Instance; import jp.primecloud.auto.entity.crud.OpenstackInstance; import jp.primecloud.auto.entity.crud.Platform; import jp.primecloud.auto.entity.crud.PlatformAws; import jp.primecloud.auto.entity.crud.VcloudInstance; import jp.primecloud.auto.exception.AutoException; import jp.primecloud.auto.log.EventLogger; import jp.primecloud.auto.service.ServiceSupport; import jp.primecloud.auto.util.MessageUtils; /** * <p> * TODO: * </p> * */ public class DnsProcess extends ServiceSupport { protected DnsStrategy dnsStrategy; protected boolean reverseEnabled = true; protected ProcessLogger processLogger; protected EventLogger eventLogger; public void startDns(Platform platform, Long instanceNo) { Instance instance = instanceDao.read(instanceNo); // TODO CLOUD BRANCHING if (PCCConstant.PLATFORM_TYPE_CLOUDSTACK.equals(platform.getPlatformType())) { startDnsCloudstack(instanceNo); } else if (PCCConstant.PLATFORM_TYPE_VCLOUD.equals(platform.getPlatformType())) { if (platform.getInternal()) { //(VPN??) startDnsVcloud(instanceNo); } else { //(VPN?) startDnsVcloudVPN(instanceNo); } // Azure?? } else if (PCCConstant.PLATFORM_TYPE_AZURE.equals(platform.getPlatformType())) { startDnsAzure(instanceNo); } else if (PCCConstant.PLATFORM_TYPE_OPENSTACK.equals(platform.getPlatformType())) { startDnsOpenstack(instanceNo); } else if (PCCConstant.PLATFORM_TYPE_AWS.equals(platform.getPlatformType())) { PlatformAws platformAws = platformAwsDao.read(platform.getPlatformNo()); if (platform.getInternal()) { // ??? if (BooleanUtils.isTrue(platformAws.getEuca())) { // Eucalyptus?? log.debug("DnsProcess:startDnsVpc[internal=true, vpc=euca]"); startDnsNormalEuca(instanceNo); } else { // Amazon EC2?? if (BooleanUtils.isTrue(platformAws.getVpc())) { // VPC???? log.debug("DnsProcess:startDnsVpc[internal=true, vpc=true]"); startDnsVpc(instanceNo); } else { // VPC???? log.debug("DnsProcess:startDnsNormalEc2[internal=true, vpc=false]"); startDnsNormalEc2(instanceNo); } } } else { // ??? // Windows???VPN??? Image image = imageDao.read(instance.getImageNo()); if (StringUtils.startsWithIgnoreCase(image.getOs(), PCCConstant.OS_NAME_WIN)) { // VPN?????? log.debug("DnsProcess:startDnsNormalEc2[internal=false, os=windows]"); startDnsNormalEc2(instanceNo); } else { // VPN????(VPC+VPN?????VPN) log.debug("DnsProcess:startDnsVpn[internal=false, os=linux]VPC+VPN"); startDnsVpn(instanceNo); } } } // instance = instanceDao.read(instanceNo); processLogger.writeLogSupport(ProcessLogger.LOG_DEBUG, null, instance, "DnsRegist", new Object[] { instance.getFqdn(), instance.getPublicIp() }); } public void stopDns(Platform platform, Long instanceNo) { Instance instance = instanceDao.read(instanceNo); // TODO CLOUD BRANCHING if (PCCConstant.PLATFORM_TYPE_CLOUDSTACK.equals(platform.getPlatformType())) { stopDnsNormal(instanceNo); } else if (PCCConstant.PLATFORM_TYPE_VCLOUD.equals(platform.getPlatformType())) { stopDnsNormal(instanceNo); } else if (PCCConstant.PLATFORM_TYPE_AZURE.equals(platform.getPlatformType())) { stopDnsNormal(instanceNo); } else if (PCCConstant.PLATFORM_TYPE_OPENSTACK.equals(platform.getPlatformType())) { stopDnsNormal(instanceNo); } else if (PCCConstant.PLATFORM_TYPE_AWS.equals(platform.getPlatformType())) { PlatformAws platformAws = platformAwsDao.read(platform.getPlatformNo()); if (platform.getInternal()) { // ??? if (BooleanUtils.isTrue(platformAws.getEuca())) { // Eucalyptus?? log.debug("DnsProcess:stopDnsNormalEuca[internal=true, vpc=euca]"); stopDnsNormal(instanceNo); } else { if (BooleanUtils.isTrue(platformAws.getVpc())) { // VPC???? log.debug("DnsProcess:stopDnsVpc[internal=true, vpc=true]"); stopDnsNormal(instanceNo); } else { // VPC???? log.debug("DnsProcess:stopDnsNormalEc2[internal=true, vpc=false]"); stopDnsNormalEc2(instanceNo); } } } else { // ??? // Windows???VPN??? Image image = imageDao.read(instance.getImageNo()); if (StringUtils.startsWithIgnoreCase(image.getOs(), PCCConstant.OS_NAME_WIN)) { log.debug("DnsProcess:stopDnsNormalEc2[internal=false, os=windows]"); stopDnsNormalEc2(instanceNo); } else { // VPN???? log.debug("DnsProcess:stopDnsVpn[internal=false, os=linux]VPC+VPN"); stopDnsNormal(instanceNo); } } } // processLogger.writeLogSupport(ProcessLogger.LOG_DEBUG, null, instance, "DnsUnregist", new Object[] { instance.getFqdn(), instance.getPublicIp() }); } protected void startDnsNormalEuca(Long instanceNo) { Instance instance = instanceDao.read(instanceNo); AwsInstance awsInstance = awsInstanceDao.read(instanceNo); // ?AwsInstance?Instance??????? if (StringUtils.equals(instance.getPublicIp(), awsInstance.getDnsName())) { return; } // DnsName, PrivateDnsName?????IP???? String fqdn = instance.getFqdn(); String publicIp = awsInstance.getDnsName(); String privateIp = awsInstance.getPrivateDnsName(); // ??? addForward(fqdn, publicIp); // ??? addReverse(fqdn, publicIp); // ? instance.setPublicIp(publicIp); instance.setPrivateIp(privateIp); instanceDao.update(instance); } protected void startDnsNormalEc2(Long instanceNo) { Instance instance = instanceDao.read(instanceNo); AwsInstance awsInstance = awsInstanceDao.read(instanceNo); // ?AwsInstance?Instance??????? if (StringUtils.equals(instance.getPublicIp(), awsInstance.getDnsName())) { return; } String fqdn = instance.getFqdn(); String publicIp = awsInstance.getIpAddress(); String privateIp = awsInstance.getPrivateIpAddress(); // CNAME? addCanonicalName(fqdn, awsInstance.getDnsName()); // ? instance.setPublicIp(publicIp); instance.setPrivateIp(privateIp); instanceDao.update(instance); } protected void startDnsCloudstack(Long instanceNo) { Instance instance = instanceDao.read(instanceNo); CloudstackInstance csInstance = cloudstackInstanceDao.read(instanceNo); // ?CloudStackInstance?Instance??????? if (StringUtils.equals(instance.getPublicIp(), csInstance.getIpaddress())) { return; } String fqdn = instance.getFqdn(); String publicIp = resolveHost(fqdn); String privateIp = csInstance.getIpaddress(); // ??? addReverse(fqdn, publicIp); // ? instance.setPublicIp(publicIp); instance.setPrivateIp(privateIp); instanceDao.update(instance); } protected void startDnsAzure(Long instanceNo) { Instance instance = instanceDao.read(instanceNo); AzureInstance azureInstance = azureInstanceDao.read(instanceNo); // ?AzureInstance?Instance??????? if (StringUtils.equals(instance.getPublicIp(), azureInstance.getPrivateIpAddress())) { return; } String fqdn = instance.getFqdn(); //String publicIp = resolveHost(fqdn); String publicIp = azureInstance.getPrivateIpAddress(); String privateIp = azureInstance.getPrivateIpAddress(); // ??? addForward(fqdn, publicIp); // ??? addReverse(fqdn, publicIp); // ? instance.setPublicIp(publicIp); instance.setPrivateIp(privateIp); instanceDao.update(instance); } protected void startDnsOpenstack(Long instanceNo) { Instance instance = instanceDao.read(instanceNo); OpenstackInstance osInstance = openstackInstanceDao.read(instanceNo); // Instance?IP?????? if (!StringUtils.isEmpty(instance.getPublicIp())) { return; } // IP??????????? String fqdn = instance.getFqdn(); String publicIp = resolveHost(fqdn); // VPN?IP String privateIp = osInstance.getPrivateIpAddress(); // ??? addReverse(fqdn, publicIp); // ? instance.setPublicIp(publicIp); instance.setPrivateIp(privateIp); instanceDao.update(instance); } protected void startDnsVpn(Long instanceNo) { Instance instance = instanceDao.read(instanceNo); AwsInstance awsInstance = awsInstanceDao.read(instanceNo); // Instance?IP?????? if (!StringUtils.isEmpty(instance.getPublicIp())) { return; } // IP??????????? String fqdn = instance.getFqdn(); String publicIp = resolveHost(fqdn); // VPN?IP String privateIp = awsInstance.getPrivateIpAddress(); // ??? addReverse(fqdn, publicIp); // ? instance.setPublicIp(publicIp); instance.setPrivateIp(privateIp); instanceDao.update(instance); } protected void startDnsVpc(Long instanceNo) { Instance instance = instanceDao.read(instanceNo); AwsInstance awsInstance = awsInstanceDao.read(instanceNo); // Instance?IP?????? if (!StringUtils.isEmpty(instance.getPublicIp())) { return; } // PrivateIpAddressIP?? String fqdn = instance.getFqdn(); String publicIp = awsInstance.getPrivateIpAddress(); String privateIp = awsInstance.getPrivateIpAddress(); // ??? addForward(fqdn, publicIp); // ??? addReverse(fqdn, publicIp); // ? instance.setPublicIp(publicIp); instance.setPrivateIp(privateIp); instanceDao.update(instance); } //VPN???VCloudDNS //PublicIP VCloud????????IP //PrivateIP VCloud??????????IP protected void startDnsVcloud(Long instanceNo) { Instance instance = instanceDao.read(instanceNo); VcloudInstance vcloudInstance = vcloudInstanceDao.read(instanceNo); // ?VcloudInstance?Instance??????? if (StringUtils.equals(instance.getPublicIp(), vcloudInstance.getIpAddress())) { return; } String fqdn = instance.getFqdn(); String publicIp = vcloudInstance.getIpAddress(); //TODO ?? //VM?VCloud??????? //????DNS?????IP??????DNS?? // contactHost(publicIp); String privateIp = vcloudInstance.getPrivateIpAddress(); // ??? addForward(fqdn, publicIp); // ??? addReverse(fqdn, publicIp); // ? instance.setPublicIp(publicIp); instance.setPrivateIp(privateIp); instanceDao.update(instance); } //VPN?VCloudDNS //PublicIP PCC(OpenVPN)????IP //PrivateIP VCloud??????????IP protected void startDnsVcloudVPN(Long instanceNo) { Instance instance = instanceDao.read(instanceNo); VcloudInstance vcloudInstance = vcloudInstanceDao.read(instanceNo); // ?VcloudInstance?Instance??????? if (StringUtils.equals(instance.getPublicIp(), vcloudInstance.getIpAddress())) { return; } // IP??????????(OpenVPN?)?? String fqdn = instance.getFqdn(); String publicIp = resolveHost(fqdn); // VPN?IP String privateIp = vcloudInstance.getPrivateIpAddress(); // ??? addReverse(fqdn, publicIp); // ? instance.setPublicIp(publicIp); instance.setPrivateIp(privateIp); instanceDao.update(instance); } protected void stopDnsNormal(Long instanceNo) { Instance instance = instanceDao.read(instanceNo); // IP????? if (StringUtils.isEmpty(instance.getPublicIp())) { return; } String fqdn = instance.getFqdn(); String publicIp = instance.getPublicIp(); // ??? deleteForward(fqdn); // ??? deleteReverse(publicIp); // ? instance.setPublicIp(null); instance.setPrivateIp(null); instanceDao.update(instance); } protected void stopDnsNormalEc2(Long instanceNo) { Instance instance = instanceDao.read(instanceNo); // IP????? if (StringUtils.isEmpty(instance.getPublicIp())) { return; } String fqdn = instance.getFqdn(); // CNAME? deleteCanonicalName(fqdn); // ? instance.setPublicIp(null); instance.setPrivateIp(null); instanceDao.update(instance); } protected void addForward(String fqdn, String publicIp) { // ??? dnsStrategy.addForward(fqdn, publicIp); // if (log.isInfoEnabled()) { log.info(MessageUtils.getMessage("IPROCESS-100141", fqdn, publicIp)); } } protected void addReverse(String fqdn, String publicIp) { if (!reverseEnabled) { // ?????? return; } // ??? dnsStrategy.addReverse(fqdn, publicIp); // if (log.isInfoEnabled()) { log.info(MessageUtils.getMessage("IPROCESS-100142", publicIp, fqdn)); } } protected void addCanonicalName(String fqdn, String canonicalName) { // CNAME? dnsStrategy.addCanonicalName(fqdn, canonicalName); // if (log.isInfoEnabled()) { log.info(MessageUtils.getMessage("IPROCESS-100145", fqdn, canonicalName)); } } protected void deleteForward(String fqdn) { // ??? dnsStrategy.deleteForward(fqdn); // if (log.isInfoEnabled()) { log.info(MessageUtils.getMessage("IPROCESS-100143", fqdn)); } } protected void deleteReverse(String publicIp) { if (!reverseEnabled) { // ?????? return; } // ??? dnsStrategy.deleteReverse(publicIp); // if (log.isInfoEnabled()) { log.info(MessageUtils.getMessage("IPROCESS-100144", publicIp)); } } protected void deleteCanonicalName(String fqdn) { // CNAME? dnsStrategy.deleteCanonicalName(fqdn); // if (log.isInfoEnabled()) { log.info(MessageUtils.getMessage("IPROCESS-100146", fqdn)); } } protected String resolveHost(String fqdn) { long timeout = 1000L * 60 * 5; long startTime = System.currentTimeMillis(); while (true) { try { InetAddress address = InetAddress.getByName(fqdn); return address.getHostAddress(); } catch (UnknownHostException ignore) { } if (System.currentTimeMillis() - startTime > timeout) { // throw new AutoException("EPROCESS-000205", fqdn); } try { Thread.sleep(5000); } catch (InterruptedException ignore) { } } } protected void contactHost(String ip) { long timeout = 1000L * 60 * 5; long startTime = System.currentTimeMillis(); int count = 0; boolean beforeReached = false; while (true) { try { InetAddress address = InetAddress.getByName(ip); boolean reached = address.isReachable(1500); if (reached) { if (beforeReached != reached) { count = 0; } count++; log.debug(MessageUtils.format("contactHost {0} time.(ipAddress={1})", count, ip)); if (count >= 30) { return; } } beforeReached = reached; } catch (IOException ignore) { } if (System.currentTimeMillis() - startTime > timeout) { // throw new AutoException("EPROCESS-000206", ip); } try { Thread.sleep(1000); } catch (InterruptedException ignore) { } } } /** * dnsStrategy??? * * @param dnsStrategy dnsStrategy */ public void setDnsStrategy(DnsStrategy dnsStrategy) { this.dnsStrategy = dnsStrategy; } /** * reverseEnabled??? * * @param reverseEnabled reverseEnabled */ public void setReverseEnabled(boolean reverseEnabled) { this.reverseEnabled = reverseEnabled; } /** * eventLogger??? * * @param eventLogger eventLogger */ public void setEventLogger(EventLogger eventLogger) { this.eventLogger = eventLogger; } /** * processLogger??? * * @param processLogger processLogger */ public void setProcessLogger(ProcessLogger processLogger) { this.processLogger = processLogger; } }