-
Notifications
You must be signed in to change notification settings - Fork 298
/
build.go
220 lines (192 loc) · 6.7 KB
/
build.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package cmd
import (
"context"
"fmt"
"io"
"os"
"github.com/loft-sh/devpod/cmd/flags"
"github.com/loft-sh/devpod/pkg/agent"
"github.com/loft-sh/devpod/pkg/agent/tunnelserver"
"github.com/loft-sh/devpod/pkg/client"
"github.com/loft-sh/devpod/pkg/config"
"github.com/loft-sh/devpod/pkg/image"
"github.com/loft-sh/devpod/pkg/provider"
workspace2 "github.com/loft-sh/devpod/pkg/workspace"
"github.com/loft-sh/log"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
// BuildCmd holds the cmd flags
type BuildCmd struct {
*flags.GlobalFlags
provider.CLIOptions
ProviderOptions []string
SkipDelete bool
Machine string
}
// NewBuildCmd creates a new command
func NewBuildCmd(flags *flags.GlobalFlags) *cobra.Command {
cmd := &BuildCmd{
GlobalFlags: flags,
}
buildCmd := &cobra.Command{
Use: "build",
Short: "Builds a workspace",
RunE: func(_ *cobra.Command, args []string) error {
ctx := context.Background()
devPodConfig, err := config.LoadConfig(cmd.Context, cmd.Provider)
if err != nil {
return err
}
// check permissions
if !cmd.SkipPush && cmd.Repository != "" {
err = image.CheckPushPermissions(cmd.Repository)
if err != nil {
return fmt.Errorf("cannot push to %s, please make sure you have push permissions to repository %s", cmd.Repository, cmd.Repository)
}
}
// create a temporary workspace
exists := workspace2.Exists(devPodConfig, args)
sshConfigFile, err := os.CreateTemp("", "devpodssh.config")
if err != nil {
return err
}
sshConfigPath := sshConfigFile.Name()
// defer removal of temporary ssh config file
defer os.Remove(sshConfigPath)
baseWorkspaceClient, err := workspace2.ResolveWorkspace(
ctx,
devPodConfig,
"",
nil,
args,
"",
cmd.Machine,
cmd.ProviderOptions,
cmd.DevContainerImage,
cmd.DevContainerPath,
sshConfigPath,
nil,
cmd.GitBranch,
cmd.GitCommit,
cmd.UID,
false,
log.Default,
)
if err != nil {
return err
}
// delete workspace if we have created it
if exists == "" {
defer func() {
err = baseWorkspaceClient.Delete(ctx, client.DeleteOptions{Force: true})
if err != nil {
log.Default.Errorf("Error deleting workspace: %v", err)
}
}()
}
// check if regular workspace client
workspaceClient, ok := baseWorkspaceClient.(client.WorkspaceClient)
if !ok {
return fmt.Errorf("building is currently not supported for proxy providers")
}
return cmd.Run(ctx, workspaceClient)
},
}
buildCmd.Flags().StringVar(&cmd.DevContainerImage, "devcontainer-image", "", "The container image to use, this will override the devcontainer.json value in the project")
buildCmd.Flags().StringVar(&cmd.DevContainerPath, "devcontainer-path", "", "The path to the devcontainer.json relative to the project")
buildCmd.Flags().StringSliceVar(&cmd.ProviderOptions, "provider-option", []string{}, "Provider option in the form KEY=VALUE")
buildCmd.Flags().BoolVar(&cmd.SkipDelete, "skip-delete", false, "If true will not delete the workspace after building it")
buildCmd.Flags().StringVar(&cmd.Machine, "machine", "", "The machine to use for this workspace. The machine needs to exist beforehand or the command will fail. If the workspace already exists, this option has no effect")
buildCmd.Flags().StringVar(&cmd.Repository, "repository", "", "The repository to push to")
buildCmd.Flags().StringSliceVar(&cmd.Platform, "platform", []string{}, "Set target platform for build")
buildCmd.Flags().BoolVar(&cmd.SkipPush, "skip-push", false, "If true will not push the image to the repository, useful for testing")
buildCmd.Flags().StringVar(&cmd.GitBranch, "git-branch", "", "The git branch to use")
buildCmd.Flags().StringVar(&cmd.GitCommit, "git-commit", "", "The git commit SHA to use")
buildCmd.Flags().Var(&cmd.GitCloneStrategy, "git-clone-strategy", "The git clone strategy DevPod uses to checkout git based workspaces. Can be full (default), blobless, treeless or shallow")
// TESTING
buildCmd.Flags().BoolVar(&cmd.ForceBuild, "force-build", false, "TESTING ONLY")
buildCmd.Flags().BoolVar(&cmd.ForceInternalBuildKit, "force-internal-buildkit", false, "TESTING ONLY")
_ = buildCmd.Flags().MarkHidden("force-build")
_ = buildCmd.Flags().MarkHidden("force-internal-buildkit")
return buildCmd
}
func (cmd *BuildCmd) Run(ctx context.Context, client client.WorkspaceClient) error {
// build workspace
err := cmd.build(ctx, client, log.Default)
if err != nil {
return err
}
return nil
}
func (cmd *BuildCmd) build(ctx context.Context, workspaceClient client.WorkspaceClient, log log.Logger) error {
err := workspaceClient.Lock(ctx)
if err != nil {
return err
}
defer workspaceClient.Unlock()
err = startWait(ctx, workspaceClient, true, log)
if err != nil {
return err
}
return cmd.buildAgentClient(ctx, workspaceClient, log)
}
func (cmd *BuildCmd) buildAgentClient(ctx context.Context, workspaceClient client.WorkspaceClient, log log.Logger) error {
// compress info
workspaceInfo, _, err := workspaceClient.AgentInfo(cmd.CLIOptions)
if err != nil {
return err
}
// create container etc.
log.Infof("Building devcontainer...")
defer log.Debugf("Done building devcontainer")
command := fmt.Sprintf("'%s' agent workspace build --workspace-info '%s'", workspaceClient.AgentPath(), workspaceInfo)
if log.GetLevel() == logrus.DebugLevel {
command += " --debug"
}
// create pipes
stdoutReader, stdoutWriter, err := os.Pipe()
if err != nil {
return err
}
stdinReader, stdinWriter, err := os.Pipe()
if err != nil {
return err
}
defer stdoutWriter.Close()
defer stdinWriter.Close()
// start machine on stdio
cancelCtx, cancel := context.WithCancel(ctx)
defer cancel()
errChan := make(chan error, 1)
go func() {
defer log.Debugf("Done executing up command")
defer cancel()
writer := log.ErrorStreamOnly().Writer(logrus.InfoLevel, false)
defer writer.Close()
errChan <- agent.InjectAgentAndExecute(cancelCtx, func(ctx context.Context, command string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
return workspaceClient.Command(ctx, client.CommandOptions{
Command: command,
Stdin: stdin,
Stdout: stdout,
Stderr: stderr,
})
}, workspaceClient.AgentLocal(), workspaceClient.AgentPath(), workspaceClient.AgentURL(), true, command, stdinReader, stdoutWriter, writer, log.ErrorStreamOnly())
}()
// create container etc.
_, err = tunnelserver.RunUpServer(
cancelCtx,
stdoutReader,
stdinWriter,
workspaceClient.AgentInjectGitCredentials(),
workspaceClient.AgentInjectDockerCredentials(),
workspaceClient.WorkspaceConfig(),
log,
)
if err != nil {
return errors.Wrap(err, "run tunnel machine")
}
// wait until command finished
return <-errChan
}