2022-02-09 14:01:19 +00:00
|
|
|
package setup
|
|
|
|
|
|
|
|
import (
|
2022-03-23 08:02:39 +00:00
|
|
|
"context"
|
2022-02-09 14:01:19 +00:00
|
|
|
_ "embed"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2022-03-23 08:02:39 +00:00
|
|
|
"github.com/spf13/viper"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/logging"
|
2022-03-23 08:02:39 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/cmd/admin/key"
|
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/migration"
|
2022-03-23 08:02:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
//go:embed steps.yaml
|
|
|
|
defaultSteps []byte
|
2022-04-25 15:05:20 +00:00
|
|
|
stepFiles []string
|
2022-02-09 14:01:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func New() *cobra.Command {
|
2022-04-25 15:05:20 +00:00
|
|
|
cmd := &cobra.Command{
|
2022-02-09 14:01:19 +00:00
|
|
|
Use: "setup",
|
|
|
|
Short: "setup ZITADEL instance",
|
|
|
|
Long: `sets up data to start ZITADEL.
|
|
|
|
Requirements:
|
|
|
|
- cockroachdb`,
|
2022-03-23 08:02:39 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2022-03-28 08:05:09 +00:00
|
|
|
config := MustNewConfig(viper.GetViper())
|
|
|
|
steps := MustNewSteps(viper.New())
|
2022-03-23 08:02:39 +00:00
|
|
|
|
2022-04-12 14:20:17 +00:00
|
|
|
masterKey, err := key.MasterKey(cmd)
|
|
|
|
logging.OnError(err).Panic("No master key provided")
|
|
|
|
|
|
|
|
Setup(config, steps, masterKey)
|
2022-02-09 14:01:19 +00:00
|
|
|
},
|
|
|
|
}
|
2022-04-25 15:05:20 +00:00
|
|
|
|
|
|
|
Flags(cmd)
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func Flags(cmd *cobra.Command) {
|
|
|
|
cmd.PersistentFlags().StringArrayVar(&stepFiles, "steps", nil, "paths to step files to overwrite default steps")
|
|
|
|
key.AddMasterKeyFlag(cmd)
|
2022-02-09 14:01:19 +00:00
|
|
|
}
|
2022-03-23 08:02:39 +00:00
|
|
|
|
2022-04-12 14:20:17 +00:00
|
|
|
func Setup(config *Config, steps *Steps, masterKey string) {
|
2022-03-23 08:02:39 +00:00
|
|
|
dbClient, err := database.Connect(config.Database)
|
|
|
|
logging.OnError(err).Fatal("unable to connect to database")
|
|
|
|
|
|
|
|
eventstoreClient, err := eventstore.Start(dbClient)
|
|
|
|
logging.OnError(err).Fatal("unable to start eventstore")
|
2022-03-28 08:05:09 +00:00
|
|
|
migration.RegisterMappers(eventstoreClient)
|
|
|
|
|
2022-04-06 06:13:40 +00:00
|
|
|
steps.s1ProjectionTable = &ProjectionTable{dbClient: dbClient}
|
|
|
|
steps.s2AssetsTable = &AssetTable{dbClient: dbClient}
|
2022-04-13 05:42:48 +00:00
|
|
|
|
2022-04-28 08:30:41 +00:00
|
|
|
steps.S3DefaultInstance.instanceSetup = config.DefaultInstance
|
|
|
|
steps.S3DefaultInstance.userEncryptionKey = config.EncryptionKeys.User
|
|
|
|
steps.S3DefaultInstance.masterKey = masterKey
|
2022-04-12 14:20:17 +00:00
|
|
|
steps.S3DefaultInstance.db = dbClient
|
2022-04-28 08:30:41 +00:00
|
|
|
steps.S3DefaultInstance.es = eventstoreClient
|
2022-04-12 14:20:17 +00:00
|
|
|
steps.S3DefaultInstance.defaults = config.SystemDefaults
|
|
|
|
steps.S3DefaultInstance.zitadelRoles = config.InternalAuthZ.RolePermissionMappings
|
2022-04-28 08:30:41 +00:00
|
|
|
steps.S3DefaultInstance.externalDomain = config.ExternalDomain
|
2022-04-21 10:37:39 +00:00
|
|
|
steps.S3DefaultInstance.externalSecure = config.ExternalSecure
|
2022-04-25 09:16:36 +00:00
|
|
|
steps.S3DefaultInstance.externalPort = config.ExternalPort
|
2022-03-23 08:02:39 +00:00
|
|
|
|
2022-03-28 08:05:09 +00:00
|
|
|
ctx := context.Background()
|
2022-04-21 10:37:39 +00:00
|
|
|
err = migration.Migrate(ctx, eventstoreClient, steps.s1ProjectionTable)
|
|
|
|
logging.OnError(err).Fatal("unable to migrate step 1")
|
|
|
|
err = migration.Migrate(ctx, eventstoreClient, steps.s2AssetsTable)
|
2022-04-28 08:30:41 +00:00
|
|
|
logging.OnError(err).Fatal("unable to migrate step 2")
|
2022-04-21 10:37:39 +00:00
|
|
|
err = migration.Migrate(ctx, eventstoreClient, steps.S3DefaultInstance)
|
2022-04-28 08:30:41 +00:00
|
|
|
logging.OnError(err).Fatal("unable to migrate step 3")
|
2022-03-23 08:02:39 +00:00
|
|
|
}
|
2022-04-25 15:05:20 +00:00
|
|
|
|
|
|
|
func initSteps(v *viper.Viper, files ...string) func() {
|
|
|
|
return func() {
|
|
|
|
for _, file := range files {
|
|
|
|
v.SetConfigFile(file)
|
|
|
|
err := v.MergeInConfig()
|
|
|
|
logging.WithFields("file", file).OnError(err).Warn("unable to read setup file")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|