Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions pkg/modulewriter/modulewriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,44 @@ func (s *zeroSuite) TestRestoreTfState(c *C) {
c.Check(err, IsNil)
}

func (s *zeroSuite) TestRestorePackerState(c *C) {
// set up dir structure
//
// └── test_dir
// ├── .ghpc
// └── previous_deployment_groups
// └── packer_group
// └── image
// └── packer-manifest.json
// └── packer_group
// └── image
depDir := c.MkDir()
groupName := "packer_group"
subPath := "image"
manifestContent := `{"builds": [{"artifact_id": "my-image"}]}`

prevGroup := filepath.Join(HiddenGhpcDir(depDir), prevGroupDirName, groupName, subPath)
curGroup := filepath.Join(depDir, groupName, subPath)
prevManifest := filepath.Join(prevGroup, packerManifestFileName)

c.Assert(os.MkdirAll(prevGroup, 0755), IsNil)
c.Assert(os.MkdirAll(curGroup, 0755), IsNil)
c.Assert(os.WriteFile(prevManifest, []byte(manifestContent), 0644), IsNil)

testWriter := PackerWriter{}
c.Check(testWriter.restoreState(depDir), IsNil)

// check manifest file was copied to current resource group dir
curManifest := filepath.Join(curGroup, packerManifestFileName)
_, err := os.Stat(curManifest)
c.Check(err, IsNil)

// check content is identical
gotContent, err := os.ReadFile(curManifest)
c.Check(err, IsNil)
c.Check(string(gotContent), Equals, manifestContent)
}

func TestGetTypeTokensRelaxed(t *testing.T) {
type test struct {
input cty.Type
Expand Down
54 changes: 53 additions & 1 deletion pkg/modulewriter/packerwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ package modulewriter
import (
"fmt"
"io"
"os"
"path/filepath"

"hpc-toolkit/pkg/config"
"hpc-toolkit/pkg/logging"

"github.com/zclconf/go-cty/cty"
)
Expand Down Expand Up @@ -85,8 +87,58 @@ func (w PackerWriter) writeGroup(
return nil
}

const packerManifestFileName = "packer-manifest.json"

func (w PackerWriter) restoreState(deploymentDir string) error {
// TODO: restore packer-manifest.json if it exists
prevGroupPath := filepath.Join(HiddenGhpcDir(deploymentDir), prevGroupDirName)

// If the previous deployment groups directory doesn't exist, there is nothing to restore.
if _, err := os.Stat(prevGroupPath); os.IsNotExist(err) {
return nil
}

err := filepath.WalkDir(prevGroupPath, func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() || d.Name() != packerManifestFileName {
return nil
}

// Found a packer-manifest.json. Determine its relative path from prevGroupPath.
relPath, err := filepath.Rel(prevGroupPath, path)
if err != nil {
return fmt.Errorf("failed to get relative path for %s: %w", path, err)
}

dest := filepath.Join(deploymentDir, relPath)

// Ensure the destination directory exists before writing (it should have been created by writeGroup).
_, err = os.Stat(filepath.Dir(dest))
if err != nil {
if os.IsNotExist(err) {
return nil
}
return fmt.Errorf("failed to stat destination directory %s: %w", filepath.Dir(dest), err)
}

bytesRead, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to read previous packer manifest %s: %w", path, err)
}
err = os.WriteFile(dest, bytesRead, 0644)
if err != nil {
return fmt.Errorf("failed to write previous packer manifest %s: %w", dest, err)
}
logging.Info("Restored packer manifest to %s", dest)

return nil
})

if err != nil {
return fmt.Errorf("error trying to restore packer manifests: %w", err)
}

return nil
}

Expand Down
Loading