-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
180 lines (148 loc) · 4.23 KB
/
run.go
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
170
171
172
173
174
175
176
177
178
179
180
package run
import (
"context"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/silphid/yey/src/cmd"
yey "github.com/silphid/yey/src/internal"
"github.com/silphid/yey/src/internal/docker"
"github.com/spf13/cobra"
)
// New creates a cobra command
func New() *cobra.Command {
options := Options{Remove: new(bool)}
cmd := &cobra.Command{
Use: "run",
Short: "Runs container using given context",
Args: cobra.ArbitraryArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if !cmd.Flag("rm").Changed {
options.Remove = nil
}
return run(cmd.Context(), args, options)
},
}
cmd.Flags().BoolVar(options.Remove, "rm", false, "remove container upon exit")
cmd.Flags().BoolVar(&options.Reset, "reset", false, "remove previous container before starting a fresh one")
cmd.Flags().BoolVar(&options.Pull, "pull", false, "force pulling image from registry before running")
return cmd
}
type Options struct {
Remove *bool
Reset bool
Pull bool
}
func run(ctx context.Context, names []string, options Options) error {
contexts, err := yey.LoadContexts()
if err != nil {
return err
}
lastNames, err := cmd.LoadLastNames()
if err != nil {
return err
}
names, err = cmd.GetOrPromptContexts(contexts.Context, names, lastNames)
if err != nil {
return err
}
err = cmd.SaveLastNames(names)
if err != nil {
return err
}
yeyContext, err := contexts.GetContext(names)
if err != nil {
return fmt.Errorf("failed to get context: %w", err)
}
if options.Remove != nil {
yeyContext.Remove = options.Remove
}
if yeyContext.Image == "" {
var err error
yeyContext.Image, err = readAndBuildDockerfile(ctx, yeyContext.Build, options)
if err != nil {
return fmt.Errorf("failed to build yey context image: %w", err)
}
yey.Log("using image: %s", yeyContext.Image)
}
yey.Log("context:\n--\n%v--", yeyContext)
// Container name
containerName := yey.ContainerName(contexts.Path, yeyContext)
yey.Log("container: %s", containerName)
// Reset
if options.Reset {
yey.Log("removing container first")
if err := docker.Remove(ctx, containerName, docker.RemoveOptions{}); err != nil {
return fmt.Errorf("failed to remove container %q: %w", containerName, err)
}
}
// Working directory
var runOptions docker.RunOptions
workDir, err := getContainerWorkDir(yeyContext)
if err != nil {
return err
}
if workDir != "" {
runOptions.WorkDir = workDir
}
yey.Log("working directory: %s", workDir)
// Pull image first?
if options.Pull || shouldPull(yeyContext.Image) {
// TODO: Check running containers with image
// and prompt for killing it
yey.Log("Pulling %s", yeyContext.Image)
docker.Pull(ctx, yeyContext.Image)
}
// Banner
if !yey.IsDryRun {
if err := ShowBanner(yeyContext.Name); err != nil {
return err
}
}
return docker.Run(ctx, yeyContext, containerName, runOptions)
}
var tagRegex = regexp.MustCompile(`.*/.*:(.*)`)
func getTagFromImageName(imageName string) string {
groups := tagRegex.FindStringSubmatch(imageName)
if groups == nil {
return ""
}
return groups[1]
}
// shouldPull returns whether image should be pulled before running it.
// It returns true when image tag is `latest` or when it is not specified.
func shouldPull(imageName string) bool {
tag := getTagFromImageName(imageName)
return tag == "" || tag == "latest"
}
func getContainerWorkDir(yeyContext yey.Context) (string, error) {
workDir, err := os.Getwd()
if err != nil {
return "", err
}
for key, value := range yeyContext.Mounts {
// Where is work dir relatively to mount dir?
subDir, err := filepath.Rel(key, workDir)
if err != nil {
return "", err
}
// Is work dir within mount dir?
if !strings.HasPrefix(subDir, fmt.Sprintf("..%c", filepath.Separator)) {
return filepath.Join(value, subDir), nil
}
}
return "", nil
}
func readAndBuildDockerfile(ctx context.Context, build yey.DockerBuild, options Options) (string, error) {
dockerBytes, err := os.ReadFile(build.Dockerfile)
if err != nil {
return "", fmt.Errorf("failed to read dockerfile: %w", err)
}
imageName := yey.ImageName(dockerBytes)
if err := docker.Build(ctx, build.Dockerfile, imageName, build.Args, build.Context); err != nil {
return "", fmt.Errorf("failed to build image: %w", err)
}
return imageName, nil
}