-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathdeploy_compose.go
More file actions
291 lines (244 loc) · 9.08 KB
/
Copy pathdeploy_compose.go
File metadata and controls
291 lines (244 loc) · 9.08 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
Copyright © 2024 Mahmoud Mousa <m.mousa@hey.com>
Licensed under the GNU GPL License, Version 3.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.gnu.org/licenses/gpl-3.0.en.html
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package deploy implements Docker Compose deployment support for Sidekick
// Author: madebycm (https://github.com/madebycm)
package deploy
import (
"crypto/md5"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/joho/godotenv"
tea "github.com/charmbracelet/bubbletea"
"github.com/mightymoud/sidekick/render"
"github.com/mightymoud/sidekick/utils"
"github.com/pterm/pterm"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
)
func deployCompose(appConfig utils.SidekickAppConfig) {
start := time.Now()
// Parse the compose file
compose, err := utils.ParseComposeFile(appConfig.ComposeFile)
if err != nil {
pterm.Error.Printf("Failed to parse compose file: %s", err)
os.Exit(1)
}
// Get services with build contexts
buildServices := utils.GetServicesWithBuildContext(compose)
// Get main service
mainService := compose.Services[appConfig.MainService]
// Update Traefik labels on main service
if mainService.Labels == nil {
mainService.Labels = []string{}
}
// Update labels with new domain if changed
var updatedLabels []string
for _, label := range mainService.Labels {
if strings.HasPrefix(label, "traefik.http.routers.") && strings.Contains(label, ".rule=Host") {
updatedLabels = append(updatedLabels, fmt.Sprintf("traefik.http.routers.%s.rule=Host(`%s`)", appConfig.Name, appConfig.Url))
} else {
updatedLabels = append(updatedLabels, label)
}
}
mainService.Labels = updatedLabels
compose.Services[appConfig.MainService] = mainService
// Check env file changes
shouldUpdateEnv := false
if appConfig.Env.File != "" && utils.FileExists(appConfig.Env.File) {
// Calculate current env file hash
envFile, _ := os.Open(appConfig.Env.File)
envMap, _ := godotenv.Parse(envFile)
envFileContent, _ := godotenv.Marshal(envMap)
currentHash := fmt.Sprintf("%x", md5.Sum([]byte(envFileContent)))
if currentHash != appConfig.Env.Hash {
pterm.Info.Println("Detected changes to environment file - Will re-encrypt")
shouldUpdateEnv = true
appConfig.Env.Hash = currentHash
}
}
// Prepare stages
stages := []render.Stage{
render.MakeStage("Checking connection with VPS", "VPS is reachable", false),
}
// Add build stages for services with build contexts
imageMap := make(map[string]string)
for serviceName := range buildServices {
imageName := fmt.Sprintf("%s-%s:v%s", appConfig.Name, serviceName, appConfig.Version)
imageMap[serviceName] = imageName
stages = append(stages, render.MakeStage(
fmt.Sprintf("Building image for service: %s", serviceName),
fmt.Sprintf("Image built for %s", serviceName),
true,
))
}
if len(imageMap) > 0 {
stages = append(stages,
render.MakeStage("Preparing to deploy", "Images saved", false),
render.MakeStage("Deploying new version", "Images pushed to server", false),
)
}
stages = append(stages,
render.MakeStage("Running new version", "New version is up", false),
)
p := tea.NewProgram(render.TuiModel{
Stages: stages,
BannerMsg: fmt.Sprintf("Deploying version %s of %s 🚀", appConfig.Version, appConfig.Name),
ActiveIndex: 0,
Quitting: false,
AllDone: false,
})
go func() {
// SSH connection
sshClient, err := utils.Login(viper.GetString("serverAddress"), "sidekick")
if err != nil {
p.Send(render.ErrorMsg{ErrorStr: "Something went wrong logging in to your VPS"})
}
p.Send(render.NextStageMsg{})
// Build services with build contexts
stageIndex := 1
for serviceName := range buildServices {
service := buildServices[serviceName]
imageName := imageMap[serviceName]
// Determine build context
buildContext := "."
dockerfile := "Dockerfile"
buildArgs := []string{}
if service.Build != nil {
if service.Build.Context != "" {
buildContext = service.Build.Context
}
if service.Build.Dockerfile != "" {
dockerfile = service.Build.Dockerfile
}
for key, value := range service.Build.Args {
buildArgs = append(buildArgs, "--build-arg", fmt.Sprintf("%s=%v", key, value))
}
}
// Build with cache
dockerBuildCmd := exec.Command("docker", append([]string{
"build",
"--tag", imageName,
"--progress=plain",
"--platform=linux/amd64",
"--cache-from", fmt.Sprintf("%s-%s:v%d", appConfig.Name, serviceName, getVersionNumber(appConfig.Version)-1),
"-f", filepath.Join(buildContext, dockerfile),
}, append(buildArgs, buildContext)...)...)
dockerBuildCmdErrPipe, _ := dockerBuildCmd.StderrPipe()
go render.SendLogsToTUI(dockerBuildCmdErrPipe, p)
if dockerBuildErr := dockerBuildCmd.Run(); dockerBuildErr != nil {
p.Send(render.ErrorMsg{ErrorStr: fmt.Sprintf("Failed to build %s", serviceName)})
}
time.Sleep(time.Millisecond * 100)
p.Send(render.NextStageMsg{})
stageIndex++
}
// Update compose file with new image tags
for serviceName, imageName := range imageMap {
if service, exists := compose.Services[serviceName]; exists {
service.Image = imageName
service.Build = nil
compose.Services[serviceName] = service
}
}
// Write updated compose file
updatedComposeFile := fmt.Sprintf("sidekick-%s", appConfig.ComposeFile)
composeData, _ := yaml.Marshal(&compose)
os.WriteFile(updatedComposeFile, composeData, 0644)
defer os.Remove(updatedComposeFile)
// Save and transfer images if any were built
if len(imageMap) > 0 {
imgFileName := fmt.Sprintf("%s-v%s.tar", appConfig.Name, appConfig.Version)
var imageNames []string
for _, imageName := range imageMap {
imageNames = append(imageNames, imageName)
}
imgSaveCmd := exec.Command("docker", append([]string{"save", "-o", imgFileName}, imageNames...)...)
imgSaveCmdErrPipe, _ := imgSaveCmd.StderrPipe()
go render.SendLogsToTUI(imgSaveCmdErrPipe, p)
if imgSaveCmdErr := imgSaveCmd.Run(); imgSaveCmdErr != nil {
p.Send(render.ErrorMsg{})
}
defer os.Remove(imgFileName)
time.Sleep(time.Millisecond * 100)
p.Send(render.NextStageMsg{})
// Transfer
remoteDist := fmt.Sprintf("%s@%s:./%s", "sidekick", viper.GetString("serverAddress"), appConfig.Name)
imgMoveCmd := exec.Command("scp", "-C", imgFileName, remoteDist)
imgMoveCmdErrorPipe, _ := imgMoveCmd.StderrPipe()
go render.SendLogsToTUI(imgMoveCmdErrorPipe, p)
if imgMovCmdErr := imgMoveCmd.Run(); imgMovCmdErr != nil {
p.Send(render.ErrorMsg{})
}
// Load on server
dockerLoadOutChan, _, sessionErr := utils.RunCommand(sshClient, fmt.Sprintf("cd %s && docker load -i %s && rm %s", appConfig.Name, imgFileName, imgFileName))
go func() {
for line := range dockerLoadOutChan {
p.Send(render.LogMsg{LogLine: line + "\n"})
}
}()
if sessionErr != nil {
p.Send(render.ErrorMsg{ErrorStr: sessionErr.Error()})
}
time.Sleep(time.Millisecond * 100)
p.Send(render.NextStageMsg{})
}
// Transfer updated compose file
rsyncCmd := exec.Command("rsync", updatedComposeFile, fmt.Sprintf("%s@%s:%s/%s", "sidekick", viper.GetString("serverAddress"), appConfig.Name, "docker-compose.yml"))
if rsyncCmErr := rsyncCmd.Run(); rsyncCmErr != nil {
p.Send(render.ErrorMsg{ErrorStr: rsyncCmErr.Error()})
}
// Handle env update if needed
if shouldUpdateEnv {
dockerEnvProperty := []string{}
utils.HandleEnvFile(appConfig.Env.File, &dockerEnvProperty, &appConfig.Env.Hash)
encryptSync := exec.Command("rsync", "encrypted.env", fmt.Sprintf("%s@%s:%s", "sidekick", viper.GetString("serverAddress"), appConfig.Name))
encryptSync.Run()
os.Remove("encrypted.env")
}
// Deploy with zero downtime
var deployCmd string
if appConfig.Env.File != "" {
deployCmd = fmt.Sprintf(`cd %s && export SOPS_AGE_KEY=%s && sops exec-env encrypted.env 'docker compose -p %s up -d'`,
appConfig.Name, viper.GetString("secretKey"), appConfig.Name)
} else {
deployCmd = fmt.Sprintf(`cd %s && docker compose -p %s up -d`, appConfig.Name, appConfig.Name)
}
deployOutChan, _, sessionErr := utils.RunCommand(sshClient, deployCmd)
go func() {
for line := range deployOutChan {
p.Send(render.LogMsg{LogLine: line + "\n"})
}
}()
if sessionErr != nil {
p.Send(render.ErrorMsg{ErrorStr: sessionErr.Error()})
}
// Update version
appConfig.Version = fmt.Sprintf("V%d", getVersionNumber(appConfig.Version)+1)
ymlData, _ := yaml.Marshal(&appConfig)
os.WriteFile("./sidekick.yml", ymlData, 0644)
p.Send(render.AllDoneMsg{Duration: time.Since(start).Round(time.Second), URL: appConfig.Url})
}()
if _, err := p.Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
}
func getVersionNumber(version string) int {
var v int
fmt.Sscanf(version, "V%d", &v)
return v
}