-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
175 lines (141 loc) · 4.56 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
package main
import (
"context"
"fmt"
"github.com/gin-gonic/gin"
qovery "github.com/qovery/qovery-client-go"
log "github.com/sirupsen/logrus"
"http-handler-to-redeploy/src"
"strings"
"time"
)
type QueueData struct {
Webhook src.PrismicWebhook
Context gin.Context
}
func redeployHandler(q *src.Queue[QueueData]) gin.HandlerFunc {
fn := func(c *gin.Context) {
webhook := src.PrismicWebhook{}
if err := c.BindJSON(&webhook); err != nil {
c.JSON(400, gin.H{
"status": "error",
"error": err.Error(),
"body": c.Request.Body,
})
return
}
_ = q.Enqueue(QueueData{
Webhook: webhook,
Context: *c.Copy(), // copy gin context since it's not thread safe
})
c.JSON(200, gin.H{
"status": "ok",
})
}
return fn
}
func redeploy(c *gin.Context, client *qovery.APIClient, environmentId string, applicationId string) {
for {
// check environment status before trying to redeploy; otherwise it will fail
status, _, err := client.EnvironmentMainCallsApi.GetEnvironmentStatus(context.Background(), environmentId).Execute()
if err != nil {
log.Errorf("error while getting environment status: %v", err)
return
}
if isTerminalState(status.State) {
log.Infof("environment %s is in a terminal state, continuing", environmentId)
break
}
log.Infof("environment %s is not in a terminal state, waiting 5 seconds before trying again", environmentId)
time.Sleep(5 * time.Second)
}
// change environment variable to force app rebuild
envVars, _, err := client.ApplicationEnvironmentVariableApi.ListApplicationEnvironmentVariable(context.Background(), applicationId).Execute()
if err != nil {
log.Errorf("error while getting environment variables: %v", err)
return
}
envVarKey := "TO_REDEPLOY_FAKE"
envVar := findEnvironmentVariable(envVars.Results, envVarKey)
if envVar == nil {
log.Errorf("error while getting environment variable: %s", envVarKey)
return
}
_, _, err = client.ApplicationEnvironmentVariableApi.EditApplicationEnvironmentVariable(context.Background(), applicationId, envVar.Id).
EnvironmentVariableEditRequest(qovery.EnvironmentVariableEditRequest{
Key: envVar.Key,
Value: fmt.Sprintf("%d", time.Now().Unix()),
}).Execute()
if err != nil {
log.Errorf("error while editing environment variable: %v", err)
return
}
// Redeploy with Qovery
_, _, err = client.ApplicationActionsApi.RedeployApplication(context.Background(), applicationId).Execute()
if err != nil {
log.Errorf("error while redeploying application: %v", err)
return
}
}
func isTerminalState(state qovery.StateEnum) bool {
return state == qovery.STATEENUM_RUNNING || state == qovery.STATEENUM_DEPLOYED || state == qovery.STATEENUM_DELETED ||
state == qovery.STATEENUM_STOPPED || state == qovery.STATEENUM_CANCELED ||
state == qovery.STATEENUM_READY || strings.HasSuffix(string(state), "ERROR")
}
func findEnvironmentVariable(envVars []qovery.EnvironmentVariable, key string) *qovery.EnvironmentVariable {
for _, envVar := range envVars {
if envVar.Key == key {
return &envVar
}
}
return nil
}
func processQueue(q *src.Queue[QueueData], config *src.Config) {
conf := qovery.NewConfiguration()
conf.DefaultHeader["Authorization"] = "Token " + strings.TrimSpace(config.QoveryApiToken)
client := qovery.NewAPIClient(conf)
app, _, err := client.ApplicationMainCallsApi.GetApplication(context.Background(), config.QoveryProdApplicationId).Execute()
if err != nil {
panic(err)
}
environmentId := app.Environment.Id
for {
if queueData, ok := q.Dequeue(); ok {
redeploy(&queueData.Context, client, environmentId, config.QoveryProdApplicationId)
// TODO: add support for staging
/*var tags []src.Tag
tags = append(tags, queueData.Webhook.Tags.Addition...)
tags = append(tags, queueData.Webhook.Tags.Deletion...)
for _, tag := range tags {
switch strings.ToLower(tag.ID) {
case "production":
// redeploy production
redeploy(&queueData.Context, client, environmentId, config.QoveryProdApplicationId)
case "staging":
// redeploy staging
redeploy(&queueData.Context, client, environmentId, config.QoveryStagingApplicationId)
}
}*/
}
}
}
func main() {
config := src.LoadConfig()
q := src.NewQueue[QueueData](1000, true)
go func(q *src.Queue[QueueData], config *src.Config) {
processQueue(q, config)
}(q, &config)
r := gin.Default()
//r.Use(gin.Logger())
r.Use(gin.Recovery())
r.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"status": "ok",
})
})
r.POST("/handler", redeployHandler(q))
err := r.Run(fmt.Sprintf(":%d", config.HttpPort))
if err != nil {
return
}
}