Ajout d'une notification par email en cas de changement d'ip ou d'erreur

This commit is contained in:
Laurent Le Houerou 2019-04-06 15:56:17 +04:00
parent 63db3293d7
commit a31a89c128

44
main.go
View File

@ -11,6 +11,7 @@ import (
githttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
"io/ioutil"
"net/http"
"net/smtp"
"os"
"os/signal"
"path"
@ -103,7 +104,7 @@ func start(c *cli.Context) error {
defer func() { tryLockSem <- v }()
if err := update(c); err != nil {
log.Println(err)
SendStatusMail(fmt.Sprintf("Error during update process : %s", err))
}
default:
log.Debug("Skipped another update already running.")
@ -150,6 +151,12 @@ func update(c *cli.Context) error {
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)
err = os.RemoveAll(path.Join(configPath, "data"))
if err != nil {
return err
@ -219,9 +226,44 @@ func update(c *cli.Context) error {
if err != nil {
return err
}
SendStatusMail(fmt.Sprintf(`Home IP has changed : %s -> %s
Dns updated successfully`, currentIp, newIp))
return nil
}
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!")
}
}
func GetCurrentIp() (string, error) {
b, err := ioutil.ReadFile(path.Join(configPath, "data/templates/home_a.lua"))
if err != nil {