Ajout d'une notification par email en cas de changement d'ip ou d'erreur
This commit is contained in:
parent
63db3293d7
commit
a31a89c128
44
main.go
44
main.go
@ -11,6 +11,7 @@ import (
|
|||||||
githttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
|
githttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/smtp"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"path"
|
"path"
|
||||||
@ -103,7 +104,7 @@ func start(c *cli.Context) error {
|
|||||||
defer func() { tryLockSem <- v }()
|
defer func() { tryLockSem <- v }()
|
||||||
if err := update(c); err != nil {
|
if err := update(c); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
SendStatusMail(fmt.Sprintf("Error during update process : %s", err))
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
log.Debug("Skipped another update already running.")
|
log.Debug("Skipped another update already running.")
|
||||||
@ -150,6 +151,12 @@ func update(c *cli.Context) error {
|
|||||||
configuration := Configuration{}
|
configuration := Configuration{}
|
||||||
err := decoder.Decode(&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"))
|
err = os.RemoveAll(path.Join(configPath, "data"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -219,9 +226,44 @@ func update(c *cli.Context) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SendStatusMail(fmt.Sprintf(`Home IP has changed : %s -> %s
|
||||||
|
Dns updated successfully`, currentIp, newIp))
|
||||||
|
|
||||||
return nil
|
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) {
|
func GetCurrentIp() (string, error) {
|
||||||
b, err := ioutil.ReadFile(path.Join(configPath, "data/templates/home_a.lua"))
|
b, err := ioutil.ReadFile(path.Join(configPath, "data/templates/home_a.lua"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user