1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
package main
import (
"encoding/json"
"fmt"
"strings"
"github.com/pkg/errors"
"github.com/projectatomic/libpod/libpod"
"github.com/projectatomic/libpod/libpod/image"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"os"
)
var runDescription = "Runs a command in a new container from the given image"
var runFlags []cli.Flag = append(createFlags, cli.BoolTFlag{
Name: "sig-proxy",
Usage: "proxy received signals to the process (default true)",
})
var runCommand = cli.Command{
Name: "run",
Usage: "run a command in a new container",
Description: runDescription,
Flags: runFlags,
Action: runCmd,
ArgsUsage: "IMAGE [COMMAND [ARG...]]",
SkipArgReorder: true,
UseShortOptionHandling: true,
}
func runCmd(c *cli.Context) error {
if err := validateFlags(c, createFlags); err != nil {
return err
}
if c.String("cidfile") != "" {
if _, err := os.Stat(c.String("cidfile")); err == nil {
return errors.Errorf("container id file exists. ensure another container is not using it or delete %s", c.String("cidfile"))
}
if err := libpod.WriteFile("", c.String("cidfile")); err != nil {
return errors.Wrapf(err, "unable to write cidfile %s", c.String("cidfile"))
}
}
runtime, err := getRuntime(c)
if err != nil {
return errors.Wrapf(err, "error creating libpod runtime")
}
defer runtime.Shutdown(false)
if len(c.Args()) < 1 {
return errors.Errorf("image name or ID is required")
}
rtc := runtime.GetConfig()
newImage, err := runtime.ImageRuntime().New(c.Args()[0], rtc.SignaturePolicyPath, "", os.Stderr, nil, image.SigningOptions{})
data, err := newImage.Inspect()
if err != nil {
return err
}
createConfig, err := parseCreateOpts(c, runtime, newImage.Names()[0], data)
if err != nil {
return err
}
useImageVolumes := createConfig.ImageVolumeType == "bind"
runtimeSpec, err := createConfigToOCISpec(createConfig)
if err != nil {
return err
}
options, err := createConfig.GetContainerCreateOptions()
if err != nil {
return errors.Wrapf(err, "unable to parse new container options")
}
// Gather up the options for NewContainer which consist of With... funcs
options = append(options, libpod.WithRootFSFromImage(createConfig.ImageID, createConfig.Image, useImageVolumes))
options = append(options, libpod.WithSELinuxLabels(createConfig.ProcessLabel, createConfig.MountLabel))
options = append(options, libpod.WithLabels(createConfig.Labels))
options = append(options, libpod.WithUser(createConfig.User))
options = append(options, libpod.WithShmDir(createConfig.ShmDir))
options = append(options, libpod.WithShmSize(createConfig.Resources.ShmSize))
// Default used if not overridden on command line
if createConfig.CgroupParent != "" {
options = append(options, libpod.WithCgroupParent(createConfig.CgroupParent))
}
ctr, err := runtime.NewContainer(runtimeSpec, options...)
if err != nil {
return err
}
if logrus.GetLevel() == logrus.DebugLevel {
logrus.Debugf("New container created %q", ctr.ID())
p, _ := ctr.CGroupPath()("")
logrus.Debugf("container %q has CgroupParent %q", ctr.ID(), p)
}
if err := ctr.Init(); err != nil {
// This means the command did not exist
exitCode = 127
if strings.Index(err.Error(), "permission denied") > -1 {
exitCode = 126
}
return err
}
logrus.Debugf("container storage created for %q", ctr.ID())
createConfigJSON, err := json.Marshal(createConfig)
if err != nil {
return err
}
if err := ctr.AddArtifact("create-config", createConfigJSON); err != nil {
return err
}
if c.String("cidfile") != "" {
if err := libpod.WriteFile(ctr.ID(), c.String("cidfile")); err != nil {
logrus.Error(err)
}
}
// Handle detached start
if createConfig.Detach {
if err := ctr.Start(); err != nil {
return errors.Wrapf(err, "unable to start container %q", ctr.ID())
}
fmt.Printf("%s\n", ctr.ID())
exitCode = 0
return nil
}
// TODO: that "false" should probably be linked to -i
// Handle this when we split streams to allow attaching just stdin/out/err
attachChan, err := ctr.StartAndAttach(false, c.String("detach-keys"))
if err != nil {
return errors.Wrapf(err, "unable to start container %q", ctr.ID())
}
if c.BoolT("sig-proxy") {
ProxySignals(ctr)
}
// Wait for attach to complete
err = <-attachChan
if err != nil {
return errors.Wrapf(err, "error attaching to container %s", ctr.ID())
}
if ecode, err := ctr.ExitCode(); err != nil {
logrus.Errorf("unable to get exit code of container %s: %q", ctr.ID(), err)
} else {
exitCode = int(ecode)
}
if createConfig.Rm {
return runtime.RemoveContainer(ctr, true)
}
return ctr.Cleanup()
}
|