introduce ecs-local context

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2020-08-31 14:26:00 +02:00
parent c1ecb2b7be
commit fed50d79f2
No known key found for this signature in database
GPG key ID: 9858809D6F8F6E7E
13 changed files with 189 additions and 67 deletions

View file

@ -60,7 +60,7 @@ func (o *composeOptions) toProjectOptions() (*cli.ProjectOptions, error) {
}
// Command returns the compose command with its child commands
func Command(contextType string) *cobra.Command {
func Command() *cobra.Command {
command := &cobra.Command{
Short: "Docker Compose",
Use: "compose",
@ -70,7 +70,7 @@ func Command(contextType string) *cobra.Command {
}
command.AddCommand(
upCommand(contextType),
upCommand(),
downCommand(),
psCommand(),
logsCommand(),

View file

@ -24,17 +24,15 @@ import (
"github.com/spf13/cobra"
"github.com/docker/compose-cli/client"
"github.com/docker/compose-cli/context/store"
"github.com/docker/compose-cli/progress"
)
func upCommand(contextType string) *cobra.Command {
func upCommand() *cobra.Command {
opts := composeOptions{}
var simulation bool
upCmd := &cobra.Command{
Use: "up",
RunE: func(cmd *cobra.Command, args []string) error {
return runUp(cmd.Context(), opts, simulation)
return runUp(cmd.Context(), opts)
},
}
upCmd.Flags().StringVarP(&opts.Name, "project-name", "p", "", "Project name")
@ -42,14 +40,10 @@ func upCommand(contextType string) *cobra.Command {
upCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
upCmd.Flags().StringArrayVarP(&opts.Environment, "environment", "e", []string{}, "Environment variables")
upCmd.Flags().BoolP("detach", "d", true, " Detached mode: Run containers in the background")
if contextType == store.EcsContextType {
upCmd.Flags().BoolVar(&simulation, "simulate", false, " Simulation mode: run compose app with ECS local container endpoints")
}
return upCmd
}
func runUp(ctx context.Context, opts composeOptions, simulation bool) error {
func runUp(ctx context.Context, opts composeOptions) error {
c, err := client.New(ctx)
if err != nil {
return err
@ -65,9 +59,6 @@ func runUp(ctx context.Context, opts composeOptions, simulation bool) error {
return err
}
if simulation {
return c.ComposeService().Emulate(ctx, options)
}
return c.ComposeService().Up(ctx, project)
})
}

View file

@ -38,17 +38,22 @@ $ docker context create ecs CONTEXT [flags]
}
func createEcsCommand() *cobra.Command {
var localSimulation bool
var opts ecs.ContextParams
cmd := &cobra.Command{
Use: "ecs CONTEXT [flags]",
Short: "Create a context for Amazon ECS",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if localSimulation {
return runCreateLocalSimulation(cmd.Context(), args[0], opts)
}
return runCreateEcs(cmd.Context(), args[0], opts)
},
}
addDescriptionFlag(cmd, &opts.Description)
cmd.Flags().BoolVar(&localSimulation, "local-simulation", false, "Create context for ECS local simulation endpoints")
cmd.Flags().StringVar(&opts.Profile, "profile", "", "Profile")
cmd.Flags().StringVar(&opts.Region, "region", "", "Region")
cmd.Flags().StringVar(&opts.AwsID, "key-id", "", "AWS Access Key ID")
@ -56,6 +61,21 @@ func createEcsCommand() *cobra.Command {
return cmd
}
func runCreateLocalSimulation(ctx context.Context, contextName string, opts ecs.ContextParams) error {
if contextExists(ctx, contextName) {
return errors.Wrapf(errdefs.ErrAlreadyExists, "context %q", contextName)
}
cs, err := client.GetCloudService(ctx, store.EcsLocalSimulationContextType)
if err != nil {
return errors.Wrap(err, "cannot connect to ECS backend")
}
data, description, err := cs.CreateContextData(ctx, opts)
if err != nil {
return err
}
return createDockerContext(ctx, contextName, store.EcsLocalSimulationContextType, description, data)
}
func runCreateEcs(ctx context.Context, contextName string, opts ecs.ContextParams) error {
if contextExists(ctx, contextName) {
return errors.Wrapf(errdefs.ErrAlreadyExists, "context %q", contextName)
@ -71,7 +91,7 @@ func runCreateEcs(ctx context.Context, contextName string, opts ecs.ContextParam
func getEcsContextData(ctx context.Context, opts ecs.ContextParams) (interface{}, string, error) {
cs, err := client.GetCloudService(ctx, store.EcsContextType)
if err != nil {
return nil, "", errors.Wrap(err, "cannot connect to AWS backend")
return nil, "", errors.Wrap(err, "cannot connect to ECS backend")
}
return cs.CreateContextData(ctx, opts)
}

View file

@ -27,6 +27,8 @@ import (
"syscall"
"time"
"github.com/docker/compose-cli/cli/cmd/compose"
"github.com/docker/compose-cli/cli/cmd/logout"
"github.com/docker/compose-cli/errdefs"
@ -38,12 +40,12 @@ import (
// Backend registrations
_ "github.com/docker/compose-cli/aci"
_ "github.com/docker/compose-cli/ecs"
_ "github.com/docker/compose-cli/ecs/local"
_ "github.com/docker/compose-cli/example"
_ "github.com/docker/compose-cli/local"
"github.com/docker/compose-cli/metrics"
"github.com/docker/compose-cli/cli/cmd"
"github.com/docker/compose-cli/cli/cmd/compose"
contextcmd "github.com/docker/compose-cli/cli/cmd/context"
"github.com/docker/compose-cli/cli/cmd/login"
"github.com/docker/compose-cli/cli/cmd/run"
@ -126,6 +128,7 @@ func main() {
cmd.VersionCommand(version),
cmd.StopCommand(),
cmd.SecretCommand(),
compose.Command(),
// Place holders
cmd.EcsCommand(),
@ -183,8 +186,6 @@ func main() {
$ docker context create %s <name>`, cc.Type(), store.EcsContextType))
}
root.AddCommand(compose.Command(ctype))
metrics.Track(ctype, os.Args[1:], root.PersistentFlags())
ctx = apicontext.WithCurrentContext(ctx, currentContext)