File: operation_run.go

package info (click to toggle)
vagrant 2.3.7%2Bgit20230731.5fc64cde%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 17,616 kB
  • sloc: ruby: 111,820; sh: 462; makefile: 123; ansic: 34; lisp: 1
file content (59 lines) | stat: -rw-r--r-- 1,509 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package runner

import (
	"context"
	"fmt"

	"github.com/hashicorp/vagrant/internal/core"
	"github.com/hashicorp/vagrant/internal/server/proto/vagrant_server"
	"google.golang.org/genproto/googleapis/rpc/status"
	"google.golang.org/grpc/codes"
)

type Runs interface {
	Run(context.Context, *vagrant_server.Job_CommandOp) error
}

// Keeping this around as an example
func (r *Runner) executeRunOp(
	ctx context.Context,
	job *vagrant_server.Job,
	scope Runs,
) (result *vagrant_server.Job_Result, err error) {
	r.logger.Debug("starting execution of run operation", "scope", scope, "job", job)

	op, ok := job.Operation.(*vagrant_server.Job_Command) //op
	if !ok {
		// this shouldn't happen since the call to this function is gated
		// on the above type match.
		panic("operation not expected type")
	}

	var jrr vagrant_server.Job_CommandResult

	err = scope.Run(ctx, op.Command)

	r.logger.Debug("execution of run operation complete", "job", job, "error", err)

	jrr.RunResult = err == nil
	if err != nil {
		if cmdErr, ok := err.(core.CommandError); ok {
			jrr.RunError = err.(core.CommandError).Status()
			jrr.ExitCode = int32(cmdErr.ExitCode())
		} else {
			// If we have an error without a status we'll make one here
			jrr.RunError = &status.Status{
				Code:    int32(codes.Unknown),
				Message: fmt.Sprintf("Unexpected error from run operation: %s", err),
			}
			jrr.ExitCode = 1
		}

	}

	r.logger.Info("run operation is complete!")

	return &vagrant_server.Job_Result{
		Run: &jrr,
	}, nil
}