Skip to content

Commit b2d70af

Browse files
committed
chore: switch to interface to swap implementations
1 parent 6fc7c5a commit b2d70af

File tree

6 files changed

+33
-13
lines changed

6 files changed

+33
-13
lines changed

provisioner/terraform/executor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type executor struct {
4141
// cachePath and files must not be used by multiple processes at once.
4242
cachePath string
4343
cliConfigPath string
44-
files tfpath.Layout
44+
files tfpath.LayoutInterface
4545
// used to capture execution times at various stages
4646
timings *timingAggregator
4747
}

provisioner/terraform/modules.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func parseModulesFile(filePath string) ([]*proto.Module, error) {
5858
// getModules returns the modules from the modules file if it exists.
5959
// It returns nil if the file does not exist.
6060
// Modules become available after terraform init.
61-
func getModules(files tfpath.Layout) ([]*proto.Module, error) {
61+
func getModules(files tfpath.LayoutInterface) ([]*proto.Module, error) {
6262
filePath := files.ModulesFilePath()
6363
if _, err := os.Stat(filePath); os.IsNotExist(err) {
6464
return nil, nil

provisioner/terraform/serve.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func (s *server) startTrace(ctx context.Context, name string, opts ...trace.Span
161161
))...)
162162
}
163163

164-
func (s *server) executor(files tfpath.Layout, stage database.ProvisionerJobTimingStage) *executor {
164+
func (s *server) executor(files tfpath.LayoutInterface, stage database.ProvisionerJobTimingStage) *executor {
165165
return &executor{
166166
server: s,
167167
mut: s.execMut,

provisionersdk/session.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/coder/coder/v2/codersdk"
1717
"github.com/coder/coder/v2/codersdk/drpcsdk"
1818
"github.com/coder/coder/v2/provisionersdk/tfpath"
19+
"github.com/coder/coder/v2/provisionersdk/x"
1920

2021
protobuf "google.golang.org/protobuf/proto"
2122

@@ -78,7 +79,6 @@ func (p *protoServer) Session(stream proto.DRPCProvisioner_SessionStream) error
7879
}
7980
}
8081

81-
8282
// Extract the template source archive into the work directory.
8383
err = s.Files.ExtractArchive(s.Context(), s.Logger, afero.NewOsFs(), s.Config)
8484
if err != nil {
@@ -214,7 +214,7 @@ func (s *Session) handleRequests() error {
214214

215215
type Session struct {
216216
Logger slog.Logger
217-
Files tfpath.Layout
217+
Files tfpath.LayoutInterface
218218
Config *proto.Config
219219

220220
server Server

provisionersdk/tfpath/tfpath.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ import (
1919
"github.com/coder/coder/v2/provisionersdk/proto"
2020
)
2121

22+
type LayoutInterface interface {
23+
WorkDirectory() string
24+
StateFilePath() string
25+
PlanFilePath() string
26+
TerraformLockFile() string
27+
ReadmeFilePath() string
28+
TerraformMetadataDir() string
29+
ModulesDirectory() string
30+
ModulesFilePath() string
31+
ExtractArchive(ctx context.Context, logger slog.Logger, fs afero.Fs, cfg *proto.Config) error
32+
Cleanup(ctx context.Context, logger slog.Logger, fs afero.Fs)
33+
}
34+
2235
const (
2336
// ReadmeFile is the location we look for to extract documentation from template versions.
2437
ReadmeFile = "README.md"

provisionersdk/x/directories.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ import (
1818

1919
"cdr.dev/slog"
2020
"github.com/coder/coder/v2/provisionersdk/proto"
21+
"github.com/coder/coder/v2/provisionersdk/tfpath"
2122
)
2223

24+
var _ tfpath.LayoutInterface = (*TerraformDirectory)(nil)
25+
2326
func SessionDir(parentDir, sessID string, config *proto.Config) TerraformDirectory {
2427
if config.TemplateId == "" || config.TemplateId == uuid.Nil.String() ||
2528
config.TemplateVersionId == "" || config.TemplateVersionId == uuid.Nil.String() {
@@ -57,16 +60,16 @@ const (
5760
sessionDirPrefix = "Session"
5861
)
5962

60-
func (td TerraformDirectory) Cleanup(ctx context.Context, logger slog.Logger) {
63+
func (td TerraformDirectory) Cleanup(ctx context.Context, logger slog.Logger, fs afero.Fs) {
6164
var err error
62-
path := td.workDirectory
65+
path := td.WorkDirectory()
6366
if !td.ephemeral {
6467
// Non-ephemeral directories only clean up the session subdirectory.
6568
// Leaving in place the wider work directory for reuse.
6669
path = td.StateSessionDirectory()
6770
}
6871
for attempt := 0; attempt < 5; attempt++ {
69-
err := os.RemoveAll(path)
72+
err := fs.RemoveAll(path)
7073
if err != nil {
7174
// On Windows, open files cannot be removed.
7275
// When the provisioner daemon is shutting down,
@@ -116,20 +119,24 @@ func (td TerraformDirectory) ReadmeFilePath() string {
116119
return filepath.Join(td.WorkDirectory(), ReadmeFile)
117120
}
118121

122+
func (td TerraformDirectory) TerraformMetadataDir() string {
123+
return filepath.Join(td.WorkDirectory(), ".terraform")
124+
}
125+
119126
func (td TerraformDirectory) ModulesDirectory() string {
120-
return filepath.Join(td.WorkDirectory(), ".terraform", "modules")
127+
return filepath.Join(td.TerraformMetadataDir(), "modules")
121128
}
122129

123130
func (td TerraformDirectory) ModulesFilePath() string {
124131
return filepath.Join(td.ModulesDirectory(), "modules.json")
125132
}
126133

127-
func (td TerraformDirectory) ExtractArchive(ctx context.Context, logger slog.Logger, cfg *proto.Config) error {
134+
func (td TerraformDirectory) ExtractArchive(ctx context.Context, logger slog.Logger, fs afero.Fs, cfg *proto.Config) error {
128135
logger.Info(ctx, "unpacking template source archive",
129136
slog.F("size_bytes", len(cfg.TemplateSourceArchive)),
130137
)
131138

132-
err := os.MkdirAll(td.WorkDirectory(), 0o700)
139+
err := fs.MkdirAll(td.WorkDirectory(), 0o700)
133140
if err != nil {
134141
return xerrors.Errorf("create work directory %q: %w", td.WorkDirectory(), err)
135142
}
@@ -175,7 +182,7 @@ func (td TerraformDirectory) ExtractArchive(ctx context.Context, logger slog.Log
175182
}
176183
switch header.Typeflag {
177184
case tar.TypeDir:
178-
err = os.MkdirAll(headerPath, mode)
185+
err = fs.MkdirAll(headerPath, mode)
179186
if err != nil {
180187
return xerrors.Errorf("mkdir %q: %w", headerPath, err)
181188
}
@@ -186,7 +193,7 @@ func (td TerraformDirectory) ExtractArchive(ctx context.Context, logger slog.Log
186193
// TODO: If we are overwriting an existing file, that means we are reusing
187194
// the terraform directory. In that case, we should check the file content
188195
// matches what already exists on disk.
189-
file, err := os.OpenFile(headerPath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, mode)
196+
file, err := fs.OpenFile(headerPath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, mode)
190197
if err != nil {
191198
return xerrors.Errorf("create file %q (mode %s): %w", headerPath, mode, err)
192199
}

0 commit comments

Comments
 (0)