forked from sourcegraph/srclib
/
remote_import_build_data_cmd.go
166 lines (145 loc) · 4.29 KB
/
remote_import_build_data_cmd.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
package src
import (
"fmt"
"log"
"os"
"time"
"sourcegraph.com/sourcegraph/go-flags"
"sourcegraph.com/sourcegraph/go-sourcegraph/sourcegraph"
)
func initRemoteImportBuildCmd(remoteGroup *flags.Command) {
importBuildCmd, err := remoteGroup.AddCommand("import-build",
"tell a remote to import a build for a repository at a specific commit",
"The import-build command tells the remote to import build data for a repository at a specific commit. To import build data that was produced locally, first run `src build-data upload` (or run `src push`, which performs both steps).",
&remoteImportBuildCmd,
)
if err != nil {
log.Fatal(err)
}
if lrepo, err := openLocalRepo(); err == nil {
SetOptionDefaultValue(importBuildCmd.Group, "commit", lrepo.CommitID)
}
}
type RemoteImportBuildCmd struct {
CommitID string `short:"c" long:"commit" description:"commit ID of data to import" required:"yes"`
}
var remoteImportBuildCmd RemoteImportBuildCmd
func (c *RemoteImportBuildCmd) Execute(args []string) error {
cl := NewAPIClientWithAuthIfPresent()
if GlobalOpt.Verbose {
log.Printf("Creating a new import-only build for repo %q commit %q", remoteCmd.RepoURI, c.CommitID)
}
repo, _, err := cl.Repos.GetOrCreate(sourcegraph.RepoSpec{URI: remoteCmd.RepoURI}, nil)
if err != nil {
return err
}
repoSpec := sourcegraph.RepoSpec{URI: remoteCmd.RepoURI}
repoRevSpec := sourcegraph.RepoRevSpec{RepoSpec: repoSpec, Rev: c.CommitID}
// Resolve to the full commit ID, and ensure that the remote
// server knows about the commit.
commit, err := getCommitWithRefreshAndRetry(cl, repoRevSpec)
if err != nil {
return err
}
repoRevSpec.CommitID = string(commit.ID)
build, _, err := cl.Builds.Create(repoRevSpec, &sourcegraph.BuildCreateOptions{
BuildConfig: sourcegraph.BuildConfig{
Import: true,
Queue: false,
},
Force: true,
})
if err != nil {
return err
}
if GlobalOpt.Verbose {
log.Printf("Created build #%d", build.BID)
}
now := time.Now()
host := fmt.Sprintf("local (USER=%s)", os.Getenv("USER"))
buildUpdate := sourcegraph.BuildUpdate{StartedAt: &now, Host: &host}
if _, _, err := cl.Builds.Update(build.Spec(), buildUpdate); err != nil {
return err
}
importTask := &sourcegraph.BuildTask{
BID: build.BID,
Op: sourcegraph.ImportTaskOp,
Queue: true,
}
tasks, _, err := cl.Builds.CreateTasks(build.Spec(), []*sourcegraph.BuildTask{importTask})
if err != nil {
return err
}
importTask = tasks[0]
if GlobalOpt.Verbose {
log.Printf("Created import task #%d", importTask.TaskID)
}
// Stream logs.
done := make(chan struct{})
go func() {
var logOpt sourcegraph.BuildGetLogOptions
loopsSinceLastLog := 0
for {
select {
case <-done:
return
case <-time.After(time.Duration(loopsSinceLastLog+1) * 500 * time.Millisecond):
logs, _, err := cl.Builds.GetTaskLog(importTask.Spec(), &logOpt)
if err != nil {
log.Printf("Warning: failed to get build logs: %s.", err)
return
}
if len(logs.Entries) == 0 {
loopsSinceLastLog++
continue
}
logOpt.MinID = logs.MaxID
for _, e := range logs.Entries {
fmt.Println(e)
}
loopsSinceLastLog = 0
}
}
}()
defer func() {
done <- struct{}{}
}()
taskID := importTask.TaskID
started := false
log.Printf("# Import queued. Waiting for task #%d in build #%d to start...", importTask.TaskID, build.BID)
for i, start := 0, time.Now(); ; i++ {
if time.Since(start) > 45*time.Minute {
return fmt.Errorf("import timed out after %s", time.Since(start))
}
tasks, _, err := cl.Builds.ListBuildTasks(build.Spec(), nil)
if err != nil {
return err
}
importTask = nil
for _, task := range tasks {
if task.TaskID == taskID {
importTask = task
break
}
}
if importTask == nil {
return fmt.Errorf("task #%d not found in task list for build #%d", taskID, build.BID)
}
if !started && importTask.StartedAt.Valid {
log.Printf("# Import started.")
started = true
}
if importTask.EndedAt.Valid {
if importTask.Success {
log.Printf("# Import succeeded!")
} else if importTask.Failure {
log.Printf("# Import failed!")
}
break
}
time.Sleep(time.Duration(i) * 200 * time.Millisecond)
}
log.Printf("# View the repository at:")
log.Printf("# %s://%s/%s@%s", cl.BaseURL.Scheme, cl.BaseURL.Host, repo.URI, repoRevSpec.Rev)
return nil
}