2020-12-03 09:24:15 +01:00
|
|
|
/*
|
|
|
|
Copyright 2020 Docker Compose CLI authors
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
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
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package compose
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-12-16 18:50:03 +01:00
|
|
|
"os"
|
2020-12-03 09:24:15 +01:00
|
|
|
|
2020-12-16 15:25:31 +01:00
|
|
|
"github.com/compose-spec/compose-go/types"
|
2020-12-03 09:24:15 +01:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2020-12-16 18:50:03 +01:00
|
|
|
"github.com/docker/compose-cli/api/client"
|
2020-12-03 09:24:15 +01:00
|
|
|
"github.com/docker/compose-cli/api/compose"
|
|
|
|
"github.com/docker/compose-cli/progress"
|
|
|
|
)
|
|
|
|
|
|
|
|
type runOptions struct {
|
|
|
|
Name string
|
|
|
|
Command []string
|
|
|
|
WorkingDir string
|
2020-12-14 17:21:16 +01:00
|
|
|
ConfigPaths []string
|
2020-12-03 09:24:15 +01:00
|
|
|
Environment []string
|
|
|
|
Detach bool
|
|
|
|
Remove bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func runCommand() *cobra.Command {
|
|
|
|
opts := runOptions{}
|
|
|
|
runCmd := &cobra.Command{
|
|
|
|
Use: "run [options] [-v VOLUME...] [-p PORT...] [-e KEY=VAL...] [-l KEY=VALUE...] SERVICE [COMMAND] [ARGS...]",
|
|
|
|
Short: "Run a one-off command on a service.",
|
|
|
|
Args: cobra.MinimumNArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if len(args) > 1 {
|
|
|
|
opts.Command = args[1:]
|
|
|
|
}
|
|
|
|
opts.Name = args[0]
|
|
|
|
return runRun(cmd.Context(), opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
runCmd.Flags().StringVar(&opts.WorkingDir, "workdir", "", "Work dir")
|
2020-12-14 17:21:16 +01:00
|
|
|
runCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
|
2020-12-03 09:24:15 +01:00
|
|
|
runCmd.Flags().BoolVarP(&opts.Detach, "detach", "d", false, "Run container in background and print container ID")
|
|
|
|
runCmd.Flags().StringArrayVarP(&opts.Environment, "env", "e", []string{}, "Set environment variables")
|
|
|
|
runCmd.Flags().BoolVar(&opts.Remove, "rm", false, "Automatically remove the container when it exits")
|
|
|
|
|
|
|
|
runCmd.Flags().SetInterspersed(false)
|
|
|
|
return runCmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runRun(ctx context.Context, opts runOptions) error {
|
2020-12-14 17:21:16 +01:00
|
|
|
projectOpts := composeOptions{
|
|
|
|
ConfigPaths: opts.ConfigPaths,
|
|
|
|
WorkingDir: opts.WorkingDir,
|
|
|
|
Environment: opts.Environment,
|
|
|
|
}
|
2020-12-14 18:02:56 +01:00
|
|
|
c, project, err := setup(ctx, projectOpts, []string{opts.Name})
|
2020-12-03 09:24:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-16 15:25:31 +01:00
|
|
|
originalServices := project.Services
|
2020-12-16 18:34:14 +01:00
|
|
|
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
|
2020-12-16 18:50:03 +01:00
|
|
|
return "", startDependencies(ctx, c, project, opts.Name)
|
2020-12-03 09:24:15 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-16 15:25:31 +01:00
|
|
|
|
|
|
|
project.Services = originalServices
|
2020-12-03 09:24:15 +01:00
|
|
|
// start container and attach to container streams
|
2020-12-16 18:50:03 +01:00
|
|
|
runOpts := compose.RunOptions{
|
|
|
|
Name: opts.Name,
|
|
|
|
Command: opts.Command,
|
|
|
|
Detach: opts.Detach,
|
|
|
|
AutoRemove: opts.Remove,
|
|
|
|
Writer: os.Stdout,
|
|
|
|
Reader: os.Stdin,
|
|
|
|
}
|
|
|
|
return c.ComposeService().RunOneOffContainer(ctx, project, runOpts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func startDependencies(ctx context.Context, c *client.Client, project *types.Project, requestedService string) error {
|
|
|
|
originalServices := project.Services
|
|
|
|
dependencies := types.Services{}
|
|
|
|
for _, service := range originalServices {
|
|
|
|
if service.Name != requestedService {
|
|
|
|
dependencies = append(dependencies, service)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
project.Services = dependencies
|
|
|
|
if err := c.ComposeService().Create(ctx, project); err != nil {
|
2020-12-03 09:24:15 +01:00
|
|
|
return err
|
|
|
|
}
|
2020-12-16 18:50:03 +01:00
|
|
|
if err := c.ComposeService().Start(ctx, project, nil); err != nil {
|
|
|
|
return err
|
2020-12-03 09:24:15 +01:00
|
|
|
}
|
|
|
|
return nil
|
2020-12-16 18:50:03 +01:00
|
|
|
|
2020-12-03 09:24:15 +01:00
|
|
|
}
|