Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/image-builder-bob/cmd/build.go
2498 views
1
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"errors"
9
"os"
10
"strings"
11
"time"
12
13
"github.com/spf13/cobra"
14
15
log "github.com/gitpod-io/gitpod/common-go/log"
16
"github.com/gitpod-io/gitpod/image-builder/bob/pkg/builder"
17
)
18
19
// buildCmd represents the build command
20
var buildCmd = &cobra.Command{
21
Use: "build",
22
Short: "Runs the image build and is configured using environment variables (see pkg/builder/config.go for details)",
23
Run: func(cmd *cobra.Command, args []string) {
24
log.Init("bob", "", true, true)
25
log := log.WithField("command", "build")
26
27
t0 := time.Now()
28
if os.Geteuid() != 0 {
29
log.Fatal("must run as root")
30
}
31
32
// give the headless listener some time to attach
33
time.Sleep(1 * time.Second)
34
35
cfg, err := builder.GetConfigFromEnv()
36
if err != nil {
37
if errors.Is(err, builder.DockerfilePathNotExists) {
38
dockerfilePath := strings.TrimPrefix(os.Getenv("BOB_DOCKERFILE_PATH"), "/workspace/")
39
err = os.WriteFile("/workspace/.gitpod/bob.log", []byte("could not find Dockerfile at \""+dockerfilePath+"\". Please double-check the value specified in image.file in .gitpod.yml"), 0644)
40
if err != nil {
41
log.WithError(err).Error("cannot write init message to /workspace/.gitpod/bob.log")
42
}
43
}
44
45
log.WithError(err).Fatal("cannot get config")
46
return
47
}
48
49
b := &builder.Builder{
50
Config: cfg,
51
}
52
err = b.Build()
53
if err != nil {
54
log.WithError(err).Error("build failed")
55
56
err := os.WriteFile("/workspace/.gitpod/bob.log", []byte(err.Error()), 0644)
57
if err != nil {
58
log.WithError(err).Error("cannot write error to /workspace/.gitpod/bob.log")
59
}
60
61
// make sure we're running long enough to have our logs read
62
if dt := time.Since(t0); dt < 5*time.Second {
63
time.Sleep(10 * time.Second)
64
}
65
66
os.Exit(1)
67
}
68
},
69
}
70
71
func init() {
72
rootCmd.AddCommand(buildCmd)
73
}
74
75