-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
140 lines (117 loc) · 2.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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/go-git/go-git/v5"
)
func main() {
if len(os.Args) == 0 {
println("[x] No command provided")
return
}
switch command := os.Args[1]; command {
case "deploy":
println("[-] sammy deploy command... ")
dir := os.Args[2]
deploy(dir)
case "restart-nginx":
println("[-] sammy restart-nginx command...")
case "stop":
println("[-] sammy stop command...")
case "update-field":
println("[-] sammy update-field command...")
default:
println("[x] No valid command provided...")
helperFunc()
}
}
func deploy(dir string) {
println("[>] DEPLOY_DIR : " + dir)
if _, err := os.Stat(dir); err != nil {
if os.IsNotExist(err) {
println("[x] ERR: " + dir + " doesn't exist.")
println(err)
return
} else {
println("[x] ERR: Unexpected error occured for the dir: " + dir)
println(err)
return
}
}
listDeployFolders(dir)
gitPull(dir)
// We run the docker-compose here
for _, p := range getDockerComposeFiles(dir) {
exec.Command("docker-compose", "-f "+p+" up -d")
}
// we run the docker stack deploy here
for _, p := range getDockerStackFiles(dir) {
exec.Command("docker stack deploy", "-c "+p)
}
}
func listDeployFolders(dir string) {
servicesDir := dir + "/services"
confDir := dir + "/conf"
println("[>] CONF_DIR " + confDir)
listContent(confDir)
println("[>] SERVICE_DIR " + servicesDir)
listContent(servicesDir)
}
func getDockerComposeFiles(dir string) []string {
return findFile(dir, []string{"*-compose.yml", "*-compose.yaml"})
}
func getDockerStackFiles(dir string) []string {
return findFile(dir, []string{"*-stack.yml", "*-stack.yaml"})
}
func findFile(targetDir string, pattern []string) []string {
result := []string{}
for _, v := range pattern {
matches, err := filepath.Glob(targetDir + v)
if err != nil {
fmt.Println(err)
}
if len(matches) != 0 {
fmt.Println("Found : ", matches)
result = append(result, matches...)
}
}
return result
}
func gitPull(targetDir string) {
// We instantiate a new repository targeting the given path (the .git folder)
r, err := git.PlainOpen(targetDir)
if err != nil {
println(err)
}
// Get the working directory for the repository
w, err := r.Worktree()
if err != nil {
println(err)
}
// Pull the latest changes from the origin remote and merge into the current branch
println("git pull origin")
err = w.Pull(&git.PullOptions{RemoteName: "origin"})
if err != nil {
println(err)
}
}
func listContent(stringPath string) {
err2 := filepath.Walk(stringPath,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if path != stringPath {
println(" " + path)
}
return nil
})
if err2 != nil {
println(err2)
}
}
func helperFunc() {
println("\nEx: use sammy...")
}