-
Notifications
You must be signed in to change notification settings - Fork 1
/
start.go
225 lines (190 loc) · 7.4 KB
/
start.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
package duty
import (
"fmt"
"net"
"net/url"
"os"
"path"
"strconv"
"strings"
embeddedpostgres "github.com/fergusstrange/embedded-postgres"
"github.com/flanksource/commons/logger"
"github.com/flanksource/commons/utils"
. "github.com/flanksource/duty/api"
"github.com/flanksource/duty/context"
"github.com/flanksource/duty/kubernetes"
"github.com/flanksource/duty/postgrest"
"github.com/spf13/pflag"
"gorm.io/plugin/prometheus"
)
func BindPFlags(flags *pflag.FlagSet, opts ...StartOption) {
config := DefaultConfig
for _, opt := range opts {
config = opt(config)
}
_ = flags.MarkDeprecated("postgrest-anon-role", "Use postgrest-role instead")
flags.StringVar(&DefaultConfig.ConnectionString, "db", "DB_URL", "Connection string for the postgres database")
flags.StringVar(&DefaultConfig.Schema, "db-schema", "public", "Postgres schema")
flags.StringVar(&DefaultConfig.Postgrest.URL, "postgrest-uri", "http://localhost:3000", "URL for the PostgREST instance to use. If localhost is supplied, a PostgREST instance will be started")
flags.StringVar(&DefaultConfig.Postgrest.LogLevel, "postgrest-log-level", "info", "PostgREST log level")
flags.StringVar(&DefaultConfig.Postgrest.JWTSecret, "postgrest-jwt-secret", "PGRST_JWT_SECRET", "JWT Secret Token for PostgREST")
flags.BoolVar(&DefaultConfig.Postgrest.Disable, "disable-postgrest", config.Postgrest.Disable, "Disable PostgREST. Deprecated (Use --postgrest-uri '' to disable PostgREST)")
flags.StringVar(&DefaultConfig.Postgrest.DBRole, "postgrest-role", "postgrest_api", "PostgREST role for authentication connections")
flags.StringVar(&DefaultConfig.Postgrest.AnonDBRole, "postgrest-anon-role", "postgrest_anon", "PostgREST role for unauthenticated connections")
flags.IntVar(&DefaultConfig.Postgrest.MaxRows, "postgrest-max-rows", 2000, "A hard limit to the number of rows PostgREST will fetch")
flags.StringVar(&DefaultConfig.LogLevel, "db-log-level", "error", "Set gorm logging level. trace, debug & info")
flags.BoolVar(&DefaultConfig.DisableKubernetes, "disable-kubernetes", false, "Disable Kubernetes integration")
flags.BoolVar(&DefaultConfig.Metrics, "db-metrics", false, "Expose db metrics")
if config.MigrationMode == SkipByDefault {
flags.BoolVar(&DefaultConfig.RunMigrations, "db-migrations", false, "Run database migrations")
} else {
flags.BoolVar(&DefaultConfig.SkipMigrations, "skip-migrations", false, "Skip database migrations")
flags.BoolVar(&DefaultConfig.RunMigrations, "db-migrations", true, "Run database migrations")
_ = flags.MarkDeprecated("db-migrations", "migrations are run by default. Use --skip-migrations to skip migrations.")
}
}
type StartOption func(config Config) Config
var DisablePostgrest = func(config Config) Config {
config.Postgrest.Disable = true
return config
}
var WithUrl = func(url string) func(config Config) Config {
return func(config Config) Config {
config.ConnectionString = url
return config
}
}
var SkipMigrationByDefaultMode = func(config Config) Config {
config.MigrationMode = SkipByDefault
return config
}
var SkipChangelogMigration = func(config Config) Config {
config.SkipMigrationFiles = []string{"007_events.sql", "012_changelog_triggers_others.sql", "012_changelog_triggers_scrapers.sql"}
return config
}
var EnableMetrics = func(config Config) Config {
config.Metrics = true
return config
}
var RunMigrations = func(config Config) Config {
config.MigrationMode = SkipByDefault
config.RunMigrations = true
return config
}
var SkipMigrations = func(config Config) Config {
config.MigrationMode = RunByDefault
config.RunMigrations = false
return config
}
var ClientOnly = func(config Config) Config {
config.Postgrest.Disable = true
config.SkipMigrations = true
return config
}
var DisableKubernetes = func(config Config) Config {
config.DisableKubernetes = true
return config
}
func Start(name string, opts ...StartOption) (context.Context, func(), error) {
config := DefaultConfig
for _, opt := range opts {
config = opt(config)
}
config = config.ReadEnv()
stop := func() {}
if strings.HasPrefix(config.ConnectionString, "embedded://") {
embeddedDBConnectionString, stopper, err := embeddedDB("embedded", config.ConnectionString, uint32(FreePort()))
if err != nil {
return context.Context{}, nil, fmt.Errorf("failed to setup embedded postgres: %w", err)
}
stop = func() {
if err := stopper(); err != nil {
logger.Errorf("error stopping embedded postgres: %v", err)
}
}
// override the embedded connection string with an actual postgres connection string
config.ConnectionString = embeddedDBConnectionString
DefaultConfig.ConnectionString = embeddedDBConnectionString
}
if config.Postgrest.URL != "" && !config.Postgrest.Disable {
parsedURL, err := url.Parse(config.Postgrest.URL)
if err != nil {
return context.Context{}, nil, fmt.Errorf("failed to parse PostgREST URL: %v", err)
}
host := strings.ToLower(parsedURL.Hostname())
port, _ := strconv.Atoi(parsedURL.Port())
config.Postgrest.Port = int(port)
if host == "localhost" {
if config.Postgrest.JWTSecret == "" {
logger.Warnf("PostgREST JWT secret not specified, generating random secret")
config.Postgrest.JWTSecret = utils.RandomString(32)
}
go postgrest.Start(config)
}
DefaultConfig = config
}
var ctx context.Context
if config.ConnectionString == "" {
logger.Warnf("--db not configured")
ctx = context.New()
} else {
if c, err := InitDB(config); err != nil {
return context.Context{}, stop, err
} else {
ctx = *c
stop = func() {
c.Pool().Close()
}
}
}
if config.Metrics {
if err := ctx.DB().Use(prometheus.New(prometheus.Config{
DBName: ctx.Pool().Config().ConnConfig.Database,
StartServer: false,
MetricsCollector: []prometheus.MetricsCollector{
&prometheus.Postgres{},
},
})); err != nil {
return context.Context{}, stop, fmt.Errorf("failed to register prometheus metrics: %w", err)
}
}
if !config.DisableKubernetes {
if client, config, err := kubernetes.NewClient(logger.GetLogger("k8s")); err == nil {
ctx = ctx.WithKubernetes(client, config)
} else {
ctx.Infof("Kubernetes client not available: %v", err)
ctx = ctx.WithKubernetes(kubernetes.Nil, nil)
}
}
return ctx, stop, nil
}
func embeddedDB(database, connectionString string, port uint32) (string, func() error, error) {
embeddedPath := strings.TrimSuffix(strings.TrimPrefix(connectionString, "embedded://"), "/")
if err := os.Chmod(embeddedPath, 0750); err != nil {
logger.Errorf("failed to chmod %s: %v", embeddedPath, err)
}
logger.Infof("Starting embedded postgres server at %s", embeddedPath)
embeddedPGServer := embeddedpostgres.NewDatabase(embeddedpostgres.DefaultConfig().
Port(port).
DataPath(path.Join(embeddedPath, "data")).
RuntimePath(path.Join(embeddedPath, "runtime")).
BinariesPath(path.Join(embeddedPath, "bin")).
Version(embeddedpostgres.V14).
Username("postgres").Password("postgres").
Database(database))
if err := embeddedPGServer.Start(); err != nil {
return "", nil, fmt.Errorf("error starting embedded postgres: %w", err)
}
return fmt.Sprintf("postgres://postgres:postgres@localhost:%d/%s?sslmode=disable", port, database), embeddedPGServer.Stop, nil
}
func FreePort() int {
// Bind to port 0 to let the OS choose a free port
listener, err := net.Listen("tcp", ":0")
if err != nil {
panic(err.Error())
}
defer listener.Close()
// Get the address of the listener
address := listener.Addr().(*net.TCPAddr)
return address.Port
}