internal/stack: Cleanup API

This commit cleans up the internal stack API to prepare it for an
eventual move to using a native docker client.
This commit is contained in:
Tom Wiesing 2022-09-02 17:52:06 +02:00
parent 7b38fdd801
commit 5d906169f4
No known key found for this signature in database
15 changed files with 96 additions and 65 deletions

17
env/instances.go vendored
View file

@ -179,7 +179,7 @@ func (instance *Instance) Delete() error {
}
// Shell executes a shell command inside the
func (instance Instance) Shell(io stream.IOStream, argv ...string) int {
func (instance Instance) Shell(io stream.IOStream, argv ...string) (int, error) {
return instance.Stack().Exec(io, "barrel", "/user_shell.sh", argv...)
}
@ -219,8 +219,7 @@ func (instance Instance) URL() *url.URL {
func (instance Instance) Stack() stack.Installable {
return stack.Installable{
Stack: stack.Stack{
Name: "barrel",
Dir: instance.FilesystemBase,
Dir: instance.FilesystemBase,
},
ContextResource: filepath.Join("resources", "compose", "barrel"),
@ -252,8 +251,7 @@ func (instance Instance) Stack() stack.Installable {
func (instance Instance) ReserveStack() stack.Installable {
return stack.Installable{
Stack: stack.Stack{
Name: "reserve",
Dir: instance.FilesystemBase,
Dir: instance.FilesystemBase,
},
ContextResource: filepath.Join("resources", "compose", "reserve"),
@ -308,7 +306,11 @@ func (instance Instance) Provision(io stream.IOStream) error {
// TODO: Move the provision script into the control plane!
provisionScript := "sudo PATH=$PATH -u www-data /bin/bash /provision_container.sh " + strings.Join(provisionParams, " ")
if st.Run(io, true, "barrel", "/bin/bash", "-c", provisionScript) != 0 {
code, err := st.Run(io, true, "barrel", "/bin/bash", "-c", provisionScript)
if err != nil {
return err
}
if code != 0 {
return errors.New("Unable to run provision script")
}
@ -336,7 +338,8 @@ func (instance *Instance) PrefixConfig() (config string, err error) {
// default prefixes
wu := stream.NewIOStream(&builder, nil, nil, 0)
if instance.Stack().Exec(wu, "barrel", "/bin/bash", "/user_shell.sh", "-c", "drush php:script /wisskiutils/list_uri_prefixes.php") != 0 {
code, err := instance.Stack().Exec(wu, "barrel", "/bin/bash", "/user_shell.sh", "-c", "drush php:script /wisskiutils/list_uri_prefixes.php")
if err != nil || code != 0 {
return "", errPrefixExecFailed
}

8
env/stack.go vendored
View file

@ -20,11 +20,11 @@ func (dis *Distillery) Stacks() []stack.Installable {
}
// asCoreStack treats the provided stack as a core component of this distillery.
func (dis *Distillery) asCoreStack(stack stack.Installable) stack.Installable {
stack.Dir = filepath.Join(dis.Config.DeployRoot, "core", stack.Name)
func (dis *Distillery) asCoreStack(name string, stack stack.Installable) stack.Installable {
stack.Dir = filepath.Join(dis.Config.DeployRoot, "core", name)
stack.ContextResource = filepath.Join("resources", "compose", stack.Name)
stack.EnvFileResource = filepath.Join("resources", "templates", "docker-env", stack.Name)
stack.ContextResource = filepath.Join("resources", "compose", name)
stack.EnvFileResource = filepath.Join("resources", "templates", "docker-env", name)
return stack
}

View file

@ -10,11 +10,7 @@ import (
const ResolverPrefixFile = "prefix.cfg"
func (dis *Distillery) ResolverStack() stack.Installable {
stack := dis.asCoreStack(stack.Installable{
Stack: stack.Stack{
Name: "resolver",
},
stack := dis.asCoreStack("resolver", stack.Installable{
EnvFileContext: map[string]string{
"VIRTUAL_HOST": dis.DefaultVirtualHost(),
"LETSENCRYPT_HOST": dis.DefaultLetsencryptHost(),

6
env/stack_self.go vendored
View file

@ -8,11 +8,7 @@ func (dis *Distillery) SelfStack() stack.Installable {
TARGET = dis.Config.SelfRedirect.String()
}
return dis.asCoreStack(stack.Installable{
Stack: stack.Stack{
Name: "self",
},
return dis.asCoreStack("self", stack.Installable{
EnvFileContext: map[string]string{
"VIRTUAL_HOST": dis.DefaultVirtualHost(),
"LETSENCRYPT_HOST": dis.DefaultLetsencryptHost(),

14
env/stack_sql.go vendored
View file

@ -20,11 +20,7 @@ import (
// SQLStack returns the docker stack that handles the sql database.
func (dis *Distillery) SQLStack() stack.Installable {
return dis.asCoreStack(stack.Installable{
Stack: stack.Stack{
Name: "sql",
},
return dis.asCoreStack("sql", stack.Installable{
MakeDirsPerm: fs.ModeDir | fs.ModePerm,
MakeDirs: []string{
"data",
@ -87,7 +83,7 @@ func (dis *Distillery) sqlBkTable(silent bool) (*gorm.DB, error) {
}
// SQLShell executes a mysql shell inside the SQLStack.
func (dis *Distillery) SQLShell(io stream.IOStream, argv ...string) int {
func (dis *Distillery) SQLShell(io stream.IOStream, argv ...string) (int, error) {
return dis.SQLStack().Exec(io, "sql", "mysql", argv...)
}
@ -102,7 +98,8 @@ const waitSQLInterval = 1 * time.Second
func (dis *Distillery) SQLWaitForShell() error {
n := stream.FromNil()
return wait.Wait(func() bool {
return dis.SQLShell(n, "-e", "show databases;") == 0
code, err := dis.SQLShell(n, "-e", "show databases;")
return err == nil && code == 0
}, waitSQLInterval, dis.Context())
}
@ -118,7 +115,8 @@ var errInvalidDatabaseName = errors.New("SQLProvision: Invalid database name")
func (dis *Distillery) sqlRaw(query string, args ...interface{}) bool {
sql := sqle.Format(query, args...)
return dis.SQLShell(stream.FromNil(), "-e", sql) == 0
code, err := dis.SQLShell(stream.FromNil(), "-e", sql)
return err == nil && code == 0
}
// SQLProvision provisions a new sql database and user

6
env/stack_ssh.go vendored
View file

@ -4,11 +4,7 @@ import "github.com/FAU-CDI/wisski-distillery/internal/stack"
func (dis *Distillery) SSHStack() stack.Installable {
// TODO: Ensure that .env is copied if needed
return dis.asCoreStack(stack.Installable{
Stack: stack.Stack{
Name: "sql",
},
})
return dis.asCoreStack("ssh", stack.Installable{})
}
func (dis *Distillery) SSHStackPath() string {

View file

@ -20,11 +20,7 @@ import (
)
func (dis *Distillery) TriplestoreStack() stack.Installable {
return dis.asCoreStack(stack.Installable{
Stack: stack.Stack{
Name: "triplestore",
},
return dis.asCoreStack("triplestore", stack.Installable{
CopyContextFiles: []string{"graphdb.zip"},
MakeDirsPerm: fs.ModeDir | fs.ModePerm,

6
env/stack_web.go vendored
View file

@ -3,11 +3,7 @@ package env
import "github.com/FAU-CDI/wisski-distillery/internal/stack"
func (dis *Distillery) WebStack() stack.Installable {
return dis.asCoreStack(stack.Installable{
Stack: stack.Stack{
Name: "web",
},
return dis.asCoreStack("web", stack.Installable{
EnvFileContext: map[string]string{
"DEFAULT_HOST": dis.Config.DefaultDomain,
},