This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
/
type.go
133 lines (114 loc) · 3.94 KB
/
type.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"path/filepath"
docker "github.com/fsouza/go-dockerclient"
)
const (
defaultDockerfileName = "Dockerfile"
)
// BuildSpec is docker build spec
type BuildSpec struct {
Username string `json:"username"`
BuildTagID int `json:"buildTagID"`
GithubHost string `json:"githubHost"`
GithubOrgName string `json:"githubOrgName"`
GithubRepoName string `json:"githubRepoName"`
GithubToken string `json:"githubToken"`
GitBranchType string `json:"gitBranchType"`
GitBranchName string `json:"gitBranchName"`
DockerfileLocation string `json:"dockerfileLocation"`
DockerfileName string `json:"dockerfileName"`
DockerTagName string `json:"dockerTagName"`
DockerOrgName string `json:"dockerOrgName"`
DockerRepoName string `json:"dockerRepoName"`
D2HUBPushKeyID string `json:"d2hubPushKeyID"`
D2HUBPushKeyPassword string `json:"d2hubPushKeyPassword"`
WatchCenterID string `json:"watchCenterID"`
BuildID string `json:"buildID"`
ResultCallbackURL string `json:"resultCallbackURL"`
BuildArgStr string `json:"dockerbuildArg"`
BuildArg []docker.BuildArg `json:"-"`
}
func (b *BuildSpec) dockerImageNameWithTag() string {
return fmt.Sprintf("%s:%s", b.dockerImageName(), b.DockerTagName)
}
func (b *BuildSpec) dockerImageName() string {
return fmt.Sprintf("%s/%s/%s", dockerRegistryAddr, b.DockerOrgName, b.DockerRepoName)
}
func (b *BuildSpec) dockerfilePath() string {
dockerfileName := defaultDockerfileName
if b.DockerfileName != "" {
dockerfileName = b.DockerfileName
}
return filepath.Join(b.DockerfileLocation, dockerfileName)
}
func (b *BuildSpec) d2hubAuth() docker.AuthConfiguration {
return docker.AuthConfiguration{
Username: b.D2HUBPushKeyID,
Password: b.D2HUBPushKeyPassword,
Email: fmt.Sprintf("%s@%s", b.Username, emailDomain),
}
}
// BuildResult is result that build docker image
type BuildResult struct {
URL string `json:"-"`
BuildID string `json:"buildID"`
IsSuccess bool `json:"isSuccess"`
ErrorReason string `json:"errorReason"`
Logs string `json:"logs"`
DockerImageName string `json:"dockerImageName"`
BuildSpec *BuildSpec `json:"buildSpec"`
}
func (b *BuildResult) sendError(err error, logs []byte) {
b.IsSuccess = false
b.ErrorReason = err.Error()
b.Logs = bytes.NewBuffer(logs).String()
jsonBytes, _ := json.Marshal(b)
http.DefaultClient.Post(b.URL, "application/json", bytes.NewReader(jsonBytes))
if b.BuildSpec.WatchCenterID != "" {
notifyMsg := fmt.Sprintf(`
[D2Hub] 이미지 빌드 실패 ㅠㅠ
<Docker>
이미지: %s
<GitHub>
URL: %s
Branch/Tag: %s
<빌드 결과>
URL: %s
`,
b.DockerImageName,
fmt.Sprintf("http://%s/%s/%s", b.BuildSpec.GithubHost, b.BuildSpec.GithubOrgName, b.BuildSpec.GithubRepoName),
b.BuildSpec.GitBranchName,
fmt.Sprintf("%s/%s", buildHistoryPageURL, b.BuildID),
)
sendWatchCenterMessage(b.BuildSpec.WatchCenterID, notifyMsg)
}
}
func (b *BuildResult) sendSuccess(logs []byte) {
b.IsSuccess = true
b.Logs = bytes.NewBuffer(logs).String()
jsonBytes, _ := json.Marshal(b)
http.DefaultClient.Post(b.URL, "application/json", bytes.NewReader(jsonBytes))
if b.BuildSpec.WatchCenterID != "" {
notifyMsg := fmt.Sprintf(`
[D2Hub] 이미지 빌드 성공 :)
<Docker>
이미지: %s
<GitHub>
URL: %s
Branch/Tag: %s
<빌드 결과>
URL: %s
`,
b.DockerImageName,
fmt.Sprintf("http://github.com/%s/%s", b.BuildSpec.GithubOrgName, b.BuildSpec.GithubRepoName),
b.BuildSpec.GitBranchName,
fmt.Sprintf("%s/%s", buildHistoryPageURL, b.BuildID),
)
sendWatchCenterMessage(b.BuildSpec.WatchCenterID, notifyMsg)
}
}