|
| 1 | +/* |
| 2 | +Copyright 2018 Pressinfra SRL |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package apphelper |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "io" |
| 23 | + "net/http" |
| 24 | + "os" |
| 25 | + "os/exec" |
| 26 | + |
| 27 | + "github.com/golang/glog" |
| 28 | + tb "github.com/presslabs/mysql-operator/cmd/mysql-helper/util" |
| 29 | +) |
| 30 | + |
| 31 | +type server struct { |
| 32 | + http.Server |
| 33 | +} |
| 34 | + |
| 35 | +func NewServer(stop <-chan struct{}) *server { |
| 36 | + mux := http.NewServeMux() |
| 37 | + srv := &server{ |
| 38 | + Server: http.Server{ |
| 39 | + Addr: fmt.Sprintf(":%d", tb.ServerPort), |
| 40 | + Handler: mux, |
| 41 | + }, |
| 42 | + } |
| 43 | + |
| 44 | + // Add handle functions |
| 45 | + mux.HandleFunc(tb.ServerProbePath, srv.healthHandle) |
| 46 | + mux.Handle(tb.ServerBackupPath, tb.MaxClients(http.HandlerFunc(srv.serveBackupHandle), 1)) |
| 47 | + |
| 48 | + // Shutdown gracefully the http server |
| 49 | + go func() { |
| 50 | + <-stop // wait for stop signal |
| 51 | + if err := srv.Shutdown(context.Background()); err != nil { |
| 52 | + glog.Errorf("Failed to stop probe server, err: %s", err) |
| 53 | + } |
| 54 | + }() |
| 55 | + |
| 56 | + return srv |
| 57 | +} |
| 58 | + |
| 59 | +func (s *server) healthHandle(w http.ResponseWriter, r *http.Request) { |
| 60 | + w.WriteHeader(http.StatusOK) |
| 61 | + w.Write([]byte("OK")) |
| 62 | +} |
| 63 | + |
| 64 | +func (s *server) serveBackupHandle(w http.ResponseWriter, r *http.Request) { |
| 65 | + |
| 66 | + flusher, ok := w.(http.Flusher) |
| 67 | + if !ok { |
| 68 | + http.Error(w, "Streamming unsupported!", http.StatusInternalServerError) |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + w.Header().Set("Content-Type", "application/octet-stream") |
| 73 | + w.Header().Set("Connection", "keep-alive") |
| 74 | + |
| 75 | + xtrabackup := exec.Command("xtrabackup", "--backup", "--slave-info", "--stream=xbstream", |
| 76 | + "--host=127.0.0.1", fmt.Sprintf("--user=%s", tb.GetReplUser()), |
| 77 | + fmt.Sprintf("--password=%s", tb.GetReplPass())) |
| 78 | + |
| 79 | + xtrabackup.Stderr = os.Stderr |
| 80 | + |
| 81 | + stdout, err := xtrabackup.StdoutPipe() |
| 82 | + if err != nil { |
| 83 | + glog.Errorf("Fail to create stdoutpipe: %s", err) |
| 84 | + http.Error(w, "xtrabackup failed", http.StatusInternalServerError) |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + defer stdout.Close() |
| 89 | + |
| 90 | + if err := xtrabackup.Start(); err != nil { |
| 91 | + glog.Errorf("Fail to start xtrabackup cmd: %s", err) |
| 92 | + http.Error(w, "xtrabackup failed", http.StatusInternalServerError) |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + if _, err := io.Copy(w, stdout); err != nil { |
| 97 | + glog.Errorf("Fail to copy buffer: %s", err) |
| 98 | + http.Error(w, "buffer copy failed", http.StatusInternalServerError) |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + flusher.Flush() |
| 103 | + |
| 104 | + if err := xtrabackup.Wait(); err != nil { |
| 105 | + glog.Errorf("Fail waiting for xtrabackup to finish: %s", err) |
| 106 | + http.Error(w, "xtrabackup failed", http.StatusInternalServerError) |
| 107 | + return |
| 108 | + } |
| 109 | +} |
0 commit comments