Skip to content

Commit

Permalink
Refactor code, use constants
Browse files Browse the repository at this point in the history
  • Loading branch information
AMecea authored and calind committed Mar 21, 2019
1 parent 58bae3d commit e1aa7f0
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions pkg/sidecar/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ import (
"os/exec"
)

const (
backupStatusTrailer = "X-Backup-Status"
backupSuccessfull = "Success"
backupFailed = "Failed"
)

type server struct {
cfg *Config
http.Server
Expand Down Expand Up @@ -79,7 +85,7 @@ func (s *server) backupHandler(w http.ResponseWriter, r *http.Request) {

w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Trailer", "Success")
w.Header().Set("Trailer", backupStatusTrailer)

// nolint: gosec
xtrabackup := exec.Command("xtrabackup", "--backup", "--slave-info", "--stream=xbstream",
Expand Down Expand Up @@ -115,12 +121,13 @@ func (s *server) backupHandler(w http.ResponseWriter, r *http.Request) {

if err := xtrabackup.Wait(); err != nil {
log.Error(err, "failed waiting for xtrabackup to finish")
w.Header().Set(backupStatusTrailer, backupFailed)
http.Error(w, "xtrabackup failed", http.StatusInternalServerError)
return
}

// success
w.Header().Set("Success", "true")
w.Header().Set(backupStatusTrailer, backupSuccessfull)
flusher.Flush()
}

Expand Down Expand Up @@ -170,7 +177,7 @@ func requestABackup(cfg *Config, host, endpoint string) (*http.Response, error)
}

func checkBackupTrailers(resp *http.Response) error {
if values, ok := resp.Trailer["Success"]; !ok || !stringInSlice("true", values) {
if values, ok := resp.Trailer[backupStatusTrailer]; !ok || !stringInSlice(backupSuccessfull, values) {
// backup is failed, remove from remote
return fmt.Errorf("backup failed to be taken: no 'Success' trailer found")
}
Expand Down

0 comments on commit e1aa7f0

Please sign in to comment.