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