2019-11-02 14:32:58 +00:00
|
|
|
package suites
|
|
|
|
|
|
|
|
import (
|
2019-11-16 10:38:21 +00:00
|
|
|
"fmt"
|
2020-01-17 19:46:51 +00:00
|
|
|
"os"
|
2019-11-02 14:32:58 +00:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2020-04-05 12:37:21 +00:00
|
|
|
|
|
|
|
"github.com/authelia/authelia/internal/utils"
|
2019-11-02 14:32:58 +00:00
|
|
|
)
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// DockerEnvironment represent a docker environment.
|
2019-11-02 14:32:58 +00:00
|
|
|
type DockerEnvironment struct {
|
|
|
|
dockerComposeFiles []string
|
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// NewDockerEnvironment create a new docker environment.
|
2019-11-02 14:32:58 +00:00
|
|
|
func NewDockerEnvironment(files []string) *DockerEnvironment {
|
2020-05-02 16:20:40 +00:00
|
|
|
if os.Getenv("CI") == stringTrue {
|
2020-01-17 19:46:51 +00:00
|
|
|
for i := range files {
|
|
|
|
files[i] = strings.ReplaceAll(files[i], "{}", "dist")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i := range files {
|
|
|
|
files[i] = strings.ReplaceAll(files[i], "{}", "dev")
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
return &DockerEnvironment{dockerComposeFiles: files}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (de *DockerEnvironment) createCommandWithStdout(cmd string) *exec.Cmd {
|
2020-02-09 17:04:28 +00:00
|
|
|
dockerCmdLine := fmt.Sprintf("docker-compose -p authelia -f %s %s", strings.Join(de.dockerComposeFiles, " -f "), cmd)
|
2019-11-02 14:32:58 +00:00
|
|
|
log.Trace(dockerCmdLine)
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
return utils.CommandWithStdout("bash", "-c", dockerCmdLine)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (de *DockerEnvironment) createCommand(cmd string) *exec.Cmd {
|
2020-02-09 17:04:28 +00:00
|
|
|
dockerCmdLine := fmt.Sprintf("docker-compose -p authelia -f %s %s", strings.Join(de.dockerComposeFiles, " -f "), cmd)
|
2019-11-02 14:32:58 +00:00
|
|
|
log.Trace(dockerCmdLine)
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-24 20:27:59 +00:00
|
|
|
return utils.Command("bash", "-c", dockerCmdLine)
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// Up spawn a docker environment.
|
2019-11-24 20:27:59 +00:00
|
|
|
func (de *DockerEnvironment) Up() error {
|
2019-11-30 16:49:52 +00:00
|
|
|
return de.createCommandWithStdout("up --build -d").Run()
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// Restart restarts a service.
|
2019-11-24 20:27:59 +00:00
|
|
|
func (de *DockerEnvironment) Restart(service string) error {
|
|
|
|
return de.createCommandWithStdout(fmt.Sprintf("restart %s", service)).Run()
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// Down spawn a docker environment.
|
2019-11-24 20:27:59 +00:00
|
|
|
func (de *DockerEnvironment) Down() error {
|
|
|
|
return de.createCommandWithStdout("down -v").Run()
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// Logs get logs of a given service of the environment.
|
2019-11-02 14:32:58 +00:00
|
|
|
func (de *DockerEnvironment) Logs(service string, flags []string) (string, error) {
|
2019-11-24 20:27:59 +00:00
|
|
|
cmd := de.createCommand(fmt.Sprintf("logs %s %s", strings.Join(flags, " "), service))
|
2019-11-02 14:32:58 +00:00
|
|
|
content, err := cmd.Output()
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
return string(content), err
|
|
|
|
}
|