@@ -24,12 +24,8 @@ type modulesFile struct {
24
24
Modules []* module `json:"Modules"`
25
25
}
26
26
27
- func getModulesDirectory (workdir string ) string {
28
- return filepath .Join (workdir , ".terraform" , "modules" )
29
- }
30
-
31
27
func getModulesFilePath (workdir string ) string {
32
- return filepath .Join (getModulesDirectory ( workdir ) , "modules.json" )
28
+ return filepath .Join (workdir , ".terraform" , "modules" , "modules.json" )
33
29
}
34
30
35
31
func parseModulesFile (filePath string ) ([]* proto.Module , error ) {
@@ -72,52 +68,83 @@ func getModules(workdir string) ([]*proto.Module, error) {
72
68
}
73
69
74
70
func getModulesArchive (workdir string ) ([]byte , error ) {
75
- modulesDir := getModulesDirectory ( workdir )
76
- if _ , err := os . ReadDir ( modulesDir ); err != nil {
71
+ modulesFile , err := os . ReadFile ( getModulesFilePath ( workdir ) )
72
+ if err != nil {
77
73
if os .IsNotExist (err ) {
78
74
return []byte {}, nil
79
75
}
80
-
81
- return nil , err
76
+ return nil , xerrors .Errorf ("failed to read modules.json: %w" , err )
82
77
}
78
+ var m struct { Modules []* proto.Module }
79
+ err = json .Unmarshal (modulesFile , & m )
80
+ if err != nil {
81
+ return nil , xerrors .Errorf ("failed to parse modules.json: %w" , err )
82
+ }
83
+
83
84
empty := true
84
85
var b bytes.Buffer
85
86
w := tar .NewWriter (& b )
86
- err := filepath .WalkDir (modulesDir , func (filePath string , info fs.DirEntry , err error ) error {
87
- if err != nil {
88
- return xerrors .Errorf ("failed to create modules archive: %w" , err )
89
- }
90
- if info .IsDir () {
91
- return nil
92
- }
93
- archivePath , found := strings .CutPrefix (filePath , workdir + string (os .PathSeparator ))
94
- if ! found {
95
- return xerrors .Errorf ("walked invalid file path: %q" , filePath )
96
- }
97
87
98
- content , err := os .ReadFile (filePath )
99
- if err != nil {
100
- return xerrors .Errorf ("failed to read module file while archiving: %w" , err )
88
+ for _ , module := range m .Modules {
89
+ // Check to make sure that the module is a remote module fetched by
90
+ // Terraform. Any module that doesn't start with this path is already local,
91
+ // and should be part of the template files already.
92
+ if ! strings .HasPrefix (module .Dir , ".terraform/modules/" ) {
93
+ continue
101
94
}
102
- empty = false
103
- err = w .WriteHeader (& tar.Header {
104
- Name : archivePath ,
105
- Size : int64 (len (content )),
106
- Mode : 0o644 ,
107
- Uid : 1000 ,
108
- Gid : 1000 ,
95
+
96
+ err := filepath .WalkDir (filepath .Join (workdir , module .Dir ), func (filePath string , info fs.DirEntry , err error ) error {
97
+ if err != nil {
98
+ return xerrors .Errorf ("failed to create modules archive: %w" , err )
99
+ }
100
+ if info .IsDir () {
101
+ return nil
102
+ }
103
+ archivePath , found := strings .CutPrefix (filePath , workdir + string (os .PathSeparator ))
104
+ if ! found {
105
+ return xerrors .Errorf ("walked invalid file path: %q" , filePath )
106
+ }
107
+
108
+ content , err := os .ReadFile (filePath )
109
+ if err != nil {
110
+ return xerrors .Errorf ("failed to read module file while archiving: %w" , err )
111
+ }
112
+ empty = false
113
+ err = w .WriteHeader (& tar.Header {
114
+ Name : archivePath ,
115
+ Size : int64 (len (content )),
116
+ Mode : 0o644 ,
117
+ Uid : 1000 ,
118
+ Gid : 1000 ,
119
+ })
120
+ if err != nil {
121
+ return xerrors .Errorf ("failed to add module file to archive: %w" , err )
122
+ }
123
+ if _ , err = w .Write (content ); err != nil {
124
+ return xerrors .Errorf ("failed to write module file to archive: %w" , err )
125
+ }
126
+ return nil
109
127
})
110
128
if err != nil {
111
- return xerrors .Errorf ("failed to add module file to archive: %w" , err )
112
- }
113
- if _ , err = w .Write (content ); err != nil {
114
- return xerrors .Errorf ("failed to write module file to archive: %w" , err )
129
+ return nil , err
115
130
}
116
- return nil
131
+ }
132
+
133
+ err = w .WriteHeader (& tar.Header {
134
+ Name : ".terraform/modules/modules.json" ,
135
+ Size : int64 (len (modulesFile )),
136
+ Mode : 0o644 ,
137
+ Uid : 1000 ,
138
+ Gid : 1000 ,
117
139
})
118
140
if err != nil {
119
- return nil , err
141
+ return nil , xerrors .Errorf ("failed to write modules.json to archive: %w" , err )
142
+ }
143
+ _ , err = w .Write (modulesFile )
144
+ if err != nil {
145
+ return nil , xerrors .Errorf ("failed to write modules.json to archive: %w" , err )
120
146
}
147
+
121
148
err = w .Close ()
122
149
if err != nil {
123
150
return nil , xerrors .Errorf ("failed to close module files archive: %w" , err )
@@ -126,5 +153,9 @@ func getModulesArchive(workdir string) ([]byte, error) {
126
153
if empty {
127
154
return []byte {}, nil
128
155
}
156
+
157
+ if b .Len () > (10 << 20 ) {
158
+ return nil , xerrors .New ("modules archive exceeds 10MB, modules will not be persisted" )
159
+ }
129
160
return b .Bytes (), nil
130
161
}
0 commit comments