provision: Dynamically find components to provision

This commit is contained in:
Tom Wiesing 2022-09-20 19:45:32 +02:00
parent 33cd18e9d9
commit a5d9b1a386
No known key found for this signature in database
5 changed files with 46 additions and 22 deletions

View file

@ -61,7 +61,7 @@ func (p provision) Run(context wisski_distillery.Context) error {
return errProvisionAlreadyExists.WithMessageF(slug)
}
// Store in bookkeeping
// Store in the instances table!
if err := logging.LogOperation(func() error {
if err := instance.Save(); err != nil {
return errProvisionGeneric.WithMessageF(slug, err)
@ -72,26 +72,20 @@ func (p provision) Run(context wisski_distillery.Context) error {
return err
}
// create the sql
// create all the resources!
if err := logging.LogOperation(func() error {
if err := dis.SQL().Provision(instance.SqlDatabase, instance.SqlUsername, instance.SqlPassword); err != nil {
return errProvisionGeneric.WithMessageF(slug, err)
domain := instance.Domain()
for _, pc := range dis.Provisionable() {
logging.LogMessage(context.IOStream, "Provisioning %s resources", pc.Name())
err := pc.Provision(instance.Instance, domain)
if err != nil {
return err
}
}
return nil
}, context.IOStream, "Provisioning SQL Database"); err != nil {
return err
}
// create the triplestore
if err := logging.LogOperation(func() error {
if err := dis.Triplestore().Provision(instance.GraphDBRepository, instance.Domain(), instance.GraphDBUsername, instance.GraphDBPassword); err != nil {
return errProvisionGeneric.WithMessageF(slug, err)
}
return nil
}, context.IOStream, "Provisioning Triplestore"); err != nil {
return err
}, context.IOStream, "Provisioning instance-specific resources"); err != nil {
return errProvisionGeneric.WithMessageF(slug, err)
}
// run the provision script

View file

@ -0,0 +1,14 @@
package component
import (
"github.com/FAU-CDI/wisski-distillery/internal/models"
)
// Provisionable represents a component with a Provision method.
type Provisionable interface {
Component
// Provision provisions resources specific to the provided instance.
// Domain holds the full (unique) domain name of the given instance.
Provision(instance models.Instance, domain string) error
}

View file

@ -3,17 +3,23 @@ package sql
import (
"errors"
"github.com/FAU-CDI/wisski-distillery/internal/models"
"github.com/FAU-CDI/wisski-distillery/pkg/sqle"
)
var errProvisionInvalidDatabaseParams = errors.New("Provision: Invalid parameters")
var errProvisionInvalidGrant = errors.New("Provision: Grant failed")
// Provision creates a new database with the given name.
// ProvisionInstance provisions sql-specific resource for the given instance
func (sql *SQL) Provision(instance models.Instance, domain string) error {
return sql.CreateDatabase(instance.SqlDatabase, instance.SqlUsername, instance.SqlPassword)
}
// CreateDatabase creates a new database with the given name.
// It then generates a new user, with the name 'user' and the password 'password', that is then granted access to this database.
//
// Provision internally waits for the database to become available.
func (sql *SQL) Provision(name, user, password string) error {
func (sql *SQL) CreateDatabase(name, user, password string) error {
// NOTE(twiesing): We shouldn't use string concat to build sql queries.
// But the driver doesn't support using query params for this particular query.

View file

@ -6,6 +6,7 @@ import (
_ "embed"
"github.com/FAU-CDI/wisski-distillery/internal/models"
"github.com/FAU-CDI/wisski-distillery/pkg/unpack"
"github.com/tkw1536/goprogram/exit"
)
@ -18,7 +19,11 @@ var errTripleStoreFailedRepository = exit.Error{
//go:embed create-repo.ttl
var createRepoTTL []byte
func (ts Triplestore) Provision(name, domain, user, password string) error {
func (ts *Triplestore) Provision(instance models.Instance, domain string) error {
return ts.CreateRepository(instance.GraphDBRepository, domain, instance.GraphDBUsername, instance.GraphDBPassword)
}
func (ts *Triplestore) CreateRepository(name, domain, user, password string) error {
if err := ts.Wait(); err != nil {
return err
}

View file

@ -67,7 +67,7 @@ func makeComponent[C component.Component](dis *Distillery, field *lazy.Lazy[C],
})
}
func (dis *Distillery) ComponentsX() []component.Component {
func (dis *Distillery) Components() []component.Component {
return []component.Component{
dis.Web(),
dis.Control(),
@ -93,8 +93,13 @@ func (dis *Distillery) Updateable() []component.Updatable {
return getComponents[component.Updatable](dis)
}
// Provisionable returns all components which can be provisioned
func (dis *Distillery) Provisionable() []component.Provisionable {
return getComponents[component.Provisionable](dis)
}
func getComponents[C component.Component](dis *Distillery) (result []C) {
all := dis.ComponentsX()
all := dis.Components()
result = make([]C, 0, len(all))
for _, c := range all {