@@ -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 {
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+
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 {
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