dnsupdater/main.go

322 lines
7.0 KiB
Go
Raw Normal View History

2019-04-05 11:39:35 +00:00
package main
import (
"encoding/json"
"fmt"
2019-04-05 12:49:57 +00:00
"github.com/robfig/cron"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
2019-04-05 11:39:35 +00:00
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/object"
githttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
"io/ioutil"
"net/http"
"net/smtp"
2019-04-05 11:39:35 +00:00
"os"
2019-04-05 12:49:57 +00:00
"os/signal"
2019-04-05 11:39:35 +00:00
"path"
"regexp"
2019-04-05 12:49:57 +00:00
"strconv"
2019-04-05 11:39:35 +00:00
"strings"
2019-04-05 12:49:57 +00:00
"syscall"
2019-04-05 11:39:35 +00:00
"time"
)
type Configuration struct {
RepositoryUrl string `json:"url"`
2019-04-05 12:49:57 +00:00
Username string `json:"name"`
Email string `json:"email"`
GitLogin string `json:"login"`
GitPassword string `json:"password"`
2019-04-05 11:39:35 +00:00
}
2019-04-05 12:49:57 +00:00
var (
configPath string
scheduleSpec string
)
var version = "master"
var commit = "unknown"
var date = "unknown"
2019-04-05 11:39:35 +00:00
func main() {
2019-04-05 12:49:57 +00:00
app := cli.NewApp()
app.Name = "watchtower"
app.Version = version + " - " + commit + " - " + date
app.Usage = "Automatically update running Docker containers"
app.Before = before
app.Action = start
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config, C",
Usage: "path to config file",
Value: "/config/conf.json",
EnvVar: "CONFIG_PATH",
},
cli.IntFlag{
Name: "interval, i",
Usage: "poll interval (in seconds)",
Value: 300,
EnvVar: "DNSUPDATER_POLL_INTERVAL",
},
cli.StringFlag{
Name: "schedule, s",
Usage: "the cron expression which defines when to update",
EnvVar: "DNSUPDATER_SCHEDULE",
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func before(c *cli.Context) error {
if c.GlobalBool("debug") {
log.SetLevel(log.DebugLevel)
}
pollingSet := c.IsSet("interval")
cronSet := c.IsSet("schedule")
if pollingSet && cronSet {
log.Fatal("Only schedule or interval can be defined, not both.")
} else if cronSet {
scheduleSpec = c.String("schedule")
} else {
scheduleSpec = "@every " + strconv.Itoa(c.Int("interval")) + "s"
}
return nil
}
func start(c *cli.Context) error {
tryLockSem := make(chan bool, 1)
tryLockSem <- true
cron := cron.New()
err := cron.AddFunc(
scheduleSpec,
func() {
select {
case v := <-tryLockSem:
defer func() { tryLockSem <- v }()
if err := update(c); err != nil {
log.Println(err)
SendStatusMail(fmt.Sprintf("Error during update process : %s", err))
2019-04-05 12:49:57 +00:00
}
default:
log.Debug("Skipped another update already running.")
}
nextRuns := cron.Entries()
if len(nextRuns) > 0 {
log.Debug("Scheduled next run: " + nextRuns[0].Next.String())
}
})
if err != nil {
return err
}
log.Info("First run: " + cron.Entries()[0].Schedule.Next(time.Now()).String())
cron.Start()
// Graceful shut-down on SIGINT/SIGTERM
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
signal.Notify(interrupt, syscall.SIGTERM)
<-interrupt
cron.Stop()
log.Info("Waiting for running update to be finished...")
<-tryLockSem
os.Exit(1)
return nil
}
func update(c *cli.Context) error {
2019-04-05 11:39:35 +00:00
Info("Running dnsupdater...")
2019-04-05 12:49:57 +00:00
configPath = c.String("config")
2019-04-05 11:39:35 +00:00
2019-04-05 12:49:57 +00:00
if confPathExists, _ := exists(configPath); !confPathExists {
2019-04-05 11:39:35 +00:00
configPath = "./data";
}
2019-04-05 12:49:57 +00:00
file, _ := os.Open(path.Join(configPath, "conf.json"))
2019-04-05 11:39:35 +00:00
defer file.Close()
decoder := json.NewDecoder(file)
configuration := Configuration{}
err := decoder.Decode(&configuration)
Info("Repository : %s", configuration.RepositoryUrl)
Info("Name : %s", configuration.Username)
Info("Email : %s", configuration.Email)
Info("GitLogin : %s", configuration.GitLogin)
2019-04-05 12:49:57 +00:00
err = os.RemoveAll(path.Join(configPath, "data"))
if err != nil {
return err
}
2019-04-05 11:39:35 +00:00
2019-04-05 12:49:57 +00:00
_, err = git.PlainClone(path.Join(configPath, "data"), false, &git.CloneOptions{
2019-04-05 11:39:35 +00:00
URL: configuration.RepositoryUrl,
Progress: os.Stdout,
})
2019-04-05 12:49:57 +00:00
if err != nil {
return err
}
2019-04-05 11:39:35 +00:00
2019-04-05 12:49:57 +00:00
currentIp, err := GetCurrentIp()
if err != nil {
return err
}
newIp, err := GetOutboundIP()
if err != nil {
return err
}
2019-04-05 11:39:35 +00:00
if currentIp == newIp {
Info("Ip has not changed %s", currentIp)
2019-04-05 12:49:57 +00:00
return nil
2019-04-05 11:39:35 +00:00
}
Info("Ip has changed %s -> %s", currentIp, newIp)
SetCurrentIp(currentIp, newIp)
2019-04-05 12:49:57 +00:00
repo, err := git.PlainOpen(path.Join(configPath, "data"))
if err != nil {
return err
}
2019-04-05 11:39:35 +00:00
w, err := repo.Worktree()
2019-04-05 12:49:57 +00:00
if err != nil {
return err
}
2019-04-05 11:39:35 +00:00
_, err = w.Add("templates/home_a.lua")
2019-04-05 12:49:57 +00:00
if err != nil {
return err
}
2019-04-05 11:39:35 +00:00
status, err := w.Status()
2019-04-05 12:49:57 +00:00
if err != nil {
return err
}
2019-04-05 11:39:35 +00:00
fmt.Println(status)
_, err = w.Commit(fmt.Sprintf("Changement d'IP maison : %s -> %s", currentIp, newIp), &git.CommitOptions{
Author: &object.Signature{
Name: configuration.Username,
Email: configuration.Email,
When: time.Now().Local(),
},
})
2019-04-05 12:49:57 +00:00
if err != nil {
return err
}
2019-04-05 11:39:35 +00:00
err = repo.Push(&git.PushOptions{
Auth: &githttp.BasicAuth{
Username: configuration.GitLogin,
Password: configuration.GitPassword,
},
})
2019-04-05 12:49:57 +00:00
if err != nil {
return err
}
SendStatusMail(fmt.Sprintf(`Home IP has changed : %s -> %s
Dns updated successfully`, currentIp, newIp))
2019-04-05 12:49:57 +00:00
return nil
2019-04-05 11:39:35 +00:00
}
func SendStatusMail(messageText string) {
// user we are authorizing as
from := "laurent@lehouerou.net"
// use we are sending email to
to := "laurent@lehouerou.net"
// server we are authorized to send email through
host := "smtp.fastmail.com"
// Create the authentication for the SendMail()
// using PlainText, but other authentication methods are encouraged
auth := smtp.PlainAuth("", from, "c9bd8fb8l4bhs2f8", host)
// NOTE: Using the backtick here ` works like a heredoc, which is why all the
// rest of the lines are forced to the beginning of the line, otherwise the
// formatting is wrong for the RFC 822 style
message := fmt.Sprintf(`To: "Laurent Le Houerou" <laurent@lehouerou.net>
From: "Laurent Le Houerou" <laurent@lehouerou.net>
Subject: DnsUpdater Status
%s
`,messageText)
if err := smtp.SendMail(host+":587", auth, from, []string{to}, []byte(message)); err != nil {
fmt.Println("Error SendMail: ", err)
} else {
fmt.Println("Email Sent!")
}
}
2019-04-05 12:49:57 +00:00
func GetCurrentIp() (string, error) {
b, err := ioutil.ReadFile(path.Join(configPath, "data/templates/home_a.lua"))
if err != nil {
return "", err
}
2019-04-05 11:39:35 +00:00
re := regexp.MustCompile("\"(.*?)\"")
match := re.FindStringSubmatch(string(b))
2019-04-05 12:49:57 +00:00
return match[1], nil
2019-04-05 11:39:35 +00:00
}
2019-04-05 12:49:57 +00:00
func SetCurrentIp(oldip string, newip string) error {
b, err := ioutil.ReadFile(path.Join(configPath, "data/templates/home_a.lua"))
if err != nil {
return err
}
2019-04-05 11:39:35 +00:00
s := string(b)
s = strings.Replace(s, oldip, newip, 1)
2019-04-05 12:49:57 +00:00
ioutil.WriteFile(path.Join(configPath, "data/templates/home_a.lua"), []byte(s), 0644)
return nil
2019-04-05 11:39:35 +00:00
}
2019-04-05 12:49:57 +00:00
func GetOutboundIP() (string, error) {
2019-04-05 11:39:35 +00:00
url := "https://diagnostic.opendns.com/myip"
req, err := http.NewRequest("GET", url, nil)
2019-04-05 12:49:57 +00:00
if err != nil {
return "", err
}
2019-04-05 11:39:35 +00:00
res, err := http.DefaultClient.Do(req)
2019-04-05 12:49:57 +00:00
if err != nil {
return "", err
}
2019-04-05 11:39:35 +00:00
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
2019-04-05 12:49:57 +00:00
if err != nil {
return "", err
}
return string(body), nil
2019-04-05 11:39:35 +00:00
}
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}
func Info(format string, args ...interface{}) {
fmt.Printf("\x1b[34;1m%s\x1b[0m\n", fmt.Sprintf(format, args...))
}