initial commit
This commit is contained in:
164
app/modules/compose/compose.go
Normal file
164
app/modules/compose/compose.go
Normal file
@ -0,0 +1,164 @@
|
||||
package compose
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/Masterminds/semver"
|
||||
"gopkg.in/yaml.v3"
|
||||
"io/ioutil"
|
||||
"ledo/app/modules/context"
|
||||
"log"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const DockerComposeVersion = ">= 1.28.6"
|
||||
|
||||
func CheckDockerComposeVersion() {
|
||||
cmd := exec.Command("docker-compose", "--version")
|
||||
var output bytes.Buffer
|
||||
cmd.Stdout = &output
|
||||
err := cmd.Run()
|
||||
|
||||
if err != nil {
|
||||
log.Fatal("No docker-compose installed. Please install docker-compose ie. via `pip3 install docker-compose`")
|
||||
}
|
||||
|
||||
r := regexp.MustCompile("(.*){1}(version\\ ){1}(([0-9]+)\\.([0-9]+)\\.([0-9]+))")
|
||||
result := r.FindStringSubmatch(output.String())
|
||||
composeVersion := result[3]
|
||||
|
||||
verConstraint, _ := semver.NewConstraint(DockerComposeVersion)
|
||||
composeSemVer, _ := semver.NewVersion(composeVersion)
|
||||
|
||||
if !verConstraint.Check(composeSemVer) {
|
||||
log.Fatal("Wrong docker-compose version, please update to 1.28.6 or higher.")
|
||||
}
|
||||
}
|
||||
|
||||
func MergeComposerFiles(filenames ...string) (string, error) {
|
||||
var resultValues map[string]interface{}
|
||||
|
||||
if len(filenames) <= 0 {
|
||||
return "", errors.New("You must provide at least one filename for reading Values")
|
||||
}
|
||||
|
||||
for _, filename := range filenames {
|
||||
|
||||
var override map[string]interface{}
|
||||
bs, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
continue
|
||||
}
|
||||
if err := yaml.Unmarshal(bs, &override); err != nil {
|
||||
log.Print(err)
|
||||
continue
|
||||
}
|
||||
|
||||
if resultValues == nil {
|
||||
resultValues = override
|
||||
} else {
|
||||
for k, v := range override {
|
||||
resultValues[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bs, err := yaml.Marshal(resultValues)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(bs), nil
|
||||
}
|
||||
|
||||
func ShowDockerImageFQN(ctx *context.LedoContext) string {
|
||||
return fmt.Sprintf("%s/%s/%s/master:latest", ctx.Config.Docker.Registry, ctx.Config.Docker.Namespace, ctx.Config.Docker.Name)
|
||||
}
|
||||
|
||||
func ExecComposerUp(ctx *context.LedoContext) {
|
||||
args := ctx.ComposeArgs
|
||||
args = append(args, "up", "-d")
|
||||
ctx.ExecCmd("docker-compose", args[0:])
|
||||
}
|
||||
|
||||
func ExecComposerPull(ctx *context.LedoContext) {
|
||||
args := ctx.ComposeArgs
|
||||
args = append(args, "pull")
|
||||
ctx.ExecCmd("docker-compose", args[0:])
|
||||
}
|
||||
|
||||
func ExecComposerStop(ctx *context.LedoContext) {
|
||||
args := ctx.ComposeArgs
|
||||
args = append(args, "stop")
|
||||
ctx.ExecCmd("docker-compose", args[0:])
|
||||
}
|
||||
|
||||
func ExecComposerBuild(ctx *context.LedoContext) {
|
||||
args := ctx.ComposeArgs
|
||||
args = append(args, "build", "--pull")
|
||||
ctx.ExecCmd("docker-compose", args[0:])
|
||||
}
|
||||
|
||||
|
||||
func ExecComposerDown(ctx *context.LedoContext) {
|
||||
args := ctx.ComposeArgs
|
||||
args = append(args, "down")
|
||||
ctx.ExecCmd("docker-compose", args[0:])
|
||||
}
|
||||
|
||||
func ExecComposerStart(ctx *context.LedoContext) {
|
||||
args := ctx.ComposeArgs
|
||||
args = append(args, "start")
|
||||
ctx.ExecCmd("docker-compose", args[0:])
|
||||
}
|
||||
|
||||
func ExecComposerRestart(ctx *context.LedoContext) {
|
||||
args := ctx.ComposeArgs
|
||||
args = append(args, "restart")
|
||||
ctx.ExecCmd("docker-compose", args[0:])
|
||||
}
|
||||
|
||||
func ExecComposerLogs(ctx *context.LedoContext) {
|
||||
args := ctx.ComposeArgs
|
||||
args = append(args, "logs", "--follow", "--tail", "100")
|
||||
ctx.ExecCmd("docker-compose", args[0:])
|
||||
}
|
||||
|
||||
func ExecComposerPs(ctx *context.LedoContext) {
|
||||
args := ctx.ComposeArgs
|
||||
args = append(args, "ps")
|
||||
ctx.ExecCmd("docker-compose", args[0:])
|
||||
}
|
||||
|
||||
func ExecComposerShell(ctx *context.LedoContext) {
|
||||
args := ctx.ComposeArgs
|
||||
args = append(args, "exec", strings.ToLower(ctx.Config.Docker.MainService), ctx.Config.Docker.Shell)
|
||||
ctx.ExecCmd("docker-compose", args[0:])
|
||||
}
|
||||
|
||||
func ExecComposerDebug(ctx *context.LedoContext) {
|
||||
args := ctx.ComposeArgs
|
||||
args = append(args, "run", "--entrypoint=", strings.ToLower(ctx.Config.Docker.MainService), ctx.Config.Docker.Shell)
|
||||
ctx.ExecCmd("docker-compose", args[0:])
|
||||
}
|
||||
|
||||
func ExecComposerRun(ctx *context.LedoContext) {
|
||||
args := ctx.ComposeArgs
|
||||
args = append(args, "run", strings.ToLower(ctx.Config.Docker.MainService), ctx.Config.Docker.Shell)
|
||||
ctx.ExecCmd("docker-compose", args[0:])
|
||||
}
|
||||
|
||||
func ExecComposerUpOnce(ctx *context.LedoContext) {
|
||||
args := ctx.ComposeArgs
|
||||
args = append(args, "up", "--force-recreate", "--renew-anon-volumes", "--abort-on-container-exit", "--exit-code-from", ctx.Config.Docker.MainService)
|
||||
ctx.ExecCmd("docker-compose", args[0:])
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user