-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathruntime.go
More file actions
406 lines (353 loc) · 12.6 KB
/
runtime.go
File metadata and controls
406 lines (353 loc) · 12.6 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package runtime_runbit
import (
"fmt"
"net/url"
"os"
"strings"
anaConsts "github.com/ActiveState/cli/internal/analytics/constants"
"github.com/ActiveState/cli/internal/analytics/dimensions"
"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/instanceid"
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/logging"
configMediator "github.com/ActiveState/cli/internal/mediators/config"
"github.com/ActiveState/cli/internal/multilog"
"github.com/ActiveState/cli/internal/osutils"
"github.com/ActiveState/cli/internal/output"
"github.com/ActiveState/cli/internal/primer"
"github.com/ActiveState/cli/internal/rtutils"
"github.com/ActiveState/cli/internal/rtutils/ptr"
buildscript_runbit "github.com/ActiveState/cli/internal/runbits/buildscript"
"github.com/ActiveState/cli/internal/runbits/checkout"
"github.com/ActiveState/cli/internal/runbits/rationalize"
"github.com/ActiveState/cli/internal/runbits/runtime/progress"
"github.com/ActiveState/cli/internal/runbits/runtime/trigger"
"github.com/ActiveState/cli/pkg/buildplan"
"github.com/ActiveState/cli/pkg/localcommit"
"github.com/ActiveState/cli/pkg/platform/api"
"github.com/ActiveState/cli/pkg/platform/model"
bpModel "github.com/ActiveState/cli/pkg/platform/model/buildplanner"
"github.com/ActiveState/cli/pkg/project"
"github.com/ActiveState/cli/pkg/runtime"
"github.com/ActiveState/cli/pkg/runtime/events"
"github.com/ActiveState/cli/pkg/runtime_helpers"
"github.com/ActiveState/cli/pkg/sysinfo"
"github.com/go-openapi/strfmt"
"golang.org/x/net/context"
)
func init() {
configMediator.RegisterHiddenOption(constants.AsyncRuntimeConfig, configMediator.Bool, false)
}
type Opts struct {
PrintHeaders bool
TargetDir string
// Note CommitID and Commit are mutually exclusive. If Commit is provided then CommitID is disregarded.
// Also, Archive and Commit are mutually exclusive, as both contain a BuildPlan.
CommitID strfmt.UUID
Commit *bpModel.Commit
Archive *checkout.Archive
ValidateBuildscript bool
IgnoreAsync bool
}
type SetOpt func(*Opts)
func WithoutHeaders() SetOpt {
return func(opts *Opts) {
opts.PrintHeaders = false
}
}
func WithTargetDir(targetDir string) SetOpt {
return func(opts *Opts) {
opts.TargetDir = targetDir
}
}
func WithCommit(commit *bpModel.Commit) SetOpt {
return func(opts *Opts) {
opts.Commit = commit
}
}
func WithCommitID(commitID strfmt.UUID) SetOpt {
return func(opts *Opts) {
opts.CommitID = commitID
}
}
// WithoutBuildscriptValidation skips validating whether the local buildscript has changed. This is useful when trying
// to source a runtime that doesn't yet reflect the state of the project files (ie. as.yaml and buildscript).
func WithoutBuildscriptValidation() SetOpt {
return func(opts *Opts) {
opts.ValidateBuildscript = false
}
}
func WithArchive(archive *checkout.Archive) SetOpt {
return func(opts *Opts) {
opts.Archive = archive
}
}
func WithIgnoreAsync() SetOpt {
return func(opts *Opts) {
opts.IgnoreAsync = true
}
}
type primeable interface {
primer.Projecter
primer.Auther
primer.Outputer
primer.Configurer
primer.SvcModeler
primer.Analyticer
}
func Update(
prime primeable,
trigger trigger.Trigger,
setOpts ...SetOpt,
) (_ *runtime.Runtime, rerr error) {
defer rationalizeUpdateError(prime, &rerr)
opts := &Opts{
PrintHeaders: true,
ValidateBuildscript: true,
}
for _, setOpt := range setOpts {
setOpt(opts)
}
proj := prime.Project()
if proj == nil {
return nil, rationalize.ErrNoProject
}
if proj.IsHeadless() {
return nil, rationalize.ErrHeadless
}
targetDir := opts.TargetDir
if targetDir == "" {
targetDir = runtime_helpers.TargetDirFromProject(proj)
}
rt, err := runtime.New(targetDir)
if err != nil {
return nil, errs.Wrap(err, "Could not initialize runtime")
}
commitID := opts.CommitID
if opts.Commit != nil {
commitID = opts.Commit.CommitID
}
if commitID == "" {
commitID, err = localcommit.Get(proj.Dir())
if err != nil {
return nil, errs.Wrap(err, "Failed to get local commit")
}
}
ah, err := newAnalyticsHandler(prime, trigger, commitID)
if err != nil {
return nil, errs.Wrap(err, "Could not create event handler")
}
// Runtime debugging encapsulates more than just sourcing of the runtime, so we handle some of these events
// external from the runtime event handling.
ah.fire(anaConsts.CatRuntimeDebug, anaConsts.ActRuntimeStart, nil)
defer func() {
if rerr == nil {
ah.fire(anaConsts.CatRuntimeDebug, anaConsts.ActRuntimeSuccess, nil)
} else {
ah.fireFailure(rerr)
}
}()
rtHash, err := runtime_helpers.Hash(proj, &commitID)
if err != nil {
ah.fire(anaConsts.CatRuntimeDebug, anaConsts.ActRuntimeCache, nil)
return nil, errs.Wrap(err, "Failed to get runtime hash")
}
if opts.PrintHeaders {
prime.Output().Notice(output.Title(locale.T("install_runtime")))
}
if rt.Hash() == rtHash {
prime.Output().Notice(locale.T("pkg_already_uptodate"))
return rt, nil
}
var buildPlan *buildplan.BuildPlan
commit := opts.Commit
switch {
case opts.Archive != nil:
buildPlan = opts.Archive.BuildPlan
case commit != nil:
buildPlan = commit.BuildPlan()
default:
// Solve
solveSpinner := output.StartSpinner(prime.Output(), locale.T("progress_solve"), constants.TerminalAnimationInterval)
bpm := bpModel.NewBuildPlannerModel(prime.Auth(), prime.SvcModel())
commit, err = bpm.FetchCommit(commitID, proj.Owner(), proj.Name(), nil)
if err != nil {
solveSpinner.Stop(locale.T("progress_fail"))
return nil, errs.Wrap(err, "Failed to fetch build result")
}
buildPlan = commit.BuildPlan()
solveSpinner.Stop(locale.T("progress_success"))
}
// Validate buildscript
if prime.Config().GetBool(constants.OptinBuildscriptsConfig) && opts.ValidateBuildscript && os.Getenv(constants.DisableBuildscriptDirtyCheck) != "true" {
bs, err := buildscript_runbit.ScriptFromProject(proj)
if err != nil {
return nil, errs.Wrap(err, "Failed to get buildscript")
}
isClean, err := bs.Equals(commit.BuildScript())
if err != nil {
return nil, errs.Wrap(err, "Failed to compare buildscript")
}
if !isClean {
return nil, ErrBuildScriptNeedsCommit
}
}
// Async runtimes should still do everything up to the actual update itself, because we still want to raise
// any errors regarding solves, buildscripts, etc.
if prime.Config().GetBool(constants.AsyncRuntimeConfig) && !opts.IgnoreAsync {
logging.Debug("Skipping runtime update due to async runtime")
prime.Output().Notice("") // blank line
prime.Output().Notice(locale.Tr("notice_async_runtime", constants.AsyncRuntimeConfig))
return rt, nil
}
// Determine if this runtime is currently in use.
ctx, cancel := context.WithTimeout(context.Background(), model.SvcTimeoutMinimal)
defer cancel()
if procs, err := prime.SvcModel().GetProcessesInUse(ctx, rt.Env(false).ExecutorsPath); err == nil {
if len(procs) > 0 {
list := []string{}
for _, proc := range procs {
list = append(list, fmt.Sprintf(" - %s (process: %d)", proc.Exe, proc.Pid))
}
prime.Output().Notice(locale.Tr("runtime_setup_in_use_warning", strings.Join(list, "\n")))
}
} else {
multilog.Error("Unable to determine if runtime is in use: %v", errs.JoinMessage(err))
}
pg := progress.NewRuntimeProgressIndicator(prime.Output())
defer rtutils.Closer(pg.Close, &rerr)
rtOpts := []runtime.SetOpt{
runtime.WithAnnotations(proj.Owner(), proj.Name(), commitID),
runtime.WithEventHandlers(pg.Handle, ah.handle),
runtime.WithPreferredLibcVersion(prime.Config().GetString(constants.PreferredGlibcVersionConfig)),
}
if opts.Archive != nil {
rtOpts = append(rtOpts, runtime.WithArchive(opts.Archive.Dir, opts.Archive.PlatformID, checkout.ArtifactExt))
}
if buildPlan.IsBuildInProgress() {
// Build progress URL is of the form
// https://<host>/<owner>/<project>/distributions?branch=<branch>&commitID=<commitID>
host := constants.DefaultAPIHost
if hostOverride := api.HostOverride(); hostOverride != "" {
host = hostOverride
}
path, err := url.JoinPath(proj.Owner(), proj.Name(), constants.BuildProgressUrlPathName)
if err != nil {
return nil, errs.Wrap(err, "Could not construct progress url path")
}
u := &url.URL{Scheme: "https", Host: host, Path: path}
q := u.Query()
q.Set("branch", proj.BranchName())
q.Set("commitID", commitID.String())
u.RawQuery = q.Encode()
rtOpts = append(rtOpts, runtime.WithBuildProgressUrl(u.String()))
}
if proj.IsPortable() {
rtOpts = append(rtOpts, runtime.WithPortable())
}
rtOpts = append(rtOpts, runtime.WithCacheSize(prime.Config().GetInt(constants.RuntimeCacheSizeConfigKey)))
if isArmPlatform(buildPlan) {
prime.Output().Notice(locale.Tl("warning_arm_unstable", "[WARNING]Warning:[/RESET] You are using an ARM64 architecture, which is currently unstable. While it may work, you might encounter issues."))
}
if err := rt.Update(buildPlan, rtHash, rtOpts...); err != nil {
return nil, locale.WrapError(err, "err_packages_update_runtime_install")
}
return rt, nil
}
func isArmPlatform(buildPlan *buildplan.BuildPlan) bool {
if sysinfo.OS() != sysinfo.Linux || sysinfo.Architecture() != sysinfo.Arm {
return false // only warn when using an ARM runtime on an ARM machine
}
platformID, err := model.FilterCurrentPlatform(sysinfo.OS().String(), buildPlan.Platforms(), "")
if err != nil {
// Note: do not log this as an error because it's likely the buildplan does not have an ARM
// platform configured.
logging.Debug("Unable to filter current platform: %v", err)
return false
}
platforms, err := model.FetchPlatforms()
if err != nil {
multilog.Error("Unable to fetch platforms: %v", err)
return false
}
for _, platform := range platforms {
if platform.PlatformID != nil && *platform.PlatformID == platformID {
return true
}
}
return false
}
type analyticsHandler struct {
prime primeable
trigger trigger.Trigger
commitID strfmt.UUID
dimensionJson string
errorStage string
}
func newAnalyticsHandler(prime primeable, trig trigger.Trigger, commitID strfmt.UUID) (*analyticsHandler, error) {
h := &analyticsHandler{prime, trig, commitID, "", ""}
dims := h.dimensions()
dimsJson, err := dims.Marshal()
if err != nil {
return nil, errs.Wrap(err, "Could not marshal dimensions")
}
h.dimensionJson = dimsJson
return h, nil
}
func (h *analyticsHandler) fire(category, action string, dimensions *dimensions.Values) {
if dimensions == nil {
dimensions = h.dimensions()
}
h.prime.Analytics().Event(category, action, dimensions)
}
func (h *analyticsHandler) fireFailure(err error) {
errorType := h.errorStage
if errorType == "" {
errorType = "unknown"
if locale.IsInputError(err) {
errorType = "input"
}
}
dims := h.dimensions()
dims.Error = ptr.To(errorType)
dims.Message = ptr.To(errs.JoinMessage(err))
h.prime.Analytics().Event(anaConsts.CatRuntimeDebug, anaConsts.ActRuntimeFailure, dims)
}
func (h *analyticsHandler) dimensions() *dimensions.Values {
return &dimensions.Values{
Trigger: ptr.To(h.trigger.String()),
CommitID: ptr.To(h.commitID.String()),
ProjectNameSpace: ptr.To(project.NewNamespace(h.prime.Project().Owner(), h.prime.Project().Name(), h.commitID.String()).String()),
InstanceID: ptr.To(instanceid.ID()),
}
}
func (h *analyticsHandler) handle(event events.Event) error {
switch event.(type) {
case events.Start:
h.prime.Analytics().Event(anaConsts.CatRuntimeUsage, anaConsts.ActRuntimeAttempt, h.dimensions())
case events.Success:
if err := h.prime.SvcModel().ReportRuntimeUsage(context.Background(), os.Getpid(), osutils.Executable(), anaConsts.SrcStateTool, h.dimensionJson); err != nil {
multilog.Critical("Could not report runtime usage: %s", errs.JoinMessage(err))
}
case events.ArtifactBuildFailure:
h.errorStage = anaConsts.ActRuntimeBuild
h.fire(anaConsts.CatRuntimeDebug, anaConsts.ActRuntimeBuild, nil)
case events.ArtifactDownloadFailure:
h.errorStage = anaConsts.ActRuntimeDownload
h.fire(anaConsts.CatRuntimeDebug, anaConsts.ActRuntimeDownload, nil)
case events.ArtifactUnpackFailure:
h.errorStage = anaConsts.ActRuntimeUnpack
h.fire(anaConsts.CatRuntimeDebug, anaConsts.ActRuntimeUnpack, nil)
case events.ArtifactInstallFailure:
h.errorStage = anaConsts.ActRuntimeInstall
h.fire(anaConsts.CatRuntimeDebug, anaConsts.ActRuntimeInstall, nil)
case events.ArtifactUninstallFailure:
h.errorStage = anaConsts.ActRuntimeUninstall
h.fire(anaConsts.CatRuntimeDebug, anaConsts.ActRuntimeUninstall, nil)
case events.PostProcessFailure:
h.errorStage = anaConsts.ActRuntimePostprocess
h.fire(anaConsts.CatRuntimeDebug, anaConsts.ActRuntimePostprocess, nil)
}
return nil
}