pupirka

Форк
0
/
device.go 
98 строк · 2.9 Кб
1
package main
2

3
import (
4
	"fmt"
5
	"github.com/sirupsen/logrus"
6
	"gopkg.in/natefinch/lumberjack.v2"
7
	"os/exec"
8
	"strings"
9
)
10

11
func (device *Device) LogConfig() {
12
	device.Logdevice = logrus.New()
13

14
	switch ConfigV.GetString("log.format") {
15
	case "text":
16
		device.Logdevice.SetFormatter(&logrus.TextFormatter{})
17
	case "json":
18
		device.Logdevice.SetFormatter(&logrus.JSONFormatter{})
19
	default:
20
		device.Logdevice.SetFormatter(&logrus.TextFormatter{})
21
	}
22

23
	device.Logdevice.SetOutput(&lumberjack.Logger{
24
		Filename:   fmt.Sprintf("%s/%s/%s", ConfigV.GetString("path.log"), device.Name, "device.log"),
25
		MaxSize:    ConfigV.GetInt("log.maxsize"),
26
		MaxAge:     ConfigV.GetInt("log.maxday"),
27
		MaxBackups: ConfigV.GetInt("log.maxbackups"),
28
		LocalTime:  true,
29
		Compress:   false,
30
	})
31

32
	device.Logdevice.SetLevel(LogConsole.GetLevel())
33
}
34

35
func (device *Device) LogError(s ...interface{}) {
36
	device.Logdevice.Errorln(s...)
37
	device.Logdevice.Infoln(s...)
38
	device.Logdevice.Debugln(s...)
39
	device.Logdevice.Warnln(s...)
40
	LogConsole.Errorln(s...)
41
	LogGlobal.Errorln(s...)
42
}
43
func (device *Device) LogInfo(s ...interface{}) {
44
	device.Logdevice.Infoln(s...)
45
	device.Logdevice.Debugln(s...)
46
}
47
func (device *Device) LogWarn(s ...interface{}) {
48
	device.Logdevice.Warnln(s...)
49
	device.Logdevice.Debugln(s...)
50
	LogGlobal.Warnln(s...)
51
}
52
func (device *Device) LogDebug(s ...interface{}) {
53
	device.Logdevice.Debug(s...)
54
	LogGlobal.Debugln(s...)
55
	LogConsole.Debugln(s...)
56
}
57

58
func (device *Device) Hook() {
59
	device.LogDebug(fmt.Sprintf("Run Hook '%s': Device %s", device.StatusJob, device.Name))
60
	var command string
61
	switch device.StatusJob {
62
	case "skip":
63
		command = device.DeviceHooks.Skip
64
	case "error":
65
		command = device.DeviceHooks.Error
66
	case "backup":
67
		command = device.DeviceHooks.Backup
68
	default:
69
		command = ""
70
	}
71
	if command == "" {
72
		device.LogDebug(fmt.Sprintf("Hook '%s': No command for hook: Device %s", device.StatusJob, device.Name))
73
		return
74
	}
75
	device.LogDebug(fmt.Sprintf("Hook '%s': Command for hook '%s': Device %s", device.StatusJob, command, device.Name))
76
	rVariable := []string{"%name", "%parent", "%filename", "%address", "%portssh"}
77
	rValue := []string{device.Name, device.Parent, device.BackupFileName, device.Address, fmt.Sprintf("%d", device.PortSSH)}
78
	execCommand := strings.NewReplacer(zipStringsForReplacer(rVariable, rValue)...).Replace(command)
79
	device.LogDebug(fmt.Sprintf("Hook '%s': Command Compile for hook '%s': Device %s", device.StatusJob, execCommand, device.Name))
80
	execComandi := strings.Fields(execCommand)
81
	cmd := exec.Command(execComandi[0], execComandi[1:]...)
82
	device.LogDebug(fmt.Sprintf("Hook '%s': Hook Runiing: Device %s", device.StatusJob, device.Name))
83
	err := cmd.Start()
84
	if err != nil {
85
		device.LogError("Error running command: ", err)
86
		return
87
	}
88

89
}
90

91
func zipStringsForReplacer(a1, a2 []string) []string {
92
	r := make([]string, 2*len(a1))
93
	for i, e := range a1 {
94
		r[i*2] = e
95
		r[i*2+1] = a2[i]
96
	}
97
	return r
98
}
99

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.