Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

first draft for supporting docker-compose v2 #232

Merged
merged 1 commit into from Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions cmd/accounts_create.go
Expand Up @@ -33,13 +33,17 @@ var accountsCreateCmd = &cobra.Command{
Long: `Create a new account in the FireFly stack`,
Args: cobra.MinimumNArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
return docker.CheckDockerConfig()
},
RunE: func(cmd *cobra.Command, args []string) error {
ctx := log.WithVerbosity(context.Background(), verbose)
ctx = log.WithLogger(ctx, logger)

version, err := docker.CheckDockerConfig()
ctx = context.WithValue(ctx, docker.CtxComposeVersionKey{}, version)
cmd.SetContext(ctx)
return err
},
RunE: func(cmd *cobra.Command, args []string) error {
stackName := args[0]
stackManager := stacks.NewStackManager(ctx)
stackManager := stacks.NewStackManager(cmd.Context())
if err := stackManager.LoadStack(stackName); err != nil {
return err
}
Expand Down
12 changes: 8 additions & 4 deletions cmd/accounts_list.go
Expand Up @@ -35,13 +35,17 @@ var accountsListCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
Aliases: []string{"ls"},
PreRunE: func(cmd *cobra.Command, args []string) error {
return docker.CheckDockerConfig()
},
RunE: func(cmd *cobra.Command, args []string) error {
ctx := log.WithVerbosity(context.Background(), verbose)
ctx = log.WithLogger(ctx, logger)

version, err := docker.CheckDockerConfig()
ctx = context.WithValue(ctx, docker.CtxComposeVersionKey{}, version)
cmd.SetContext(ctx)
return err
},
RunE: func(cmd *cobra.Command, args []string) error {
stackName := args[0]
stackManager := stacks.NewStackManager(ctx)
stackManager := stacks.NewStackManager(cmd.Context())
if err := stackManager.LoadStack(stackName); err != nil {
return err
}
Expand Down
12 changes: 8 additions & 4 deletions cmd/deploy_ethereum.go
Expand Up @@ -39,14 +39,18 @@ solc --combined-json abi,bin contract.sol > contract.json
`,
Args: cobra.MinimumNArgs(2),
PreRunE: func(cmd *cobra.Command, args []string) error {
return docker.CheckDockerConfig()
},
RunE: func(cmd *cobra.Command, args []string) error {
ctx := log.WithVerbosity(context.Background(), verbose)
ctx = log.WithLogger(ctx, logger)

version, err := docker.CheckDockerConfig()
ctx = context.WithValue(ctx, docker.CtxComposeVersionKey{}, version)
cmd.SetContext(ctx)
return err
},
RunE: func(cmd *cobra.Command, args []string) error {
stackName := args[0]
filename := args[1]
stackManager := stacks.NewStackManager(ctx)
stackManager := stacks.NewStackManager(cmd.Context())
if err := stackManager.LoadStack(stackName); err != nil {
return err
}
Expand Down
12 changes: 8 additions & 4 deletions cmd/deploy_fabric.go
Expand Up @@ -33,14 +33,18 @@ var deployFabricCmd = &cobra.Command{
Long: `Deploy a packaged chaincode to the Fabric network used by a FireFly stack`,
Args: cobra.ExactArgs(5),
PreRunE: func(cmd *cobra.Command, args []string) error {
return docker.CheckDockerConfig()
},
RunE: func(cmd *cobra.Command, args []string) error {
ctx := log.WithVerbosity(context.Background(), verbose)
ctx = log.WithLogger(ctx, logger)

version, err := docker.CheckDockerConfig()
ctx = context.WithValue(ctx, docker.CtxComposeVersionKey{}, version)
cmd.SetContext(ctx)
return err
},
RunE: func(cmd *cobra.Command, args []string) error {
stackName := args[0]
filename := args[1]
stackManager := stacks.NewStackManager(ctx)
stackManager := stacks.NewStackManager(cmd.Context())
if err := stackManager.LoadStack(stackName); err != nil {
return err
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/info.go
Expand Up @@ -34,9 +34,13 @@ var infoCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
ctx := log.WithVerbosity(context.Background(), verbose)
ctx = log.WithLogger(ctx, logger)
if err := docker.CheckDockerConfig(); err != nil {

version, err := docker.CheckDockerConfig()
if err != nil {
return err
}
ctx = context.WithValue(ctx, docker.CtxComposeVersionKey{}, version)

stackManager := stacks.NewStackManager(ctx)
if len(args) == 0 {
return fmt.Errorf("no stack specified")
Expand Down
9 changes: 6 additions & 3 deletions cmd/logs.go
Expand Up @@ -6,7 +6,7 @@
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -37,11 +37,14 @@ The most recent logs can be viewed, or you can follow the
output with the -f flag.`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := log.WithVerbosity(context.Background(), verbose)
ctx = context.WithValue(ctx, docker.CtxIsLogCmd{}, true)
ctx = context.WithValue(ctx, docker.CtxIsLogCmdKey{}, true)
ctx = log.WithLogger(ctx, logger)
if err := docker.CheckDockerConfig(); err != nil {

version, err := docker.CheckDockerConfig()
if err != nil {
return err
}
ctx = context.WithValue(ctx, docker.CtxComposeVersionKey{}, version)

stackManager := stacks.NewStackManager(ctx)
if len(args) == 0 {
Expand Down
6 changes: 5 additions & 1 deletion cmd/remove.go
Expand Up @@ -40,9 +40,13 @@ and configuration.`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := log.WithVerbosity(context.Background(), verbose)
ctx = log.WithLogger(ctx, logger)
if err := docker.CheckDockerConfig(); err != nil {

version, err := docker.CheckDockerConfig()
if err != nil {
return err
}
ctx = context.WithValue(ctx, docker.CtxComposeVersionKey{}, version)

stackManager := stacks.NewStackManager(ctx)
if len(args) == 0 {
return fmt.Errorf("no stack specified")
Expand Down
5 changes: 4 additions & 1 deletion cmd/reset.go
Expand Up @@ -39,9 +39,12 @@ Note: this will also stop the stack if it is running.
RunE: func(cmd *cobra.Command, args []string) error {
ctx := log.WithVerbosity(context.Background(), verbose)
ctx = log.WithLogger(ctx, logger)
if err := docker.CheckDockerConfig(); err != nil {

version, err := docker.CheckDockerConfig()
if err != nil {
return err
}
ctx = context.WithValue(ctx, docker.CtxComposeVersionKey{}, version)

stackManager := stacks.NewStackManager(ctx)
if len(args) == 0 {
Expand Down
4 changes: 3 additions & 1 deletion cmd/start.go
Expand Up @@ -47,9 +47,11 @@ This command will start a stack and run it in the background.
ctx := log.WithVerbosity(context.Background(), verbose)
ctx = log.WithLogger(ctx, logger)

if err := docker.CheckDockerConfig(); err != nil {
version, err := docker.CheckDockerConfig()
if err != nil {
return err
}
ctx = context.WithValue(ctx, docker.CtxComposeVersionKey{}, version)

stackManager := stacks.NewStackManager(ctx)
if len(args) == 0 {
Expand Down
6 changes: 5 additions & 1 deletion cmd/stop.go
Expand Up @@ -34,9 +34,13 @@ var stopCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
ctx := log.WithVerbosity(context.Background(), verbose)
ctx = log.WithLogger(ctx, logger)
if err := docker.CheckDockerConfig(); err != nil {

version, err := docker.CheckDockerConfig()
if err != nil {
return err
}
ctx = context.WithValue(ctx, docker.CtxComposeVersionKey{}, version)

stackManager := stacks.NewStackManager(ctx)
if len(args) == 0 {
return fmt.Errorf("no stack specified")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -28,7 +28,7 @@ require (
github.com/miracl/conflate v1.2.1
github.com/mitchellh/go-homedir v1.1.0
github.com/otiai10/copy v1.7.0
github.com/spf13/cobra v1.4.0
github.com/spf13/cobra v1.5.0
github.com/spf13/viper v1.12.1-0.20220712161005-5247643f0235
github.com/stretchr/testify v1.8.0
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Expand Up @@ -279,6 +279,7 @@ github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfc
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down Expand Up @@ -828,6 +829,8 @@ github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHN
github.com/spf13/cobra v1.3.0/go.mod h1:BrRVncBjOJa/eUcVVm9CE+oC6as8k+VYr4NY7WCi9V4=
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU=
github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk=
github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo=
Expand Down
30 changes: 24 additions & 6 deletions internal/docker/docker.go
Expand Up @@ -31,7 +31,15 @@ import (
)

type (
CtxIsLogCmd struct{}
CtxIsLogCmdKey struct{}
CtxComposeVersionKey struct{}
DockerComposeVersion int
)

const (
None DockerComposeVersion = iota
ComposeV1
ComposeV2
)

func CreateVolume(ctx context.Context, volumeName string) error {
Expand Down Expand Up @@ -81,10 +89,20 @@ func RunDockerCommand(ctx context.Context, workingDir string, command ...string)
}

func RunDockerComposeCommand(ctx context.Context, workingDir string, command ...string) error {
dockerCmd := exec.Command("docker-compose", command...)
dockerCmd.Dir = workingDir
_, err := runCommand(ctx, dockerCmd)
return err
switch ctx.Value(CtxComposeVersionKey{}) {
case ComposeV1:
dockerCmd := exec.Command("docker-compose", command...)
dockerCmd.Dir = workingDir
_, err := runCommand(ctx, dockerCmd)
return err
case ComposeV2:
dockerCmd := exec.Command("docker compose", command...)
dockerCmd.Dir = workingDir
_, err := runCommand(ctx, dockerCmd)
return err
default:
return fmt.Errorf("No version for docker-compose has been detected.")
}
}

func RunDockerCommandBuffered(ctx context.Context, workingDir string, command ...string) (string, error) {
Expand All @@ -95,7 +113,7 @@ func RunDockerCommandBuffered(ctx context.Context, workingDir string, command ..

func runCommand(ctx context.Context, cmd *exec.Cmd) (string, error) {
verbose := log.VerbosityFromContext(ctx)
isLogCmd, _ := ctx.Value(CtxIsLogCmd{}).(bool)
isLogCmd, _ := ctx.Value(CtxIsLogCmdKey{}).(bool)
if verbose {
fmt.Println(cmd.String())
}
Expand Down
28 changes: 17 additions & 11 deletions internal/docker/docker_checks.go
Expand Up @@ -21,27 +21,33 @@ import (
"os/exec"
)

// CheckDockerConfig is a function to check docker and docker-compose configuration on the host
func CheckDockerConfig() error {
func CheckDockerConfig() (DockerComposeVersion, error) {

dockerCmd := exec.Command("docker", "-v")
_, err := dockerCmd.Output()
if err != nil {
return fmt.Errorf("an error occurred while running docker. Is docker installed on your computer?")
return None, fmt.Errorf("an error occurred while running docker. Is docker installed on your computer?")
}

dockerDeamonCheck := exec.Command("docker", "ps")
_, err = dockerDeamonCheck.Output()
if err != nil {
return None, fmt.Errorf("an error occurred while running docker. Is docker running on your computer?")
}

// ckeck for docker-compose (v1) version
dockerComposeCmd := exec.Command("docker-compose", "-v")
_, err = dockerComposeCmd.Output()

if err != nil {
return fmt.Errorf("an error occurred while running docker-compose. Is docker-compose installed on your computer?")
if err == nil {
return ComposeV1, nil
}

dockerDeamonCheck := exec.Command("docker", "ps")
_, err = dockerDeamonCheck.Output()
if err != nil {
return fmt.Errorf("an error occurred while running docker. Is docker running on your computer?")
// ckeck for docker-compose (V2) version
dockerComposeCmd = exec.Command("docker compose", "version")
_, err = dockerComposeCmd.Output()
if err == nil {
return ComposeV2, nil
}

return nil
return None, fmt.Errorf("an error occurred while running docker-compose. Is docker-compose installed on your computer?")
}