Path: blob/main/components/image-builder-bob/cmd/build.go
2498 views
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package cmd56import (7"errors"8"os"9"strings"10"time"1112"github.com/spf13/cobra"1314log "github.com/gitpod-io/gitpod/common-go/log"15"github.com/gitpod-io/gitpod/image-builder/bob/pkg/builder"16)1718// buildCmd represents the build command19var buildCmd = &cobra.Command{20Use: "build",21Short: "Runs the image build and is configured using environment variables (see pkg/builder/config.go for details)",22Run: func(cmd *cobra.Command, args []string) {23log.Init("bob", "", true, true)24log := log.WithField("command", "build")2526t0 := time.Now()27if os.Geteuid() != 0 {28log.Fatal("must run as root")29}3031// give the headless listener some time to attach32time.Sleep(1 * time.Second)3334cfg, err := builder.GetConfigFromEnv()35if err != nil {36if errors.Is(err, builder.DockerfilePathNotExists) {37dockerfilePath := strings.TrimPrefix(os.Getenv("BOB_DOCKERFILE_PATH"), "/workspace/")38err = 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)39if err != nil {40log.WithError(err).Error("cannot write init message to /workspace/.gitpod/bob.log")41}42}4344log.WithError(err).Fatal("cannot get config")45return46}4748b := &builder.Builder{49Config: cfg,50}51err = b.Build()52if err != nil {53log.WithError(err).Error("build failed")5455err := os.WriteFile("/workspace/.gitpod/bob.log", []byte(err.Error()), 0644)56if err != nil {57log.WithError(err).Error("cannot write error to /workspace/.gitpod/bob.log")58}5960// make sure we're running long enough to have our logs read61if dt := time.Since(t0); dt < 5*time.Second {62time.Sleep(10 * time.Second)63}6465os.Exit(1)66}67},68}6970func init() {71rootCmd.AddCommand(buildCmd)72}737475