forked from viliproject/vili
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobs.go
82 lines (71 loc) · 1.78 KB
/
jobs.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
package api
import (
"fmt"
"net/http"
"strings"
"sync"
"github.com/airware/vili/docker"
"github.com/airware/vili/environments"
"github.com/airware/vili/log"
"github.com/airware/vili/templates"
"github.com/airware/vili/util"
"gopkg.in/labstack/echo.v1"
)
// JobResponse is the response for the job endpoint
type JobResponse struct {
Repository []*docker.Image `json:"repository,omitempty"`
PodSpec string `json:"podSpec,omitempty"`
}
func jobHandler(c *echo.Context) error {
env := c.Param("env")
job := c.Param("job")
requestFields := c.Request().URL.Query().Get("fields")
queryFields := make(map[string]bool)
if requestFields != "" {
for _, field := range strings.Split(requestFields, ",") {
queryFields[field] = true
}
}
environment, err := environments.Get(env)
if err != nil {
return err
}
resp := JobResponse{}
failed := false
// repository
var waitGroup sync.WaitGroup
if len(queryFields) == 0 || queryFields["repository"] {
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
branches := []string{"master", "develop"}
if environment.Branch != "" && !util.NewStringSet(branches).Contains(environment.Branch) {
branches = append(branches, environment.Branch)
}
images, err := docker.GetRepository(job, branches)
if err != nil {
log.Error(err)
failed = true
}
resp.Repository = images
}()
}
// podSpec
if len(queryFields) == 0 || queryFields["podSpec"] {
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
body, err := templates.Pod(environment.Name, environment.Branch, job)
if err != nil {
log.Error(err)
failed = true
}
resp.PodSpec = string(body)
}()
}
waitGroup.Wait()
if failed {
return fmt.Errorf("failed one of the service calls")
}
return c.JSON(http.StatusOK, resp)
}