-
Notifications
You must be signed in to change notification settings - Fork 2
/
funcs.go
146 lines (117 loc) · 2.67 KB
/
funcs.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"crypto/tls"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
"github.com/BurntSushi/toml"
"gopkg.in/gomail.v2"
)
type Config struct {
Mailpassword string
Mailhost string
Mailuser string
Mailport int
Maildestinos string
Ambiente string
}
func fileGetAbsolutePath(path string) (string, os.FileInfo) {
ret, err := filepath.Abs(path)
if err != nil {
LoggerError.Fatalf("Invalid file: %v", err)
}
f, err := os.Lstat(ret)
if err != nil {
LoggerError.Fatalf("File stats failed: %v", err)
}
return ret, f
}
func ReadConfig(configFile []string) Config {
var c = strings.Join(opts.ConfigFile, "")
var config Config
if _, err := toml.DecodeFile(c, &config); err != nil {
LoggerError.Println("Archivo contiene errores, se descarta su uso")
}
return config
}
func NowDate() string {
current := time.Now()
format := current.Format("02-01-2006")
return format
}
func NowHora() string {
current := time.Now()
format := current.Format("15:04:05")
return format
}
func SendEmail(config Config, estado string, body string) {
m := gomail.NewMessage()
m.SetHeader("From", config.Mailuser)
m.SetHeader("To", config.Maildestinos)
m.SetHeader("Subject", "Task ejecutado - "+estado+" - Ambiente: "+config.Ambiente)
m.SetBody("text/html", body)
//m.Attach("/home/Alex/lolcat.jpg")
d := gomail.NewDialer(config.Mailhost, config.Mailport, config.Mailuser, config.Mailpassword)
// disable ssl check
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
if err := d.DialAndSend(m); err != nil {
LoggerError.Println("Error al enviar el email", err)
} else {
LoggerInfo.Println("Notificacion de estado enviada por email")
}
}
func checkIfFileExists(path string) bool {
f, err := os.Stat(path)
// check if path exists
if err != nil {
return false
}
return (!f.IsDir()) && f.Mode().IsRegular()
}
func checkIfDirectoryExists(path string) bool {
f, err := os.Stat(path)
// check if path exists
if err != nil {
return false
}
return f.IsDir()
}
func checkIfFileExistsAndOwnedByRoot(path string) bool {
f, err := os.Stat(path)
// check if path exists
if err != nil {
return false
}
// check if it is not a file
if !f.Mode().IsRegular() {
return false
}
uidS := fmt.Sprint(f.Sys().(*syscall.Stat_t).Uid)
uid, err := strconv.Atoi(uidS)
if err != nil {
return false
}
if uid != 0 {
return false
}
return true
}
func checkIfFileIsValid(f os.FileInfo, path string) bool {
if f.IsDir() {
return false
}
if f.Mode().IsRegular() {
if f.Mode().Perm()&0022 == 0 {
return true
} else {
return true
}
} else {
LoggerInfo.Printf("Ignoring non regular file %s\n", path)
}
return false
}