This commit is contained in:
Aleksander Cynarski 2021-12-01 14:36:35 +01:00
parent aee0deba4a
commit 0283ddf0d5
Signed by: paramah
GPG Key ID: C4340BA42B9C173A
13 changed files with 213 additions and 70 deletions

View File

@ -8,15 +8,3 @@ docker:
modes:
dev: docker/docker-compose.yml docker/docker-compose.dev.yml
launch: docker/docker-compose.yml
providers:
# you can mix and match many files
dotenv:
env_sync:
path: .env-dist
vault:
env_sync:
path: environment/data/billing/web/env
env:
SMTP_PASS:
path: environment/data/billing/global
field: SMTP

1
Dockerfile Normal file
View File

@ -0,0 +1 @@
FROM alpine

View File

@ -1,9 +1,8 @@
package cmd
import (
"ledo/app/cmd/docker"
"github.com/urfave/cli/v2"
"ledo/app/cmd/docker"
)
var CmdDocker = cli.Command{
@ -12,10 +11,8 @@ var CmdDocker = cli.Command{
Category: catHelpers,
Usage: "docker helper",
Description: `Manage docker-compose in project`,
Action: runDockerDefault,
Subcommands: []*cli.Command{
&docker.CmdDockerPs,
&docker.CmdDockerFqn,
&docker.CmdDockerUp,
&docker.CmdComposeBuild,
&docker.CmdComposeDebug,
@ -31,7 +28,3 @@ var CmdDocker = cli.Command{
&docker.CmdDockerLogin,
},
}
func runDockerDefault(ctx *cli.Context) error {
return docker.RunComposePs(ctx)
}

23
app/cmd/image.go Normal file
View File

@ -0,0 +1,23 @@
package cmd
import (
"ledo/app/cmd/image"
"github.com/urfave/cli/v2"
)
var CmdImage = cli.Command{
Name: "image",
Aliases: []string{"i"},
Category: catHelpers,
Usage: "docker container helper",
Description: `docker container helper`,
Subcommands: []*cli.Command{
&image.CmdDockerFqn,
&image.CmdDockerPush,
&image.CmdDockerRetag,
&image.CmdDockerBuild,
},
}

45
app/cmd/image/build.go Normal file
View File

@ -0,0 +1,45 @@
package image
import (
"github.com/urfave/cli/v2"
"ledo/app/modules/context"
"ledo/app/modules/docker"
)
var CmdDockerBuild = cli.Command{
Name: "build",
Aliases: []string{"b"},
Usage: "build docker image",
Description: `Build docker image`,
ArgsUsage: "version",
Action: RunDockerBuild,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "stage",
Aliases: []string{"s"},
Value: "",
Usage: "select stage to build (multi-stage dockerfile)",
Required: false,
},
&cli.StringFlag{
Name: "dockerfile",
Aliases: []string{"f"},
Value: "./Dockerfile",
Usage: "select dockerfile",
Required: false,
},
&cli.StringFlag{
Name: "opts",
Aliases: []string{"o"},
Value: "--compress",
Usage: "Additional build options",
Required: false,
},
},
}
func RunDockerBuild(cmd *cli.Context) error {
ctx := context.InitCommand(cmd)
docker.ExecDockerBuild(ctx, cmd.Args(), *cmd)
return nil
}

View File

@ -1,9 +1,9 @@
package docker
package image
import (
"fmt"
"github.com/urfave/cli/v2"
"ledo/app/modules/compose"
"ledo/app/modules/docker"
"ledo/app/modules/context"
)
@ -17,6 +17,6 @@ var CmdDockerFqn = cli.Command{
func RunDockerFqn(cmd *cli.Context) error {
ctx := context.InitCommand(cmd)
fmt.Printf("%v", compose.ShowDockerImageFQN(ctx))
fmt.Printf("%v", docker.ShowDockerImageFQN(ctx))
return nil
}

22
app/cmd/image/push.go Normal file
View File

@ -0,0 +1,22 @@
package image
import (
"github.com/urfave/cli/v2"
"ledo/app/modules/context"
"ledo/app/modules/docker"
)
var CmdDockerPush = cli.Command{
Name: "push",
Aliases: []string{"p"},
Usage: "push docker to registry",
Description: `Push docker image to docker registry`,
ArgsUsage: "version",
Action: RunDockerPush,
}
func RunDockerPush(cmd *cli.Context) error {
ctx := context.InitCommand(cmd)
docker.ExecDockerPush(ctx, cmd.Args())
return nil
}

22
app/cmd/image/retag.go Normal file
View File

@ -0,0 +1,22 @@
package image
import (
"github.com/urfave/cli/v2"
"ledo/app/modules/context"
"ledo/app/modules/docker"
)
var CmdDockerRetag = cli.Command{
Name: "retag",
Aliases: []string{"r"},
Usage: "retag docker image",
Description: `Change docker image tag`,
ArgsUsage: "fromTag toTag",
Action: RunDockerRetag,
}
func RunDockerRetag(cmd *cli.Context) error {
ctx := context.InitCommand(cmd)
docker.ExecDockerRetag(ctx, cmd.Args())
return nil
}

View File

@ -15,7 +15,6 @@ import (
var CmdInit = cli.Command{
Name: "init",
Aliases: []string{"i"},
Category: catSetup,
Usage: "Init ledo in project",
Description: `Initialize LeadDocker in current project`,

View File

@ -3,7 +3,6 @@ package compose
import (
"bytes"
"errors"
"fmt"
"github.com/Masterminds/semver"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v3"
@ -78,11 +77,6 @@ func MergeComposerFiles(filenames ...string) (string, error) {
return string(bs), nil
}
func ShowDockerImageFQN(ctx *context.LedoContext) string {
fqn := fmt.Sprintf("%s/%s/%s/master", ctx.Config.Docker.Registry, ctx.Config.Docker.Namespace, ctx.Config.Docker.Name)
return strings.ToLower(fqn)
}
func ExecComposerUp(ctx *context.LedoContext) {
args := ctx.ComposeArgs
args = append(args, "up", "-d")

View File

@ -0,0 +1,95 @@
package docker
import (
b64 "encoding/base64"
"fmt"
"github.com/urfave/cli/v2"
"ledo/app/modules/aws_ledo"
"ledo/app/modules/context"
"net/url"
"os"
"strings"
)
func trimLeftChars(s string, n int) string {
m := 0
for i := range s {
if m >= n {
return s[i:]
}
m++
}
return s[:0]
}
func DockerEcrLogin(ctx *context.LedoContext) error {
ecr, err := aws_ledo.EcrLogin()
if err != nil {
fmt.Println("Ecr login error: %s", err)
os.Exit(1)
}
password := *ecr.AuthorizationData[0].AuthorizationToken
ecrUrl := *ecr.AuthorizationData[0].ProxyEndpoint
sDec, _ := b64.StdEncoding.DecodeString(password)
registryAddr, err := url.Parse(ecrUrl)
if err != nil {
fmt.Printf("Ecr endpoint addr parse error: %s", err)
os.Exit(1)
}
ctx.ExecCmdSilent("docker", []string{"login", "-u", "AWS", "-p", string(trimLeftChars(string(sDec), 4)), registryAddr.Host})
return nil
}
func ShowDockerImageFQN(ctx *context.LedoContext) string {
fqn := fmt.Sprintf("%s/%s/%s", ctx.Config.Docker.Registry, ctx.Config.Docker.Namespace, ctx.Config.Docker.Name)
return strings.ToLower(fqn)
}
func ExecDockerPush(ctx *context.LedoContext, command cli.Args) {
var version string
var args []string
if command.First() == "" {
version = "latest"
} else {
version = command.First()
}
image := ShowDockerImageFQN(ctx)
args = append(args, "push")
args = append(args, "-f", image + ":" + version)
ctx.ExecCmd("docker", args[0:])
}
func ExecDockerRetag(ctx *context.LedoContext, command cli.Args) {
var args []string
from := command.Get(0)
to := command.Get(1)
image := ShowDockerImageFQN(ctx)
args = append(args, "tag")
args = append(args, image + ":" + from)
args = append(args, image + ":" + to)
ctx.ExecCmd("docker", args[0:])
}
func ExecDockerBuild(ctx *context.LedoContext, command cli.Args, cmdCtx cli.Context) {
var version string
var args []string
if command.First() == "" {
version = "latest"
} else {
version = command.First()
}
opts := strings.Split(strings.Trim(cmdCtx.String("opts"), " "), " ")
image := ShowDockerImageFQN(ctx)
args = append(args, "build")
args = append(args, "-t", image+":"+version)
args = append(args, "-f", cmdCtx.String("dockerfile"))
args = append(args, opts...)
if cmdCtx.String("stage") != "" {
args = append(args, "--target", cmdCtx.String("stage"))
}
args = append(args, ".")
ctx.ExecCmd("docker", args[0:])
}

View File

@ -1,40 +0,0 @@
package docker
import (
b64 "encoding/base64"
"fmt"
"ledo/app/modules/aws_ledo"
"ledo/app/modules/context"
"net/url"
"os"
)
func trimLeftChars(s string, n int) string {
m := 0
for i := range s {
if m >= n {
return s[i:]
}
m++
}
return s[:0]
}
func DockerEcrLogin(ctx *context.LedoContext) error {
ecr, err := aws_ledo.EcrLogin()
if err != nil {
fmt.Println("Ecr login error: %s", err)
os.Exit(1)
}
password := *ecr.AuthorizationData[0].AuthorizationToken
ecrUrl := *ecr.AuthorizationData[0].ProxyEndpoint
sDec, _ := b64.StdEncoding.DecodeString(password)
registryAddr, err := url.Parse(ecrUrl)
if err != nil {
fmt.Printf("Ecr endpoint addr parse error: %s", err)
os.Exit(1)
}
ctx.ExecCmdSilent("docker", []string{"login", "-u", "AWS", "-p", string(trimLeftChars(string(sDec), 4)), registryAddr.Host})
return nil
}

View File

@ -29,6 +29,7 @@ func main() {
app.Commands = []*cli.Command{
&cmd.CmdInit,
&cmd.CmdDocker,
&cmd.CmdImage,
&cmd.CmdMode,
&cmd.CmdAutocomplete,
// &CmdSelfupdate,