Implement simple ACI run

This commit is contained in:
Djordje Lukic 2020-05-01 15:28:44 +02:00
parent 600feb1e00
commit 3d363643ad
11 changed files with 816 additions and 42 deletions

67
cli/cmd/run.go Normal file
View file

@ -0,0 +1,67 @@
/*
Copyright (c) 2020 Docker Inc.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH
THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package cmd
import (
"context"
"github.com/docker/api/client"
"github.com/google/uuid"
"github.com/spf13/cobra"
)
func RunCommand() *cobra.Command {
var opts runOpts
cmd := &cobra.Command{
Use: "run",
Short: "Run a container",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return runRun(cmd.Context(), args[0], opts)
},
}
cmd.Flags().StringArrayVarP(&opts.publish, "publish", "p", []string{}, "Publish a container's port(s)")
cmd.Flags().StringVar(&opts.name, "name", uuid.New().String(), "Assign a name to the container")
return cmd
}
func runRun(ctx context.Context, image string, opts runOpts) error {
c, err := client.New(ctx)
if err != nil {
return err
}
project, err := opts.ToContainerConfig(image)
if err != nil {
return err
}
return c.ContainerService().Run(ctx, project)
}

38
cli/cmd/run_opts.go Normal file
View file

@ -0,0 +1,38 @@
package cmd
import (
"strings"
"github.com/docker/api/containers"
)
type runOpts struct {
name string
publish []string
}
func toPorts(ports []string) ([]containers.Port, error) {
var result []containers.Port
for _, port := range ports {
parts := strings.Split(port, ":")
result = append(result, containers.Port{
Source: parts[0],
Destination: parts[1],
})
}
return result, nil
}
func (r *runOpts) ToContainerConfig(image string) (containers.ContainerConfig, error) {
publish, err := toPorts(r.publish)
if err != nil {
return containers.ContainerConfig{}, err
}
return containers.ContainerConfig{
ID: r.name,
Image: image,
Ports: publish,
}, nil
}

View file

@ -93,6 +93,14 @@ func main() {
},
}
root.AddCommand(
cmd.ContextCommand(),
&cmd.PsCommand,
cmd.ServeCommand(),
&cmd.ExampleCommand,
cmd.RunCommand(),
)
helpFunc := root.HelpFunc()
root.SetHelpFunc(func(cmd *cobra.Command, args []string) {
if !isOwnCommand(cmd) {
@ -110,13 +118,6 @@ func main() {
logrus.SetLevel(logrus.DebugLevel)
}
root.AddCommand(
cmd.ContextCommand(),
&cmd.PsCommand,
cmd.ServeCommand(),
&cmd.ExampleCommand,
)
ctx, cancel := util.NewSigContext()
defer cancel()