public static void main(String[] args) throws IOException { Arguments arguments = new Arguments(); CmdLineParser parser = new CmdLineParser(arguments); try { parser.parseArgument(args); } catch (CmdLineException e) { System.err.println("Usage: java -jar dyndns.jar <arguments>"); parser.printUsage(System.err); System.exit(1); } AmazonRoute53 route53 = new AmazonRoute53Client(new DefaultAWSCredentialsProviderChain()); UpdateDns updateDns = new UpdateDns(route53, arguments.hostedZoneId); updateDns.updateDns(arguments.recordSetName, arguments.force); }
public static ChangeInfo attachDomainToEip(AmazonRoute53 route53, String publicIp, String domain) { String hostedZoneId = findHostedZoneForDomain(route53, domain); if (hostedZoneId != null) { System.out.println("Found HostedZone's Id(" + hostedZoneId + ")"); } else { return null; } ResourceRecordSet resourceRecordSet = generateResourceRecordSetForARecord(publicIp, domain); ChangeInfo changeinfo = AwsRoute53Client.changeResourceRecordSet(route53, hostedZoneId, resourceRecordSet, ChangeAction.CREATE); return changeinfo; }
public static String findHostedZoneForDomain(AmazonRoute53 route53, String domain) { ListHostedZonesResult listHostedZonesResult = route53.listHostedZones(); List<HostedZone> hostedZones = listHostedZonesResult.getHostedZones(); String hostedZoneId = null; for (HostedZone hostedZone : hostedZones) { if (isRootDomain(domain, hostedZone)) { hostedZoneId = hostedZone.getId(); break; } } return hostedZoneId; }
public static ChangeInfo changeResourceRecordSet(AmazonRoute53 route53, String hostedZoneId, ResourceRecordSet resourceRecordSet, ChangeAction changeAction) { Change change = new Change(changeAction, resourceRecordSet); ChangeBatch changeBatch = new ChangeBatch().withChanges(change); ChangeResourceRecordSetsRequest changeResourceRecordSetsRequest = new ChangeResourceRecordSetsRequest( hostedZoneId, changeBatch); ChangeResourceRecordSetsResult changeResourceRecordSetsResult = route53 .changeResourceRecordSets(changeResourceRecordSetsRequest); ChangeInfo changeinfo = changeResourceRecordSetsResult.getChangeInfo(); return changeinfo; }
@Override public AmazonRoute53 getAmazonRoute53Client() { return r53; }
public static AmazonRoute53 getRoute53() { AmazonRoute53 route53 = new AmazonRoute53Client(ConfigProvider.getCredential()); route53.setRegion(ConfigProvider.getDefaultRegion()); return route53; }
private int startByName(Ec2CommandOptions options) throws FileNotFoundException { String name = options.getName(); InputStream inputStream = new FileInputStream(new File(options.getCredentialsPath())); ConfigProvider.loadConfigure(inputStream); AmazonEC2 ec2 = AwsEc2Client.getEc2(); // Check Exists Instance Instance instance = AwsEc2Client.findInstanceByName(ec2, name); if (instance == null) { System.err.println("Not exists instance (name = " + name + ")."); return 2; } String instanceId = instance.getInstanceId(); System.out.println("Exists instance (id = " + instanceId + ")"); // Start Ec2 Instance InstanceStateChange stateChange = AwsEc2Client.startInstance(ec2, instanceId); AwsEc2Client.showStateChange(stateChange, "Starting Instance"); // Allocate Address DomainType domainType = (instance.getVpcId() == null) ? DomainType.Standard : DomainType.Vpc; Address address = AwsEc2Client.allocateAddress(ec2, domainType); String publicIp = address.getPublicIp(); System.out.println("Allocated Address(" + publicIp + ", " + address.getAllocationId() + ")"); if (address != null) { // TODO: Wait for Starting Instance. waitForStartingInstance(); try { // Associate Address String associateAddress = AwsEc2Client.associateAddress(ec2, address, instanceId); System.out.println("Associated Address(" + publicIp + ", " + associateAddress + ")"); String domain = options.getDomain(); if (domain != null) { // Attach Domain to EIP AmazonRoute53 route53 = AwsRoute53Client.getRoute53(); ChangeInfo attachedResult = AwsRoute53Client.attachDomainToEip(route53, publicIp, domain); if (attachedResult != null) { System.out.println("Attached domain(" + domain + ")"); } else { System.err.println("Not Found Available Hosted Zone for specified Domain(" + domain + ")"); } } } catch (AmazonServiceException e) { AwsEc2Client.releaseAddress(ec2, address); System.out.println("Released Address (" + publicIp + ")"); return 2; } } return 0; }
@Override public AmazonRoute53 getAmazonClient() { return r53; }
public UpdateDns(AmazonRoute53 route53, String hostedZoneId) { this.route53 = route53; this.hostedZoneId = hostedZoneId; }
private AmazonRoute53 newClient() { final AmazonRoute53 amazonClient = new AmazonRoute53Client(credentials); return amazonClient; }
/** * * @return */ AmazonRoute53 getAmazonRoute53Client();
/** * * @return The amazon client */ AmazonRoute53 getAmazonClient();