Skip to content

Commit

Permalink
provision alternative sql for mattermost db
Browse files Browse the repository at this point in the history
  • Loading branch information
fmartingr committed Apr 24, 2024
1 parent 9ed759f commit 5012e4e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
9 changes: 7 additions & 2 deletions deployment/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
40 changes: 35 additions & 5 deletions deployment/terraform/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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))
Expand Down

0 comments on commit 5012e4e

Please sign in to comment.