-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
221 lines (203 loc) · 5.04 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/registry"
"github.com/docker/docker/client"
"github.com/labstack/echo/v4"
)
func main() {
username := os.Getenv("DOCKER_USERNAME")
password := os.Getenv("DOCKER_PASSWORD")
e := echo.New()
ctx := context.Background()
authConfig := registry.AuthConfig{
Username: username,
Password: password,
}
encodedJSON, err := json.Marshal(authConfig)
if err != nil {
log.Fatalf("error when encoding authConfig. err: %v", err)
}
authStr := base64.URLEncoding.EncodeToString(encodedJSON)
log.Printf("authStr created: %s", authStr)
cli, err := client.NewClientWithOpts(
client.WithVersion("1.43"),
)
if err != nil {
log.Fatalf("Error creating Docker client: %v", err)
}
defer cli.Close()
log.Printf("client created: %s", cli.ClientVersion())
ping, err := cli.Ping(ctx)
if err != nil {
log.Fatalf("Error pinging Docker client: %v", err)
}
log.Printf("client pinged: %s", ping.APIVersion)
res, err := cli.RegistryLogin(ctx, authConfig)
if err != nil {
log.Fatalf("Error RegistryLogin: %v", err)
}
log.Printf("RegistryLogin res: %s", res.Status)
log.Printf("RegistryLogin res.IdentityToken: %s", res.IdentityToken)
e.POST("/pull", func(c echo.Context) error {
var payload Payload
if err := c.Bind(&payload); err != nil {
log.Print("Error parsing JSON:", err)
return c.String(http.StatusBadRequest, "Bad Request")
}
b, err := json.Marshal(payload)
if err != nil {
log.Print("marshal err: ", err.Error())
}
log.Print("Webhook received json:", string(b))
log.Print(
"----------------------------------------------------------------",
)
log.Print("Webhook received PushedAt:", payload.PushData.PushedAt)
log.Print(
"----------------------------------------------------------------",
)
if payload.Repository.RepoName == "webhook" {
log.Print("ignoring webhook push event")
return c.String(
http.StatusBadRequest,
"webhook self push event",
)
}
tag := "latest"
// lab project location
src := "/root/docker/projects"
if c.QueryParams().Has("factory") {
tag = "factory"
src = "/home/docker/projects"
}
if payload.PushData.PushedAt != 0 {
log.Print("Image push event detected")
fullImageName := fmt.Sprintf(
"%s:%s",
payload.Repository.RepoName,
tag,
)
out, err := cli.ImagePull(
ctx,
fullImageName,
image.PullOptions{
RegistryAuth: authStr,
},
)
if err != nil {
log.Fatalf("Error pulling image: %v", err)
}
defer out.Close()
body, err := io.ReadAll(out)
if err != nil {
log.Print("body ReadAll error: ", err.Error())
}
log.Println("out body:", string(body))
if err := cli.ContainerRemove(
ctx,
payload.Repository.Name,
container.RemoveOptions{
RemoveVolumes: false,
RemoveLinks: false,
Force: true,
},
); err != nil {
log.Print("container remove error: ", err.Error())
}
mounts := []mount.Mount{
{
Type: mount.TypeBind,
Source: fmt.Sprintf(
"%s/%s/public",
src,
payload.Repository.Name,
),
Target: "/public",
},
{
Type: mount.TypeBind,
Source: fmt.Sprintf(
"%s/%s/private",
src,
payload.Repository.Name,
),
Target: "/private",
},
}
creatRes, err := cli.ContainerCreate(
ctx,
&container.Config{
Image: fullImageName,
},
&container.HostConfig{
Mounts: mounts,
},
&network.NetworkingConfig{
EndpointsConfig: map[string]*network.EndpointSettings{
"sadeem": {
NetworkID: "sadeem",
},
},
},
nil,
payload.Repository.Name,
)
if err != nil {
log.Print("container create error: ", err.Error())
}
log.Println("container created id: ", creatRes.ID)
log.Println("container created warnings: ", creatRes.Warnings)
if err := cli.ContainerStart(
ctx,
creatRes.ID,
container.StartOptions{},
); err != nil {
log.Print("container start error: ", err.Error())
}
statusCh, errCh := cli.ContainerWait(
ctx,
creatRes.ID,
container.WaitConditionNotRunning,
)
select {
case err := <-errCh:
if err != nil {
log.Print("container status error: ", err.Error())
}
case <-statusCh:
}
execRes, err := cli.ContainerExecCreate(
ctx,
"nginx",
types.ExecConfig{
AttachStderr: true,
AttachStdout: true,
Cmd: []string{"nginx", "-s", "reload"},
},
)
if err != nil {
log.Print("container exec error: ", err.Error())
}
log.Print("container exec res.ID: ", execRes.ID)
return c.String(
http.StatusOK,
"Webhook received and processed successfully",
)
}
return c.String(http.StatusBadRequest, "Invalid Webhook Event")
})
e.Logger.Fatal(e.Start(":8000"))
}