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"
|
|
|
|
"fmt"
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
"github.com/docker/compose-cli/api/compose"
|
|
|
|
"github.com/docker/compose-cli/api/containers"
|
|
|
|
"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
|
|
|
dependencies := []types.ServiceConfig{}
|
|
|
|
originalServices := project.Services
|
2020-12-03 09:24:15 +01:00
|
|
|
containerID, err := progress.Run(ctx, func(ctx context.Context) (string, error) {
|
2020-12-16 15:25:31 +01:00
|
|
|
for _, service := range originalServices {
|
|
|
|
if service.Name != opts.Name {
|
|
|
|
dependencies = append(dependencies, service)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
project.Services = types.Services(dependencies)
|
|
|
|
if err := c.ComposeService().Create(ctx, project); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if err := c.ComposeService().Start(ctx, project, nil); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return "", nil
|
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 15:25:31 +01:00
|
|
|
containerID, err = c.ComposeService().RunOneOffContainer(ctx, project, compose.RunOptions{Name: opts.Name, Command: opts.Command, Detach: opts.Detach})
|
2020-12-03 09:24:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if opts.Detach {
|
|
|
|
fmt.Printf("%s", containerID)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if opts.Remove {
|
|
|
|
return c.ContainerService().Delete(ctx, containerID, containers.DeleteRequest{
|
|
|
|
Force: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|