diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a56c58cc93..1a34eed02e 100755 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -45,6 +45,7 @@ jobs: fail-fast: false runs-on: ${{ matrix.platform }} env: + ACTIVESTATE_CI: true ACTIVESTATE_CLI_DISABLE_RUNTIME: true SHELL: bash GITHUB_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} @@ -388,6 +389,7 @@ jobs: - os_specific runs-on: ubuntu-20.04 env: + ACTIVESTATE_CI: true ACTIVESTATE_CLI_DISABLE_RUNTIME: true SHELL: bash GITHUB_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/propagate.yml b/.github/workflows/propagate.yml index a3925a939d..b5d5048b08 100644 --- a/.github/workflows/propagate.yml +++ b/.github/workflows/propagate.yml @@ -18,6 +18,7 @@ jobs: go-version: - 1.20.x env: + ACTIVESTATE_CI: true ACTIVESTATE_CLI_DISABLE_RUNTIME: true SHELL: bash GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 96e7d573ce..188adc8487 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -20,6 +20,7 @@ jobs: go-version: - 1.20.x env: + ACTIVESTATE_CI: true ACTIVESTATE_CLI_DISABLE_RUNTIME: true SHELL: bash GITHUB_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/verify.yml b/.github/workflows/verify.yml index 6788479d3d..78fde2fe8b 100644 --- a/.github/workflows/verify.yml +++ b/.github/workflows/verify.yml @@ -17,6 +17,7 @@ jobs: name: Target & Verify PR runs-on: ubuntu-20.04 env: + ACTIVESTATE_CI: true ACTIVESTATE_CLI_DISABLE_RUNTIME: true SHELL: bash GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/changelog.md b/changelog.md index bfc3292299..162340cff7 100644 --- a/changelog.md +++ b/changelog.md @@ -6,6 +6,22 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +### 0.40.1 + +### Added + +* State tool will now warn users if its executables are deleted during + installation, indicating a false-positive action from antivirus software. + +### Fixed + +* Fixed auto updates not being run (if you are on an older version: + run `state update`). +* Fixed a rare parsing panic that would happen when running particularly complex + builds. +* Fixed race condition during artifact installation that could lead to errors + like "Could not unpack artifact .. file already exists". + ### 0.40.0 ### Added diff --git a/cmd/state-installer/cmd.go b/cmd/state-installer/cmd.go index a69b8b564b..462444eff5 100644 --- a/cmd/state-installer/cmd.go +++ b/cmd/state-installer/cmd.go @@ -12,6 +12,7 @@ import ( "github.com/ActiveState/cli/internal/analytics" "github.com/ActiveState/cli/internal/analytics/client/sync" + anaConst "github.com/ActiveState/cli/internal/analytics/constants" "github.com/ActiveState/cli/internal/captain" "github.com/ActiveState/cli/internal/config" "github.com/ActiveState/cli/internal/constants" @@ -37,9 +38,6 @@ import ( "golang.org/x/crypto/ssh/terminal" ) -const AnalyticsCat = "installer" -const AnalyticsFunnelCat = "installer-funnel" - type Params struct { sourceInstaller string path string @@ -125,8 +123,8 @@ func main() { logging.Debug("Original Args: %v", os.Args) logging.Debug("Processed Args: %v", processedArgs) - an = sync.New(cfg, nil, out) - an.Event(AnalyticsFunnelCat, "start") + an = sync.New(anaConst.SrcStateInstaller, cfg, nil, out) + an.Event(anaConst.CatInstallerFunnel, "start") params := newParams() cmd := captain.NewCommand( @@ -193,30 +191,30 @@ func main() { }, ) - an.Event(AnalyticsFunnelCat, "pre-exec") + an.Event(anaConst.CatInstallerFunnel, "pre-exec") err = cmd.Execute(processedArgs[1:]) if err != nil { errors.ReportError(err, cmd, an) if locale.IsInputError(err) { - an.EventWithLabel(AnalyticsCat, "input-error", errs.JoinMessage(err)) + an.EventWithLabel(anaConst.CatInstaller, "input-error", errs.JoinMessage(err)) logging.Debug("Installer input error: " + errs.JoinMessage(err)) } else { - an.EventWithLabel(AnalyticsCat, "error", errs.JoinMessage(err)) + an.EventWithLabel(anaConst.CatInstaller, "error", errs.JoinMessage(err)) multilog.Critical("Installer error: " + errs.JoinMessage(err)) } - an.EventWithLabel(AnalyticsFunnelCat, "fail", errs.JoinMessage(err)) + an.EventWithLabel(anaConst.CatInstallerFunnel, "fail", errs.JoinMessage(err)) exitCode, err = errors.ParseUserFacing(err) if err != nil { out.Error(err) } } else { - an.Event(AnalyticsFunnelCat, "success") + an.Event(anaConst.CatInstallerFunnel, "success") } } func execute(out output.Outputer, cfg *config.Instance, an analytics.Dispatcher, args []string, params *Params) error { - an.Event(AnalyticsFunnelCat, "exec") + an.Event(anaConst.CatInstallerFunnel, "exec") if params.path == "" { var err error @@ -274,13 +272,13 @@ func execute(out output.Outputer, cfg *config.Instance, an analytics.Dispatcher, if params.isUpdate { route = "update" } - an.Event(AnalyticsFunnelCat, route) + an.Event(anaConst.CatInstallerFunnel, route) // Check if state tool already installed if !params.isUpdate && !params.force && stateToolInstalled && !targetingSameBranch { logging.Debug("Cancelling out because State Tool is already installed") out.Print(fmt.Sprintf("State Tool Package Manager is already installed at [NOTICE]%s[/RESET]. To reinstall use the [ACTIONABLE]--force[/RESET] flag.", installPath)) - an.Event(AnalyticsFunnelCat, "already-installed") + an.Event(anaConst.CatInstallerFunnel, "already-installed") params.isUpdate = true return postInstallEvents(out, cfg, an, params) } @@ -295,7 +293,7 @@ func execute(out output.Outputer, cfg *config.Instance, an analytics.Dispatcher, // installOrUpdateFromLocalSource is invoked when we're performing an installation where the payload is already provided func installOrUpdateFromLocalSource(out output.Outputer, cfg *config.Instance, an analytics.Dispatcher, payloadPath string, params *Params) error { logging.Debug("Install from local source") - an.Event(AnalyticsFunnelCat, "local-source") + an.Event(anaConst.CatInstallerFunnel, "local-source") if !params.isUpdate { // install.sh or install.ps1 downloaded this installer and is running it. out.Print(output.Title("Installing State Tool Package Manager")) @@ -324,12 +322,12 @@ func installOrUpdateFromLocalSource(out output.Outputer, cfg *config.Instance, a } // Run installer - an.Event(AnalyticsFunnelCat, "pre-installer") + an.Event(anaConst.CatInstallerFunnel, "pre-installer") if err := installer.Install(); err != nil { out.Print("[ERROR]x Failed[/RESET]") return err } - an.Event(AnalyticsFunnelCat, "post-installer") + an.Event(anaConst.CatInstallerFunnel, "post-installer") out.Print("[SUCCESS]✔ Done[/RESET]") if !params.isUpdate { @@ -342,7 +340,7 @@ func installOrUpdateFromLocalSource(out output.Outputer, cfg *config.Instance, a } func postInstallEvents(out output.Outputer, cfg *config.Instance, an analytics.Dispatcher, params *Params) error { - an.Event(AnalyticsFunnelCat, "post-install-events") + an.Event(anaConst.CatInstallerFunnel, "post-install-events") installPath, err := resolveInstallPath(params.path) if err != nil { @@ -368,30 +366,30 @@ func postInstallEvents(out output.Outputer, cfg *config.Instance, an analytics.D switch { // Execute provided --command case params.command != "": - an.Event(AnalyticsFunnelCat, "forward-command") + an.Event(anaConst.CatInstallerFunnel, "forward-command") out.Print(fmt.Sprintf("\nRunning `[ACTIONABLE]%s[/RESET]`\n", params.command)) cmd, args := exeutils.DecodeCmd(params.command) if _, _, err := exeutils.ExecuteAndPipeStd(cmd, args, envSlice(binPath)); err != nil { - an.EventWithLabel(AnalyticsFunnelCat, "forward-command-err", err.Error()) + an.EventWithLabel(anaConst.CatInstallerFunnel, "forward-command-err", err.Error()) return errs.Silence(errs.Wrap(err, "Running provided command failed, error returned: %s", errs.JoinMessage(err))) } // Activate provided --activate Namespace case params.activate.IsValid(): - an.Event(AnalyticsFunnelCat, "forward-activate") + an.Event(anaConst.CatInstallerFunnel, "forward-activate") out.Print(fmt.Sprintf("\nRunning `[ACTIONABLE]state activate %s[/RESET]`\n", params.activate.String())) if _, _, err := exeutils.ExecuteAndPipeStd(stateExe, []string{"activate", params.activate.String()}, envSlice(binPath)); err != nil { - an.EventWithLabel(AnalyticsFunnelCat, "forward-activate-err", err.Error()) + an.EventWithLabel(anaConst.CatInstallerFunnel, "forward-activate-err", err.Error()) return errs.Silence(errs.Wrap(err, "Could not activate %s, error returned: %s", params.activate.String(), errs.JoinMessage(err))) } // Activate provided --activate-default Namespace case params.activateDefault.IsValid(): - an.Event(AnalyticsFunnelCat, "forward-activate-default") + an.Event(anaConst.CatInstallerFunnel, "forward-activate-default") out.Print(fmt.Sprintf("\nRunning `[ACTIONABLE]state activate --default %s[/RESET]`\n", params.activateDefault.String())) if _, _, err := exeutils.ExecuteAndPipeStd(stateExe, []string{"activate", params.activateDefault.String(), "--default"}, envSlice(binPath)); err != nil { - an.EventWithLabel(AnalyticsFunnelCat, "forward-activate-default-err", err.Error()) + an.EventWithLabel(anaConst.CatInstallerFunnel, "forward-activate-default-err", err.Error()) return errs.Silence(errs.Wrap(err, "Could not activate %s, error returned: %s", params.activateDefault.String(), errs.JoinMessage(err))) } case !params.isUpdate && terminal.IsTerminal(int(os.Stdin.Fd())) && os.Getenv(constants.InstallerNoSubshell) != "true" && os.Getenv("TERM") != "dumb": diff --git a/cmd/state-offline-installer/main.go b/cmd/state-offline-installer/main.go index 89dc821608..82b8cd86e7 100644 --- a/cmd/state-offline-installer/main.go +++ b/cmd/state-offline-installer/main.go @@ -8,6 +8,7 @@ import ( "github.com/ActiveState/cli/internal/analytics" "github.com/ActiveState/cli/internal/analytics/client/sync" + anaConst "github.com/ActiveState/cli/internal/analytics/constants" "github.com/ActiveState/cli/internal/captain" "github.com/ActiveState/cli/internal/config" "github.com/ActiveState/cli/internal/constants" @@ -78,7 +79,7 @@ func main() { return } - an = sync.New(cfg, nil, out) + an = sync.New(anaConst.SrcOfflineInstaller, cfg, nil, out) prime := primer.New( nil, out, nil, diff --git a/cmd/state-offline-uninstaller/main.go b/cmd/state-offline-uninstaller/main.go index 822e831dfb..0ba17527c5 100644 --- a/cmd/state-offline-uninstaller/main.go +++ b/cmd/state-offline-uninstaller/main.go @@ -8,6 +8,7 @@ import ( "github.com/ActiveState/cli/internal/analytics" "github.com/ActiveState/cli/internal/analytics/client/sync" + anaConst "github.com/ActiveState/cli/internal/analytics/constants" "github.com/ActiveState/cli/internal/captain" "github.com/ActiveState/cli/internal/config" "github.com/ActiveState/cli/internal/constants" @@ -78,7 +79,7 @@ func main() { return } - an = sync.New(cfg, nil, out) + an = sync.New(anaConst.SrcOfflineInstaller, cfg, nil, out) prime := primer.New( nil, out, nil, diff --git a/cmd/state-remote-installer/main.go b/cmd/state-remote-installer/main.go index 36ec3eca7a..bf2f913a43 100644 --- a/cmd/state-remote-installer/main.go +++ b/cmd/state-remote-installer/main.go @@ -10,6 +10,7 @@ import ( "github.com/ActiveState/cli/internal/analytics" "github.com/ActiveState/cli/internal/analytics/client/sync" + anaConst "github.com/ActiveState/cli/internal/analytics/constants" "github.com/ActiveState/cli/internal/captain" "github.com/ActiveState/cli/internal/config" "github.com/ActiveState/cli/internal/constants" @@ -94,7 +95,7 @@ func main() { return } - an = sync.New(cfg, nil, out) + an = sync.New(anaConst.SrcStateRemoteInstaller, cfg, nil, out) // Set up prompter prompter := prompt.New(true, an) diff --git a/cmd/state-svc/autostart/autostart.go b/cmd/state-svc/autostart/autostart.go index b979d89ed9..245843201d 100644 --- a/cmd/state-svc/autostart/autostart.go +++ b/cmd/state-svc/autostart/autostart.go @@ -13,7 +13,7 @@ import ( var Options = autostart.Options{ Name: constants.SvcAppName, LaunchFileName: constants.SvcLaunchFileName, - Args: []string{"start"}, + Args: []string{"start", "--autostart"}, } func RegisterConfigListener(cfg *config.Instance) error { diff --git a/cmd/state-svc/internal/resolver/resolver.go b/cmd/state-svc/internal/resolver/resolver.go index 93714f827a..055586e00f 100644 --- a/cmd/state-svc/internal/resolver/resolver.go +++ b/cmd/state-svc/internal/resolver/resolver.go @@ -74,7 +74,9 @@ func New(cfg *config.Instance, an *sync.Client, auth *authentication.Auth) (*Res usageChecker := rtusage.NewChecker(cfg, auth) - anForClient := sync.New(cfg, auth, nil) + // Note: source does not matter here, as analytics sent via the resolver have a source + // (e.g. State Tool or Executor), and that source will be used. + anForClient := sync.New(anaConsts.SrcStateTool, cfg, auth, nil) return &Resolver{ cfg, msg, @@ -184,10 +186,10 @@ func (r *Resolver) Projects(ctx context.Context) ([]*graph.Project, error) { return projects, nil } -func (r *Resolver) AnalyticsEvent(_ context.Context, category, action string, _label *string, dimensionsJson string) (*graph.AnalyticsEventResponse, error) { +func (r *Resolver) AnalyticsEvent(_ context.Context, category, action, source string, _label *string, dimensionsJson string) (*graph.AnalyticsEventResponse, error) { defer func() { handlePanics(recover(), debug.Stack()) }() - logging.Debug("Analytics event resolver: %s - %s", category, action) + logging.Debug("Analytics event resolver: %s - %s (%s)", category, action, source) label := "" if _label != nil { @@ -213,12 +215,12 @@ func (r *Resolver) AnalyticsEvent(_ context.Context, category, action string, _l return nil }) - r.anForClient.EventWithLabel(category, action, label, dims) + r.anForClient.EventWithSourceAndLabel(category, action, source, label, dims) return &graph.AnalyticsEventResponse{Sent: true}, nil } -func (r *Resolver) ReportRuntimeUsage(_ context.Context, pid int, exec string, dimensionsJSON string) (*graph.ReportRuntimeUsageResponse, error) { +func (r *Resolver) ReportRuntimeUsage(_ context.Context, pid int, exec, source string, dimensionsJSON string) (*graph.ReportRuntimeUsageResponse, error) { defer func() { handlePanics(recover(), debug.Stack()) }() logging.Debug("Runtime usage resolver: %d - %s", pid, exec) @@ -227,7 +229,7 @@ func (r *Resolver) ReportRuntimeUsage(_ context.Context, pid int, exec string, d return &graph.ReportRuntimeUsageResponse{Received: false}, errs.Wrap(err, "Could not unmarshal") } - r.rtwatch.Watch(pid, exec, dims) + r.rtwatch.Watch(pid, exec, source, dims) return &graph.ReportRuntimeUsageResponse{Received: true}, nil } diff --git a/cmd/state-svc/internal/rtwatcher/entry.go b/cmd/state-svc/internal/rtwatcher/entry.go index 78275ad703..7626e57af4 100644 --- a/cmd/state-svc/internal/rtwatcher/entry.go +++ b/cmd/state-svc/internal/rtwatcher/entry.go @@ -11,9 +11,10 @@ import ( ) type entry struct { - PID int `json:"pid"` - Exec string `json:"exec"` - Dims *dimensions.Values `json:"dims"` + PID int `json:"pid"` + Exec string `json:"exec"` + Source string `json:"source"` + Dims *dimensions.Values `json:"dims"` } func (e entry) IsRunning() (bool, error) { diff --git a/cmd/state-svc/internal/rtwatcher/watcher.go b/cmd/state-svc/internal/rtwatcher/watcher.go index 60634c7040..dac20f12b2 100644 --- a/cmd/state-svc/internal/rtwatcher/watcher.go +++ b/cmd/state-svc/internal/rtwatcher/watcher.go @@ -30,7 +30,7 @@ type Watcher struct { } type analytics interface { - Event(category string, action string, dim ...*dimensions.Values) + EventWithSource(category, action, source string, dim ...*dimensions.Values) } func New(cfg *config.Instance, an analytics) *Watcher { @@ -97,7 +97,7 @@ func (w *Watcher) check() { func (w *Watcher) RecordUsage(e entry) { logging.Debug("Recording usage of %s (%d)", e.Exec, e.PID) - w.an.Event(anaConst.CatRuntimeUsage, anaConst.ActRuntimeHeartbeat, e.Dims) + w.an.EventWithSource(anaConst.CatRuntimeUsage, anaConst.ActRuntimeHeartbeat, e.Source, e.Dims) } func (w *Watcher) Close() error { @@ -116,10 +116,10 @@ func (w *Watcher) Close() error { return nil } -func (w *Watcher) Watch(pid int, exec string, dims *dimensions.Values) { +func (w *Watcher) Watch(pid int, exec, source string, dims *dimensions.Values) { logging.Debug("Watching %s (%d)", exec, pid) dims.Sequence = ptr.To(-1) // sequence is meaningless for heartbeat events - e := entry{pid, exec, dims} + e := entry{pid, exec, source, dims} w.watching = append(w.watching, e) go w.RecordUsage(e) // initial event } diff --git a/cmd/state-svc/internal/server/generated/generated.go b/cmd/state-svc/internal/server/generated/generated.go index b454d6f934..59cc4344fc 100644 --- a/cmd/state-svc/internal/server/generated/generated.go +++ b/cmd/state-svc/internal/server/generated/generated.go @@ -78,14 +78,14 @@ type ComplexityRoot struct { } Query struct { - AnalyticsEvent func(childComplexity int, category string, action string, label *string, dimensionsJSON string) int + AnalyticsEvent func(childComplexity int, category string, action string, source string, label *string, dimensionsJSON string) int AvailableUpdate func(childComplexity int, desiredChannel string, desiredVersion string) int CheckMessages func(childComplexity int, command string, flags []string) int CheckRuntimeUsage func(childComplexity int, organizationName string) int ConfigChanged func(childComplexity int, key string) int FetchLogTail func(childComplexity int) int Projects func(childComplexity int) int - ReportRuntimeUsage func(childComplexity int, pid int, exec string, dimensionsJSON string) int + ReportRuntimeUsage func(childComplexity int, pid int, exec string, source string, dimensionsJSON string) int Version func(childComplexity int) int } @@ -110,8 +110,8 @@ type QueryResolver interface { Version(ctx context.Context) (*graph.Version, error) AvailableUpdate(ctx context.Context, desiredChannel string, desiredVersion string) (*graph.AvailableUpdate, error) Projects(ctx context.Context) ([]*graph.Project, error) - AnalyticsEvent(ctx context.Context, category string, action string, label *string, dimensionsJSON string) (*graph.AnalyticsEventResponse, error) - ReportRuntimeUsage(ctx context.Context, pid int, exec string, dimensionsJSON string) (*graph.ReportRuntimeUsageResponse, error) + AnalyticsEvent(ctx context.Context, category string, action string, source string, label *string, dimensionsJSON string) (*graph.AnalyticsEventResponse, error) + ReportRuntimeUsage(ctx context.Context, pid int, exec string, source string, dimensionsJSON string) (*graph.ReportRuntimeUsageResponse, error) CheckRuntimeUsage(ctx context.Context, organizationName string) (*graph.CheckRuntimeUsageResponse, error) CheckMessages(ctx context.Context, command string, flags []string) ([]*graph.MessageInfo, error) ConfigChanged(ctx context.Context, key string) (*graph.ConfigChangedResponse, error) @@ -262,7 +262,7 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return 0, false } - return e.complexity.Query.AnalyticsEvent(childComplexity, args["category"].(string), args["action"].(string), args["label"].(*string), args["dimensionsJson"].(string)), true + return e.complexity.Query.AnalyticsEvent(childComplexity, args["category"].(string), args["action"].(string), args["source"].(string), args["label"].(*string), args["dimensionsJson"].(string)), true case "Query.availableUpdate": if e.complexity.Query.AvailableUpdate == nil { @@ -336,7 +336,7 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return 0, false } - return e.complexity.Query.ReportRuntimeUsage(childComplexity, args["pid"].(int), args["exec"].(string), args["dimensionsJson"].(string)), true + return e.complexity.Query.ReportRuntimeUsage(childComplexity, args["pid"].(int), args["exec"].(string), args["source"].(string), args["dimensionsJson"].(string)), true case "Query.version": if e.complexity.Query.Version == nil { @@ -517,8 +517,8 @@ type Query { version: Version availableUpdate(desiredChannel: String!, desiredVersion: String!): AvailableUpdate projects: [Project]! - analyticsEvent(category: String!, action: String!, label: String, dimensionsJson: String!): AnalyticsEventResponse - reportRuntimeUsage(pid: Int!, exec: String!, dimensionsJson: String!): ReportRuntimeUsageResponse + analyticsEvent(category: String!, action: String!, source: String!, label: String, dimensionsJson: String!): AnalyticsEventResponse + reportRuntimeUsage(pid: Int!, exec: String!, source: String!, dimensionsJson: String!): ReportRuntimeUsageResponse checkRuntimeUsage(organizationName: String!): CheckRuntimeUsageResponse checkMessages(command: String!, flags: [String!]!): [MessageInfo!]! configChanged(key: String!): ConfigChangedResponse @@ -572,24 +572,33 @@ func (ec *executionContext) field_Query_analyticsEvent_args(ctx context.Context, } } args["action"] = arg1 - var arg2 *string + var arg2 string + if tmp, ok := rawArgs["source"]; ok { + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("source")) + arg2, err = ec.unmarshalNString2string(ctx, tmp) + if err != nil { + return nil, err + } + } + args["source"] = arg2 + var arg3 *string if tmp, ok := rawArgs["label"]; ok { ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("label")) - arg2, err = ec.unmarshalOString2ᚖstring(ctx, tmp) + arg3, err = ec.unmarshalOString2ᚖstring(ctx, tmp) if err != nil { return nil, err } } - args["label"] = arg2 - var arg3 string + args["label"] = arg3 + var arg4 string if tmp, ok := rawArgs["dimensionsJson"]; ok { ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("dimensionsJson")) - arg3, err = ec.unmarshalNString2string(ctx, tmp) + arg4, err = ec.unmarshalNString2string(ctx, tmp) if err != nil { return nil, err } } - args["dimensionsJson"] = arg3 + args["dimensionsJson"] = arg4 return args, nil } @@ -693,14 +702,23 @@ func (ec *executionContext) field_Query_reportRuntimeUsage_args(ctx context.Cont } args["exec"] = arg1 var arg2 string + if tmp, ok := rawArgs["source"]; ok { + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("source")) + arg2, err = ec.unmarshalNString2string(ctx, tmp) + if err != nil { + return nil, err + } + } + args["source"] = arg2 + var arg3 string if tmp, ok := rawArgs["dimensionsJson"]; ok { ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("dimensionsJson")) - arg2, err = ec.unmarshalNString2string(ctx, tmp) + arg3, err = ec.unmarshalNString2string(ctx, tmp) if err != nil { return nil, err } } - args["dimensionsJson"] = arg2 + args["dimensionsJson"] = arg3 return args, nil } @@ -1660,7 +1678,7 @@ func (ec *executionContext) _Query_analyticsEvent(ctx context.Context, field gra }() resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return ec.resolvers.Query().AnalyticsEvent(rctx, fc.Args["category"].(string), fc.Args["action"].(string), fc.Args["label"].(*string), fc.Args["dimensionsJson"].(string)) + return ec.resolvers.Query().AnalyticsEvent(rctx, fc.Args["category"].(string), fc.Args["action"].(string), fc.Args["source"].(string), fc.Args["label"].(*string), fc.Args["dimensionsJson"].(string)) }) if err != nil { ec.Error(ctx, err) @@ -1715,7 +1733,7 @@ func (ec *executionContext) _Query_reportRuntimeUsage(ctx context.Context, field }() resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return ec.resolvers.Query().ReportRuntimeUsage(rctx, fc.Args["pid"].(int), fc.Args["exec"].(string), fc.Args["dimensionsJson"].(string)) + return ec.resolvers.Query().ReportRuntimeUsage(rctx, fc.Args["pid"].(int), fc.Args["exec"].(string), fc.Args["source"].(string), fc.Args["dimensionsJson"].(string)) }) if err != nil { ec.Error(ctx, err) diff --git a/cmd/state-svc/main.go b/cmd/state-svc/main.go index 7189d0a00e..0234ad634b 100644 --- a/cmd/state-svc/main.go +++ b/cmd/state-svc/main.go @@ -13,6 +13,7 @@ import ( "github.com/ActiveState/cli/cmd/state-svc/autostart" anaSync "github.com/ActiveState/cli/internal/analytics/client/sync" + anaConst "github.com/ActiveState/cli/internal/analytics/constants" "github.com/ActiveState/cli/internal/captain" "github.com/ActiveState/cli/internal/config" "github.com/ActiveState/cli/internal/constants" @@ -98,7 +99,7 @@ func run(cfg *config.Instance) error { } auth := authentication.New(cfg) - an := anaSync.New(cfg, auth, out) + an := anaSync.New(anaConst.SrcStateService, cfg, auth, out) defer an.Wait() if err := autostart.RegisterConfigListener(cfg); err != nil { @@ -122,16 +123,25 @@ func run(cfg *config.Instance) error { ) var foregroundArgText string + var autostart bool cmd.AddChildren( captain.NewCommand( cmdStart, "", "Start the ActiveState Service (Background)", - p, nil, nil, + p, + []*captain.Flag{ + {Name: "autostart", Value: &autostart, Hidden: true}, // differentiate between autostart and cli invocation + }, + nil, func(ccmd *captain.Command, args []string) error { logging.Debug("Running CmdStart") - return runStart(out, "svc-start:cli") + argText := "svc-start:cli" + if autostart { + argText = "svc-start:auto" + } + return runStart(out, argText) }, ), captain.NewCommand( diff --git a/cmd/state-svc/schema/schema.graphqls b/cmd/state-svc/schema/schema.graphqls index d5bfb2e2c0..1cf1f47a3a 100644 --- a/cmd/state-svc/schema/schema.graphqls +++ b/cmd/state-svc/schema/schema.graphqls @@ -69,8 +69,8 @@ type Query { version: Version availableUpdate(desiredChannel: String!, desiredVersion: String!): AvailableUpdate projects: [Project]! - analyticsEvent(category: String!, action: String!, label: String, dimensionsJson: String!): AnalyticsEventResponse - reportRuntimeUsage(pid: Int!, exec: String!, dimensionsJson: String!): ReportRuntimeUsageResponse + analyticsEvent(category: String!, action: String!, source: String!, label: String, dimensionsJson: String!): AnalyticsEventResponse + reportRuntimeUsage(pid: Int!, exec: String!, source: String!, dimensionsJson: String!): ReportRuntimeUsageResponse checkRuntimeUsage(organizationName: String!): CheckRuntimeUsageResponse checkMessages(command: String!, flags: [String!]!): [MessageInfo!]! configChanged(key: String!): ConfigChangedResponse diff --git a/cmd/state/autoupdate.go b/cmd/state/autoupdate.go index 1b3a850552..8551c89f5e 100644 --- a/cmd/state/autoupdate.go +++ b/cmd/state/autoupdate.go @@ -75,21 +75,21 @@ func autoUpdate(svc *model.SvcModel, args []string, cfg *config.Instance, an ana if err != nil { if os.IsPermission(err) { an.EventWithLabel(anaConst.CatUpdates, anaConst.ActUpdateInstall, anaConst.UpdateLabelFailed, &dimensions.Values{ - Version: ptr.To(avUpdate.Version), - Error: ptr.To("Could not update the state tool due to insufficient permissions."), + TargetVersion: ptr.To(avUpdate.Version), + Error: ptr.To("Could not update the state tool due to insufficient permissions."), }) return false, locale.WrapInputError(err, locale.Tl("auto_update_permission_err", "", constants.DocumentationURL, errs.JoinMessage(err))) } if errs.Matches(err, &updater.ErrorInProgress{}) { an.EventWithLabel(anaConst.CatUpdates, anaConst.ActUpdateInstall, anaConst.UpdateLabelFailed, &dimensions.Values{ - Version: ptr.To(avUpdate.Version), - Error: ptr.To(anaConst.UpdateErrorInProgress), + TargetVersion: ptr.To(avUpdate.Version), + Error: ptr.To(anaConst.UpdateErrorInProgress), }) return false, nil } an.EventWithLabel(anaConst.CatUpdates, anaConst.ActUpdateInstall, anaConst.UpdateLabelFailed, &dimensions.Values{ - Version: ptr.To(avUpdate.Version), - Error: ptr.To(anaConst.UpdateErrorInstallFailed), + TargetVersion: ptr.To(avUpdate.Version), + Error: ptr.To(anaConst.UpdateErrorInstallFailed), }) return false, locale.WrapError(err, locale.T("auto_update_failed")) } @@ -106,14 +106,14 @@ func autoUpdate(svc *model.SvcModel, args []string, cfg *config.Instance, an ana msg = anaConst.UpdateErrorRelaunch } an.EventWithLabel(anaConst.CatUpdates, anaConst.ActUpdateRelaunch, anaConst.UpdateLabelFailed, &dimensions.Values{ - Version: ptr.To(avUpdate.Version), - Error: ptr.To(msg), + TargetVersion: ptr.To(avUpdate.Version), + Error: ptr.To(msg), }) return true, errs.Silence(errs.WrapExitCode(err, code)) } an.EventWithLabel(anaConst.CatUpdates, anaConst.ActUpdateRelaunch, anaConst.UpdateLabelSuccess, &dimensions.Values{ - Version: ptr.To(avUpdate.Version), + TargetVersion: ptr.To(avUpdate.Version), }) return true, nil } diff --git a/cmd/state/internal/cmdtree/init.go b/cmd/state/internal/cmdtree/init.go index 57ece7db64..08d2f33a80 100644 --- a/cmd/state/internal/cmdtree/init.go +++ b/cmd/state/internal/cmdtree/init.go @@ -48,5 +48,5 @@ func newInitCommand(prime *primer.Values) *captain.Command { func(ccmd *captain.Command, _ []string) error { return initRunner.Run(¶ms) }, - ).SetGroup(EnvironmentSetupGroup).SetUnstable(true) + ).SetGroup(EnvironmentSetupGroup) } diff --git a/cmd/state/main.go b/cmd/state/main.go index a4f2b09a35..1d0dba8224 100644 --- a/cmd/state/main.go +++ b/cmd/state/main.go @@ -11,11 +11,11 @@ import ( "strings" "time" - "github.com/ActiveState/cli/cmd/state/internal/cmdtree/exechandlers/messenger" - "github.com/ActiveState/cli/internal/captain" - "github.com/ActiveState/cli/cmd/state/internal/cmdtree" + "github.com/ActiveState/cli/cmd/state/internal/cmdtree/exechandlers/messenger" anAsync "github.com/ActiveState/cli/internal/analytics/client/async" + anaConst "github.com/ActiveState/cli/internal/analytics/constants" + "github.com/ActiveState/cli/internal/captain" "github.com/ActiveState/cli/internal/config" "github.com/ActiveState/cli/internal/constants" "github.com/ActiveState/cli/internal/constraints" @@ -201,7 +201,7 @@ func run(args []string, isInteractive bool, cfg *config.Instance, out output.Out logging.Warning("Could not sync authenticated state: %s", errs.JoinMessage(err)) } - an := anAsync.New(svcmodel, cfg, auth, out, pjNamespace) + an := anAsync.New(anaConst.SrcStateTool, svcmodel, cfg, auth, out, pjNamespace) defer func() { if err := events.WaitForEvents(time.Second, an.Wait); err != nil { logging.Warning("Failed waiting for events: %v", err) diff --git a/internal/analytics/analytics.go b/internal/analytics/analytics.go index 4edc267590..9292b954e9 100644 --- a/internal/analytics/analytics.go +++ b/internal/analytics/analytics.go @@ -8,8 +8,9 @@ import ( // Dispatcher describes a struct that can send analytics event in the background type Dispatcher interface { - Event(category string, action string, dim ...*dimensions.Values) - EventWithLabel(category string, action string, label string, dim ...*dimensions.Values) + Event(category, action string, dim ...*dimensions.Values) + EventWithLabel(category, action, label string, dim ...*dimensions.Values) + EventWithSource(category, action, source string, dim ...*dimensions.Values) Wait() Close() } diff --git a/internal/analytics/client/async/client.go b/internal/analytics/client/async/client.go index ecd9a43441..4aa4fa2452 100644 --- a/internal/analytics/client/async/client.go +++ b/internal/analytics/client/async/client.go @@ -37,13 +37,16 @@ type Client struct { sequence int ci bool interactive bool + activestateCI bool + source string } var _ analytics.Dispatcher = &Client{} -func New(svcModel *model.SvcModel, cfg *config.Instance, auth *authentication.Auth, out output.Outputer, projectNameSpace string) *Client { +func New(source string, svcModel *model.SvcModel, cfg *config.Instance, auth *authentication.Auth, out output.Outputer, projectNameSpace string) *Client { a := &Client{ eventWaitGroup: &sync.WaitGroup{}, + source: source, } o := string(output.PlainFormatName) @@ -55,6 +58,7 @@ func New(svcModel *model.SvcModel, cfg *config.Instance, auth *authentication.Au a.auth = auth a.ci = condition.OnCI() a.interactive = out.Config().Interactive + a.activestateCI = condition.InActiveStateCI() if condition.InUnitTest() { return a @@ -73,13 +77,24 @@ func New(svcModel *model.SvcModel, cfg *config.Instance, auth *authentication.Au } // Event logs an event to google analytics -func (a *Client) Event(category string, action string, dims ...*dimensions.Values) { +func (a *Client) Event(category, action string, dims ...*dimensions.Values) { a.EventWithLabel(category, action, "", dims...) } // EventWithLabel logs an event with a label to google analytics -func (a *Client) EventWithLabel(category string, action string, label string, dims ...*dimensions.Values) { - err := a.sendEvent(category, action, label, dims...) +func (a *Client) EventWithLabel(category, action, label string, dims ...*dimensions.Values) { + a.eventWithSourceAndLabel(category, action, a.source, label, dims...) +} + +// EventWithSource logs an event with another source to google analytics. +// For example, log runtime events triggered by executors as coming from an executor instead of from +// State Tool. +func (a *Client) EventWithSource(category, action, source string, dims ...*dimensions.Values) { + a.eventWithSourceAndLabel(category, action, source, "", dims...) +} + +func (a *Client) eventWithSourceAndLabel(category, action, source, label string, dims ...*dimensions.Values) { + err := a.sendEvent(category, action, source, label, dims...) if err != nil { multilog.Error("Error during analytics.sendEvent: %v", errs.JoinMessage(err)) } @@ -96,7 +111,7 @@ func (a *Client) Wait() { a.eventWaitGroup.Wait() } -func (a *Client) sendEvent(category, action, label string, dims ...*dimensions.Values) error { +func (a *Client) sendEvent(category, action, source, label string, dims ...*dimensions.Values) error { if a.svcModel == nil { // this is only true on CI return nil } @@ -118,6 +133,7 @@ func (a *Client) sendEvent(category, action, label string, dims ...*dimensions.V a.sequence++ dim.CI = &a.ci dim.Interactive = &a.interactive + dim.ActiveStateCI = &a.activestateCI dim.Merge(dims...) dimMarshalled, err := dim.Marshal() @@ -130,7 +146,7 @@ func (a *Client) sendEvent(category, action, label string, dims ...*dimensions.V defer func() { handlePanics(recover(), debug.Stack()) }() defer a.eventWaitGroup.Done() - if err := a.svcModel.AnalyticsEvent(context.Background(), category, action, label, string(dimMarshalled)); err != nil { + if err := a.svcModel.AnalyticsEvent(context.Background(), category, action, source, label, string(dimMarshalled)); err != nil { logging.Debug("Failed to report analytics event via state-svc: %s", errs.JoinMessage(err)) } }() diff --git a/internal/analytics/client/blackhole/client.go b/internal/analytics/client/blackhole/client.go index c36cf79d1b..e533ac7203 100644 --- a/internal/analytics/client/blackhole/client.go +++ b/internal/analytics/client/blackhole/client.go @@ -13,10 +13,13 @@ func New() *Client { return &Client{} } -func (c Client) Event(category string, action string, dim ...*dimensions.Values) { +func (c Client) Event(category, action string, dim ...*dimensions.Values) { } -func (c Client) EventWithLabel(category string, action string, label string, dim ...*dimensions.Values) { +func (c Client) EventWithLabel(category, action, label string, dim ...*dimensions.Values) { +} + +func (c Client) EventWithSource(category, action, source string, dim ...*dimensions.Values) { } func (c Client) Wait() { diff --git a/internal/analytics/client/sync/client.go b/internal/analytics/client/sync/client.go index 6b769c2e0f..751c377149 100644 --- a/internal/analytics/client/sync/client.go +++ b/internal/analytics/client/sync/client.go @@ -32,7 +32,7 @@ import ( type Reporter interface { ID() string - Event(category, action, label string, dimensions *dimensions.Values) error + Event(category, action, source, label string, dimensions *dimensions.Values) error } // Client instances send analytics events to GA and S3 endpoints without delay. It is only supposed to be used inside the `state-svc`. All other processes should use the DefaultClient. @@ -45,16 +45,18 @@ type Client struct { reporters []Reporter sequence int auth *authentication.Auth + source string } var _ analytics.Dispatcher = &Client{} // New initializes the analytics instance with all custom dimensions known at this time -func New(cfg *config.Instance, auth *authentication.Auth, out output.Outputer) *Client { +func New(source string, cfg *config.Instance, auth *authentication.Auth, out output.Outputer) *Client { a := &Client{ eventWaitGroup: &sync.WaitGroup{}, sendReports: true, auth: auth, + source: source, } installSource, err := storage.InstallSource() @@ -115,6 +117,7 @@ func New(cfg *config.Instance, auth *authentication.Auth, out output.Outputer) * Sequence: ptr.To(0), CI: ptr.To(condition.OnCI()), Interactive: ptr.To(interactive), + ActiveStateCI: ptr.To(condition.InActiveStateCI()), } a.customDimensions = customDimensions @@ -149,13 +152,13 @@ func (a *Client) Wait() { } // Events returns a channel to feed eventData directly to the report loop -func (a *Client) report(category, action, label string, dimensions *dimensions.Values) { +func (a *Client) report(category, action, source, label string, dimensions *dimensions.Values) { if !a.sendReports { return } for _, reporter := range a.reporters { - if err := reporter.Event(category, action, label, dimensions); err != nil { + if err := reporter.Event(category, action, source, label, dimensions); err != nil { logging.Debug( "Reporter failed: %s, category: %s, action: %s, error: %s", reporter.ID(), category, action, errs.JoinMessage(err), @@ -164,7 +167,7 @@ func (a *Client) report(category, action, label string, dimensions *dimensions.V } } -func (a *Client) Event(category string, action string, dims ...*dimensions.Values) { +func (a *Client) Event(category, action string, dims ...*dimensions.Values) { a.EventWithLabel(category, action, "", dims...) } @@ -179,7 +182,20 @@ func mergeDimensions(target *dimensions.Values, dims ...*dimensions.Values) *dim return actualDims } -func (a *Client) EventWithLabel(category string, action, label string, dims ...*dimensions.Values) { +func (a *Client) EventWithLabel(category, action, label string, dims ...*dimensions.Values) { + a.EventWithSourceAndLabel(category, action, a.source, label, dims...) +} + +// EventWithSource should only be used by clients forwarding events on behalf of another source. +// Otherwise, use Event(). +func (a *Client) EventWithSource(category, action, source string, dims ...*dimensions.Values) { + a.EventWithSourceAndLabel(category, action, source, "", dims...) +} + +// EventWithSourceAndLabel should only be used by clients forwarding events on behalf of another +// source (for example, state-svc forwarding events on behalf of State Tool or an executor). +// Otherwise, use EventWithLabel(). +func (a *Client) EventWithSourceAndLabel(category, action, source, label string, dims ...*dimensions.Values) { if a.customDimensions == nil { if condition.InUnitTest() { return @@ -209,7 +225,7 @@ func (a *Client) EventWithLabel(category string, action, label string, dims ...* go func() { defer a.eventWaitGroup.Done() defer func() { handlePanics(recover(), debug.Stack()) }() - a.report(category, action, label, actualDims) + a.report(category, action, source, label, actualDims) }() } diff --git a/internal/analytics/client/sync/reporters/ga-state.go b/internal/analytics/client/sync/reporters/ga-state.go index 40f7fe217b..afdec89b64 100644 --- a/internal/analytics/client/sync/reporters/ga-state.go +++ b/internal/analytics/client/sync/reporters/ga-state.go @@ -44,7 +44,7 @@ func (r *GaCLIReporter) AddOmitCategory(category string) { r.omit[category] = struct{}{} } -func (r *GaCLIReporter) Event(category, action, label string, d *dimensions.Values) error { +func (r *GaCLIReporter) Event(category, action, source, label string, d *dimensions.Values) error { if _, ok := r.omit[category]; ok { logging.Debug("Not sending event with category: %s to Google Analytics", category) return nil @@ -99,5 +99,6 @@ func legacyDimensionMap(d *dimensions.Values) map[string]string { "24": ptr.From(d.TargetVersion, ""), "25": ptr.From(d.Error, ""), "26": ptr.From(d.Message, ""), + "27": strconv.FormatBool(ptr.From(d.ActiveStateCI, false)), } } diff --git a/internal/analytics/client/sync/reporters/pixel.go b/internal/analytics/client/sync/reporters/pixel.go index 8a9fde09d7..d42588862b 100644 --- a/internal/analytics/client/sync/reporters/pixel.go +++ b/internal/analytics/client/sync/reporters/pixel.go @@ -29,7 +29,7 @@ func (r *PixelReporter) ID() string { return "PixelReporter" } -func (r *PixelReporter) Event(category, action, label string, d *dimensions.Values) error { +func (r *PixelReporter) Event(category, action, source, label string, d *dimensions.Values) error { pixelURL, err := url.Parse(r.url) if err != nil { return errs.Wrap(err, "Invalid pixel URL: %s", r.url) @@ -38,6 +38,7 @@ func (r *PixelReporter) Event(category, action, label string, d *dimensions.Valu query := &url.Values{} query.Add("x-category", category) query.Add("x-action", action) + query.Add("x-source", source) query.Add("x-label", label) for num, value := range legacyDimensionMap(d) { diff --git a/internal/analytics/client/sync/reporters/test.go b/internal/analytics/client/sync/reporters/test.go index b8c60dfe08..95ff0c426a 100644 --- a/internal/analytics/client/sync/reporters/test.go +++ b/internal/analytics/client/sync/reporters/test.go @@ -36,12 +36,13 @@ func (r *TestReporter) ID() string { type TestLogEntry struct { Category string Action string + Source string Label string Dimensions *dimensions.Values } -func (r *TestReporter) Event(category, action, label string, d *dimensions.Values) error { - b, err := json.Marshal(TestLogEntry{category, action, label, d}) +func (r *TestReporter) Event(category, action, source, label string, d *dimensions.Values) error { + b, err := json.Marshal(TestLogEntry{category, action, source, label, d}) if err != nil { return errs.Wrap(err, "Could not marshal test log entry") } diff --git a/internal/analytics/constants/constants.go b/internal/analytics/constants/constants.go index b2bf3dfa4f..268000d438 100644 --- a/internal/analytics/constants/constants.go +++ b/internal/analytics/constants/constants.go @@ -25,6 +25,30 @@ const CatConfig = "config" // CatUpdate is the event category used for all update events const CatUpdates = "updates" +// CatInstaller is the event category used for installer events. +const CatInstaller = "installer" + +// CatInstallerFunnel is the event category used for installer funnel events. +const CatInstallerFunnel = "installer-funnel" + +// SrcStateTool is the event source for events sent by state. +const SrcStateTool = "State Tool" + +// SrcStateService is the event source for events sent by state-svc. +const SrcStateService = "State Service" + +// SrcStateInstaller is the event source for events sent by state-installer. +const SrcStateInstaller = "State Installer" + +// SrcStateRemoteInstaller is the event source for events sent by state-remote-installer. +const SrcStateRemoteInstaller = "State Remote Installer" + +// SrcOfflineInstaller is the event source for events sent by offline installers. +const SrcOfflineInstaller = "Offline Installer" + +// SrcExecutor is the event source for events sent by executors. +const SrcExecutor = "Executor" + // ActRuntimeHeartbeat is the event action sent when a runtime is in use const ActRuntimeHeartbeat = "heartbeat" diff --git a/internal/analytics/dimensions/dimensions.go b/internal/analytics/dimensions/dimensions.go index 37c5669b63..fbcd340e34 100644 --- a/internal/analytics/dimensions/dimensions.go +++ b/internal/analytics/dimensions/dimensions.go @@ -45,6 +45,7 @@ type Values struct { Message *string CI *bool Interactive *bool + ActiveStateCI *bool preProcessor func(*Values) error } @@ -98,6 +99,7 @@ func NewDefaultDimensions(pjNamespace, sessionToken, updateTag string) *Values { ptr.To(""), ptr.To(false), ptr.To(false), + ptr.To(false), nil, } } @@ -128,6 +130,7 @@ func (v *Values) Clone() *Values { Message: ptr.Clone(v.Message), CI: ptr.Clone(v.CI), Interactive: ptr.Clone(v.Interactive), + ActiveStateCI: ptr.Clone(v.ActiveStateCI), preProcessor: v.preProcessor, } } @@ -208,6 +211,9 @@ func (m *Values) Merge(mergeWith ...*Values) { if dim.Interactive != nil { m.Interactive = dim.Interactive } + if dim.ActiveStateCI != nil { + m.ActiveStateCI = dim.ActiveStateCI + } if dim.preProcessor != nil { m.preProcessor = dim.preProcessor } diff --git a/internal/condition/condition.go b/internal/condition/condition.go index d81a68a1a3..65e5e15011 100644 --- a/internal/condition/condition.go +++ b/internal/condition/condition.go @@ -41,6 +41,10 @@ func BuiltOnDevMachine() bool { return !BuiltViaCI() } +func InActiveStateCI() bool { + return os.Getenv(constants.ActiveStateCIEnvVarName) == "true" +} + func OptInUnstable(cfg Configurable) bool { if v := os.Getenv(constants.OptinUnstableEnvVarName); v != "" { return v == "true" diff --git a/internal/constants/constants.go b/internal/constants/constants.go index d62c67ba3d..7f8121e5e3 100644 --- a/internal/constants/constants.go +++ b/internal/constants/constants.go @@ -504,3 +504,6 @@ const TerminalAnimationInterval = 150 * time.Millisecond // RuntimeSetupWaitEnvVarName is only used for an integration test to pause installation and wait // for Ctrl+C. const RuntimeSetupWaitEnvVarName = "ACTIVESTATE_CLI_RUNTIME_SETUP_WAIT" + +// ActiveStateCIEnvVarName is the environment variable set when running in an ActiveState CI environment. +const ActiveStateCIEnvVarName = "ACTIVESTATE_CI" diff --git a/internal/errs/userfacing.go b/internal/errs/userfacing.go new file mode 100644 index 0000000000..5ae0df22dc --- /dev/null +++ b/internal/errs/userfacing.go @@ -0,0 +1,58 @@ +package errs + +import "errors" + +type UserFacingError interface { + error + UserError() string +} + +type ErrOpt func(err *userFacingError) *userFacingError + +type userFacingError struct { + wrapped error + message string + tips []string +} + +func (e *userFacingError) Error() string { + return "User Facing Error: " + e.UserError() +} + +func (e *userFacingError) UserError() string { + return e.message +} + +func (e *userFacingError) ErrorTips() []string { + return e.tips +} + +func NewUserFacingError(message string, tips ...string) *userFacingError { + return WrapUserFacingError(nil, message) +} + +func WrapUserFacingError(wrapTarget error, message string, opts ...ErrOpt) *userFacingError { + err := &userFacingError{ + wrapTarget, + message, + nil, + } + + for _, opt := range opts { + err = opt(err) + } + + return err +} + +func IsUserFacing(err error) bool { + var userFacingError UserFacingError + return errors.As(err, &userFacingError) +} + +func WithTips(tips ...string) ErrOpt { + return func(err *userFacingError) *userFacingError { + err.tips = append(err.tips, tips...) + return err + } +} diff --git a/internal/ipc/client.go b/internal/ipc/client.go index dc6da797bf..d60e593ff1 100644 --- a/internal/ipc/client.go +++ b/internal/ipc/client.go @@ -23,6 +23,10 @@ func NewClient(n *SockPath) *Client { } } +func (c *Client) SockPath() *SockPath { + return c.sockpath +} + func (c *Client) Request(ctx context.Context, key string) (string, error) { spath := c.sockpath.String() conn, err := c.dialer.DialContext(ctx, network, spath) diff --git a/internal/locale/errors.go b/internal/locale/errors.go index ae3460afe6..61cde512e5 100644 --- a/internal/locale/errors.go +++ b/internal/locale/errors.go @@ -28,7 +28,7 @@ func (e *LocalizedError) Error() string { } // UserError is the user facing error message, it's the same as Error() but identifies it as being user facing -func (e *LocalizedError) UserError() string { +func (e *LocalizedError) LocalizedError() string { return e.localized } @@ -58,7 +58,7 @@ func (e *LocalizedError) AddTips(tips ...string) { // ErrorLocalizer represents a localized error type ErrorLocalizer interface { error - UserError() string + LocalizedError() string } type AsError interface { @@ -162,7 +162,7 @@ func JoinedErrorMessage(err error) string { var message []string for _, err := range UnpackError(err) { if lerr, isLocaleError := err.(ErrorLocalizer); isLocaleError { - message = append(message, lerr.UserError()) + message = append(message, lerr.LocalizedError()) } } if len(message) == 0 { @@ -177,7 +177,7 @@ func JoinedErrorMessage(err error) string { func ErrorMessage(err error) string { if errr, ok := err.(ErrorLocalizer); ok { - return errr.UserError() + return errr.LocalizedError() } return err.Error() } diff --git a/internal/locale/locales/en-us.yaml b/internal/locale/locales/en-us.yaml index af61afa733..41814b9ec1 100644 --- a/internal/locale/locales/en-us.yaml +++ b/internal/locale/locales/en-us.yaml @@ -937,7 +937,7 @@ prompt_signup_browser_action: prompt_login_after_browser_signup: other: Please login once you've registered your account err_browser_open: - other: "Could not open your browser, please manually open the following URL in your browser: {{.V0}}" + other: "Could not open your browser, please manually open the following URL above." err_activate_auth_required: other: Activating a project requires you to be authenticated against the ActiveState Platform err_os_not_a_directory: @@ -1032,6 +1032,10 @@ auth_device_verify_security_code: Please sign into the ActiveState Platform (if you have not already done so) and click "Authorize". After that, it may take a few seconds for the authorization process here to complete. + + If the URL does not open automatically, please copy and paste it into your browser. + + [ACTIONABLE]{{.V1}}[/RESET] auth_device_timeout: other: Authorization timeout. Please try again. auth_device_success: @@ -1340,6 +1344,8 @@ namespace_label_platform: other: "Platform" namespace_label_preplatform: other: "Bits" +namespace_label_packager: + other: "Docker/Installer packagers" print_commit: other: "[NOTICE]commit {{.V0}}[/RESET]" print_commit_author: @@ -1750,6 +1756,8 @@ err_user_network_solution: other: | Please ensure your device has access to internet during installation. Make sure software like a VPN, Firewall or Anti-Virus are not blocking your connectivity. If your issue persists consider reporting it on our forums at {{.V0}}. +err_revert_get_commit: + other: "Could not fetch commit details for commit with ID: {{.V0}}" err_revert_refresh: other: Could not get revert commit to check if changes were indeed made err_revert_commit: diff --git a/internal/osutils/user/user.go b/internal/osutils/user/user.go index 44b51d7c4f..56bacb3791 100644 --- a/internal/osutils/user/user.go +++ b/internal/osutils/user/user.go @@ -23,7 +23,7 @@ func (e *HomeDirNotFoundError) Error() string { return homeDirNotFoundErrorMessage } -func (e *HomeDirNotFoundError) UserError() string { +func (e *HomeDirNotFoundError) LocalizedError() string { return homeDirNotFoundErrorMessage } diff --git a/internal/prompt/prompt.go b/internal/prompt/prompt.go index ad5044acc8..6da18f055c 100644 --- a/internal/prompt/prompt.go +++ b/internal/prompt/prompt.go @@ -15,7 +15,7 @@ import ( ) type EventDispatcher interface { - EventWithLabel(category string, action string, label string, dim ...*dimensions.Values) + EventWithLabel(category, action string, label string, dim ...*dimensions.Values) } // Prompter is the interface used to run our prompt from, useful for mocking in tests diff --git a/internal/rollbar/rollbar.go b/internal/rollbar/rollbar.go index 7b7e5f88d7..688033e3bf 100644 --- a/internal/rollbar/rollbar.go +++ b/internal/rollbar/rollbar.go @@ -94,10 +94,7 @@ func SetupRollbar(token string) { data["request"] = map[string]string{} } if request, ok := data["request"].(map[string]string); ok { - // Rollbar specially interprets the body["request"]["user_ip"] key, but it does not have to - // actually be an IP address. Use device_id for now. We may want to use real IP later if it - // is easily accessible (Rollbar does not provide it for us with non-http.Request errors). - request["user_ip"] = uniqid.Text() + request["user_ip"] = "$remote_ip" // ask Rollbar to log the user's IP } }) diff --git a/internal/runbits/errors/centralized.go b/internal/runbits/errors/centralized.go new file mode 100644 index 0000000000..3f005a1fda --- /dev/null +++ b/internal/runbits/errors/centralized.go @@ -0,0 +1,7 @@ +package errors + +import "github.com/ActiveState/cli/internal/errs" + +type ErrNoProject struct { + *errs.WrapperError +} diff --git a/internal/runbits/hello_example.go b/internal/runbits/hello_example.go index 93c0c65c32..53e852d31f 100644 --- a/internal/runbits/hello_example.go +++ b/internal/runbits/hello_example.go @@ -1,14 +1,19 @@ package runbits import ( + "github.com/ActiveState/cli/internal/errs" "github.com/ActiveState/cli/internal/locale" "github.com/ActiveState/cli/internal/output" ) +type NoNameProvidedError struct { + *errs.WrapperError +} + func SayHello(out output.Outputer, name string) error { if name == "" { // Errors that are due to USER input should use `NewInputError` or `WrapInputError` - return locale.NewInputError("hello_err_no_name", "No name provided.") + return &NoNameProvidedError{errs.New("No name provided.")} } out.Print(locale.Tl("hello_message", "Hello, {{.V0}}!", name)) diff --git a/internal/runners/auth/signup.go b/internal/runners/auth/signup.go index 9d9adad467..1b7e8bb656 100644 --- a/internal/runners/auth/signup.go +++ b/internal/runners/auth/signup.go @@ -30,7 +30,7 @@ func (s *Signup) Run(params *SignupParams) error { } if !params.Prompt { - return authlet.AuthenticateWithBrowser(s.Outputer, s.Auth, s.Prompter) // user can sign up from this page too + return authlet.SignupWithBrowser(s.Outputer, s.Auth, s.Prompter) } return authlet.Signup(s.Configurable, s.Outputer, s.Prompter, s.Auth) } diff --git a/internal/runners/fork/fork.go b/internal/runners/fork/fork.go index 1bfb64c5d9..15fbbcc914 100644 --- a/internal/runners/fork/fork.go +++ b/internal/runners/fork/fork.go @@ -95,11 +95,17 @@ func determineOwner(username string, prompter prompt.Prompter) (string, error) { } options := make([]string, len(orgs)) + displayNameToURLNameMap := make(map[string]string) for i, org := range orgs { options[i] = org.DisplayName + displayNameToURLNameMap[org.DisplayName] = org.URLname } options = append([]string{username}, options...) r, err := prompter.Select(locale.Tl("fork_owner_title", "Owner"), locale.Tl("fork_select_org", "Who should the new project belong to?"), options, new(string)) - return r, err + owner, exists := displayNameToURLNameMap[r] + if !exists { + return "", errs.New("Selected organization does not have a URL name") + } + return owner, err } diff --git a/internal/runners/hello/hello_example.go b/internal/runners/hello/hello_example.go index d336a88640..41f43a8064 100644 --- a/internal/runners/hello/hello_example.go +++ b/internal/runners/hello/hello_example.go @@ -13,6 +13,7 @@ import ( "github.com/ActiveState/cli/internal/output" "github.com/ActiveState/cli/internal/primer" "github.com/ActiveState/cli/internal/runbits" + "github.com/ActiveState/cli/internal/runbits/errors" "github.com/ActiveState/cli/pkg/platform/model" "github.com/ActiveState/cli/pkg/project" ) @@ -37,9 +38,7 @@ type RunParams struct { // can be set. If no default or construction-time values are necessary, direct // construction of RunParams is fine, and this construction func may be dropped. func NewRunParams() *RunParams { - return &RunParams{ - Name: "Friend", - } + return &RunParams{} } // Hello defines the app-level dependencies that are accessible within the Run @@ -58,20 +57,40 @@ func New(p primeable) *Hello { } } +// processError contains the scope in which errors are processed. This is +// useful for ensuring that errors are wrapped in a user-facing error and +// localized. +func processError(err *error) { + if err == nil { + return + } + + switch { + case errs.Matches(*err, &runbits.NoNameProvidedError{}): + // Errors that we are looking for should be wrapped in a user-facing error. + // Ensure we wrap the top-level error returned from the runner and not + // the unpacked error that we are inspecting. + *err = errs.WrapUserFacingError(*err, locale.Tl("hello_err_no_name", "Cannot say hello because no name was provided.")) + case errs.Matches(*err, &errors.ErrNoProject{}): + // It's useful to offer users reasonable tips on recourses. + *err = errs.WrapUserFacingError( + *err, + locale.Tl("hello_err_no_project", "Cannot say hello because you are not in a project directory."), + errs.WithTips( + locale.Tl("hello_suggest_checkout", "Try using [ACTIONABLE]`state checkout`[/RESET] first."), + ), + ) + } +} + // Run contains the scope in which the hello runner logic is executed. -func (h *Hello) Run(params *RunParams) error { +func (h *Hello) Run(params *RunParams) (rerr error) { + defer processError(&rerr) + h.out.Print(locale.Tl("hello_notice", "This command is for example use only")) if h.project == nil { - err := locale.NewInputError( - "hello_info_err_no_project", "Not in a project directory.", - ) - - // It's useful to offer users reasonable tips on recourses. - return errs.AddTips(err, locale.Tl( - "hello_suggest_checkout", - "Try using [ACTIONABLE]`state checkout`[/RESET] first.", - )) + return &errors.ErrNoProject{errs.New("Not in a project directory")} } // Reusable runner logic is contained within the runbits package. @@ -79,8 +98,8 @@ func (h *Hello) Run(params *RunParams) error { // runners. Runners should NEVER invoke other runners. if err := runbits.SayHello(h.out, params.Name); err != nil { // Errors should nearly always be localized. - return locale.WrapError( - err, "hello_cannot_say", "Cannot say hello.", + return errs.Wrap( + err, "Cannot say hello.", ) } @@ -98,8 +117,8 @@ func (h *Hello) Run(params *RunParams) error { // Grab data from the platform. commitMsg, err := currentCommitMessage(h.project) if err != nil { - err = locale.WrapError( - err, "hello_info_err_get_commit_msg", " Cannot get commit message", + err = errs.Wrap( + err, "Cannot get commit message", ) return errs.AddTips( err, @@ -127,9 +146,7 @@ func currentCommitMessage(proj *project.Project) (string, error) { commit, err := model.GetCommit(proj.CommitUUID()) if err != nil { - return "", locale.NewError( - "hello_info_err_get_commitr", "Cannot get commit from server", - ) + return "", errs.Wrap(err, "Cannot get commit from server") } commitMsg := locale.Tl("hello_info_warn_no_commit", "Commit description not provided.") diff --git a/internal/runners/initialize/init.go b/internal/runners/initialize/init.go index b0182e4b76..6186975148 100644 --- a/internal/runners/initialize/init.go +++ b/internal/runners/initialize/init.go @@ -156,8 +156,8 @@ func (r *Initialize) Run(params *RunParams) (rerr error) { return errs.Wrap(err, "Unable to get the user's writable orgs") } for _, org := range orgs { - if strings.EqualFold(org.DisplayName, params.Namespace.Owner) { - owner = org.DisplayName + if strings.EqualFold(org.URLname, params.Namespace.Owner) { + owner = org.URLname break } } @@ -234,6 +234,12 @@ func (r *Initialize) Run(params *RunParams) (rerr error) { err = runbits.RefreshRuntime(r.auth, r.out, r.analytics, proj, commitID, true, target.TriggerInit, r.svcModel) if err != nil { + logging.Debug("Deleting remotely created project due to runtime setup error") + err2 := model.DeleteProject(namespace.Owner, namespace.Project, r.auth) + if err2 != nil { + multilog.Error("Error deleting remotely created project after runtime setup error: %v", errs.JoinMessage(err2)) + return locale.WrapError(err, "err_init_refresh_delete_project", "Could not setup runtime after init, and could not delete newly created Platform project. Please delete it manually before trying again") + } return locale.WrapError(err, "err_init_refresh", "Could not setup runtime after init") } diff --git a/internal/runners/learn/learn.go b/internal/runners/learn/learn.go index 0c5967830e..3e87504d2c 100644 --- a/internal/runners/learn/learn.go +++ b/internal/runners/learn/learn.go @@ -27,7 +27,7 @@ func (l *Learn) Run() error { err := open.Run(constants.CheatSheetURL) if err != nil { logging.Warning("Could not open browser: %v", err) - l.out.Notice(locale.Tr("err_browser_open", constants.CheatSheetURL)) + l.out.Notice(locale.Tr("err_browser_open")) } return nil diff --git a/internal/runners/revert/revert.go b/internal/runners/revert/revert.go index 4239973b97..0f1c22b156 100644 --- a/internal/runners/revert/revert.go +++ b/internal/runners/revert/revert.go @@ -76,7 +76,7 @@ func (r *Revert) Run(params *Params) error { priorCommits, err := model.CommitHistoryPaged(commitID, 0, 2) if err != nil { return errs.AddTips( - locale.WrapError(err, "err_revert_get_commit", "Could not fetch commit details for commit with ID: {{.V0}}", params.CommitID), + locale.WrapError(err, "err_revert_get_commit", "", params.CommitID), locale.T("tip_private_project_auth"), ) } @@ -90,7 +90,10 @@ func (r *Revert) Run(params *Params) error { var err error targetCommit, err = model.GetCommitWithinCommitHistory(latestCommit, commitID) if err != nil { - return locale.WrapError(err, "err_revert_to_get_commit", "Could not fetch commit details for commit with ID: {{.V0}}", params.CommitID) + return errs.AddTips( + locale.WrapError(err, "err_revert_get_commit", "", params.CommitID), + locale.T("tip_private_project_auth"), + ) } fromCommit = latestCommit toCommit = targetCommit.CommitID diff --git a/internal/runners/tutorial/tutorial.go b/internal/runners/tutorial/tutorial.go index 2bc961ed35..c1f9fb00dc 100644 --- a/internal/runners/tutorial/tutorial.go +++ b/internal/runners/tutorial/tutorial.go @@ -12,8 +12,8 @@ import ( "github.com/ActiveState/cli/internal/fileutils" "github.com/ActiveState/cli/internal/language" "github.com/ActiveState/cli/internal/locale" - "github.com/ActiveState/cli/internal/output" "github.com/ActiveState/cli/internal/osutils/user" + "github.com/ActiveState/cli/internal/output" "github.com/ActiveState/cli/internal/primer" "github.com/ActiveState/cli/internal/prompt" "github.com/ActiveState/cli/internal/runbits" diff --git a/internal/svcctl/comm.go b/internal/svcctl/comm.go index eda636e675..7ada81cdcf 100644 --- a/internal/svcctl/comm.go +++ b/internal/svcctl/comm.go @@ -73,12 +73,12 @@ func (c *Comm) GetLogFileName(ctx context.Context) (string, error) { } type Resolver interface { - ReportRuntimeUsage(ctx context.Context, pid int, exec, dimensionsJSON string) (*graph.ReportRuntimeUsageResponse, error) + ReportRuntimeUsage(ctx context.Context, pid int, exec, source string, dimensionsJSON string) (*graph.ReportRuntimeUsageResponse, error) CheckRuntimeUsage(ctx context.Context, organizationName string) (*graph.CheckRuntimeUsageResponse, error) } type AnalyticsReporter interface { - Event(category string, action string, dims ...*dimensions.Values) + EventWithSource(category, action, source string, dims ...*dimensions.Values) } func HeartbeatHandler(cfg *config.Instance, resolver Resolver, analyticsReporter AnalyticsReporter) ipc.RequestHandler { @@ -138,8 +138,8 @@ func HeartbeatHandler(cfg *config.Instance, resolver Resolver, analyticsReporter } logging.Debug("Firing runtime usage events for %s", metaData.Namespace) - analyticsReporter.Event(constants.CatRuntimeUsage, constants.ActRuntimeAttempt, dims) - _, err = resolver.ReportRuntimeUsage(context.Background(), pidNum, hb.ExecPath, dimsJSON) + analyticsReporter.EventWithSource(constants.CatRuntimeUsage, constants.ActRuntimeAttempt, constants.SrcExecutor, dims) + _, err = resolver.ReportRuntimeUsage(context.Background(), pidNum, hb.ExecPath, constants.SrcExecutor, dimsJSON) if err != nil { multilog.Critical("Heartbeat Failure: Failed to report runtime usage in heartbeat handler: %s", errs.JoinMessage(err)) return diff --git a/internal/svcctl/debugdata.go b/internal/svcctl/debugdata.go new file mode 100644 index 0000000000..d981380e51 --- /dev/null +++ b/internal/svcctl/debugdata.go @@ -0,0 +1,144 @@ +package svcctl + +import ( + "fmt" + "io/fs" + "os" + "path/filepath" + "strings" + "time" + + "github.com/ActiveState/cli/internal/fileutils" + "github.com/ActiveState/cli/internal/logging" +) + +type execKind string + +const ( + startSvc execKind = "Start" + stopSvc execKind = "Stop" +) + +type debugData struct { + argText string + + sockInfo *fileInfo + sockDirInfo *fileInfo + sockDirList []string + + execStart time.Time + execDur time.Duration + execKind execKind + + waitStart time.Time + waitAttempts []*waitAttempt + waitDur time.Duration +} + +func newDebugData(ipComm IPCommunicator, kind execKind, argText string) *debugData { + sock := ipComm.SockPath().String() + sockDir := filepath.Dir(sock) + + return &debugData{ + argText: argText, + sockInfo: newFileInfo(sock), + sockDirInfo: newFileInfo(sockDir), + sockDirList: fileutils.ListDirSimple(sockDir, false), + execStart: time.Now(), + execKind: kind, + } +} + +func (d *debugData) Error() string { + var sockDirList string + for _, entry := range d.sockDirList { + sockDirList += "\n " + entry + } + if sockDirList == "" { + sockDirList = "No entries found" + } + + var attemptsMsg string + for _, wa := range d.waitAttempts { + attemptsMsg += "\n " + wa.LogString() + } + + return fmt.Sprintf(strings.TrimSpace(` +Arg Text : %s +Sock Info : %s +Sock Dir Info : %s +Sock Dir List : %s +%s Start : %s +%s Duration : %s +Wait Start : %s +Wait Duration : %s +Wait Log: %s +`), + d.argText, + strings.ReplaceAll(d.sockInfo.LogString(), "\n", "\n "), + strings.ReplaceAll(d.sockDirInfo.LogString(), "\n", "\n "), + sockDirList, + d.execKind, d.execStart, + d.execKind, d.execDur, + d.waitStart, + d.waitDur, + attemptsMsg, + ) +} + +func (d *debugData) startWait() { + d.execDur = time.Since(d.execStart) + logging.Debug("Exec time before wait was %v", d.execDur) + d.waitStart = time.Now() +} + +func (d *debugData) addWaitAttempt(start time.Time, iter int, timeout time.Duration) { + attempt := &waitAttempt{ + waitStart: d.waitStart, + start: start, + iter: iter, + timeout: timeout, + } + d.waitAttempts = append(d.waitAttempts, attempt) + logging.Debug("%s", attempt) +} + +func (d *debugData) stopWait() { + d.waitDur = time.Since(d.waitStart) + logging.Debug("Wait duration: %s", d.waitDur) +} + +type fileInfo struct { + path string + fs.FileInfo + osFIErr error +} + +func newFileInfo(path string) *fileInfo { + fi := &fileInfo{path: path} + fi.FileInfo, fi.osFIErr = os.Stat(path) + return fi +} + +func (f *fileInfo) LogString() string { + if f.osFIErr != nil { + return fmt.Sprintf("Path: %s Error: %s", f.path, f.osFIErr) + } + return fmt.Sprintf("Path: %s Size: %d Mode: %s ModTime: %s", f.path, f.Size(), f.Mode(), f.ModTime()) +} + +type waitAttempt struct { + waitStart time.Time + start time.Time + iter int + timeout time.Duration +} + +func (a *waitAttempt) String() string { + return fmt.Sprintf("Attempt %2d at %10s with timeout %v", + a.iter, a.start.Sub(a.waitStart).Round(time.Microsecond), a.timeout) +} + +func (a *waitAttempt) LogString() string { + return fmt.Sprintf("%2d: %10s/%v", a.iter, a.start.Sub(a.waitStart).Round(time.Microsecond), a.timeout) +} diff --git a/internal/svcctl/svcctl.go b/internal/svcctl/svcctl.go index e3b11ace73..73eb8fd888 100644 --- a/internal/svcctl/svcctl.go +++ b/internal/svcctl/svcctl.go @@ -40,6 +40,7 @@ type IPCommunicator interface { Requester PingServer(context.Context) (time.Duration, error) StopServer(context.Context) error + SockPath() *ipc.SockPath } func NewIPCSockPathFromGlobals() *ipc.SockPath { @@ -153,12 +154,14 @@ func startAndWait(ctx context.Context, ipComm IPCommunicator, exec, argText stri args = args[:len(args)-1] } + debugInfo := newDebugData(ipComm, startSvc, argText) + if _, err := exeutils.ExecuteAndForget(exec, args); err != nil { return locale.WrapError(err, "svcctl_cannot_exec_and_forget", "Cannot execute service in background: {{.V0}}", err.Error()) } logging.Debug("Waiting for service") - if err := waitUp(ctx, ipComm); err != nil { + if err := waitUp(ctx, ipComm, debugInfo); err != nil { return locale.WrapError(err, "svcctl_wait_startup_failed", "Waiting for service startup confirmation failed") } @@ -169,19 +172,23 @@ var ( waitTimeoutL10nKey = "svcctl_wait_timeout" ) -func waitUp(ctx context.Context, ipComm IPCommunicator) error { +func waitUp(ctx context.Context, ipComm IPCommunicator, debugInfo *debugData) error { + debugInfo.startWait() + defer debugInfo.stopWait() + start := time.Now() for try := 1; try <= pingRetryIterations; try++ { select { case <-ctx.Done(): - return locale.WrapError(ctx.Err(), waitTimeoutL10nKey, "", time.Since(start).String(), "1", constants.ForumsURL) + err := locale.WrapError(ctx.Err(), waitTimeoutL10nKey, "", time.Since(start).String(), "1", constants.ForumsURL) + return errs.Pack(err, debugInfo) default: } tryStart := time.Now() timeout := time.Millisecond * time.Duration(try*try) - logging.Debug("Attempt: %d, timeout: %v, total: %v", try, timeout, time.Since(start)) + debugInfo.addWaitAttempt(tryStart, try, timeout) if err := ping(ctx, ipComm, timeout); err != nil { // Timeout does not reveal enough info, try again. // We don't need to sleep for this type of error because, @@ -190,7 +197,8 @@ func waitUp(ctx context.Context, ipComm IPCommunicator) error { continue } if !errors.Is(err, ctlErrNotUp) { - return locale.WrapError(err, "svcctl_ping_failed", "Ping encountered unexpected failure: {{.V0}}", err.Error()) + err := locale.WrapError(err, "svcctl_ping_failed", "Ping encountered unexpected failure: {{.V0}}", err.Error()) + return errs.Pack(err, debugInfo) } elapsed := time.Since(tryStart) time.Sleep(timeout - elapsed) @@ -199,35 +207,42 @@ func waitUp(ctx context.Context, ipComm IPCommunicator) error { return nil } - return locale.NewError(waitTimeoutL10nKey, "", time.Since(start).Round(time.Millisecond).String(), "2", constants.ForumsURL) + err := locale.NewError(waitTimeoutL10nKey, "", time.Since(start).Round(time.Millisecond).String(), "2", constants.ForumsURL) + return errs.Pack(err, debugInfo) } func stopAndWait(ctx context.Context, ipComm IPCommunicator) error { + debugInfo := newDebugData(ipComm, stopSvc, "") + if err := ipComm.StopServer(ctx); err != nil { return locale.WrapError(err, "svcctl_stop_req_failed", "Service stop request failed") } logging.Debug("Waiting for service to die") - if err := waitDown(ctx, ipComm); err != nil { + if err := waitDown(ctx, ipComm, debugInfo); err != nil { return locale.WrapError(err, "svcctl_wait_shutdown_failed", "Waiting for service shutdown confirmation failed") } return nil } -func waitDown(ctx context.Context, ipComm IPCommunicator) error { +func waitDown(ctx context.Context, ipComm IPCommunicator, debugInfo *debugData) error { + debugInfo.startWait() + defer debugInfo.stopWait() + start := time.Now() for try := 1; try <= pingRetryIterations; try++ { select { case <-ctx.Done(): - return locale.WrapError(ctx.Err(), waitTimeoutL10nKey, "", time.Since(start).String(), "3", constants.ForumsURL) + err := locale.WrapError(ctx.Err(), waitTimeoutL10nKey, "", time.Since(start).String(), "3", constants.ForumsURL) + return errs.Pack(err, debugInfo) default: } tryStart := time.Now() timeout := time.Millisecond * time.Duration(try*try) - logging.Debug("Attempt: %d, timeout: %v, total: %v", try, timeout, time.Since(start)) + debugInfo.addWaitAttempt(tryStart, try, timeout) if err := ping(ctx, ipComm, timeout); err != nil { // Timeout does not reveal enough info, try again. // We don't need to sleep for this type of error because, @@ -242,14 +257,16 @@ func waitDown(ctx context.Context, ipComm IPCommunicator) error { return nil } if !errors.Is(err, ctlErrTempNotUp) { - return locale.WrapError(err, "svcctl_ping_failed", "Ping encountered unexpected failure: {{.V0}}", err.Error()) + err := locale.WrapError(err, "svcctl_ping_failed", "Ping encountered unexpected failure: {{.V0}}", err.Error()) + return errs.Pack(err, debugInfo) } } elapsed := time.Since(tryStart) time.Sleep(timeout - elapsed) } - return locale.NewError(waitTimeoutL10nKey, "", time.Since(start).Round(time.Millisecond).String(), "4", constants.ForumsURL) + err := locale.NewError(waitTimeoutL10nKey, "", time.Since(start).Round(time.Millisecond).String(), "4", constants.ForumsURL) + return errs.Pack(err, debugInfo) } func ping(ctx context.Context, ipComm IPCommunicator, timeout time.Duration) error { diff --git a/internal/testhelpers/e2e/session.go b/internal/testhelpers/e2e/session.go index 663647bd24..711feb8649 100644 --- a/internal/testhelpers/e2e/session.go +++ b/internal/testhelpers/e2e/session.go @@ -526,6 +526,11 @@ func (s *Session) Close() error { } } + // Trap "flisten in use" errors to help debug DX-2090. + if contents := s.SvcLog(); strings.Contains(contents, "flisten in use") { + s.t.Fatal(s.DebugMessage("Found 'flisten in use' error in state-svc log file")) + } + return nil } @@ -614,10 +619,11 @@ func (s *Session) DebugLogs() string { return result } +var errorOrPanicRegex = regexp.MustCompile(`(?:\[ERR:|Panic:)`) + func (s *Session) DetectLogErrors() { - rx := regexp.MustCompile(`(?:\[ERR:|Panic:)`) for _, path := range s.LogFiles() { - if contents := string(fileutils.ReadFileUnsafe(path)); rx.MatchString(contents) { + if contents := string(fileutils.ReadFileUnsafe(path)); errorOrPanicRegex.MatchString(contents) { s.t.Errorf("Found error and/or panic in log file %s, contents:\n%s", path, contents) } } diff --git a/pkg/cmdlets/auth/login.go b/pkg/cmdlets/auth/login.go index 108bcebeef..8424bd7c1c 100644 --- a/pkg/cmdlets/auth/login.go +++ b/pkg/cmdlets/auth/login.go @@ -1,8 +1,10 @@ package auth import ( + "net/url" "time" + "github.com/ActiveState/cli/internal/constants" "github.com/ActiveState/cli/internal/errs" "github.com/ActiveState/cli/internal/keypairs" "github.com/ActiveState/cli/internal/locale" @@ -121,7 +123,7 @@ func RequireAuthentication(message string, cfg keypairs.Configurable, out output return errs.Wrap(err, "Authenticate failed") } case locale.T("prompt_signup_browser_action"): - if err := AuthenticateWithBrowser(out, auth, prompt); err != nil { // user can sign up from this page too + if err := SignupWithBrowser(out, auth, prompt); err != nil { return errs.Wrap(err, "Signup failed") } case locale.T("prompt_signup_action"): @@ -226,25 +228,60 @@ func promptToken(credentials *mono_models.Credentials, out output.Outputer, prom func AuthenticateWithBrowser(out output.Outputer, auth *authentication.Auth, prompt prompt.Prompter) error { logging.Debug("Authenticating with browser") + err := authenticateWithBrowser(out, auth, prompt, false) + if err != nil { + return errs.Wrap(err, "Error authenticating with browser") + } + + out.Notice(locale.T("auth_device_success")) + + return nil +} + +// authenticateWithBrowser authenticates after signup if applicable. +func authenticateWithBrowser(out output.Outputer, auth *authentication.Auth, prompt prompt.Prompter, signup bool) error { response, err := model.RequestDeviceAuthorization() if err != nil { return locale.WrapError(err, "err_auth_device") } + if response.VerificationURIComplete == nil { + return errs.New("Invalid response: Missing verification URL.") + } + + verificationURL := *response.VerificationURIComplete + if signup { + // verificationURL is of the form: + // https://platform.activestate.com/authorize/device?user-code=... + // Transform it to the form: + // https://platform.activestate.com/create-account?nextRoute=%2Fauthorize%2Fdevice%3Fuser-code%3D... + parsedURL, err := url.Parse(verificationURL) + if err != nil { + return errs.Wrap(err, "Verification URL is not valid") + } + + signupURL, err := url.Parse(constants.PlatformSignupURL) + if err != nil { + return errs.Wrap(err, "constants.PlatformSignupURL is not valid") + } + query := signupURL.Query() + query.Add("nextRoute", parsedURL.RequestURI()) + signupURL.RawQuery = query.Encode() + + verificationURL = signupURL.String() + } + // Print code to user if response.UserCode == nil { return errs.New("Invalid response: Missing user code.") } - out.Notice(locale.Tr("auth_device_verify_security_code", *response.UserCode)) + out.Notice(locale.Tr("auth_device_verify_security_code", *response.UserCode, verificationURL)) // Open URL in browser - if response.VerificationURIComplete == nil { - return errs.New("Invalid response: Missing verification URL.") - } - err = OpenURI(*response.VerificationURIComplete) + err = OpenURI(verificationURL) if err != nil { logging.Warning("Could not open browser: %v", err) - out.Notice(locale.Tr("err_browser_open", *response.VerificationURIComplete)) + out.Notice(locale.Tr("err_browser_open")) } var apiKey string @@ -275,7 +312,5 @@ func AuthenticateWithBrowser(out output.Outputer, auth *authentication.Auth, pro return locale.WrapError(err, "err_auth_token", "Failed to create token after authenticating with browser.") } - out.Notice(locale.T("auth_device_success")) - return nil } diff --git a/pkg/cmdlets/auth/signup.go b/pkg/cmdlets/auth/signup.go index fd25b08838..1ebdf43408 100644 --- a/pkg/cmdlets/auth/signup.go +++ b/pkg/cmdlets/auth/signup.go @@ -1,12 +1,13 @@ package auth import ( + "github.com/ActiveState/cli/internal/errs" "github.com/ActiveState/cli/internal/keypairs" "github.com/ActiveState/cli/internal/locale" + "github.com/ActiveState/cli/internal/logging" "github.com/ActiveState/cli/internal/multilog" "github.com/ActiveState/cli/internal/output" "github.com/ActiveState/cli/internal/prompt" - "github.com/ActiveState/cli/pkg/cmdlets/legalprompt" "github.com/ActiveState/cli/pkg/platform/api" "github.com/ActiveState/cli/pkg/platform/api/mono" @@ -165,3 +166,16 @@ func UsernameValidator(val interface{}) error { } return nil } + +func SignupWithBrowser(out output.Outputer, auth *authentication.Auth, prompt prompt.Prompter) error { + logging.Debug("Signing up with browser") + + err := authenticateWithBrowser(out, auth, prompt, true) + if err != nil { + return errs.Wrap(err, "Error signing up with browser") + } + + out.Notice(locale.Tl("auth_signup_success", "Successfully signed up and authorized this device")) + + return nil +} diff --git a/pkg/cmdlets/checkout/checkout.go b/pkg/cmdlets/checkout/checkout.go index 42068316dc..a75201bc99 100644 --- a/pkg/cmdlets/checkout/checkout.go +++ b/pkg/cmdlets/checkout/checkout.go @@ -112,7 +112,7 @@ func (r *Checkout) Run(ns *project.Namespaced, branchName, cachePath, targetPath if len(owners) == 0 { return "", locale.NewInputError("err_no_org_name", "Your project's organization name could not be found") } - owner := owners[0].DisplayName + owner := owners[0].URLName // Create the config file, if the repo clone didn't already create it configFile := filepath.Join(path, constants.ConfigFileName) diff --git a/pkg/cmdlets/commit/commit.go b/pkg/cmdlets/commit/commit.go index 6512a0ca5d..678387c107 100644 --- a/pkg/cmdlets/commit/commit.go +++ b/pkg/cmdlets/commit/commit.go @@ -105,6 +105,14 @@ func FormatChanges(commit *mono_models.Commit) ([]string, []*requirementChange) versionConstraints = "" } + // This is a temporary fix until we start getting history in the form of build expressions + // https://activestatef.atlassian.net/browse/DX-2197 + if model.NamespaceMatch(change.Namespace, model.NamespaceBuildFlagsMatch) && + (strings.Contains(change.Requirement, "docker") || strings.Contains(change.Requirement, "installer")) { + requirement = locale.T("namespace_label_packager") + versionConstraints = "" + } + var result, oldConstraints, newConstraints string switch change.Operation { case string(model.OperationAdded): diff --git a/pkg/cmdlets/errors/errors.go b/pkg/cmdlets/errors/errors.go index a47b2747c0..7c2691de79 100644 --- a/pkg/cmdlets/errors/errors.go +++ b/pkg/cmdlets/errors/errors.go @@ -39,19 +39,29 @@ func (o *OutputError) MarshalOutput(f output.Format) interface{} { outLines = append(outLines, output.Title(locale.Tl("err_what_happened", "[ERROR]Something Went Wrong[/RESET]")).String()) } - rerrs := locale.UnpackError(o.error) - if len(rerrs) == 0 { - // It's possible the error came from cobra or something else low level that doesn't use localization - logging.Warning("Error does not have localization: %s", errs.JoinMessage(o.error)) - rerrs = []error{o.error} - } - for _, errv := range rerrs { - message := trimError(locale.ErrorMessage(errv)) + var userFacingError errs.UserFacingError + if errors.As(o.error, &userFacingError) { + message := userFacingError.UserError() if f == output.PlainFormatName { outLines = append(outLines, fmt.Sprintf(" [NOTICE][ERROR]x[/RESET] %s", message)) } else { outLines = append(outLines, message) } + } else { + rerrs := locale.UnpackError(o.error) + if len(rerrs) == 0 { + // It's possible the error came from cobra or something else low level that doesn't use localization + logging.Warning("Error does not have localization: %s", errs.JoinMessage(o.error)) + rerrs = []error{o.error} + } + for _, errv := range rerrs { + message := trimError(locale.ErrorMessage(errv)) + if f == output.PlainFormatName { + outLines = append(outLines, fmt.Sprintf(" [NOTICE][ERROR]x[/RESET] %s", message)) + } else { + outLines = append(outLines, message) + } + } } // Concatenate error tips @@ -103,6 +113,16 @@ func ParseUserFacing(err error) (int, error) { return code, nil } + // If there is a user facing error in the error stack we want to ensure + // that is it forwarded to the user. + var userFacingError errs.UserFacingError + if errors.As(err, &userFacingError) { + logging.Debug("Returning user facing error, error stack: \n%s", errs.JoinMessage(err)) + return code, &OutputError{userFacingError} + } + + // If the error already has a marshalling function we do not want to wrap + // it again in the OutputError type. if hasMarshaller { return code, err } @@ -157,7 +177,7 @@ func ReportError(err error, cmd *captain.Command, an analytics.Dispatcher) { Error: ptr.To(errorMsg), }) - if !locale.HasError(err) && isErrs && !hasMarshaller { + if (!locale.HasError(err) && !errs.IsUserFacing(err)) && isErrs && !hasMarshaller { multilog.Error("MUST ADDRESS: Error does not have localization: %s", errs.JoinMessage(err)) // If this wasn't built via CI then this is a dev workstation, and we should be more aggressive diff --git a/pkg/platform/api/buildplanner/model/buildplan.go b/pkg/platform/api/buildplanner/model/buildplan.go index 8c7f64b137..bb4376ba79 100644 --- a/pkg/platform/api/buildplanner/model/buildplan.go +++ b/pkg/platform/api/buildplanner/model/buildplan.go @@ -92,6 +92,7 @@ func (o Operation) String() string { } type BuildPlannerError struct { + Err error ValidationErrors []string IsTransient bool } @@ -106,11 +107,15 @@ func (e *BuildPlannerError) InputError() bool { // UserError returns the error message to be displayed to the user. // This function is added so that BuildPlannerErrors will be displayed // to the user -func (e *BuildPlannerError) UserError() string { +func (e *BuildPlannerError) LocalizedError() string { return e.Error() } func (e *BuildPlannerError) Error() string { + if e.Err != nil { + return e.Err.Error() + } + // Append last five lines to error message offset := 0 numLines := len(e.ValidationErrors) @@ -273,10 +278,6 @@ func ProcessCommitError(commit *Commit, fallbackMessage string) error { } func ProcessBuildError(build *Build, fallbackMessage string) error { - if build.Error == nil { - return errs.New(fallbackMessage) - } - if build.Type == PlanningErrorType { var errs []string var isTransient bool @@ -299,18 +300,19 @@ func ProcessBuildError(build *Build, fallbackMessage string) error { ValidationErrors: errs, IsTransient: isTransient, } + } else if build.Error == nil { + return errs.New(fallbackMessage) } return locale.NewInputError("err_buildplanner_build", "Encountered error processing build response") } func ProcessProjectError(project *Project, fallbackMessage string) error { - if project.Error == nil { - return errs.New(fallbackMessage) - } - if project.Type == NotFoundErrorType { - return locale.NewInputError("err_buildplanner_project_not_found", "Unable to find project, recieved message: {{.V0}}", project.Message) + return errs.AddTips( + locale.NewInputError("err_buildplanner_project_not_found", "Unable to find project, received message: {{.V0}}", project.Message), + locale.T("tip_private_project_auth"), + ) } return errs.New(fallbackMessage) @@ -484,7 +486,7 @@ type Artifact struct { // Error fields Errors []string `json:"errors"` - Attempts string `json:"attempts"` + Attempts float64 `json:"attempts"` NextAttempt string `json:"nextAttempt"` } diff --git a/pkg/platform/api/svc/request/analytics.go b/pkg/platform/api/svc/request/analytics.go index 386319d12a..713ebd2b87 100644 --- a/pkg/platform/api/svc/request/analytics.go +++ b/pkg/platform/api/svc/request/analytics.go @@ -1,26 +1,28 @@ package request type AnalyticsEvent struct { - category string - action string - label string - dimensionsJson string - outputType string - userID string + category string + action string + source string + label string + dimensionsJson string + outputType string + userID string } -func NewAnalyticsEvent(category, action, label, dimensionsJson string) *AnalyticsEvent { +func NewAnalyticsEvent(category, action, source, label, dimensionsJson string) *AnalyticsEvent { return &AnalyticsEvent{ - category: category, - action: action, - label: label, - dimensionsJson: dimensionsJson, + category: category, + action: action, + source: source, + label: label, + dimensionsJson: dimensionsJson, } } func (e *AnalyticsEvent) Query() string { - return `query($category: String!, $action: String!, $label: String, $dimensionsJson: String!) { - analyticsEvent(category: $category, action: $action, label: $label, dimensionsJson: $dimensionsJson) { + return `query($category: String!, $action: String!, $source: String!, $label: String, $dimensionsJson: String!) { + analyticsEvent(category: $category, action: $action, source: $source, label: $label, dimensionsJson: $dimensionsJson) { sent } }` @@ -28,9 +30,10 @@ func (e *AnalyticsEvent) Query() string { func (e *AnalyticsEvent) Vars() map[string]interface{} { return map[string]interface{}{ - "category": e.category, - "action": e.action, - "label": e.label, + "category": e.category, + "action": e.action, + "source": e.source, + "label": e.label, "dimensionsJson": e.dimensionsJson, } } diff --git a/pkg/platform/api/svc/request/reportrtusage.go b/pkg/platform/api/svc/request/reportrtusage.go index 48ff8b4da8..027c04b6ca 100644 --- a/pkg/platform/api/svc/request/reportrtusage.go +++ b/pkg/platform/api/svc/request/reportrtusage.go @@ -3,20 +3,22 @@ package request type ReportRuntimeUsage struct { pid int exec string + source string dimensionsJson string } -func NewReportRuntimeUsage(pid int, exec string, dimensionsJson string) *ReportRuntimeUsage { +func NewReportRuntimeUsage(pid int, exec, source string, dimensionsJson string) *ReportRuntimeUsage { return &ReportRuntimeUsage{ pid: pid, exec: exec, + source: source, dimensionsJson: dimensionsJson, } } func (e *ReportRuntimeUsage) Query() string { - return `query($pid: Int!, $exec: String!, $dimensionsJson: String!) { - reportRuntimeUsage(pid: $pid, exec: $exec, dimensionsJson: $dimensionsJson) { + return `query($pid: Int!, $exec: String!, $source: String!, $dimensionsJson: String!) { + reportRuntimeUsage(pid: $pid, exec: $exec, source: $source, dimensionsJson: $dimensionsJson) { received } }` @@ -26,6 +28,7 @@ func (e *ReportRuntimeUsage) Vars() map[string]interface{} { return map[string]interface{}{ "pid": e.pid, "exec": e.exec, + "source": e.source, "dimensionsJson": e.dimensionsJson, } } diff --git a/pkg/platform/model/buildplanner.go b/pkg/platform/model/buildplanner.go index e6d72eee51..cddded07d7 100644 --- a/pkg/platform/model/buildplanner.go +++ b/pkg/platform/model/buildplanner.go @@ -367,10 +367,10 @@ func processBuildPlannerError(bpErr error, fallbackMessage string) error { if errors.As(bpErr, graphqlErr) { code, ok := graphqlErr.Extensions[codeExtensionKey].(string) if ok && code == clientDeprecationErrorKey { - return locale.NewInputError("err_buildplanner_deprecated", "Encountered deprecation error: {{.V0}}", graphqlErr.Message) + return &bpModel.BuildPlannerError{Err: locale.NewInputError("err_buildplanner_deprecated", "Encountered deprecation error: {{.V0}}", graphqlErr.Message)} } } - return errs.Wrap(bpErr, fallbackMessage) + return &bpModel.BuildPlannerError{Err: errs.Wrap(bpErr, fallbackMessage)} } var versionRe = regexp.MustCompile(`^\d+(\.\d+)*$`) diff --git a/pkg/platform/model/svc.go b/pkg/platform/model/svc.go index a16bd84801..4a86d8b5f5 100644 --- a/pkg/platform/model/svc.go +++ b/pkg/platform/model/svc.go @@ -95,10 +95,10 @@ func (m *SvcModel) Ping() error { return err } -func (m *SvcModel) AnalyticsEvent(ctx context.Context, category, action, label string, dimJson string) error { +func (m *SvcModel) AnalyticsEvent(ctx context.Context, category, action, source, label string, dimJson string) error { defer profile.Measure("svc:analyticsEvent", time.Now()) - r := request.NewAnalyticsEvent(category, action, label, dimJson) + r := request.NewAnalyticsEvent(category, action, source, label, dimJson) u := graph.AnalyticsEventResponse{} if err := m.request(ctx, r, &u); err != nil { return errs.Wrap(err, "Error sending analytics event via state-svc") @@ -107,10 +107,10 @@ func (m *SvcModel) AnalyticsEvent(ctx context.Context, category, action, label s return nil } -func (m *SvcModel) ReportRuntimeUsage(ctx context.Context, pid int, exec string, dimJson string) error { +func (m *SvcModel) ReportRuntimeUsage(ctx context.Context, pid int, exec, source string, dimJson string) error { defer profile.Measure("svc:ReportRuntimeUsage", time.Now()) - r := request.NewReportRuntimeUsage(pid, exec, dimJson) + r := request.NewReportRuntimeUsage(pid, exec, source, dimJson) u := graph.ReportRuntimeUsageResponse{} if err := m.request(ctx, r, &u); err != nil { return errs.Wrap(err, "Error sending report runtime usage event via state-svc") diff --git a/pkg/platform/model/vcs.go b/pkg/platform/model/vcs.go index c82b7e160f..f42acd88de 100644 --- a/pkg/platform/model/vcs.go +++ b/pkg/platform/model/vcs.go @@ -85,6 +85,9 @@ const ( // NamespaceSharedMatch is the namespace used for shared requirements (usually runtime libraries) NamespaceSharedMatch = `^shared$` + + // NamespaceBuildFlagsMatch is the namespace used for passing build flags + NamespaceBuildFlagsMatch = `^build-flags$` ) type TrackingType string diff --git a/pkg/platform/runtime/runtime.go b/pkg/platform/runtime/runtime.go index 95a2a7b813..4ccb330ed8 100644 --- a/pkg/platform/runtime/runtime.go +++ b/pkg/platform/runtime/runtime.go @@ -192,7 +192,7 @@ func (r *Runtime) recordCompletion(err error) { errorType = "input" case errs.Matches(err, &model.SolverError{}): errorType = "solve" - case errs.Matches(err, &setup.BuildError{}) || errs.Matches(err, &buildlog.BuildError{}): + case errs.Matches(err, &setup.BuildError{}), errs.Matches(err, &buildlog.BuildError{}): errorType = "build" case errs.Matches(err, &bpModel.BuildPlannerError{}): errorType = "buildplan" @@ -206,6 +206,9 @@ func (r *Runtime) recordCompletion(err error) { case errs.Matches(err, &setup.ArtifactInstallError{}): errorType = "install" // Note: do not break because there could be download errors, and those take precedence + case errs.Matches(err, &setup.BuildError{}), errs.Matches(err, &buildlog.BuildError{}): + errorType = "build" + break // it only takes one build failure to report the runtime failure as due to build error } } } @@ -213,6 +216,8 @@ func (r *Runtime) recordCompletion(err error) { // and those errors actually caused the failure, not these. case errs.Matches(err, &setup.ProgressReportError{}) || errs.Matches(err, &buildlog.EventHandlerError{}): errorType = "progress" + case errs.Matches(err, &setup.ExecutorSetupError{}): + errorType = "postprocess" } var message string @@ -242,7 +247,7 @@ func (r *Runtime) recordUsage() { multilog.Critical("Could not marshal dimensions for runtime-usage: %s", errs.JoinMessage(err)) } if r.svcm != nil { - r.svcm.ReportRuntimeUsage(context.Background(), os.Getpid(), osutils.Executable(), dimsJson) + r.svcm.ReportRuntimeUsage(context.Background(), os.Getpid(), osutils.Executable(), anaConsts.SrcStateTool, dimsJson) } } diff --git a/pkg/platform/runtime/setup/setup.go b/pkg/platform/runtime/setup/setup.go index 15b85b36f6..accba52564 100644 --- a/pkg/platform/runtime/setup/setup.go +++ b/pkg/platform/runtime/setup/setup.go @@ -12,9 +12,6 @@ import ( "sync" "time" - bpModel "github.com/ActiveState/cli/pkg/platform/api/buildplanner/model" - "github.com/ActiveState/cli/pkg/platform/model" - "github.com/ActiveState/cli/internal/analytics" anaConsts "github.com/ActiveState/cli/internal/analytics/constants" "github.com/ActiveState/cli/internal/analytics/dimensions" @@ -31,8 +28,10 @@ import ( "github.com/ActiveState/cli/internal/rtutils/ptr" "github.com/ActiveState/cli/internal/svcctl" "github.com/ActiveState/cli/internal/unarchiver" + bpModel "github.com/ActiveState/cli/pkg/platform/api/buildplanner/model" "github.com/ActiveState/cli/pkg/platform/api/inventory/inventory_models" "github.com/ActiveState/cli/pkg/platform/authentication" + "github.com/ActiveState/cli/pkg/platform/model" apimodel "github.com/ActiveState/cli/pkg/platform/model" "github.com/ActiveState/cli/pkg/platform/runtime/artifact" "github.com/ActiveState/cli/pkg/platform/runtime/artifactcache" @@ -79,6 +78,10 @@ type ArtifactSetupErrors struct { errs []error } +type ExecutorSetupError struct { + *errs.WrapperError +} + func (a *ArtifactSetupErrors) Error() string { var errors []string for _, err := range a.errs { @@ -93,7 +96,7 @@ func (a *ArtifactSetupErrors) Errors() []error { } // UserError returns a message including all user-facing sub-error messages -func (a *ArtifactSetupErrors) UserError() string { +func (a *ArtifactSetupErrors) LocalizedError() string { var errStrings []string for _, err := range a.errs { errStrings = append(errStrings, locale.JoinedErrorMessage(err)) @@ -189,7 +192,7 @@ func (s *Setup) Update() (rerr error) { // Update executors if err := s.updateExecutors(artifacts); err != nil { - return errs.Wrap(err, "Failed to update executors") + return ExecutorSetupError{errs.Wrap(err, "Failed to update executors")} } // Mark installation as completed @@ -481,7 +484,7 @@ func (s *Setup) fetchAndInstallArtifactsFromBuildPlan(installFunc artifactInstal ) artifactsToInstall := []artifact.ArtifactID{} - buildtimeArtifacts := runtimeArtifacts + // buildtimeArtifacts := runtimeArtifacts if buildResult.BuildReady { // If the build is already done we can just look at the downloadable artifacts as they will be a fully accurate // prediction of what we will be installing. @@ -498,13 +501,6 @@ func (s *Setup) fetchAndInstallArtifactsFromBuildPlan(installFunc artifactInstal artifactsToInstall = append(artifactsToInstall, a.ArtifactID) } } - - // We also caclulate the artifacts to be built which includes more than the runtime artifacts. - // This is used to determine if we need to show the "build in progress" screen. - buildtimeArtifacts, err = buildplan.BuildtimeArtifacts(buildResult.Build, false) - if err != nil { - return nil, nil, errs.Wrap(err, "Could not get buildtime artifacts") - } } // The log file we want to use for builds @@ -521,7 +517,7 @@ func (s *Setup) fetchAndInstallArtifactsFromBuildPlan(installFunc artifactInstal ArtifactNames: artifactNames, LogFilePath: logFilePath, ArtifactsToBuild: func() []artifact.ArtifactID { - return artifact.ArtifactIDsFromBuildPlanMap(buildtimeArtifacts) // This does not account for cached builds + return artifact.ArtifactIDsFromBuildPlanMap(runtimeArtifacts) // This does not account for cached builds }(), // Yes these have the same value; this is intentional. // Separating these out just allows us to be more explicit and intentional in our event handling logic. @@ -537,7 +533,7 @@ func (s *Setup) fetchAndInstallArtifactsFromBuildPlan(installFunc artifactInstal // only send the download analytics event, if we have to install artifacts that are not yet installed if len(artifactsToInstall) > 0 { - // if we get here, we dowload artifacts + // if we get here, we download artifacts s.analytics.Event(anaConsts.CatRuntime, anaConsts.ActRuntimeDownload, dimensions) } @@ -745,12 +741,16 @@ func (s *Setup) moveToInstallPath(a artifact.ArtifactID, unpackedDir string, env func (s *Setup) downloadArtifact(a artifact.ArtifactDownload, targetFile string) (rerr error) { defer func() { if rerr != nil { - rerr = &ArtifactDownloadError{errs.Wrap(rerr, "Unable to download artifact")} + if !errs.Matches(rerr, &ProgressReportError{}) { + rerr = &ArtifactDownloadError{errs.Wrap(rerr, "Unable to download artifact")} + } + if err := s.handleEvent(events.ArtifactDownloadFailure{a.ArtifactID, rerr}); err != nil { rerr = errs.Wrap(rerr, "Could not handle ArtifactDownloadFailure event") return } } + if err := s.handleEvent(events.ArtifactDownloadSuccess{a.ArtifactID}); err != nil { rerr = errs.Wrap(rerr, "Could not handle ArtifactDownloadSuccess event") return @@ -765,7 +765,7 @@ func (s *Setup) downloadArtifact(a artifact.ArtifactDownload, targetFile string) b, err := httputil.GetWithProgress(artifactURL.String(), &progress.Report{ ReportSizeCb: func(size int) error { if err := s.handleEvent(events.ArtifactDownloadStarted{a.ArtifactID, size}); err != nil { - return errs.Wrap(err, "Could not handle ArtifactDownloadStarted event") + return ProgressReportError{errs.Wrap(err, "Could not handle ArtifactDownloadStarted event")} } return nil }, @@ -779,9 +779,11 @@ func (s *Setup) downloadArtifact(a artifact.ArtifactDownload, targetFile string) if err != nil { return errs.Wrap(err, "Download %s failed", artifactURL.String()) } + if err := fileutils.WriteFile(targetFile, b); err != nil { return errs.Wrap(err, "Writing download to target file %s failed", targetFile) } + return nil } diff --git a/test/integration/analytics_int_test.go b/test/integration/analytics_int_test.go index ab8e675c7b..0ae8d6196a 100644 --- a/test/integration/analytics_int_test.go +++ b/test/integration/analytics_int_test.go @@ -82,16 +82,16 @@ func (suite *AnalyticsIntegrationTestSuite) TestActivateEvents() { suite.Require().NotEmpty(events) // Runtime:start events - suite.assertNEvents(events, 1, anaConst.CatRuntime, anaConst.ActRuntimeStart, + suite.assertNEvents(events, 1, anaConst.CatRuntime, anaConst.ActRuntimeStart, anaConst.SrcStateTool, fmt.Sprintf("output:\n%s\n%s", cp.Snapshot(), ts.DebugLogs())) // Runtime:success events - suite.assertNEvents(events, 1, anaConst.CatRuntime, anaConst.ActRuntimeSuccess, + suite.assertNEvents(events, 1, anaConst.CatRuntime, anaConst.ActRuntimeSuccess, anaConst.SrcStateTool, fmt.Sprintf("output:\n%s\n%s", cp.Snapshot(), ts.DebugLogs())) - heartbeatInitialCount := countEvents(events, anaConst.CatRuntimeUsage, anaConst.ActRuntimeHeartbeat) + heartbeatInitialCount := countEvents(events, anaConst.CatRuntimeUsage, anaConst.ActRuntimeHeartbeat, anaConst.SrcStateTool) if heartbeatInitialCount < 2 { // It's possible due to the timing of the heartbeats and the fact that they are async that we have gotten either // one or two by this point. Technically more is possible, just very unlikely. @@ -104,7 +104,7 @@ func (suite *AnalyticsIntegrationTestSuite) TestActivateEvents() { suite.Require().NotEmpty(events) // Runtime-use:heartbeat events - should now be at least +1 because we waited - suite.assertGtEvents(events, heartbeatInitialCount, anaConst.CatRuntimeUsage, anaConst.ActRuntimeHeartbeat, + suite.assertGtEvents(events, heartbeatInitialCount, anaConst.CatRuntimeUsage, anaConst.ActRuntimeHeartbeat, anaConst.SrcStateTool, fmt.Sprintf("output:\n%s\n%s", cp.Snapshot(), ts.DebugLogs())) @@ -121,11 +121,11 @@ func (suite *AnalyticsIntegrationTestSuite) TestActivateEvents() { } return (*e.Dimensions.Trigger) == target.TriggerExecutor.String() }) - suite.Require().Equal(1, countEvents(executorEvents, anaConst.CatRuntimeUsage, anaConst.ActRuntimeAttempt), + suite.Require().Equal(1, countEvents(executorEvents, anaConst.CatRuntimeUsage, anaConst.ActRuntimeAttempt, anaConst.SrcExecutor), ts.DebugMessage("Should have a runtime attempt, events:\n"+debugEvents(suite.T(), executorEvents))) // It's possible due to the timing of the heartbeats and the fact that they are async that we have gotten either // one or two by this point. Technically more is possible, just very unlikely. - numHeartbeats := countEvents(executorEvents, anaConst.CatRuntimeUsage, anaConst.ActRuntimeHeartbeat) + numHeartbeats := countEvents(executorEvents, anaConst.CatRuntimeUsage, anaConst.ActRuntimeHeartbeat, anaConst.SrcExecutor) suite.Require().Greater(numHeartbeats, 0, "Should have a heartbeat") suite.Require().LessOrEqual(numHeartbeats, 2, "Should not have excessive heartbeats") var heartbeatEvent *reporters.TestLogEntry @@ -153,13 +153,13 @@ func (suite *AnalyticsIntegrationTestSuite) TestActivateEvents() { events = parseAnalyticsEvents(suite, ts) suite.Require().NotEmpty(events) - eventsAfterExit := countEvents(events, anaConst.CatRuntimeUsage, anaConst.ActRuntimeHeartbeat) + eventsAfterExit := countEvents(events, anaConst.CatRuntimeUsage, anaConst.ActRuntimeHeartbeat, anaConst.SrcExecutor) time.Sleep(sleepTime) eventsAfter := parseAnalyticsEvents(suite, ts) suite.Require().NotEmpty(eventsAfter) - eventsAfterWait := countEvents(eventsAfter, anaConst.CatRuntimeUsage, anaConst.ActRuntimeHeartbeat) + eventsAfterWait := countEvents(eventsAfter, anaConst.CatRuntimeUsage, anaConst.ActRuntimeHeartbeat, anaConst.SrcExecutor) suite.Equal(eventsAfterExit, eventsAfterWait, fmt.Sprintf("Heartbeats should stop ticking after exiting subshell.\n"+ @@ -177,9 +177,9 @@ func (suite *AnalyticsIntegrationTestSuite) TestActivateEvents() { suite.assertSequentialEvents(events) } -func countEvents(events []reporters.TestLogEntry, category, action string) int { +func countEvents(events []reporters.TestLogEntry, category, action, source string) int { filteredEvents := funk.Filter(events, func(e reporters.TestLogEntry) bool { - return e.Category == category && e.Action == action + return e.Category == category && e.Action == action && e.Source == source }).([]reporters.TestLogEntry) return len(filteredEvents) } @@ -203,15 +203,15 @@ func filterEvents(events []reporters.TestLogEntry, filters ...func(e reporters.T } func (suite *AnalyticsIntegrationTestSuite) assertNEvents(events []reporters.TestLogEntry, - expectedN int, category, action string, errMsg string) { - suite.Assert().Equal(expectedN, countEvents(events, category, action), + expectedN int, category, action, source string, errMsg string) { + suite.Assert().Equal(expectedN, countEvents(events, category, action, source), "Expected %d %s:%s events.\nFile location: %s\nEvents received:\n%s\nError:\n%s", expectedN, category, action, suite.eventsfile, suite.summarizeEvents(events), errMsg) } func (suite *AnalyticsIntegrationTestSuite) assertGtEvents(events []reporters.TestLogEntry, - greaterThanN int, category, action string, errMsg string) { - suite.Assert().Greater(countEvents(events, category, action), greaterThanN, + greaterThanN int, category, action, source string, errMsg string) { + suite.Assert().Greater(countEvents(events, category, action, source), greaterThanN, fmt.Sprintf("Expected more than %d %s:%s events.\nFile location: %s\nEvents received:\n%s\nError:\n%s", greaterThanN, category, action, suite.eventsfile, suite.summarizeEvents(events), errMsg)) } @@ -254,7 +254,7 @@ func (suite *AnalyticsIntegrationTestSuite) assertSequentialEvents(events []repo func (suite *AnalyticsIntegrationTestSuite) summarizeEvents(events []reporters.TestLogEntry) string { summary := []string{} for _, event := range events { - summary = append(summary, fmt.Sprintf("%s:%s:%s", event.Category, event.Action, event.Label)) + summary = append(summary, fmt.Sprintf("%s:%s:%s (%s)", event.Category, event.Action, event.Label, event.Source)) } return strings.Join(summary, "\n") } @@ -262,8 +262,8 @@ func (suite *AnalyticsIntegrationTestSuite) summarizeEvents(events []reporters.T func (suite *AnalyticsIntegrationTestSuite) summarizeEventSequence(events []reporters.TestLogEntry) string { summary := []string{} for _, event := range events { - summary = append(summary, fmt.Sprintf("%s:%s:%s (seq: %s:%s:%d)\n", - event.Category, event.Action, event.Label, + summary = append(summary, fmt.Sprintf("%s:%s:%s (%s seq: %s:%s:%d)\n", + event.Category, event.Action, event.Label, event.Source, *event.Dimensions.Command, (*event.Dimensions.InstanceID)[0:6], *event.Dimensions.Sequence)) } return strings.Join(summary, "\n") @@ -423,7 +423,7 @@ func (suite *AnalyticsIntegrationTestSuite) TestInputError() { events := parseAnalyticsEvents(suite, ts) suite.assertSequentialEvents(events) - suite.assertNEvents(events, 1, anaConst.CatDebug, anaConst.ActCommandInputError, + suite.assertNEvents(events, 1, anaConst.CatDebug, anaConst.ActCommandInputError, anaConst.SrcStateTool, fmt.Sprintf("output:\n%s\n%s", cp.Snapshot(), ts.DebugLogs())) @@ -466,7 +466,7 @@ func (suite *AnalyticsIntegrationTestSuite) TestAttempts() { for _, e := range events { if strings.Contains(e.Category, "runtime") && strings.Contains(e.Action, "attempt") { foundAttempts++ - if strings.Contains(*e.Dimensions.Trigger, "exec") { + if strings.Contains(*e.Dimensions.Trigger, "exec") && strings.Contains(e.Source, anaConst.SrcExecutor) { foundExecs++ } } @@ -564,8 +564,8 @@ func (suite *AnalyticsIntegrationTestSuite) TestConfigEvents() { suite.Fail("Should find multiple config events") } - suite.assertNEvents(events, 1, anaConst.CatConfig, anaConst.ActConfigSet, "Should be at one config set event") - suite.assertNEvents(events, 1, anaConst.CatConfig, anaConst.ActConfigUnset, "Should be at one config unset event") + suite.assertNEvents(events, 1, anaConst.CatConfig, anaConst.ActConfigSet, anaConst.SrcStateTool, "Should be at one config set event") + suite.assertNEvents(events, 1, anaConst.CatConfig, anaConst.ActConfigUnset, anaConst.SrcStateTool, "Should be at one config unset event") suite.assertSequentialEvents(events) } @@ -601,6 +601,8 @@ func (suite *AnalyticsIntegrationTestSuite) TestCIAndInteractiveDimensions() { } suite.Equal(condition.OnCI(), *e.Dimensions.CI, "analytics should report being on CI") suite.Equal(interactive, *e.Dimensions.Interactive, "analytics did not report the correct interactive value for %v", e) + suite.Equal(condition.OnCI(), // not InActiveStateCI() because if it's false, we forgot to set ACTIVESTATE_CI env var in GitHub Actions scripts + *e.Dimensions.ActiveStateCI, "analytics did not report being in ActiveState CI") processedAnEvent = true } suite.True(processedAnEvent, "did not actually test CI and Interactive dimensions") diff --git a/test/integration/offinstall_int_test.go b/test/integration/offinstall_int_test.go index ec06707b04..b79c7da7f5 100644 --- a/test/integration/offinstall_int_test.go +++ b/test/integration/offinstall_int_test.go @@ -97,7 +97,7 @@ func (suite *OffInstallIntegrationTestSuite) TestInstallAndUninstall() { heartbeat := suite.filterEvent(events, anaConst.CatRuntimeUsage, anaConst.ActRuntimeHeartbeat) suite.assertDimensions(heartbeat) - nDelete := countEvents(events, anaConst.CatRuntimeUsage, anaConst.ActRuntimeDelete) + nDelete := countEvents(events, anaConst.CatRuntimeUsage, anaConst.ActRuntimeDelete, anaConst.SrcOfflineInstaller) if nDelete != 0 { suite.FailNow(fmt.Sprintf("Expected 0 delete events, got %d, events:\n%#v", nDelete, events)) } @@ -147,7 +147,7 @@ func (suite *OffInstallIntegrationTestSuite) TestInstallAndUninstall() { // Verify that our analytics event was fired events := parseAnalyticsEvents(suite, ts) suite.Require().NotEmpty(events) - nHeartbeat := countEvents(events, anaConst.CatRuntimeUsage, anaConst.ActRuntimeHeartbeat) + nHeartbeat := countEvents(events, anaConst.CatRuntimeUsage, anaConst.ActRuntimeHeartbeat, anaConst.SrcExecutor) if nHeartbeat != 1 { suite.FailNow(fmt.Sprintf("Expected 1 heartbeat event, got %d, events:\n%#v", nHeartbeat, events)) } diff --git a/test/integration/performance_svc_int_test.go b/test/integration/performance_svc_int_test.go index de71104e59..e2fdfb96f2 100644 --- a/test/integration/performance_svc_int_test.go +++ b/test/integration/performance_svc_int_test.go @@ -78,7 +78,7 @@ func (suite *PerformanceIntegrationTestSuite) TestSvcPerformance() { suite.Run("Query Analytics", func() { t := time.Now() - err := svcmodel.AnalyticsEvent(context.Background(), "performance-test", "performance-test", "performance-test", "{}") + err := svcmodel.AnalyticsEvent(context.Background(), "performance-test", "performance-test", "performance-test", "performance-test", "{}") suite.Require().NoError(err, ts.DebugMessage(fmt.Sprintf("Error: %s\nLog Tail:\n%s", errs.JoinMessage(err), logging.ReadTail()))) duration := time.Since(t)