/
bootstrap.go
64 lines (50 loc) · 1.86 KB
/
bootstrap.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
package bootstrap
import (
"context"
"fmt"
"strings"
"github.com/go-gorp/gorp"
"github.com/ovh/cds/engine/api/action"
"github.com/ovh/cds/engine/api/group"
"github.com/ovh/cds/engine/api/integration"
"github.com/ovh/cds/engine/api/workflow"
"github.com/ovh/cds/sdk"
)
// InitiliazeDB inits the database
func InitiliazeDB(ctx context.Context, defaultValues sdk.DefaultValues, DBFunc func() *gorp.DbMap) error {
tx, err := DBFunc().Begin()
if err != nil {
return sdk.WithStack(err)
}
defer tx.Rollback() // nolint
if err := group.CreateDefaultGroup(tx, sdk.SharedInfraGroupName); err != nil {
return sdk.WrapError(err, "Cannot setup default %s group", sdk.SharedInfraGroupName)
}
if strings.TrimSpace(defaultValues.DefaultGroupName) != "" {
if err := group.CreateDefaultGroup(tx, defaultValues.DefaultGroupName); err != nil {
return sdk.WrapError(err, "Cannot setup default %s group", defaultValues.DefaultGroupName)
}
}
if err := group.InitializeDefaultGroupName(tx, defaultValues.DefaultGroupName); err != nil {
return sdk.WrapError(err, "Cannot InitializeDefaultGroupName")
}
if err := action.CreateBuiltinActions(tx); err != nil {
return sdk.WrapError(err, "Cannot setup builtin actions")
}
if err := action.CreateAsCodeAction(ctx, tx); err != nil {
return sdk.WrapError(err, "Cannot setup ascode action")
}
if err := tx.Commit(); err != nil {
return sdk.WithStack(err)
}
if err := workflow.CreateBuiltinWorkflowHookModels(DBFunc()); err != nil {
return fmt.Errorf("cannot setup builtin workflow hook models: %v", err)
}
if err := workflow.CreateBuiltinWorkflowOutgoingHookModels(DBFunc()); err != nil {
return fmt.Errorf("cannot setup builtin workflow outgoing hook models: %v", err)
}
if err := integration.CreateBuiltinModels(ctx, DBFunc()); err != nil {
return fmt.Errorf("cannot setup integrations: %v", err)
}
return nil
}