/
main.go
162 lines (138 loc) · 3.79 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
// zeplic main package
//
// ZEPLIC is an application to manage ZFS datasets.
// It establishes a connection with the syslog system service,
// make a synchronisation with Consul,
// reads the dataset configuration of a JSON file
// and execute ZFS functions:
//
// Get a dataset, get a list of snapshots, create a snapshot,
// delete it, create a clone, roll back snapshot, send a snapshot...
//
package main
import (
"fmt"
"net"
"os"
"time"
"github.com/nfrance-conseil/zeplic/config"
"github.com/nfrance-conseil/zeplic/director"
"github.com/nfrance-conseil/zeplic/lib"
"github.com/pborman/getopt/v2"
)
var (
w = config.LogBook()
)
func main() {
// Available flags
optAgent := getopt.BoolLong("agent", 'a', "Execute the orders from director")
optCleaner := getopt.BoolLong("cleaner", 'c', "Clean KV pairs with the flags #NotWritten #deleted")
optDirector := getopt.BoolLong("director", 'd', "Execute 'zeplic' in synchronization mode")
optHelp := getopt.BoolLong("help", 0, "Show help menu")
optQuit := getopt.BoolLong("quit", 0, "Gracefully shutdown")
optRun := getopt.BoolLong("run", 'r', "Execute 'zeplic' in local mode")
optSlave := getopt.BoolLong("slave", 's', "Receive a new snapshot from agent")
optVersion := getopt.BoolLong("version", 'v', "Show version of zeplic")
getopt.Parse()
if len(os.Args) == 1 || len(os.Args) > 2 {
fmt.Printf("zeplic --help\n\n")
os.Exit(0)
}
// Cases...
switch {
// AGENT
case *optAgent:
go config.Pid()
// Listen for incoming connections
l, _ := net.Listen("tcp", ":7711")
defer l.Close()
w.Notice("[NOTICE] 'zeplic --agent' is running... listen on port 7711!")
// Loop to accept a new connection
for {
// Accept a new connection
connAgent, _ := l.Accept()
// Handle connection in a new goroutine
go director.HandleRequestAgent(connAgent)
}
// CLEANER
case *optCleaner:
var dataset string
fmt.Println("[CLEANER] Running zeplic cleaner's mode...")
fmt.Printf("\nPlease, indicate dataset: ")
fmt.Scanf("%s", &dataset)
// Call to Cleaner function
code := lib.Cleaner(dataset)
if code == 1 {
fmt.Printf("[CLEANER] An error has occurred while zeplic cleaned the KV pairs, please revise your syslog...\n\n")
} else {
fmt.Printf("Done!\n\n")
}
os.Exit(code)
// DIRECTOR
case *optDirector:
alive := lib.Alive()
if alive == true {
go config.Pid()
w.Notice("[NOTICE] 'zeplic --director' is running...")
// Infinite loop to manage the datasets
for {
time.Sleep(1 * time.Minute)
go director.Director()
// No stop signal, continuing loop
// ...
}
} else {
fmt.Printf("[INFO] Consul server is not running...\n\n")
os.Exit(0)
}
// HELP
case *optHelp:
getopt.Usage()
fmt.Println("")
os.Exit(0)
// QUIT
case *optQuit:
err := config.Leave()
if err == 1 {
fmt.Printf("[INFO] zeplic is not running...\n\n")
os.Exit(0)
} else {
os.Exit(0)
}
// RUN
case *optRun:
// Read JSON configuration file
values := config.Local()
// Invoke Runner() function
var code int
for i := 0; i < len(values.Dataset); i++ {
code = lib.Runner(i, false, "", false)
if code == 1 {
fmt.Printf("[RUNNER] An error has occurred while running zeplic, please revise your syslog...\n\n")
break
} else {
continue
}
}
os.Exit(code)
// SLAVE
case *optSlave:
go config.Pid()
// Listen for incoming connections
l, _ := net.Listen("tcp", ":7722")
defer l.Close()
w.Notice("[NOTICE] 'zeplic --slave' is running... listen on port 7722!")
// Loop to accept a new connection
for {
// Accept a new connection
connSlave, _ := l.Accept()
// Handle connection in a new goroutine
go director.HandleRequestSlave(connSlave)
}
// VERSION
case *optVersion:
version := config.ShowVersion()
fmt.Printf("%s", version)
os.Exit(0)
}
}