Skip to content

Commit bd53092

Browse files
committed
fix: reduce size of terraform modules archive
specifically, only persist the directories specified by terraform's modules.json
1 parent ae3d90b commit bd53092

File tree

4 files changed

+304
-286
lines changed

4 files changed

+304
-286
lines changed

provisioner/terraform/modules.go

+43-39
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,8 @@ type modulesFile struct {
2424
Modules []*module `json:"Modules"`
2525
}
2626

27-
func getModulesDirectory(workdir string) string {
28-
return filepath.Join(workdir, ".terraform", "modules")
29-
}
30-
3127
func getModulesFilePath(workdir string) string {
32-
return filepath.Join(getModulesDirectory(workdir), "modules.json")
28+
return filepath.Join(workdir, ".terraform", "modules", "modules.json")
3329
}
3430

3531
func parseModulesFile(filePath string) ([]*proto.Module, error) {
@@ -72,51 +68,59 @@ func getModules(workdir string) ([]*proto.Module, error) {
7268
}
7369

7470
func getModulesArchive(workdir string) ([]byte, error) {
75-
modulesDir := getModulesDirectory(workdir)
76-
if _, err := os.ReadDir(modulesDir); err != nil {
71+
modulesFile := getModulesFilePath(workdir)
72+
modulesFileBytes, err := os.ReadFile(modulesFile)
73+
if err != nil {
7774
if os.IsNotExist(err) {
7875
return []byte{}, nil
7976
}
80-
81-
return nil, err
77+
return nil, xerrors.Errorf("failed to read modules.json: %w", err)
8278
}
79+
var modules []*proto.Module
80+
err = json.Unmarshal(modulesFileBytes, &modules)
81+
if err != nil {
82+
return nil, xerrors.Errorf("failed to parse modules.json: %w", err)
83+
}
84+
8385
empty := true
8486
var b bytes.Buffer
8587
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-
}
88+
for _, module := range modules {
89+
err := filepath.WalkDir(module.Dir, func(filePath string, info fs.DirEntry, err error) error {
90+
if err != nil {
91+
return xerrors.Errorf("failed to create modules archive: %w", err)
92+
}
93+
if info.IsDir() {
94+
return nil
95+
}
96+
archivePath, found := strings.CutPrefix(filePath, workdir+string(os.PathSeparator))
97+
if !found {
98+
return xerrors.Errorf("walked invalid file path: %q", filePath)
99+
}
97100

98-
content, err := os.ReadFile(filePath)
99-
if err != nil {
100-
return xerrors.Errorf("failed to read module file while archiving: %w", err)
101-
}
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,
101+
content, err := os.ReadFile(filePath)
102+
if err != nil {
103+
return xerrors.Errorf("failed to read module file while archiving: %w", err)
104+
}
105+
empty = false
106+
err = w.WriteHeader(&tar.Header{
107+
Name: archivePath,
108+
Size: int64(len(content)),
109+
Mode: 0o644,
110+
Uid: 1000,
111+
Gid: 1000,
112+
})
113+
if err != nil {
114+
return xerrors.Errorf("failed to add module file to archive: %w", err)
115+
}
116+
if _, err = w.Write(content); err != nil {
117+
return xerrors.Errorf("failed to write module file to archive: %w", err)
118+
}
119+
return nil
109120
})
110121
if err != nil {
111-
return xerrors.Errorf("failed to add module file to archive: %w", err)
122+
return nil, err
112123
}
113-
if _, err = w.Write(content); err != nil {
114-
return xerrors.Errorf("failed to write module file to archive: %w", err)
115-
}
116-
return nil
117-
})
118-
if err != nil {
119-
return nil, err
120124
}
121125
err = w.Close()
122126
if err != nil {

0 commit comments

Comments
 (0)