From 5012e4ea05000774e2389348163b4f5c10d7792b Mon Sep 17 00:00:00 2001 From: Felipe Martin Date: Wed, 24 Apr 2024 18:37:43 +0200 Subject: [PATCH] provision alternative sql for mattermost db --- deployment/config.go | 9 ++++++-- deployment/terraform/dump.go | 40 +++++++++++++++++++++++++++++++----- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/deployment/config.go b/deployment/config.go index e7f27db16..7389e958e 100644 --- a/deployment/config.go +++ b/deployment/config.go @@ -204,10 +204,15 @@ type ExternalAuthProviderSettings struct { // KeycloakDBDumpURI // An optional URI to a keycloak database dump file to be uploaded on environment // creation. - // The file is expected to be the h2 folder gzip compressed. + // The file is expected to be gzip compressed. // This can also point to a local file if prefixed with "file://". - // In such case, the dump file will be uploaded to the app servers. KeycloakDBDumpURI string `default:""` + // MattermostDBSqlURI + // An optional URI to a SQL file containing the necessary alter statements to be applied + // to the Mattermost database. + // The file is expected to be gzip compressed. + // This can also point to a local file if prefixed with "file://". + MattermostDBSqlURI string `default:""` // GenerateUsersCount is the number of users to generate in the keycloak instance. GenerateUsersCount int `default:"0" validate:"range:[0,)"` // KeycloakRealmName is the name of the realm to be used in Mattermost. Must exist in the keycloak instance. diff --git a/deployment/terraform/dump.go b/deployment/terraform/dump.go index 25b224e0c..c352b0c88 100644 --- a/deployment/terraform/dump.go +++ b/deployment/terraform/dump.go @@ -123,16 +123,17 @@ func (t *Terraform) IngestDump() error { // load dump // start - stopCmd := deployment.Cmd{ + commands := []deployment.Cmd{{ Msg: "Stopping app servers", Value: "sudo systemctl stop mattermost", Clients: appClients, - } + }} resetCmd, err := getResetCmd(t.config, output, appClients) if err != nil { return fmt.Errorf("error building reset cmd: %w", err) } + commands = append(commands, resetCmd) loadDBDumpCmd := deployment.Cmd{ Msg: "Loading DB dump", @@ -150,14 +151,43 @@ func (t *Terraform) IngestDump() error { return fmt.Errorf("error building command for loading DB dump: %w", err) } loadDBDumpCmd.Value = dbCmd + commands = append(commands, loadDBDumpCmd) + + if t.config.ExternalAuthProviderSettings.MattermostDBSqlURI != "" { + keycloakDumpURI := t.config.ExternalAuthProviderSettings.MattermostDBSqlURI + keycloakDumpFilename := filepath.Base(keycloakDumpURI) + mlog.Info("Provisioning keycloak dump file", mlog.String("uri", keycloakDumpURI)) + if err := deployment.ProvisionURL(appClients[0], keycloakDumpURI, keycloakDumpFilename); err != nil { + return err + } - startCmd := deployment.Cmd{ + keycloakSQLCmd := deployment.Cmd{ + Msg: "Executing custom Keycloak SQL", + Clients: []*ssh.Client{appClients[0]}, + } + + keycloakSQLCommand, err := deployment.BuildLoadDBDumpCmd(keycloakDumpFilename, deployment.DBSettings{ + UserName: t.config.TerraformDBSettings.UserName, + Password: t.config.TerraformDBSettings.Password, + DBName: t.config.DBName(), + Host: output.DBWriter(), + Engine: t.config.TerraformDBSettings.InstanceEngine, + }) + if err != nil { + return fmt.Errorf("error building command for altering DB dump: %w", err) + } + keycloakSQLCmd.Value = keycloakSQLCommand + + commands = append(commands, keycloakSQLCmd) + } + + commands = append(commands, deployment.Cmd{ Msg: "Restarting app server", Value: "sudo systemctl start mattermost && until $(curl -sSf http://localhost:8065 --output /dev/null); do sleep 1; done;", Clients: appClients, - } + }) - for _, c := range []deployment.Cmd{stopCmd, resetCmd, loadDBDumpCmd, startCmd} { + for _, c := range commands { mlog.Info(c.Msg) for _, client := range c.Clients { mlog.Debug("Running cmd", mlog.String("cmd", c.Value))