|
| 1 | +package evaluator |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/cirruslabs/cirrus-ci-agent/api" |
| 6 | + "github.com/cirruslabs/cirrus-cli/pkg/larker" |
| 7 | + "github.com/cirruslabs/cirrus-cli/pkg/larker/fs" |
| 8 | + "github.com/cirruslabs/cirrus-cli/pkg/larker/fs/dummy" |
| 9 | + "github.com/cirruslabs/cirrus-cli/pkg/larker/fs/github" |
| 10 | + "github.com/cirruslabs/cirrus-cli/pkg/parser" |
| 11 | + "google.golang.org/grpc" |
| 12 | + "google.golang.org/grpc/codes" |
| 13 | + "google.golang.org/grpc/status" |
| 14 | + "net" |
| 15 | + "strings" |
| 16 | +) |
| 17 | + |
| 18 | +func Serve(ctx context.Context, lis net.Listener) error { |
| 19 | + server := grpc.NewServer() |
| 20 | + |
| 21 | + api.RegisterCirrusConfigurationEvaluatorServiceService(server, &api.CirrusConfigurationEvaluatorServiceService{ |
| 22 | + EvaluateConfig: evaluateConfig, |
| 23 | + }) |
| 24 | + |
| 25 | + errChan := make(chan error) |
| 26 | + |
| 27 | + go func() { |
| 28 | + errChan <- server.Serve(lis) |
| 29 | + }() |
| 30 | + |
| 31 | + select { |
| 32 | + case <-ctx.Done(): |
| 33 | + server.GracefulStop() |
| 34 | + |
| 35 | + if err := <-errChan; err != nil { |
| 36 | + return err |
| 37 | + } |
| 38 | + |
| 39 | + return ctx.Err() |
| 40 | + case err := <-errChan: |
| 41 | + return err |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func fsFromEnvironment(env map[string]string) (fs fs.FileSystem) { |
| 46 | + // Fallback to dummy filesystem implementation |
| 47 | + fs = dummy.New() |
| 48 | + |
| 49 | + // Use GitHub filesystem if all the required variables are present |
| 50 | + owner, ok := env["CIRRUS_REPO_OWNER"] |
| 51 | + if !ok { |
| 52 | + return |
| 53 | + } |
| 54 | + repo, ok := env["CIRRUS_REPO_NAME"] |
| 55 | + if !ok { |
| 56 | + return |
| 57 | + } |
| 58 | + reference, ok := env["CIRRUS_CHANGE_IN_REPO"] |
| 59 | + if !ok { |
| 60 | + return |
| 61 | + } |
| 62 | + token, ok := env["CIRRUS_REPO_CLONE_TOKEN"] |
| 63 | + if !ok { |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + return github.New(owner, repo, reference, token) |
| 68 | +} |
| 69 | + |
| 70 | +func evaluateConfig(ctx context.Context, request *api.EvaluateConfigRequest) (*api.EvaluateConfigResponse, error) { |
| 71 | + var yamlConfigs []string |
| 72 | + |
| 73 | + // Register YAML configuration (if any) |
| 74 | + if request.YamlConfig != "" { |
| 75 | + yamlConfigs = append(yamlConfigs, request.YamlConfig) |
| 76 | + } |
| 77 | + |
| 78 | + fs := fsFromEnvironment(request.Environment) |
| 79 | + |
| 80 | + // Run Starlark script and register generated YAML configuration (if any) |
| 81 | + if request.StarlarkConfig != "" { |
| 82 | + lrk := larker.New( |
| 83 | + larker.WithFileSystem(fs), |
| 84 | + larker.WithEnvironment(request.Environment), |
| 85 | + ) |
| 86 | + |
| 87 | + generatedYamlConfig, err := lrk.Main(ctx, request.StarlarkConfig) |
| 88 | + if err != nil { |
| 89 | + return nil, status.Error(codes.InvalidArgument, err.Error()) |
| 90 | + } |
| 91 | + |
| 92 | + yamlConfigs = append(yamlConfigs, generatedYamlConfig) |
| 93 | + } |
| 94 | + |
| 95 | + // Parse combined YAML |
| 96 | + p := parser.New( |
| 97 | + parser.WithEnvironment(request.Environment), |
| 98 | + parser.WithFileSystem(fs), |
| 99 | + ) |
| 100 | + |
| 101 | + result, err := p.Parse(ctx, strings.Join(yamlConfigs, "\n")) |
| 102 | + if err != nil { |
| 103 | + return nil, status.Error(codes.InvalidArgument, err.Error()) |
| 104 | + } |
| 105 | + |
| 106 | + if len(result.Errors) != 0 { |
| 107 | + return nil, status.Error(codes.InvalidArgument, result.Errors[0]) |
| 108 | + } |
| 109 | + |
| 110 | + return &api.EvaluateConfigResponse{Tasks: result.Tasks}, nil |
| 111 | +} |
0 commit comments