From 708a5822c04593c258ab1569a4f60e8805725077 Mon Sep 17 00:00:00 2001 From: Shawna Monero Date: Mon, 18 Mar 2024 18:42:31 -0700 Subject: [PATCH] first draft --- server/neptune/adhoc/server.go | 65 +++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 12 deletions(-) diff --git a/server/neptune/adhoc/server.go b/server/neptune/adhoc/server.go index 447b929f0..38b6ad2f9 100644 --- a/server/neptune/adhoc/server.go +++ b/server/neptune/adhoc/server.go @@ -17,6 +17,7 @@ import ( "github.com/runatlantis/atlantis/server/neptune/lyft/feature" "github.com/runatlantis/atlantis/server/neptune/sync/crons" ghClient "github.com/runatlantis/atlantis/server/neptune/workflows/activities/github" + "github.com/runatlantis/atlantis/server/neptune/workflows/activities/terraform" "github.com/runatlantis/atlantis/server/vcs/provider/github" assetfs "github.com/elazarl/go-bindata-assetfs" @@ -34,23 +35,24 @@ import ( "github.com/runatlantis/atlantis/server/static" "github.com/uber-go/tally/v4" "github.com/urfave/negroni" + "go.temporal.io/sdk/client" "go.temporal.io/sdk/interceptor" "go.temporal.io/sdk/worker" ) type Server struct { - Logger logging.Logger - CronScheduler *internalSync.CronScheduler - Crons []*internalSync.Cron - HTTPServerProxy *neptune_http.ServerProxy - Port int - StatsScope tally.Scope - StatsCloser io.Closer - TemporalClient *temporal.ClientWrapper - TerraformActivities *activities.Terraform - GithubActivities *activities.Github - - TerraformTaskQueue string + Logger logging.Logger + CronScheduler *internalSync.CronScheduler + Crons []*internalSync.Cron + HTTPServerProxy *neptune_http.ServerProxy + Port int + StatsScope tally.Scope + StatsCloser io.Closer + TemporalClient *temporal.ClientWrapper + TerraformActivities *activities.Terraform + GithubActivities *activities.Github + adhocExecutionParams AdhocTerraformWorkflowExecutionParams + TerraformTaskQueue string } func NewServer(config *adhocconfig.Config) (*Server, error) { @@ -172,6 +174,39 @@ func NewServer(config *adhocconfig.Config) (*Server, error) { return &server, nil } +type AdhocTerraformWorkflowExecutionParams struct { + AtlantisRoot string + AtlantisRepo string + Revision string + DeploymentID string + // TODO: in separate PR, fill in github.Repo and terraform.Root (need helper funcs to get those) +} + +// This function constructs the request we want to send to the temporal client, +// then executes the Terraform workflow. Note normally this workflow is executed +// when a request is made to the server, but we are manually executing it here, +// since we don't care about requests in adhoc mode. +func (s Server) manuallyExecuteTerraformWorkflow(adhocExecutionParams AdhocTerraformWorkflowExecutionParams) (interface{}, error) { + request := workflows.TerraformRequest{ + DeploymentID: adhocExecutionParams.DeploymentID, + Revision: adhocExecutionParams.Revision, + WorkflowMode: terraform.Adhoc, + } + options := client.StartWorkflowOptions{ + TaskQueue: s.TerraformTaskQueue, + SearchAttributes: map[string]interface{}{ + "atlantis_repository": adhocExecutionParams.AtlantisRepo, + "atlantis_root": adhocExecutionParams.AtlantisRoot, + }, + } + + res, err := s.TemporalClient.ExecuteWorkflow(context.Background(), options, workflows.Terraform, request) + if err != nil { + s.Logger.Error(err.Error()) + } + return res, nil +} + func (s Server) Start() error { defer s.shutdown() @@ -191,6 +226,12 @@ func (s Server) Start() error { s.Logger.InfoContext(ctx, "Shutting down terraform worker, resource clean up may still be occurring in the background") }() + _, err := s.manuallyExecuteTerraformWorkflow(s.adhocExecutionParams) + if err != nil { + s.Logger.Error(err.Error()) + return err + } + // Ensure server gracefully drains connections when stopped. stop := make(chan os.Signal, 1) // Stop on SIGINTs and SIGTERMs.