Skip to content

add the tagname flag to allow arbitrary ec2 tags be the dns name #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions ec2_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type EC2Cache struct {
// NewEC2Cache creates a new EC2Cache that uses the provided
// EC2 client to lookup instances. It starts a goroutine that
// keeps the cache up-to-date.
func NewEC2Cache(regionName, accessKey, secretKey string) (*EC2Cache, error) {
func NewEC2Cache(regionName, accessKey, secretKey, tagname string) (*EC2Cache, error) {

region, ok := aws.Regions[regionName]
if !ok {
Expand All @@ -68,13 +68,13 @@ func NewEC2Cache(regionName, accessKey, secretKey string) (*EC2Cache, error) {
records: make(map[Key][]*Record),
}

if err := cache.refresh(); err != nil {
if err := cache.refresh(tagname); err != nil {
return nil, err
}

go func() {
for _ = range time.Tick(1 * time.Minute) {
err := cache.refresh()
err := cache.refresh(tagname)
if err != nil {
log.Println("ERROR: " + err.Error())
}
Expand Down Expand Up @@ -112,7 +112,7 @@ func sanitize(tag string) string {
return SANE_DNS_REPL.ReplaceAllString(out, "-")
}

func (cache *EC2Cache) refresh() error {
func (cache *EC2Cache) refresh(tagname string) error {
result, err := cache.Instances()
validUntil := time.Now().Add(TTL)

Expand All @@ -138,7 +138,7 @@ func (cache *EC2Cache) refresh() error {
}
record.ValidUntil = validUntil
for _, tag := range instance.Tags {
if tag.Key == "Name" {
if tag.Key == tagname {
name := sanitize(tag.Value)
records[Key{LOOKUP_NAME, name}] = append(records[Key{LOOKUP_NAME, name}], &record)
}
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

const USAGE = `Usage: aws-name-server --domain <domain>
[ --hostname <hostname>
--tagname <tag>
--aws-region us-east-1
--aws-access-key-id <access-key>
--aws-secret-access-key <secret-key> ]
Expand Down Expand Up @@ -42,6 +43,7 @@ func main() {
domain := flag.String("domain", "", "the domain heirarchy to serve (e.g. aws.example.com)")
hostname := flag.String("hostname", "", "the public hostname of this server (e.g. ec2-12-34-56-78.compute-1.amazonaws.com)")
help := flag.Bool("help", false, "show help")
tagname := flag.String("tagname", "Name", "the tag to use instead of NAME")

region := flag.String("aws-region", "us-east-1", "The AWS Region")
accessKey := flag.String("aws-access-key-id", "", "The AWS Access Key Id")
Expand All @@ -59,7 +61,7 @@ func main() {

hostnameFuture := getHostname()

cache, err := NewEC2Cache(*region, *accessKey, *secretKey)
cache, err := NewEC2Cache(*region, *accessKey, *secretKey, *tagname)
if err != nil {
log.Fatalf("FATAL: %s", err)
}
Expand Down