Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/dev/gpctl/cmd/imagebuilds-build-batch.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
"bufio"
9
"context"
10
"os"
11
"strings"
12
"sync"
13
"time"
14
15
"github.com/spf13/cobra"
16
17
"github.com/gitpod-io/gitpod/common-go/log"
18
builder "github.com/gitpod-io/gitpod/image-builder/api"
19
)
20
21
// imagebuildsBuildBatch represents the build command
22
23
var imagebuildsBuildBatch = &cobra.Command{
24
Use: "build-batch",
25
Short: "Builds workspace images from base-image refs read from STDIN",
26
Long: `Tip: re-build the workspace images of all workspaces started in the last 30 days.
27
mysql -N -B -u gitpod -p -h 127.0.0.1 gitpod -e 'SELECT ws.baseImageNameResolved FROM d_b_workspace_instance wsi LEFT JOIN d_b_workspace ws ON ws.id = workspaceId WHERE wsi.creationTime > (NOW() - INTERVAL 30 DAY)' | \
28
sort | \
29
uniq | \
30
gpctl imagebuilds build-batch
31
32
`,
33
Args: cobra.ExactArgs(0),
34
Run: func(cmd *cobra.Command, args []string) {
35
forceRebuild, _ := cmd.Flags().GetBool("force-rebuild")
36
37
ctx, cancel := context.WithCancel(context.Background())
38
39
conn, client, err := getImagebuildsClient(ctx)
40
if err != nil {
41
log.WithError(err).Fatal("cannot connect")
42
}
43
log.Info("connected to image-builder")
44
defer conn.Close()
45
46
timeBetweenBuilds, _ := cmd.Flags().GetInt("time-between-builds")
47
delay := time.Duration(timeBetweenBuilds) * time.Second
48
49
var wg sync.WaitGroup
50
scanner := bufio.NewScanner(os.Stdin)
51
for scanner.Scan() {
52
ref := strings.TrimSpace(scanner.Text())
53
if len(ref) == 0 {
54
continue
55
}
56
57
wg.Add(1)
58
go buildWorkspaceImage(&wg, ctx, client, forceRebuild, ref)
59
60
if delay > 0 {
61
time.Sleep(delay)
62
}
63
}
64
65
wg.Wait()
66
cancel()
67
},
68
}
69
70
func buildWorkspaceImage(wg *sync.WaitGroup, ctx context.Context, client builder.ImageBuilderClient, forceRebuild bool, ref string) {
71
defer wg.Done()
72
73
br, err := client.Build(ctx, &builder.BuildRequest{
74
Source: &builder.BuildSource{
75
From: &builder.BuildSource_Ref{
76
Ref: &builder.BuildSourceReference{
77
Ref: ref,
78
},
79
},
80
},
81
ForceRebuild: forceRebuild,
82
Auth: &builder.BuildRegistryAuth{
83
Mode: &builder.BuildRegistryAuth_Total{
84
Total: &builder.BuildRegistryAuthTotal{
85
AllowAll: true,
86
},
87
},
88
},
89
// TODO: this shouldn't be hard coded
90
SupervisorRef: "eu.gcr.io/gitpod-core-dev/build/supervisor:commit-4cb5b6b9c0e993f3964e978e387fb0e7c1c04276",
91
TriggeredBy: "c0f5dbf1-8d50-4d2a-8cd9-fe563fa53c71",
92
})
93
if err != nil {
94
log.WithField("ref", ref).WithError(err).Warn("cannot build workspace image")
95
return
96
}
97
98
r, err := br.Recv()
99
if err != nil {
100
log.WithField("ref", ref).WithError(err).Warn("cannot receive build response")
101
return
102
}
103
err = br.CloseSend()
104
if err != nil {
105
log.WithField("ref", ref).WithError(err).Warn("close send error")
106
}
107
108
switch r.Status {
109
case builder.BuildStatus_done_failure, builder.BuildStatus_done_success:
110
log.WithField("ref", ref).Infof("build done: %s, message: %s", builder.BuildStatus_name[int32(r.Status)], r.GetMessage())
111
case builder.BuildStatus_unknown:
112
log.WithField("ref", ref).Error("build status unknown")
113
case builder.BuildStatus_running:
114
log.WithField("ref", ref).Info("build running")
115
}
116
117
}
118
119
func init() {
120
imagebuildsCmd.AddCommand(imagebuildsBuildBatch)
121
122
imagebuildsBuildBatch.Flags().Bool("force-rebuild", false, "force an image build even if the image exists already")
123
imagebuildsBuildBatch.Flags().IntP("time-between-builds", "", 0, "wait N seconds between starting builds")
124
}
125
126