-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelmAPI.go
More file actions
65 lines (61 loc) · 1.96 KB
/
Copy pathhelmAPI.go
File metadata and controls
65 lines (61 loc) · 1.96 KB
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
package main
import (
"fmt"
"log"
"net/http"
"os/exec"
"bytes"
"regexp"
"strings"
"github.com/gorilla/mux"
)
func shell(w http.ResponseWriter, r *http.Request) {
var hasNotBeenDeployed bool = true
var out bytes.Buffer
var envPath,cluster,deployment string
//workDir := "/home/ichtar/helm-charts"
workDir := "/Users/ichtar/git/helm-charts"
// get last version of code
cmd := exec.Command("git","pull","--rebase","--verbose")
cmd.Dir = workDir
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
// get all informations from pulled file
myR := regexp.MustCompile(".*(environments/([^/]*)/(.*).yaml).*")
searchResult := myR.FindAllStringSubmatch(out.String(),-1)
// if it match nothing in format environments/{cluster}/{deployment}.yaml abort and send Notfound
if searchResult != nil {
for _,i := range searchResult {
envPath=i[1]
cluster=i[2]
deployment=i[3]
fmt.Println(envPath,cluster,deployment)
if strings.EqualFold(cluster,"aws1.bestmile.io") {
// remove dry-run when want to push real update
cmd = exec.Command("helm","--dry-run","--kube-context",cluster,"upgrade",deployment,"-f",envPath,"charts/bm-stack")
cmd.Dir = workDir
cmd.Stdout = &out
err = cmd.Run()
if err != nil {
log.Fatal(err)
}
fmt.Fprintf(w, out.String())
hasNotBeenDeployed = false
}
}
}
if hasNotBeenDeployed {
http.Error(w, "No Content", http.StatusNoContent)
}
}
func handleRequests() {
myRouter := mux.NewRouter().StrictSlash(true)
myRouter.HandleFunc("/trigger", shell ).Methods("POST")
log.Fatal(http.ListenAndServeTLS(":8080","/etc/ssl/certAPI/fullchain.pem","/etc/ssl/certAPI/privkey.pem", myRouter))
}
func main() {
handleRequests()
}