-
Notifications
You must be signed in to change notification settings - Fork 3
/
cleanup.go
69 lines (62 loc) · 1.68 KB
/
cleanup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net/smtp"
"os"
"strings"
"text/template"
"github.com/Mayo-QIN/grunt/dassets"
)
func Email(job *Job) {
if config.Mail.Server == "" || len(job.Address) == 0 {
return
}
var auth smtp.Auth = nil
if config.Mail.Username != "" && config.Mail.Password != "" {
auth = smtp.PlainAuth("", config.Mail.Username, config.Mail.Password, config.Mail.Server)
}
var templateData = map[string]interface{}{
"job": job,
"service": config.ServiceMap[job.EndPoint],
"to": strings.Trim(fmt.Sprint(job.Address), "[]"),
"config": config,
}
contents, _ := Asset("template/email.txt")
if debug {
contents, _ = dassets.Asset("template/email.txt")
}
t, _ := template.New("email").Parse(string(contents))
var buffer bytes.Buffer
err := t.Execute(&buffer, templateData)
if err != nil {
log.Printf("Failed to construct email: %v", err.Error())
return
}
server := fmt.Sprintf("%s:%d", config.Mail.Server, config.Mail.Port)
log.Printf("Mailing status for %v (%v) to %v", job.EndPoint, job.UUID, job.Address)
err = smtp.SendMail(server, auth, config.Mail.From, job.Address, buffer.Bytes())
if err != nil {
log.Printf("Error sending mail: %v", err.Error())
} else {
log.Printf("Mail sent")
}
}
func Cleanup(job *Job) {
log.Printf("Cleaning up %v", job.UUID)
dir, err := ioutil.TempDir("", job.UUID)
if err != nil {
log.Printf("Error creating temp dir path: %v", err.Error())
return
}
log.Printf("Deleting temp directory %v", dir)
err = os.RemoveAll(dir)
if err != nil {
log.Printf("Error removing dir: %v", err.Error())
return
}
delete(jobs, job.UUID)
log.Printf("Cleanup done of %v (%v)", job.EndPoint, job.UUID)
}