This repository has been archived on 2024-02-26. You can view files and clone it, but cannot push or open issues or pull requests.
2017-08-30 17:27:42 +09:00

232 lines
5.8 KiB
Go

// Copyright 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"context"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
"golang.org/x/sync/errgroup"
"github.com/IanLewis/cloud-dyndns-client/pkg/backend"
"github.com/IanLewis/cloud-dyndns-client/pkg/backend/gcp"
"github.com/IanLewis/cloud-dyndns-client/pkg/sync"
)
var VERSION = "0.1.4"
type Domain struct {
Provider string `json:"provider"`
ProviderConfig map[string]interface{} `json:"provider_config"`
Backend backend.DNSBackend
}
type Config struct {
Domains map[string]*Domain `json:"domains"`
}
func getFileContents(pathToFile string) ([]byte, error) {
// Open file
f, err := os.Open(pathToFile)
if err != nil {
return []byte{}, fmt.Errorf("Could not open %s: %v", pathToFile, err)
}
defer f.Close()
contents, err := ioutil.ReadAll(f)
if err != nil {
return []byte{}, fmt.Errorf("Could not read %s: %v", f, err)
}
return contents, nil
}
func getConfig(pathToJson string) (Config, error) {
var cfg Config
jsonContents, err := getFileContents(pathToJson)
if err != nil {
return cfg, err
}
err = json.Unmarshal(jsonContents, &cfg)
if err != nil {
return cfg, fmt.Errorf("Could not load %s: %v", pathToJson, err)
}
for _, d := range cfg.Domains {
if d.Provider == "gcp" {
p, ok := d.ProviderConfig["project_id"]
if !ok {
return cfg, fmt.Errorf("\"project_id\" is required for Cloud DNS config.")
}
project, ok := p.(string)
if !ok {
return cfg, fmt.Errorf("\"project_id\" must be a string.")
}
z, ok := d.ProviderConfig["managed_zone"]
if !ok {
return cfg, fmt.Errorf("\"managed_zone\" is required for Cloud DNS config.")
}
zone, ok := z.(string)
if !ok {
return cfg, fmt.Errorf("\"managed_zone\" must be a string.")
}
b, err := gcp.NewCloudDNSBackend(project, zone)
if err != nil {
return cfg, fmt.Errorf("Could not create Cloud DNS backend: %#v", err)
}
d.Backend = b
} else {
return cfg, fmt.Errorf("Unknown backend provider: %s", d.Provider)
}
}
return cfg, nil
}
func Main() int {
addr := flag.String("addr", ":8080", "Address to listen on for health checks.")
version := flag.Bool("version", false, "Print the version and exit.")
config := flag.String("config", "/etc/cloud-dyndns-client/config.json", "The path to the JSON config file.")
flag.Parse()
if *version {
fmt.Println(VERSION)
return 0
}
cfg, err := getConfig(*config)
if err != nil {
log.Fatalf("Error reading config: %#v", err)
}
// Convert config to sync records
records := []sync.Record{}
for name, d := range cfg.Domains {
if !strings.HasSuffix(name, ".") {
name = name + "."
}
records = append(records, sync.Record{
Record: backend.NewDNSRecord(
name,
"A",
600,
[]string{},
),
Backend: d.Backend,
})
}
// Create a new syncer. This will sync DNS records to backends
// and ensure records are set to the desired values.
syncer := sync.NewSyncer(records, 30*time.Second, 5*time.Second)
// The IP Address poller will poll for the Internet IP address.
// When a new address is polled the data will be forwarded to the syncer.
poller := sync.NewIPAddressPoller(5 * time.Minute)
// Create a waitgroup to manage the goroutines for the main loops.
// The waitgroup can be used to wait for goroutines to finish.
ctx, cancel := context.WithCancel(context.Background())
wg, ctx := errgroup.WithContext(ctx)
wg.Go(func() error { return syncer.Run(ctx.Done()) })
wg.Go(func() error { return poller.Run(ctx.Done()) })
wg.Go(func() error {
// This goroutine receives IP address polling results
// and updates the desired records in the Syncer.
c := poller.Channel()
for {
select {
case ip := <-c:
for _, r := range records {
syncer.UpdateRecord(
r.Record.Name(),
r.Record.Type(),
r.Record.Ttl(),
[]string{ip},
)
}
case <-ctx.Done():
return nil
}
}
})
wg.Go(func() error {
// This goroutine sets up health checks on an HTTP endpoint.
// It's a bit complicated as it is necessary to gracefully
// shutdown the http server.
mux := http.NewServeMux()
mux.HandleFunc("/_status/healthz", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
})
srv := &http.Server{
Addr: *addr,
Handler: mux,
}
// Since srv.ListenAndServe() blocks we need to start it in
// a goroutine so the select can monitor the context's done
// channel as well as if the server returns an error.
errChan := make(chan error, 1)
go func(errChan chan error) {
log.Printf("Listening on %s...", *addr)
errChan <- srv.ListenAndServe()
}(errChan)
select {
case <-ctx.Done():
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 10*time.Second)
defer shutdownCancel()
return srv.Shutdown(shutdownCtx)
case err := <-errChan:
return err
}
})
// Wait for SIGINT or SIGTERM signals and shutdown the application if
// one is received.
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
select {
case s := <-signals:
log.Printf("Received signal %#v, exiting...", s)
case <-ctx.Done():
}
cancel()
if err := wg.Wait(); err != nil {
log.Printf("Unhandled error received. Exiting: %s %#v", err.Error(), err)
return 1
}
return 0
}