2019-11-02 21:32:58 +07:00
|
|
|
package suites
|
|
|
|
|
|
|
|
import (
|
2019-11-16 17:38:21 +07:00
|
|
|
"fmt"
|
2019-11-02 21:32:58 +07:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
|
2019-11-17 17:47:07 +07:00
|
|
|
"github.com/clems4ever/authelia/internal/utils"
|
2019-11-02 21:32:58 +07:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DockerEnvironment represent a docker environment
|
|
|
|
type DockerEnvironment struct {
|
|
|
|
dockerComposeFiles []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDockerEnvironment create a new docker environment
|
|
|
|
func NewDockerEnvironment(files []string) *DockerEnvironment {
|
|
|
|
return &DockerEnvironment{dockerComposeFiles: files}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (de *DockerEnvironment) createCommandWithStdout(cmd string) *exec.Cmd {
|
2019-11-25 03:27:59 +07:00
|
|
|
dockerCmdLine := fmt.Sprintf("docker-compose -f %s %s", strings.Join(de.dockerComposeFiles, " -f "), cmd)
|
2019-11-02 21:32:58 +07:00
|
|
|
log.Trace(dockerCmdLine)
|
|
|
|
return utils.CommandWithStdout("bash", "-c", dockerCmdLine)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (de *DockerEnvironment) createCommand(cmd string) *exec.Cmd {
|
2019-11-25 03:27:59 +07:00
|
|
|
dockerCmdLine := fmt.Sprintf("docker-compose -f %s %s", strings.Join(de.dockerComposeFiles, " -f "), cmd)
|
2019-11-02 21:32:58 +07:00
|
|
|
log.Trace(dockerCmdLine)
|
2019-11-25 03:27:59 +07:00
|
|
|
return utils.Command("bash", "-c", dockerCmdLine)
|
2019-11-02 21:32:58 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Up spawn a docker environment
|
2019-11-25 03:27:59 +07:00
|
|
|
func (de *DockerEnvironment) Up() error {
|
|
|
|
return de.createCommandWithStdout("up -d").Run()
|
2019-11-02 21:32:58 +07:00
|
|
|
}
|
|
|
|
|
2019-11-16 17:38:21 +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
|
|
|
}
|
|
|
|
|
2019-11-02 21:32:58 +07:00
|
|
|
// Down spawn 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
|
|
|
}
|
|
|
|
|
|
|
|
// Logs get logs of a given service of the environment
|
|
|
|
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()
|
|
|
|
return string(content), err
|
|
|
|
}
|