-
Notifications
You must be signed in to change notification settings - Fork 318
/
single.go
105 lines (90 loc) · 3.2 KB
/
single.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
package devcontainer
import (
"context"
"encoding/json"
"strings"
"github.com/loft-sh/devpod/pkg/devcontainer/config"
"github.com/loft-sh/devpod/pkg/devcontainer/metadata"
"github.com/pkg/errors"
)
func (r *Runner) runSingleContainer(parsedConfig *config.SubstitutedConfig, workspaceMount string, options UpOptions) (*config.Result, error) {
labels := r.getLabels()
containerDetails, err := r.Driver.FindDevContainer(context.TODO(), labels)
if err != nil {
return nil, errors.Wrap(err, "find dev container")
}
// does the container already exist?
var mergedConfig *config.MergedDevContainerConfig
if !options.Recreate && containerDetails != nil {
// start container if not running
if strings.ToLower(containerDetails.State.Status) != "running" {
err = r.Driver.StartDevContainer(context.TODO(), containerDetails.ID, labels)
if err != nil {
return nil, err
}
}
imageMetadataConfig, err := metadata.GetImageMetadataFromContainer(containerDetails, r.SubstitutionContext, r.Log)
if err != nil {
return nil, err
}
mergedConfig, err = config.MergeConfiguration(parsedConfig.Config, imageMetadataConfig.Config)
if err != nil {
return nil, errors.Wrap(err, "merge config")
}
} else {
// we need to build container
buildInfo, err := r.build(parsedConfig, config.BuildOptions{
PrebuildRepositories: options.PrebuildRepositories,
NoBuild: options.NoBuild,
})
if err != nil {
return nil, errors.Wrap(err, "build image")
}
// delete container on recreation
if options.Recreate {
err := r.Delete(labels, false)
if err != nil {
return nil, errors.Wrap(err, "delete devcontainer")
}
}
// merge configuration
mergedConfig, err = config.MergeConfiguration(parsedConfig.Config, buildInfo.ImageMetadata.Config)
if err != nil {
return nil, errors.Wrap(err, "merge config")
}
// add metadata as label here
marshalled, err := json.Marshal(buildInfo.ImageMetadata.Raw)
if err != nil {
return nil, errors.Wrap(err, "marshal config")
}
labels = append(labels, metadata.ImageMetadataLabel+"="+string(marshalled))
// run dev container
err = r.Driver.RunDevContainer(context.TODO(), parsedConfig.Config, mergedConfig, buildInfo.ImageName, workspaceMount, labels, r.WorkspaceConfig.Workspace.IDE.Name, r.WorkspaceConfig.Workspace.IDE.Options, buildInfo.ImageDetails)
if err != nil {
return nil, errors.Wrap(err, "start dev container")
}
//TODO: wait here a bit for correct startup?
// get container details
containerDetails, err = r.Driver.FindDevContainer(context.TODO(), labels)
if err != nil {
return nil, err
}
}
// substitute config with container env
newMergedConfig := &config.MergedDevContainerConfig{}
err = config.SubstituteContainerEnv(config.ListToObject(containerDetails.Config.Env), mergedConfig, newMergedConfig)
if err != nil {
return nil, errors.Wrap(err, "substitute container env")
}
// setup container
err = r.setupContainer(containerDetails, newMergedConfig)
if err != nil {
return nil, errors.Wrap(err, "setup container")
}
// return result
return &config.Result{
ContainerDetails: containerDetails,
MergedConfig: newMergedConfig,
SubstitutionContext: r.SubstitutionContext,
}, nil
}