-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
169 lines (158 loc) · 4.58 KB
/
main.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"fmt"
"github.com/juju/fslock"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"gopkg.in/natefinch/lumberjack.v2"
"log"
"os"
"os/exec"
"runtime"
"strings"
"time"
)
var ConfigV = viper.New()
var DeviceFiles []string
var LogGlobal = logrus.New()
var LogConsole = logrus.New()
func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
ConfigV.SetConfigType("json")
ConfigV.SetConfigName("pupirka.config")
ConfigV.AddConfigPath("./")
ConfigV.SetDefault("path.backup", "./backup")
ConfigV.SetDefault("path.key", "./keys")
ConfigV.SetDefault("path.devices", "./device")
ConfigV.SetDefault("path.log", "./log")
ConfigV.SetDefault("devicedefault.portshh", 22) //todo need delete
ConfigV.SetDefault("devicedefault.portssh", 22)
ConfigV.SetDefault("devicedefault.timeout", 10)
ConfigV.SetDefault("devicedefault.every", 3600)
ConfigV.SetDefault("devicedefault.rotate", 730)
ConfigV.SetDefault("devicedefault.key", "")
ConfigV.SetDefault("devicedefault.command", "/export")
ConfigV.SetDefault("devicedefault.timeformat", "20060102T1504")
ConfigV.SetDefault("devicedefault.prefix", "")
ConfigV.SetDefault("devicedefault.filenameformat", "%p%t.rcs")
ConfigV.SetDefault("devicedefault.clearstring", "")
ConfigV.SetDefault("devicedefault.hook.skip", "")
ConfigV.SetDefault("devicedefault.hook.backup", "")
ConfigV.SetDefault("devicedefault.hook.error", "")
ConfigV.SetDefault("process.max", 10)
ConfigV.SetDefault("log.maxday", 1)
ConfigV.SetDefault("log.maxsize", 10)
ConfigV.SetDefault("log.maxbackups", 10)
ConfigV.SetDefault("log.format", "text")
ConfigV.SetDefault("log.level", "info")
ConfigV.SetDefault("global.onlyone", false)
ConfigV.SetDefault("global.hook.pre", "")
ConfigV.SetDefault("global.hook.post", "")
ConfigV.SetDefault("git.email", "[email protected]")
ConfigV.SetDefault("git.user", "Pupirka")
ConfigV.SetDefault("git.password", "")
ConfigV.SetDefault("git.branch", "master")
if err := ConfigV.ReadInConfig(); err != nil { // error read config
log.Println(err)
_ = ConfigV.SafeWriteConfig()
//todo need try other exception
}
for _, path := range ConfigV.GetStringMapString("path") {
if _, err := os.Stat(path); os.IsNotExist(err) {
_ = os.Mkdir(path, os.ModePerm)
log.Printf("Create Folder %s", path)
}
}
LogConsole.SetOutput(os.Stdout)
switch ConfigV.GetString("log.level") {
case "info":
LogConsole.SetLevel(logrus.InfoLevel)
case "debug":
LogConsole.SetLevel(logrus.DebugLevel)
default:
LogConsole.SetLevel(logrus.InfoLevel)
}
LogGlobal.SetOutput(&lumberjack.Logger{
Filename: fmt.Sprintf("%s/%s", ConfigV.GetString("path.log"), "pupirka.log"),
MaxSize: 0,
MaxAge: ConfigV.GetInt("log.maxday"),
MaxBackups: 0,
LocalTime: true,
Compress: false,
})
LogGlobal.SetLevel(LogConsole.GetLevel())
if err := RunGlobalPreStart(); err != nil {
log.Fatalln(err)
}
}
func main() {
filenameLock := "../pupirka.lock"
if ConfigV.GetBool("global.onlyone") == true {
lock := fslock.New(filenameLock)
err := lock.TryLock()
if err != nil {
LogConsole.Info("Only One start Process Pupirka")
return
}
defer lock.Unlock()
}
LogConsole.Info("Starting....")
LogConsole.Info("Scan Devices....")
RunnningGlobalHookPre()
ScanDevice()
var Dev DeviceList
ReadDevice(&Dev)
if len(Dev.Devices) == 0 {
LogConsole.Info("Not Device for backup")
os.Exit(0)
}
LogConsole.Info(fmt.Sprintf("Devices count %d", len(Dev.Devices)))
RotateDevice(&Dev)
if len(Dev.Devices) == 0 {
LogConsole.Info("All Device backups actual")
os.Exit(0)
}
time.Sleep(5 * time.Second)
RunBackups(&Dev)
err := gitClient.CheckPush()
if err != nil {
LogConsole.Errorln(err)
}
RunnningGlobalHookPost()
}
func RunnningGlobalHookPre() {
LogConsole.Info("Running Hook....")
command := ConfigV.GetString("global.hook.pre")
if command == "" {
LogConsole.Info("Running Not Hook")
return
}
o, err := RunCommandInOS(command)
if err != nil {
LogConsole.Warn(fmt.Sprintf("Error HOOK %s", err.Error()))
return
}
LogConsole.Info(fmt.Sprintf("Result HOOK %s", o))
}
func RunnningGlobalHookPost() {
command := ConfigV.GetString("global.hook.post")
LogConsole.Info("Running Hook....")
if command == "" {
LogConsole.Info("Running Not Hook")
return
}
o, err := RunCommandInOS(command)
if err != nil {
LogConsole.Warn(fmt.Sprintf("Error HOOK %s", err.Error()))
return
}
LogConsole.Info(fmt.Sprintf("Result HOOK %s", o))
}
func RunCommandInOS(command string) (string, error) {
execComandi := strings.Fields(command)
res, err := exec.Command(execComandi[0], execComandi[1:]...).Output()
if err != nil {
return "", err
}
return string(res), nil
}