Skip to content

Commit

Permalink
feat: always use latest terraform version (#616)
Browse files Browse the repository at this point in the history
Fixes #608
  • Loading branch information
leg100 authored Oct 13, 2023
1 parent 84aa299 commit 83469ca
Show file tree
Hide file tree
Showing 40 changed files with 797 additions and 221 deletions.
24 changes: 9 additions & 15 deletions internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ type agent struct {
logr.Logger

client
spooler // spools new run events
*terminator // terminates runs
Downloader // terraform cli downloader
*TerraformPathFinder // determines destination dir for terraform bins
spooler // spools new run events
*terminator // terminates runs

envs []string // terraform environment variables
}
Expand All @@ -61,17 +59,13 @@ func NewAgent(logger logr.Logger, app client, cfg Config) (*agent, error) {
logger.V(0).Info("enabled debug mode")
}

pathFinder := newTerraformPathFinder(cfg.TerraformBinDir)

agent := &agent{
client: app,
Config: cfg,
Logger: logger,
envs: DefaultEnvs,
spooler: newSpooler(app, logger, cfg),
terminator: newTerminator(),
Downloader: NewDownloader(pathFinder),
TerraformPathFinder: pathFinder,
client: app,
Config: cfg,
Logger: logger,
envs: DefaultEnvs,
spooler: newSpooler(app, logger, cfg),
terminator: newTerminator(),
}

if cfg.PluginCache {
Expand All @@ -89,7 +83,7 @@ func NewAgent(logger logr.Logger, app client, cfg Config) (*agent, error) {
// via http
func NewExternalAgent(ctx context.Context, logger logr.Logger, cfg ExternalConfig) (*agent, error) {
// Sends unauthenticated ping to server
app, err := newClient(cfg.HTTPConfig)
app, err := newClient(cfg)
if err != nil {
return nil, err
}
Expand Down
11 changes: 8 additions & 3 deletions internal/agent/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/leg100/otf/internal/http"
"github.com/leg100/otf/internal/logs"
"github.com/leg100/otf/internal/pubsub"
"github.com/leg100/otf/internal/releases"
"github.com/leg100/otf/internal/resource"
"github.com/leg100/otf/internal/run"
"github.com/leg100/otf/internal/state"
Expand Down Expand Up @@ -43,6 +44,7 @@ type (

tokens.RunTokenService
internal.PutChunkService
releases.Downloader
}

// LocalClient is the client for an internal agent.
Expand All @@ -53,6 +55,7 @@ type (
workspace.WorkspaceService
internal.HostnameService
configversion.ConfigurationVersionService
releases.Downloader
run.RunService
logs.LogsService
}
Expand All @@ -69,6 +72,7 @@ type (
*workspaceClient
*runClient
*logsClient
releases.Downloader
}

stateClient = state.Client
Expand All @@ -82,20 +86,21 @@ type (

// New constructs a client that uses http to remotely invoke OTF
// services.
func newClient(config http.Config) (*remoteClient, error) {
httpClient, err := http.NewClient(config)
func newClient(config ExternalConfig) (*remoteClient, error) {
httpClient, err := http.NewClient(config.HTTPConfig)
if err != nil {
return nil, err
}

return &remoteClient{
Client: httpClient,
Downloader: releases.NewDownloader(config.TerraformBinDir),
stateClient: &stateClient{JSONAPIClient: httpClient},
configClient: &configClient{JSONAPIClient: httpClient},
variableClient: &variableClient{JSONAPIClient: httpClient},
tokensClient: &tokensClient{JSONAPIClient: httpClient},
workspaceClient: &workspaceClient{JSONAPIClient: httpClient},
runClient: &runClient{JSONAPIClient: httpClient, Config: config},
runClient: &runClient{JSONAPIClient: httpClient, Config: config.HTTPConfig},
logsClient: &logsClient{JSONAPIClient: httpClient},
}, nil
}
27 changes: 12 additions & 15 deletions internal/agent/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
type environment struct {
client
logr.Logger
Downloader // Downloader for workers to download terraform cli on demand

steps []step // sequence of steps to execute

Expand Down Expand Up @@ -86,21 +85,19 @@ func newEnvironment(
})

env := &environment{
Logger: logger,
client: agent,
Downloader: agent,
out: writer,
workdir: wd,
variables: variables,
ctx: ctx,
runner: &runner{out: writer},
Logger: logger,
client: agent,
out: writer,
workdir: wd,
variables: variables,
ctx: ctx,
runner: &runner{out: writer},
executor: &executor{
Config: agent.Config,
TerraformPathFinder: agent.TerraformPathFinder,
version: ws.TerraformVersion,
out: writer,
envs: envs,
workdir: wd,
Config: agent.Config,
version: run.TerraformVersion,
out: writer,
envs: envs,
workdir: wd,
},
}

Expand Down
7 changes: 0 additions & 7 deletions internal/agent/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ type (
// executor executes processes.
executor struct {
Config
*TerraformPathFinder

version string // terraform cli version
out io.Writer
Expand Down Expand Up @@ -79,12 +78,6 @@ func (e *executor) execute(args []string, opts ...executionOption) error {
return nil
}

// executeTerraform executes a terraform process
func (e *executor) executeTerraform(args []string, opts ...executionOption) error {
args = append([]string{e.TerraformPath(e.version)}, args...)
return e.execute(args, opts...)
}

func (e *execution) execute(args []string) error {
if len(args) == 0 {
return fmt.Errorf("missing command name")
Expand Down
16 changes: 11 additions & 5 deletions internal/agent/steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type (
stepsBuilder struct {
*run.Run
*environment

terraformPath string
}

runner struct {
Expand Down Expand Up @@ -106,7 +108,8 @@ func (r *runner) cancel(force bool) {
}

func (b *stepsBuilder) downloadTerraform(ctx context.Context) error {
_, err := b.Download(ctx, b.version, b.out)
var err error
b.terraformPath, err = b.Download(ctx, b.version, b.out)
return err
}

Expand Down Expand Up @@ -163,7 +166,7 @@ func (b *stepsBuilder) writeTerraformVars(ctx context.Context) error {
}

func (b *stepsBuilder) terraformInit(ctx context.Context) error {
return b.executeTerraform([]string{"init"})
return b.executor.execute([]string{b.terraformPath, "init"})
}

func (b *stepsBuilder) terraformPlan(ctx context.Context) error {
Expand All @@ -172,7 +175,7 @@ func (b *stepsBuilder) terraformPlan(ctx context.Context) error {
args = append(args, "-destroy")
}
args = append(args, "-out="+planFilename)
return b.executeTerraform(args)
return b.executor.execute(append([]string{b.terraformPath}, args...))
}

func (b *stepsBuilder) terraformApply(ctx context.Context) (err error) {
Expand Down Expand Up @@ -208,12 +211,15 @@ func (b *stepsBuilder) terraformApply(ctx context.Context) (err error) {
args = append(args, "-destroy")
}
args = append(args, planFilename)
return b.executeTerraform(args)
return b.executor.execute(append([]string{b.terraformPath}, args...))
}

func (b *stepsBuilder) convertPlanToJSON(ctx context.Context) error {
args := []string{"show", "-json", planFilename}
return b.executeTerraform(args, redirectStdout(jsonPlanFilename))
return b.executor.execute(
append([]string{b.terraformPath}, args...),
redirectStdout(jsonPlanFilename),
)
}

func (b *stepsBuilder) uploadPlan(ctx context.Context) error {
Expand Down
93 changes: 0 additions & 93 deletions internal/agent/terraform_downloader.go

This file was deleted.

27 changes: 0 additions & 27 deletions internal/agent/terraform_path_finder.go

This file was deleted.

2 changes: 2 additions & 0 deletions internal/daemon/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type Config struct {
DisableScheduler bool
RestrictOrganizationCreation bool
SiteAdmins []string
// skip checks for latest terraform version
DisableLatestChecker *bool

tokens.GoogleIAPConfig
}
Expand Down
Loading

0 comments on commit 83469ca

Please sign in to comment.