Skip to content

Commit 9210bdf

Browse files
authored
feat: haproxy persistent storage support added (#170)
1 parent 6f15fc6 commit 9210bdf

File tree

1 file changed

+109
-4
lines changed

1 file changed

+109
-4
lines changed

cmd/haproxy.go

Lines changed: 109 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package cmd
22

33
import (
4+
"errors"
5+
"fmt"
6+
"github.com/swiftwave-org/swiftwave/system_config"
7+
"io"
8+
"net/http"
49
"os"
510
"os/exec"
611
"path/filepath"
@@ -69,14 +74,20 @@ var haproxyStartCmd = &cobra.Command{
6974
dockerImage = dockerImage + "-http"
7075
}
7176
// base directory for socket file
72-
mountDir := filepath.Dir(systemConfig.HAProxyConfig.UnixSocketPath)
77+
unixSocketMountDir := filepath.Dir(systemConfig.HAProxyConfig.UnixSocketPath)
78+
err := generateDefaultHAProxyConfiguration(systemConfig)
79+
if err != nil {
80+
printError("Failed to generate default HAProxy configuration")
81+
printError("Error : " + err.Error())
82+
return
83+
}
7384
// Start HAProxy service
7485
dockerCmd := exec.Command("docker", "service", "create",
7586
"--name", systemConfig.HAProxyConfig.ServiceName,
7687
"--mode", "global",
7788
"--network", systemConfig.ServiceConfig.NetworkName,
78-
"--mount", "type=bind,source="+systemConfig.HAProxyConfig.DataDir+",destination=/var/lib/haproxy",
79-
"--mount", "type=bind,source="+mountDir+",destination=/home/",
89+
"--mount", "type=bind,source="+systemConfig.HAProxyConfig.DataDir+",destination=/etc/haproxy",
90+
"--mount", "type=bind,source="+unixSocketMountDir+",destination=/home/",
8091
"--publish", "mode=host,target=80,published=80",
8192
"--publish", "mode=host,target=443,published=443",
8293
"--env", "ADMIN_USER="+systemConfig.HAProxyConfig.User,
@@ -86,7 +97,7 @@ var haproxyStartCmd = &cobra.Command{
8697
dockerCmd.Stdout = os.Stdout
8798
dockerCmd.Stderr = os.Stderr
8899
dockerCmd.Stdin = os.Stdin
89-
err := dockerCmd.Run()
100+
err = dockerCmd.Run()
90101
if err != nil {
91102
printError("Failed to start HAProxy service")
92103
return
@@ -159,3 +170,97 @@ func removeHashFromDockerImageName(image string) string {
159170
// return the first part
160171
return s[0]
161172
}
173+
174+
func generateDefaultHAProxyConfiguration(config *system_config.Config) error {
175+
// Check if the directory exists
176+
if _, err := os.Stat(config.HAProxyConfig.DataDir); os.IsNotExist(err) {
177+
return errors.New(fmt.Sprintf("Directory does not exist > %s", config.HAProxyConfig.DataDir))
178+
}
179+
baseUrl, err := generateHAProxyConfigDownloadBaseUrl(config)
180+
if err != nil {
181+
return err
182+
}
183+
// Check if `haproxy.cfg` file exists
184+
if !checkIfFileExists(config.HAProxyConfig.DataDir + "/haproxy.cfg") {
185+
err := downloadFile(baseUrl+"/haproxy.cfg", config.HAProxyConfig.DataDir+"/haproxy.cfg")
186+
if err != nil {
187+
return err
188+
} else {
189+
printSuccess("Downloaded `haproxy.cfg` file")
190+
}
191+
}
192+
// Check if `dataplaneapi.yaml` file exists
193+
if !checkIfFileExists(config.HAProxyConfig.DataDir + "/dataplaneapi.yaml") {
194+
err := downloadFile(baseUrl+"/dataplaneapi.yaml", config.HAProxyConfig.DataDir+"/dataplaneapi.yaml")
195+
if err != nil {
196+
return err
197+
} else {
198+
printSuccess("Downloaded `dataplaneapi.yaml` file")
199+
}
200+
}
201+
// Create `ssl` directory if it does not exist
202+
if _, err := os.Stat(config.HAProxyConfig.DataDir + "/ssl"); os.IsNotExist(err) {
203+
err := os.MkdirAll(config.HAProxyConfig.DataDir+"/ssl", os.ModePerm)
204+
if err != nil {
205+
return err
206+
} else {
207+
printSuccess("Created `ssl` directory")
208+
}
209+
}
210+
// Check if `ssl/default.pem` file exists
211+
if !checkIfFileExists(config.HAProxyConfig.DataDir + "/ssl/default.pem") {
212+
err := downloadFile(baseUrl+"/default.pem", config.HAProxyConfig.DataDir+"/ssl/default.pem")
213+
if err != nil {
214+
return err
215+
} else {
216+
printSuccess("Downloaded `ssl/default.pem` file")
217+
}
218+
}
219+
return nil
220+
}
221+
222+
func generateHAProxyConfigDownloadBaseUrl(config *system_config.Config) (string, error) {
223+
if config == nil {
224+
return "", errors.New("config is nil")
225+
}
226+
splitString := strings.Split(config.HAProxyConfig.DockerImage, ":")
227+
if len(splitString) < 2 {
228+
return "", errors.New("invalid docker image name")
229+
}
230+
version := splitString[1]
231+
url := "https://raw.githubusercontent.com/swiftwave-org/haproxy/main/" + version
232+
return url, nil
233+
}
234+
235+
func downloadFile(url string, filePath string) error {
236+
// download with GET request
237+
res, err := http.Get(url)
238+
if err != nil {
239+
return errors.New("failed to download file > " + url)
240+
}
241+
defer func(Body io.ReadCloser) {
242+
err := Body.Close()
243+
if err != nil {
244+
printError("Failed to close response body")
245+
}
246+
}(res.Body)
247+
248+
// Create the file
249+
out, err := os.Create(filePath)
250+
if err != nil {
251+
return errors.New("failed to create file > " + filePath)
252+
}
253+
defer func(out *os.File) {
254+
err := out.Close()
255+
if err != nil {
256+
printError("Failed to close file")
257+
}
258+
}(out)
259+
260+
// Write the body to file
261+
_, err = io.Copy(out, res.Body)
262+
if err != nil {
263+
return errors.New("failed to write file > " + filePath)
264+
}
265+
return nil
266+
}

0 commit comments

Comments
 (0)