Skip to content

Commit

Permalink
[DEVCON-6862] Execute terraform workflow from adhoc server mode (#732)
Browse files Browse the repository at this point in the history
  • Loading branch information
smonero authored Apr 23, 2024
1 parent 3d8d6c8 commit 146af39
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 140 deletions.
25 changes: 10 additions & 15 deletions cmd/adhoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ import (
"github.com/runatlantis/atlantis/server/legacy"
"github.com/runatlantis/atlantis/server/logging"
adhoc "github.com/runatlantis/atlantis/server/neptune/adhoc"
adhocHelpers "github.com/runatlantis/atlantis/server/neptune/adhoc/adhocexecutionhelpers"
adhocconfig "github.com/runatlantis/atlantis/server/neptune/adhoc/config"
neptune "github.com/runatlantis/atlantis/server/neptune/temporalworker/config"
"github.com/runatlantis/atlantis/server/neptune/workflows/activities/github"
"github.com/runatlantis/atlantis/server/neptune/workflows/activities/terraform"
)

type Adhoc struct{}
Expand Down Expand Up @@ -58,23 +55,21 @@ func (a *Adhoc) NewServer(userConfig legacy.UserConfig, config legacy.Config) (S
DownloadURL: userConfig.TFDownloadURL,
LogFilters: globalCfg.TerraformLogFilter,
},
DataDir: userConfig.DataDir,
TemporalCfg: globalCfg.Temporal,
GithubCfg: globalCfg.Github,
App: appConfig,
CtxLogger: ctxLogger,
StatsNamespace: userConfig.StatsNamespace,
Metrics: globalCfg.Metrics,
AdhocExecutionParams: adhocHelpers.AdhocTerraformWorkflowExecutionParams{
Revision: "",
TerraformRoots: []terraform.Root{},
GithubRepo: github.Repo{},
},
DataDir: userConfig.DataDir,
TemporalCfg: globalCfg.Temporal,
GithubCfg: globalCfg.Github,
App: appConfig,
CtxLogger: ctxLogger,
StatsNamespace: userConfig.StatsNamespace,
Metrics: globalCfg.Metrics,
GithubHostname: userConfig.GithubHostname,
GithubAppID: userConfig.GithubAppID,
GithubAppKeyFile: userConfig.GithubAppKeyFile,
GithubAppSlug: userConfig.GithubAppSlug,
GlobalCfg: globalCfg,
GithubUser: userConfig.GithubUser,
GithubToken: userConfig.GithubToken,
JobConfig: globalCfg.PersistenceConfig.Jobs,
}
return adhoc.NewServer(cfg)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"context"

"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/neptune/adhoc/adhocgithubhelpers"
"github.com/runatlantis/atlantis/server/neptune/gateway/config"
root_config "github.com/runatlantis/atlantis/server/neptune/gateway/config"
"github.com/runatlantis/atlantis/server/neptune/workflows/activities/github"
"github.com/runatlantis/atlantis/server/neptune/workflows/activities/terraform"
internal_gh "github.com/runatlantis/atlantis/server/vcs/provider/github"
"github.com/runatlantis/atlantis/server/vcs/provider/github/converter"
)

type AdhocTerraformWorkflowExecutionParams struct {
Expand All @@ -19,11 +19,29 @@ type AdhocTerraformWorkflowExecutionParams struct {
// Note that deploymentID is used in NewWorkflowStore(), but we don't care about that in adhoc mode so can leave it blank
}

func ConstructAdhocExecParamsWithRootCfgBuilderAndRepoRetriever(ctx context.Context, repoName string, revision string, githubRetriever *adhocgithubhelpers.AdhocGithubRetriever, rootCfgBuilder *root_config.Builder) (AdhocTerraformWorkflowExecutionParams, error) {
func ConstructAdhocExecParams(
ctx context.Context,
repoName string,
PRNum int,
pullFetcher *internal_gh.PRFetcher,
pullConverter converter.PullConverter,
installationRetriever *internal_gh.InstallationRetriever,
rootCfgBuilder *root_config.Builder) (AdhocTerraformWorkflowExecutionParams, error) {
orgName := "lyft"
installationToken, err := installationRetriever.FindOrganizationInstallation(ctx, orgName)
if err != nil {
return AdhocTerraformWorkflowExecutionParams{}, errors.Wrap(err, "finding organization installation")
}

// TODO: in the future, could potentially pass in the owner instead of hardcoding lyft
repo, err := githubRetriever.GetRepository(ctx, "lyft", repoName)
ghCommit, err := pullFetcher.Fetch(ctx, installationToken.Token, orgName, repoName, PRNum)
if err != nil {
return AdhocTerraformWorkflowExecutionParams{}, errors.Wrap(err, "getting repo")
return AdhocTerraformWorkflowExecutionParams{}, errors.Wrap(err, "fetching commit")
}

actualCommit, err := pullConverter.Convert(ghCommit)
if err != nil {
return AdhocTerraformWorkflowExecutionParams{}, errors.Wrap(err, "converting commit")
}

opts := config.BuilderOptions{
Expand All @@ -33,16 +51,27 @@ func ConstructAdhocExecParamsWithRootCfgBuilderAndRepoRetriever(ctx context.Cont
},
}

rootCfgs, err := rootCfgBuilder.Build(ctx, &root_config.RepoCommit{}, repo.Credentials.InstallationToken, opts)
rootCfgs, err := rootCfgBuilder.Build(ctx, &root_config.RepoCommit{
Repo: actualCommit.HeadRepo,
Branch: actualCommit.HeadBranch,
Sha: actualCommit.HeadCommit,
OptionalPRNum: actualCommit.Num,
}, installationToken.Token, opts)
if err != nil {
return AdhocTerraformWorkflowExecutionParams{}, errors.Wrap(err, "building root cfgs")
}

roots := getRootsFromMergedProjectCfgs(rootCfgs)

return AdhocTerraformWorkflowExecutionParams{
Revision: revision,
GithubRepo: repo,
Revision: actualCommit.HeadCommit,
GithubRepo: github.Repo{
Owner: orgName,
Name: repoName,
URL: actualCommit.HeadRepo.CloneURL,
DefaultBranch: actualCommit.HeadRepo.DefaultBranch,
Credentials: github.AppCredentials{InstallationToken: installationToken.Token},
},
TerraformRoots: roots,
}, nil
}
50 changes: 0 additions & 50 deletions server/neptune/adhoc/adhocgithubhelpers/adhoc_github_helpers.go

This file was deleted.

11 changes: 6 additions & 5 deletions server/neptune/adhoc/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/palantir/go-githubapp/githubapp"
"github.com/runatlantis/atlantis/server/config/valid"
"github.com/runatlantis/atlantis/server/logging"
adhoc "github.com/runatlantis/atlantis/server/neptune/adhoc/adhocexecutionhelpers"
neptune "github.com/runatlantis/atlantis/server/neptune/temporalworker/config"
)

Expand All @@ -18,17 +17,19 @@ type Config struct {
TerraformCfg neptune.TerraformConfig
Metrics valid.Metrics

JobConfig valid.StoreConfig
StatsNamespace string

DataDir string
CtxLogger logging.Logger
App githubapp.Config
AdhocExecutionParams adhoc.AdhocTerraformWorkflowExecutionParams
DataDir string
CtxLogger logging.Logger
App githubapp.Config

GithubAppID int64
GithubAppKeyFile string
GithubAppSlug string
GithubHostname string
GithubUser string
GithubToken string

GlobalCfg valid.GlobalCfg
}
Loading

0 comments on commit 146af39

Please sign in to comment.