Skip to content

Commit

Permalink
Don't write to route53 because we can't clean it up reliably from here
Browse files Browse the repository at this point in the history
  • Loading branch information
deedubs committed Jun 15, 2018
1 parent 7524076 commit f8b9eb3
Showing 1 changed file with 15 additions and 33 deletions.
48 changes: 15 additions & 33 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import (
"flag"
"io/ioutil"
"log"
"strings"
"syscall"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/massiveco/aws-hostname/identity"
)

Expand All @@ -20,9 +18,14 @@ const hostnamePath = "/etc/hostname"
var ec2TagName string
var setEc2Tag, writeToDisk, setHostname, writeToRoute53 bool

var sess *session.Session

func init() {
sess = session.New()
}

func main() {
flag.BoolVar(&writeToDisk, "write-disk", true, "Instruct aws-hostname to write the generated hostname to disk")
flag.BoolVar(&writeToRoute53, "write-route53", true, "Instruct aws-hostname to write the generated hostname to a Route53 A record")
flag.BoolVar(&setEc2Tag, "write-tag", true, "Instruct aws-hostname to write the generated hostname to an Ec2 instance tag")
flag.BoolVar(&setHostname, "apply", true, "Instruct aws-hostname to syscall.Sethostname")
flag.StringVar(&ec2TagName, "tag", "Name", "Which tag to write the hostname to")
Expand All @@ -46,7 +49,7 @@ func main() {
}

if setEc2Tag {
svc := ec2.New(session.New())
svc := ec2.New(sess)

input := &ec2.CreateTagsInput{
Resources: []*string{
Expand Down Expand Up @@ -74,55 +77,34 @@ func main() {
}
}

if writeToRoute53 {
zoneID := extractTag("massive:DNS-SD:Route53:zone", instance.Tags)
r53 := route53.New(session.New())
zone, err := r53.GetHostedZone(&route53.GetHostedZoneInput{Id: zoneID})
if err != nil {
log.Fatal(err)
}
fqdn := strings.Join([]string{*hostname, *zone.HostedZone.Name}, ".")

_, err = r53.ChangeResourceRecordSets(&route53.ChangeResourceRecordSetsInput{
ChangeBatch: &route53.ChangeBatch{
Changes: []*route53.Change{{
Action: aws.String("UPSERT"),
ResourceRecordSet: &route53.ResourceRecordSet{
Name: aws.String(fqdn),
Type: aws.String("A"),
ResourceRecords: []*route53.ResourceRecord{&route53.ResourceRecord{Value: aws.String(*instance.PrivateIpAddress)}},
TTL: aws.Int64(60),
},
}},
},
HostedZoneId: aws.String(*zoneID),
})
}

log.Printf("Set hostname to: %s", *hostname)
}

func getInstance() (*ec2.Instance, error) {

meta := ec2metadata.New(session.New())
meta := ec2metadata.New(sess)

identity, err := meta.GetInstanceIdentityDocument()
if err != nil {
log.Fatal(err)
}
svc := ec2.New(session.New())
svc := ec2.New(sess)

describedInstances, err := svc.DescribeInstances(&ec2.DescribeInstancesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("resource-id"),
Name: aws.String("instance-id"),
Values: []*string{
aws.String(identity.InstanceID),
},
},
},
})
if err != nil {
return nil, err
}

if describedInstances.Reservations[0] != nil && describedInstances.Reservations[0].Instances[0] != nil {
if describedInstances.Reservations != nil && describedInstances.Reservations[0] != nil && describedInstances.Reservations[0].Instances[0] != nil {
return describedInstances.Reservations[0].Instances[0], nil
}
return nil, nil
Expand Down

0 comments on commit f8b9eb3

Please sign in to comment.