-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
110 lines (97 loc) · 3.31 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
/*******************************************************************************
*
* Copyright 2023 SAP SE
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You should have received a copy of the License along with this
* program. If not, you may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 main
import (
"context"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sapcc/go-api-declarations/bininfo"
"github.com/sapcc/go-bits/httpapi"
"github.com/sapcc/go-bits/httpext"
"github.com/sapcc/go-bits/logg"
"github.com/sapcc/go-bits/must"
"github.com/sapcc/go-bits/osext"
"go.uber.org/automaxprocs/maxprocs"
"github.com/sapcc/backup-tools/internal/api"
"github.com/sapcc/backup-tools/internal/backup"
"github.com/sapcc/backup-tools/internal/core"
)
func main() {
bininfo.HandleVersionArgument()
logg.ShowDebug = osext.GetenvBool("BACKUP_TOOLS_DEBUG")
undoMaxprocs := must.Return(maxprocs.Set(maxprocs.Logger(logg.Debug)))
defer undoMaxprocs()
wrap := httpext.WrapTransport(&http.DefaultTransport)
wrap.SetOverrideUserAgent(bininfo.Component(), bininfo.VersionOr("unknown"))
ctx := httpext.ContextWithSIGINT(context.Background(), 1*time.Second)
cfg := must.Return(core.NewConfiguration(ctx))
// listen to SIGUSR1 (this signal causes a backup to be created immediately, regardless of schedule)
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGUSR1)
// fork off the main loop
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
ticker := time.NewTicker(1 * time.Minute)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
err := backup.CreateIfNecessary(ctx, cfg)
if err != nil {
logg.Error(err.Error())
}
case <-signalChan:
_, err := backup.Create(cfg, "because of user request (SIGUSR1)")
if err != nil {
logg.Error(err.Error())
}
}
}
}()
// serve Prometheus metrics on another goroutine (this needs to be separate
// from the rest of the HTTP API because the metrics port is exposed to
// outside the container)
go func() {
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
must.Succeed(httpext.ListenAndServeContext(ctx, ":9188", mux))
}()
// serve the HTTP API on the main thread
//
//NOTE: This API does not do any authentication at all, and that's okay
// because it listens on 127.0.0.1 only. Therefore you can only access it via
// `kubectl exec` or `kubectl port-forward`.
handler := httpapi.Compose(
api.API{Config: cfg},
httpapi.HealthCheckAPI{SkipRequestLog: true},
)
must.Succeed(httpext.ListenAndServeContext(ctx, "0.0.0.0:8080", handler))
// on SIGINT/SIGTERM, give the backup main loop a chance to complete a backup
// that's currently in flight
wg.Wait()
}