2022-09-26 09:52:31 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io/fs"
|
2023-05-30 06:02:43 -04:00
|
|
|
slashpath "path"
|
2022-11-14 12:18:58 -05:00
|
|
|
"path/filepath"
|
2022-09-26 09:52:31 -04:00
|
|
|
"strings"
|
|
|
|
|
2023-11-08 13:10:01 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/terraform"
|
|
|
|
|
2022-09-26 09:52:31 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
)
|
|
|
|
|
|
|
|
// prepareWorkspace loads the embedded Terraform files,
|
|
|
|
// and writes them into the workspace.
|
2023-05-22 07:31:20 -04:00
|
|
|
func prepareWorkspace(rootDir string, fileHandler file.Handler, workingDir string) error {
|
2023-10-26 04:55:50 -04:00
|
|
|
return terraformCopier(fileHandler, rootDir, workingDir)
|
2023-05-22 07:31:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// terraformCopier copies the embedded Terraform files into the workspace.
|
2023-10-26 04:55:50 -04:00
|
|
|
func terraformCopier(fileHandler file.Handler, rootDir, workingDir string) error {
|
2023-05-30 06:02:43 -04:00
|
|
|
goEmbedRootDir := filepath.ToSlash(rootDir)
|
2023-11-08 13:10:01 -05:00
|
|
|
return fs.WalkDir(terraform.Assets, goEmbedRootDir, func(path string, d fs.DirEntry, err error) error {
|
2022-09-26 09:52:31 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if d.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-30 06:02:43 -04:00
|
|
|
goEmbedPath := filepath.ToSlash(path)
|
2023-11-08 13:10:01 -05:00
|
|
|
content, err := terraform.Assets.ReadFile(goEmbedPath)
|
2022-09-26 09:52:31 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-30 06:02:43 -04:00
|
|
|
// normalize
|
|
|
|
fileName := strings.Replace(slashpath.Join(workingDir, path), goEmbedRootDir+"/", "", 1)
|
2023-09-14 05:51:20 -04:00
|
|
|
opts := []file.Option{
|
|
|
|
file.OptMkdirAll,
|
2023-10-26 04:55:50 -04:00
|
|
|
// Allow overwriting existing files.
|
|
|
|
// If we are creating a new cluster, the workspace must have been empty before,
|
|
|
|
// so there is no risk of overwriting existing files.
|
|
|
|
// If we are upgrading an existing cluster, we want to overwrite the existing files,
|
|
|
|
// and we have already created a backup of the existing workspace.
|
|
|
|
file.OptOverwrite,
|
2023-09-14 05:51:20 -04:00
|
|
|
}
|
2023-10-26 04:55:50 -04:00
|
|
|
return fileHandler.Write(fileName, content, opts...)
|
2022-09-26 09:52:31 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-14 12:18:58 -05:00
|
|
|
func cleanUpWorkspace(fileHandler file.Handler, workingDir string) error {
|
|
|
|
return ignoreFileNotFoundErr(fileHandler.RemoveAll(workingDir))
|
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
|
|
|
|
}
|