Silvan 4272ea6fe1
fix: init sub commands (#3218)
* fix(init): add sub commands

* fix(init): admin user in config,
test(init): verify functions

* refactor: config, remove second commands

* refactor: init steps

* chore: fix link in readme

* chore: numerate sql files

* Update cmd/admin/initialise/sql/README.md

Co-authored-by: Livio Amstutz <livio.a@gmail.com>

* Update cmd/admin/initialise/sql/README.md

Co-authored-by: Livio Amstutz <livio.a@gmail.com>

* fix(init): remove unused index

* user

* fix database username in defaults.yaml

Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2022-02-16 12:30:49 +00:00

67 lines
1.4 KiB
Go

package initialise
import (
"database/sql"
_ "embed"
"github.com/caos/logging"
"github.com/caos/zitadel/internal/database"
"github.com/spf13/cobra"
"github.com/spf13/viper"
//sql import
_ "github.com/lib/pq"
)
func New() *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Short: "initialize ZITADEL instance",
Long: `Sets up the minimum requirements to start ZITADEL.
Prereqesits:
- cockroachdb
The user provided by flags needs priviledge to
- create the database if it does not exist
- see other users and create a new one if the user does not exist
- grant all rights of the ZITADEL database to the user created if not yet set
`,
RunE: func(cmd *cobra.Command, args []string) error {
config := Config{}
if err := viper.Unmarshal(&config); err != nil {
return err
}
if err := initialise(config,
verifyUser(config.Database),
verifyDatabase(config.Database),
verifyGrant(config.Database),
); err != nil {
return err
}
return verifyZitadel(config.Database)
},
}
cmd.AddCommand(newZitadel(), newDatabase(), newUser(), newGrant())
return cmd
}
func initialise(config Config, steps ...func(*sql.DB) error) error {
logging.Info("initialization started")
db, err := database.Connect(adminConfig(config))
if err != nil {
return err
}
for _, step := range steps {
if err = step(db); err != nil {
return err
}
}
return db.Close()
}