@@ -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+
2326func SessionDir (parentDir ,sessID string ,config * proto.Config )TerraformDirectory {
2427if config .TemplateId == "" || config .TemplateId == uuid .Nil .String ()||
2528config .TemplateVersionId == "" || config .TemplateVersionId == uuid .Nil .String () {
@@ -57,16 +60,16 @@ const (
5760sessionDirPrefix = "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 ) {
6164var err error
62- path := td .workDirectory
65+ path := td .WorkDirectory ()
6366if ! td .ephemeral {
6467// Non-ephemeral directories only clean up the session subdirectory.
6568// Leaving in place the wider work directory for reuse.
6669path = td .StateSessionDirectory ()
6770}
6871for attempt := 0 ;attempt < 5 ;attempt ++ {
69- err := os .RemoveAll (path )
72+ err := fs .RemoveAll (path )
7073if 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 {
116119return filepath .Join (td .WorkDirectory (),ReadmeFile )
117120}
118121
122+ func (td TerraformDirectory )TerraformMetadataDir ()string {
123+ return filepath .Join (td .WorkDirectory (),".terraform" )
124+ }
125+
119126func (td TerraformDirectory )ModulesDirectory ()string {
120- return filepath .Join (td .WorkDirectory (), ".terraform" ,"modules" )
127+ return filepath .Join (td .TerraformMetadataDir () ,"modules" )
121128}
122129
123130func (td TerraformDirectory )ModulesFilePath ()string {
124131return 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 {
128135logger .Info (ctx ,"unpacking template source archive" ,
129136slog .F ("size_bytes" ,len (cfg .TemplateSourceArchive )),
130137)
131138
132- err := os .MkdirAll (td .WorkDirectory (),0o700 )
139+ err := fs .MkdirAll (td .WorkDirectory (),0o700 )
133140if err != nil {
134141return 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}
176183switch header .Typeflag {
177184case tar .TypeDir :
178- err = os .MkdirAll (headerPath ,mode )
185+ err = fs .MkdirAll (headerPath ,mode )
179186if err != nil {
180187return 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 )
190197if err != nil {
191198return xerrors .Errorf ("create file %q (mode %s): %w" ,headerPath ,mode ,err )
192199}