-
Notifications
You must be signed in to change notification settings - Fork 111
/
main.go
495 lines (423 loc) · 12.6 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// pg-helper: A command-line utility to call p-sidecar-service RPC's to
// perform database operations. It is intended to be used in hab hooks,
// though you can also use the pg-sidecar-service client library package
// in your application if you prefer to perform database operations in
// your application.
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/grpc/status"
pgs "github.com/chef/automate/api/interservice/pg_sidecar"
"github.com/chef/automate/components/pg-sidecar-service/pkg/client"
platform_config "github.com/chef/automate/lib/platform/config"
)
var ErrUserRequired = errors.New("user not specified")
// HelperOpts are all the available configuration options
type HelperOpts struct {
Debug bool `mapstructure:"debug"`
CfgFile string `mapstructure:"cfg_file"`
Client ClientOpts `mapstructure:"client"`
Migrate MigrateTablesOpts `mapstructure:"migrate"`
AlterRole *pgs.AlterRoleReq_Options `mapstructure:"alter_role"`
DropTables *pgs.DropTablesReq `mapstructure:"drop_tables"`
}
// MigrateTablesOpts are the migration specific options
type MigrateTablesOpts struct {
// SkipDBCreate allows you to skip the creation of the destination database
// when trying to migrate tables
SkipDBCreate bool `mapstructure:"skip_db_create"`
// FailIfSrcMissing allows you to fail the command if the src database does not
// exist. The default behaviour is false, which means do nothing and exit 0
FailIfSrcMissing bool `mapstructure:"fail_if_src_missing"`
}
// ClientOpts are the pg-sidecar client specific options
type ClientOpts struct {
ConTimeout int `mapstructure:"con_timeout"`
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
User string `mapstructure:"user"`
TLSCertPath string `mapstructure:"tls_cert_path"`
TLSKeyPath string `mapstructure:"tls_key_path"`
TLSRootCAPath string `mapstructure:"tls_root_ca_path"`
}
var opts = HelperOpts{
Client: ClientOpts{},
Migrate: MigrateTablesOpts{},
AlterRole: &pgs.AlterRoleReq_Options{
Password: &pgs.AlterRoleReq_Password{},
},
DropTables: &pgs.DropTablesReq{
Tables: []string{},
},
}
func main() {
cobra.OnInitialize(func() {
// If we have been configured to use a config file we'll want to load
// it and use the values. Go's default values make this tricky so we'll
// have to assume that if you provided a config file then what's in it
// is the preferred source of configuration.
if opts.CfgFile != "" {
viper.SetConfigFile(opts.CfgFile)
log := logrus.WithField("file", opts.CfgFile)
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err != nil {
log.WithError(err).Warn("Failed to load configuration file")
} else {
log.Info("Loading configuration file overrides")
}
if err := viper.Unmarshal(&opts); err != nil {
log.WithError(err).Error("Failed to merge configuration from from")
}
} else {
config, err := platform_config.ConfigFromEnvironment()
if err != nil {
if err == platform_config.ErrNoPlatformEnvironment {
return
}
logrus.WithError(err).Fatal("Failed to load platform configuration")
}
opts.Client.Host = config.GetPgSidecar().GetIp()
opts.Client.Port = int(config.GetPgSidecar().GetCfg().GetPort())
opts.Client.User, err = config.PGServiceUser()
if err != nil {
logrus.WithError(err).Fatal("Failed to load the pg user from the platform configuration")
}
opts.Client.TLSCertPath = config.GetService().GetTls().GetCertPath()
opts.Client.TLSKeyPath = config.GetService().GetTls().GetKeyPath()
opts.Client.TLSRootCAPath = config.GetService().GetTls().GetRootCaPath()
}
})
err := newCmd().Execute()
if err != nil {
if eerr, ok := err.(ExitError); ok {
fmt.Println(eerr.Error())
os.Exit(eerr.ExitCode())
}
logrus.Fatal(err)
}
}
func newCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "pg-helper COMMAND",
Short: "Automate Postgresql Helper",
Long: "A tool to simplify postgresql operations in Habitat hooks",
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if opts.Debug {
logrus.SetLevel(logrus.DebugLevel)
} else {
logrus.SetLevel(logrus.WarnLevel)
}
// Disable HTTP proxying for the host we are trying to connect
// to. Currently we assume that any user-provided proxies will
// only be used for external services.
err := os.Setenv("NO_PROXY", opts.Client.Host)
if err != nil {
return errors.Wrap(err, "could not override NO_PROXY environment variable")
}
return nil
},
}
cmd.PersistentFlags().BoolVarP(
&opts.Debug,
"debug",
"d",
false,
"Enabled debug output")
cmd.PersistentFlags().StringVarP(
&opts.CfgFile,
"config-file",
"",
"",
"The path to config file")
cmd.PersistentFlags().StringVarP(
&opts.Client.Host,
"host",
"",
"localhost",
"The hostname of the pg-sidecar-service")
cmd.PersistentFlags().IntVarP(
&opts.Client.Port,
"port",
"p",
10100,
"The listen port of the pg-sidecar-service")
cmd.PersistentFlags().StringVarP(
&opts.Client.TLSCertPath,
"tls-cert",
"c",
"",
"The mutual TLS certificate for pg-sidecar-server auth")
cmd.PersistentFlags().StringVarP(
&opts.Client.TLSKeyPath,
"tls-key",
"k",
"",
"The mutual TLS key for pg-sidecar-server auth")
cmd.PersistentFlags().StringVarP(
&opts.Client.TLSRootCAPath,
"tls-root-ca",
"r",
"",
"The mutual TLS root CA cert for pg-sidecar-server auth")
cmd.PersistentFlags().IntVarP(
&opts.Client.ConTimeout,
"timeout",
"t",
1800,
"The timeout in seconds")
migrateCmd := &cobra.Command{
Use: "migrate-tables SRC_DB_NAME DST_DB_NAME OWNER_ROLE_NAME [TABLE...]",
Short: "Migrate the table(s) from one database to another one. If no table are supplied all tables will be migrated",
RunE: func(_ *cobra.Command, args []string) error {
return migrateTables(args[0], args[1], args[2], args[3:])
},
Args: cobra.MinimumNArgs(3),
}
migrateCmdV2 := &cobra.Command{
Use: "migrate-tables-v2 SRC_DB_NAME DST_DB_NAME [TABLE...]",
Short: "Migrate the table(s) from one database to another one. If no table are supplied all tables will be migrated",
RunE: func(_ *cobra.Command, args []string) error {
if opts.Client.User == "" {
return ErrUserRequired
}
return migrateTables(args[0], args[1], opts.Client.User, args[2:])
},
Args: cobra.MinimumNArgs(2),
}
for _, c := range []*cobra.Command{migrateCmd, migrateCmdV2} {
c.Flags().BoolVarP(
&opts.Migrate.SkipDBCreate,
"skip-db-creation", "n", false,
"Skip the creation of the destination database",
)
c.Flags().BoolVar(
&opts.Migrate.FailIfSrcMissing,
"fail-if-src-missing", false,
"Fail if the source database does not exist",
)
}
renameCmd := &cobra.Command{
Use: "rename-if-exists OLD_DB_NAME NEW_DB_NAME",
Short: "Rename OLD_DB_NAME to NEW_DB_NAME if it exists",
RunE: renameDBIfExists,
Args: cobra.ExactArgs(2),
}
createCmd := &cobra.Command{
Use: "ensure-service-database DATABASE_NAME [USER_NAME]",
Short: "Creates a standard A2 service database and user.",
RunE: createDB,
Args: cobra.RangeArgs(1, 2),
}
createExtensionCmd := &cobra.Command{
Use: "create-extension DBNAME EXTENSION",
Short: "Run CREATE EXTENSION as the superuser.",
RunE: createExtension,
Args: cobra.ExactArgs(2),
}
sqitchDeployCmd := &cobra.Command{
Use: "sqitch-deploy DBNAME SQITCH_DIR",
Short: "Run sqitch deploy for the given DBNAME using the migration in SQITCH_DIR",
RunE: sqitchDeploy,
Args: cobra.ExactArgs(2),
}
fixPermsCmd := &cobra.Command{
Use: "fix-permissions DBNAME [ROLENAME]",
Short: "Change owner of all tables in the public and sqitch schemas to the given role name",
RunE: fixPermissions,
Args: cobra.RangeArgs(1, 2),
}
alterRoleCmd := &cobra.Command{
Use: "alter-role ROLENAME",
Short: "Alter the role",
RunE: alterRole,
Args: cobra.ExactArgs(1),
}
alterRoleCmd.Flags().BoolVarP(
&opts.AlterRole.Superuser,
"superuser", "", false,
"Add SUPERUSER",
)
alterRoleCmd.Flags().StringVarP(
&opts.AlterRole.Password.Value,
"password", "", "",
"Add a PASSWORD",
)
alterRoleCmd.Flags().BoolVarP(
&opts.AlterRole.Password.Unencrypted,
"unencrypted", "", false,
"Set password UNENCRYPTED",
)
dropTablesCmd := &cobra.Command{
Use: "drop-tables DBNAME [TABLE...]",
Short: "Alter the role",
RunE: dropTables,
Args: cobra.MinimumNArgs(2),
}
dropTablesCmd.Flags().BoolVarP(
&opts.DropTables.Cascade,
"cascade", "", false,
"Drop objects that depend on the tables",
)
cmd.AddCommand(migrateCmd)
cmd.AddCommand(migrateCmdV2)
cmd.AddCommand(renameCmd)
cmd.AddCommand(createCmd)
cmd.AddCommand(createExtensionCmd)
cmd.AddCommand(sqitchDeployCmd)
cmd.AddCommand(fixPermsCmd)
cmd.AddCommand(alterRoleCmd)
cmd.AddCommand(dropTablesCmd)
return cmd
}
func pgsClient() (*client.Client, context.Context, func(), error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(opts.Client.ConTimeout)*time.Second)
cl, err := client.NewClient(
client.WithHost(opts.Client.Host),
client.WithPort(opts.Client.Port),
client.WithTLSCertPath(opts.Client.TLSCertPath),
client.WithTLSKeyPath(opts.Client.TLSKeyPath),
client.WithTLSRootCAPath(opts.Client.TLSRootCAPath),
)
return cl, ctx, cancel, err
}
func renameDBIfExists(_ *cobra.Command, args []string) error {
pgsClient, ctx, cancel, err := pgsClient()
defer cancel()
if err != nil {
return err
}
_, err = pgsClient.RenameDB(ctx, &pgs.RenameDBReq{
FromDb: args[0],
ToDb: args[1],
})
return err
}
// createDB sets up a "A2 standard" service database and user. It also
// accounts for possible previous mistakes such as a database user
// with a password or database tables not owned by the user.
func createDB(_ *cobra.Command, args []string) error {
pgsClient, ctx, cancel, err := pgsClient()
defer cancel()
if err != nil {
return err
}
user := ""
if len(args) == 2 {
user = args[1]
} else {
user = opts.Client.User
}
if user == "" {
return ErrUserRequired
}
_, err = pgsClient.CreateDB(ctx, &pgs.CreateDBReq{
Db: args[0],
User: user,
})
return err
}
func migrateTables(fromDB string, toDB string, importUser string, tables []string) error {
pgsClient, ctx, cancel, err := pgsClient()
defer cancel()
if err != nil {
return err
}
_, err = pgsClient.MigrateTables(ctx, &pgs.MigrateTablesReq{
FromDb: fromDB,
ToDb: toDB,
ImportUser: importUser,
Table: tables,
FailIfSrcDbMissing: opts.Migrate.FailIfSrcMissing,
SkipDbCreate: opts.Migrate.SkipDBCreate,
})
if err != nil {
st := status.Convert(err)
for _, detail := range st.Details() {
switch t := detail.(type) {
// If we encountered a SrcDbMissing error then we need to exit
// with error code 10 instead of 1 so that clients can act
// accordingly.
case *pgs.ErrorDetails:
if t.Code == pgs.ErrorDetails_SRC_DB_MISSING {
return NewExitError(10, st.Message())
}
}
}
}
return err
}
func fixPermissions(_ *cobra.Command, args []string) error {
pgsClient, ctx, cancel, err := pgsClient()
defer cancel()
if err != nil {
return err
}
user := ""
if len(args) == 2 {
user = args[1]
} else {
user = opts.Client.User
}
_, err = pgsClient.SetPublicSchemaRole(ctx, &pgs.SetPublicSchemaRoleReq{
Db: args[0],
Role: user,
})
return err
}
func createExtension(_ *cobra.Command, args []string) error {
pgsClient, ctx, cancel, err := pgsClient()
defer cancel()
if err != nil {
return err
}
_, err = pgsClient.CreateExtension(ctx, &pgs.CreateExtensionReq{
Db: args[0],
Ext: args[1],
})
return err
}
func sqitchDeploy(_ *cobra.Command, args []string) error {
pgsClient, ctx, cancel, err := pgsClient()
defer cancel()
if err != nil {
return err
}
_, err = pgsClient.DeploySqitch(ctx, &pgs.DeploySqitchReq{
Db: args[0],
Dir: args[1],
User: "automate",
})
return err
}
func alterRole(_ *cobra.Command, args []string) error {
pgsClient, ctx, cancel, err := pgsClient()
defer cancel()
if err != nil {
return err
}
_, err = pgsClient.AlterRole(ctx, &pgs.AlterRoleReq{
Role: args[0],
With: opts.AlterRole,
})
return err
}
func dropTables(_ *cobra.Command, args []string) error {
pgsClient, ctx, cancel, err := pgsClient()
defer cancel()
if err != nil {
return err
}
opts.DropTables.Db = args[0]
opts.DropTables.Tables = args[1:]
_, err = pgsClient.DropTables(ctx, opts.DropTables)
return err
}