authelia/internal/suites/docker.go
James Elliott e041143f87
feat(session): add redis sentinel provider (#1768)
* feat(session): add redis sentinel provider

* refactor(session): use int for ports as per go standards

* refactor(configuration): adjust tests and validation

* refactor(configuration): add err format consts

* refactor(configuration): explicitly map redis structs

* refactor(session): merge redis/redis sentinel providers

* refactor(session): add additional checks to redis providers

* feat(session): add redis cluster provider

* fix: update config for new values

* fix: provide nil certpool to affected tests/mocks

* test: add additional tests to cover uncovered code

* docs: expand explanation of host and nodes relation for redis

* ci: add redis-sentinel to suite highavailability, add redis-sentinel quorum

* fix(session): sentinel password

* test: use redis alpine library image for redis sentinel, use expose instead of ports, use redis ip, adjust redis ip range, adjust redis config

* test: make entrypoint.sh executable, fix entrypoint.sh if/elif

* test: add redis failover tests

* test: defer docker start, adjust sleep, attempt logout before login, attempt visit before login and tune timeouts, add additional logging

* test: add sentinel integration test

* test: add secondary node failure to tests, fix password usage, bump test timeout, add sleep

* feat: use sentinel failover cluster

* fix: renamed addrs to sentineladdrs upstream

* test(session): sentinel failover

* test: add redis standard back into testing

* test: move redis standalone test to traefik2

* fix/docs: apply suggestions from code review
2021-03-10 10:03:05 +11:00

88 lines
2.5 KiB
Go

package suites
import (
"fmt"
"os"
"os/exec"
"strings"
log "github.com/sirupsen/logrus"
"github.com/authelia/authelia/internal/utils"
)
// DockerEnvironment represent a docker environment.
type DockerEnvironment struct {
dockerComposeFiles []string
}
// NewDockerEnvironment create a new docker environment.
func NewDockerEnvironment(files []string) *DockerEnvironment {
if os.Getenv("CI") == stringTrue {
for i := range files {
files[i] = strings.ReplaceAll(files[i], "{}", "dist")
}
} else {
for i := range files {
files[i] = strings.ReplaceAll(files[i], "{}", "dev")
}
}
return &DockerEnvironment{dockerComposeFiles: files}
}
func (de *DockerEnvironment) createCommandWithStdout(cmd string) *exec.Cmd {
dockerCmdLine := fmt.Sprintf("docker-compose -p authelia -f %s %s", strings.Join(de.dockerComposeFiles, " -f "), cmd)
log.Trace(dockerCmdLine)
return utils.CommandWithStdout("bash", "-c", dockerCmdLine)
}
func (de *DockerEnvironment) createCommand(cmd string) *exec.Cmd {
dockerCmdLine := fmt.Sprintf("docker-compose -p authelia -f %s %s", strings.Join(de.dockerComposeFiles, " -f "), cmd)
log.Trace(dockerCmdLine)
return utils.Command("bash", "-c", dockerCmdLine)
}
// Up spawn a docker environment.
func (de *DockerEnvironment) Up() error {
return de.createCommandWithStdout("up --build -d").Run()
}
// Restart restarts a service.
func (de *DockerEnvironment) Restart(service string) error {
return de.createCommandWithStdout(fmt.Sprintf("restart %s", service)).Run()
}
// Stop a docker service.
func (de *DockerEnvironment) Stop(service string) error {
return de.createCommandWithStdout(fmt.Sprintf("stop %s", service)).Run()
}
// Start a docker service.
func (de *DockerEnvironment) Start(service string) error {
return de.createCommandWithStdout(fmt.Sprintf("start %s", service)).Run()
}
// Down destroy a docker environment.
func (de *DockerEnvironment) Down() error {
return de.createCommandWithStdout("down -v").Run()
}
// Exec execute a command within a given service of the environment.
func (de *DockerEnvironment) Exec(service string, command []string) (string, error) {
cmd := de.createCommand(fmt.Sprintf("exec -T %s %s", service, strings.Join(command, " ")))
content, err := cmd.CombinedOutput()
return string(content), err
}
// Logs get logs of a given service of the environment.
func (de *DockerEnvironment) Logs(service string, flags []string) (string, error) {
cmd := de.createCommand(fmt.Sprintf("logs %s %s", strings.Join(flags, " "), service))
content, err := cmd.Output()
return string(content), err
}