-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
179 lines (154 loc) · 4.33 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
170
171
172
173
174
175
176
177
178
179
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"github.com/go-git/go-git/v5"
)
func main() {
command := os.Args
proceedCommand(command)
}
//proceedCommand will check for given arguments and execute the appropriate operation
func proceedCommand(command []string) {
if len(command) > 1 {
switch command[1] {
case "help":
helperFunc()
case "h":
helperFunc()
case "deploy":
if len(command) > 2 {
fmt.Println("[-] sammy deploy command... ")
deploy(command[2])
} else {
throwError("No Directory provided !", 1)
helperFunc()
}
case "restart-nginx":
fmt.Println("[-] sammy restart-nginx command...")
case "stop":
fmt.Println("[-] sammy stop command...")
case "update-field":
fmt.Println("[-] sammy update-field command...")
default:
throwError("No valid command provided...", 0)
helperFunc()
}
} else {
throwError("No command provided", 0)
helperFunc()
}
}
//throwError will throw an error message and exit depending on the given status code
func throwError(err string, statusCode int) {
if statusCode == 1 {
log.Fatal("[x] Error: " + err)
} else if statusCode == 0 {
log.Println("[x] Error: " + err)
}
os.Exit(statusCode)
}
//execCommand will execute a command with provided program and arguments
func execCommand(program string, args string) {
fmt.Println("[-] Exec: " + program + " " + args)
cmd := exec.Command(program, args)
err := cmd.Run()
if err != nil {
log.Println(err)
}
}
//deploy is the deployment method that will pull the given repo and execute dockers command
func deploy(dir string) {
fmt.Printf("Deploying %s\n", dir)
if _, err := os.Stat(dir); err != nil {
if os.IsNotExist(err) {
throwError("Directory '"+dir+"' not found : "+err.Error(), 1)
} else {
throwError(dir+" : "+err.Error(), 1)
}
}
listDeployFolders(dir)
gitPull(dir)
// We run the docker-compose here
for _, p := range getDockerComposeFiles(dir) {
execCommand("docker-compose", "-f "+p+" up -d")
}
// we run the docker stack deploy here
for _, p := range getDockerStackFiles(dir) {
execCommand("docker stack deploy", "-c "+p)
}
}
//listDeployFolders is an util function to print the content of the config dir and the service dir
func listDeployFolders(dir string) {
servicesDir := dir + "/services"
confDir := dir + "/conf"
fmt.Printf("Config folder %s\n", confDir)
listContent(confDir)
fmt.Printf("Services folder %s\n", servicesDir)
listContent(servicesDir)
}
// getDockerComposeFiles will use findFiles to return all docker-compose files
func getDockerComposeFiles(dir string) []string {
return findFiles(dir, []string{"*-compose.yml", "*-compose.yaml"})
}
//getDockerStackFiles will use findFiles to return all docker-stack files
func getDockerStackFiles(dir string) []string {
return findFiles(dir, []string{"*-stack.yml", "*-stack.yaml"})
}
//findFiles is an util function that will search in the tree of a directory for files matching a pattern
func findFiles(targetDir string, pattern []string) []string {
result := []string{}
for _, v := range pattern {
matches, err := filepath.Glob(targetDir + v)
if err != nil {
throwError("failed to find files: "+err.Error(), 1)
}
if len(matches) != 0 {
fmt.Println("Found: ", matches)
result = append(result, matches...)
}
}
return result
}
//gitPull will pull recent changes from the current dir
func gitPull(targetDir string) {
// We instantiate a new repository targeting the given path (the .git folder)
r, err := git.PlainOpen(targetDir)
if err != nil {
fmt.Println(err)
}
// Get the working directory for the repository
w, err := r.Worktree()
if err != nil {
fmt.Println(err)
}
// Pull the latest changes from the origin remote and merge into the current branch
fmt.Println("git pull origin")
err = w.Pull(&git.PullOptions{RemoteName: "origin"})
if err != nil {
fmt.Println(err)
}
}
//listContent will just list the content of a given directory
func listContent(stringPath string) {
err := filepath.Walk(stringPath,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if path != stringPath {
fmt.Println(" " + path)
}
return nil
})
if err != nil {
throwError("failed to list content: "+err.Error(), 1)
}
}
//helperFunc is an util function to list available commands
func helperFunc() {
fmt.Println("\nEx: use sammy...")
}