2022-09-26 09:52:31 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"errors"
|
|
|
|
"io/fs"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed terraform/*
|
2022-10-25 04:10:46 -04:00
|
|
|
//go:embed terraform/*/.terraform.lock.hcl
|
2022-09-26 09:52:31 -04:00
|
|
|
var terraformFS embed.FS
|
|
|
|
|
|
|
|
// prepareWorkspace loads the embedded Terraform files,
|
|
|
|
// and writes them into the workspace.
|
|
|
|
func prepareWorkspace(fileHandler file.Handler, provider cloudprovider.Provider) error {
|
|
|
|
// use path.Join to ensure no forward slashes are used to read the embedded FS
|
|
|
|
rootDir := path.Join("terraform", strings.ToLower(provider.String()))
|
|
|
|
return fs.WalkDir(terraformFS, rootDir, func(path string, d fs.DirEntry, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if d.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err := terraformFS.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fileName := strings.TrimPrefix(path, rootDir+"/")
|
|
|
|
return fileHandler.Write(fileName, content, file.OptMkdirAll)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanUpWorkspace removes files that were loaded into the workspace.
|
2022-10-26 09:57:00 -04:00
|
|
|
func cleanUpWorkspace(fileHandler file.Handler) error {
|
|
|
|
// try to remove any terraform files in the workspace
|
|
|
|
for _, csp := range []string{"aws", "azure", "gcp", "qemu"} {
|
|
|
|
rootDir := path.Join("terraform", csp)
|
|
|
|
if err := fs.WalkDir(terraformFS, rootDir, func(path string, d fs.DirEntry, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fileName := strings.TrimPrefix(path, rootDir+"/")
|
|
|
|
return ignoreFileNotFoundErr(fileHandler.RemoveAll(fileName))
|
|
|
|
}); err != nil {
|
2022-09-26 09:52:31 -04:00
|
|
|
return err
|
|
|
|
}
|
2022-10-26 09:57:00 -04:00
|
|
|
}
|
|
|
|
return nil
|
2022-09-26 09:52:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ignoreFileNotFoundErr ignores the error if it is a file not found error.
|
|
|
|
func ignoreFileNotFoundErr(err error) error {
|
|
|
|
if errors.Is(err, afero.ErrFileNotFound) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|