-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
262 lines (236 loc) · 7.65 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
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
// A simple "http-wrapper" around pg_dump.
// Allowing to easily trigger (by invoking an HTTP endpoint) the dumping and restoring of database dumps.
// Source: https://github.com/rlindooren/pg_dumper
package main
import (
"errors"
"fmt"
"io/fs"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
)
var config *Config
func main() {
http.HandleFunc("/list", listDumps)
http.HandleFunc("/dump", createDump)
http.HandleFunc("/restore", restoreDump)
http.HandleFunc("/delete", deleteDump)
http.HandleFunc("/download", downloadDump)
http.HandleFunc("/health", health)
config = readConfig()
addr := fmt.Sprintf("%s:%s", config.host, config.port)
log.Printf("Starting (binding to '%s', config = %+v) \n", addr, config)
log.Println("Source: https://github.com/rlindooren/pg_dumper")
err := http.ListenAndServe(addr, nil)
if err != nil {
log.Fatal(err)
}
}
func listDumps(w http.ResponseWriter, _ *http.Request) {
fileInfos, err := getFileInfoOfExistingDumps()
if err != nil {
errMsg := fmt.Sprintf("Error while listing dump files %s", err)
handleError(w, http.StatusInternalServerError, errMsg, err)
return
}
for _, fileInfo := range fileInfos {
fileName := fileInfo.Name()
extIdx := strings.LastIndex(fileName, fmt.Sprintf(".%s", config.dumpfileExt))
dumpName := fileName[0:extIdx]
fmt.Fprintf(w, "%s (%s%s @ %s)\n", dumpName, config.dir, fileInfo.Name(), fileInfo.ModTime())
}
}
func createDump(w http.ResponseWriter, req *http.Request) {
name, err := getParamValueFromRequestExpectingItToExist("name", req)
if err != nil {
errMsg := fmt.Sprintf("%s", err)
handleError(w, http.StatusBadRequest, errMsg, err)
return
}
fileName := getAbsolutePathForDumpName(name)
var args []string
args = append(args, config.dumpArgs...)
args = append(args, "-f", fileName)
executeCommand("pg_dump", args, w)
}
func restoreDump(w http.ResponseWriter, req *http.Request) {
name, err := getParamValueFromRequestExpectingItToExist("name", req)
if err != nil {
errMsg := fmt.Sprintf("%s", err)
handleError(w, http.StatusBadRequest, errMsg, err)
return
}
fileName := getAbsolutePathForDumpName(name)
var command string
var args []string
// When the file has a '.sql' extension, then considering it to be a plain text dump
sqlFile, _ := regexp.MatchString("^.*\\.sql$", fileName)
if sqlFile {
command = "psql"
} else {
command = "pg_restore"
args = append(args, config.restoreArgs...)
}
args = append(args, "-f", fileName)
executeCommand(command, args, w)
}
func deleteDump(w http.ResponseWriter, req *http.Request) {
name, err := getParamValueFromRequestExpectingItToExist("name", req)
if err != nil {
errMsg := fmt.Sprintf("%s", err)
handleError(w, http.StatusBadRequest, errMsg, err)
return
}
fileName := getAbsolutePathForDumpName(name)
err = exec.Command("rm", fileName).Run()
if err != nil {
errMsg := fmt.Sprintf("Error while deleting dump '%s' %s", fileName, err)
handleError(w, http.StatusInternalServerError, errMsg, err)
}
}
func downloadDump(w http.ResponseWriter, req *http.Request) {
name, err := getParamValueFromRequestExpectingItToExist("name", req)
if err != nil {
errMsg := fmt.Sprintf("%s", err)
handleError(w, http.StatusBadRequest, errMsg, err)
return
}
fileName := getAbsolutePathForDumpName(name)
fileNameWithoutPath := fileName[strings.LastIndex(fileName, "/")+1:]
println(fileNameWithoutPath)
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", strconv.Quote(fileNameWithoutPath)))
w.Header().Set("Content-Type", "application/octet-stream")
http.ServeFile(w, req, fileName)
}
func executeCommand(command string, args []string, w http.ResponseWriter) {
cmd := exec.Command(command, args...)
stderr, err := cmd.StderrPipe()
if err != nil {
log.Fatal(err)
}
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
output, _ := ioutil.ReadAll(stderr)
fmt.Printf("%s\n", output)
if err := cmd.Wait(); err != nil {
errMsg := fmt.Sprintf("Error executing %s with arguments: %s):\n%s", command, args, output)
handleError(w, http.StatusInternalServerError, errMsg, err)
} else {
msg := fmt.Sprintf("Succesfully executed %s with arguments: %s):\n%s", command, args, output)
handleSuccess(w, msg)
}
}
func getFileInfoOfExistingDumps() ([]fs.FileInfo, error) {
files, err := ioutil.ReadDir(config.dir)
if err != nil {
return nil, err
}
filenamePattern := fmt.Sprintf("^.*\\.%s$", config.dumpfileExt)
var fileInfos []fs.FileInfo
for _, file := range files {
match, _ := regexp.MatchString(filenamePattern, file.Name())
if match && !file.IsDir() {
fileInfos = append(fileInfos, file)
}
}
return fileInfos, nil
}
func getAbsolutePathForDumpName(name string) string {
return fmt.Sprintf("%s%s.%s", config.dir, name, config.dumpfileExt)
}
// Can be used to see if the application responds and can connect to the database (e.g. a health check)
func health(w http.ResponseWriter, _ *http.Request) {
versionBytes, err := exec.Command("pg_dump", "--version").Output()
if err != nil {
errMsg := fmt.Sprintf("Error while getting version of pg_dump %s", err)
handleError(w, http.StatusInternalServerError, errMsg, err)
return
}
isReadyBytes, err := exec.Command("pg_isready").Output()
if err != nil {
errMsg := fmt.Sprintf("Error while executing pg_isready %s", err)
handleError(w, http.StatusInternalServerError, errMsg, err)
return
}
_, _ = fmt.Fprintln(w, "OK")
fmt.Fprintf(w, "pg_dump version: %v", string(versionBytes))
fmt.Fprintf(w, "pg_isready: %v", string(isReadyBytes))
}
type Config struct {
host string
port string
dir string
dumpfileExt string
dumpArgs []string
restoreArgs []string
}
func readConfig() *Config {
dumpArgs := strings.Split(getEnvVariableOrDefault("PG_DUMP_DATA_ARGS", "--clean --format=plain"), " ")
restoreArgs := strings.Split(getEnvVariableOrDefault("PG_RESTORE_ARGS", ""), " ")
dumpfileExt := determineDumpFileExtension(dumpArgs)
config := Config{
host: getEnvVariableOrDefault("HOST", ""),
port: getEnvVariableOrDefault("PORT", "8090"),
dir: getEnvVariableOrDefault("DIR", os.TempDir()),
dumpfileExt: dumpfileExt,
dumpArgs: dumpArgs,
restoreArgs: restoreArgs,
}
return &config
}
func getEnvVariableOrDefault(name, defaultVal string) string {
value := os.Getenv(name)
if len(value) == 0 {
log.Printf("No value for environment variable '%s' using default value '%s'\n", name, defaultVal)
return defaultVal
}
return value
}
func getParamValueFromRequestExpectingItToExist(queryParamName string, req *http.Request) (string, error) {
value := req.URL.Query().Get(queryParamName)
if len(value) == 0 {
errMsg := fmt.Sprintf("No value provided for query parameter '%s'", queryParamName)
return "", errors.New(errMsg)
}
return value, nil
}
func handleSuccess(w http.ResponseWriter, optionalMsg string) {
_, _ = fmt.Fprintln(w, "SUCCESS")
if len(optionalMsg) != 0 {
log.Println(optionalMsg)
_, _ = fmt.Fprintln(w, optionalMsg)
}
}
func handleError(w http.ResponseWriter, statusCode int, errMsg string, _ error) {
log.Println(errMsg)
w.WriteHeader(statusCode)
_, _ = fmt.Fprintln(w, "ERROR")
fmt.Fprintf(w, "%s\n", errMsg)
}
func determineDumpFileExtension(dumpArgs []string) string {
pattern := regexp.MustCompile("^--format=(.*)$")
var format string
for _, dumpArg := range dumpArgs {
if pattern.MatchString(dumpArg) {
format = pattern.FindStringSubmatch(dumpArg)[1]
break
}
}
if format == "plain" {
return "dump.sql"
} else if format == "custom" {
return "dump.custom"
} else if format == "tar" {
return "dump.tar"
} else {
log.Fatal("No output format specified for dumping. Please provide '--format=plain', '--format=custom' or '--format=tar'")
return "?"
}
}