-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add function to provisioner interface to for setting up a new bundle (#…
…139) * Add function to provisioner interface to for setting up a new bundle (copying files) * remove unnecessary comment * remove unnecessary comment
- Loading branch information
1 parent
4ce53f4
commit 4ef28e9
Showing
17 changed files
with
256 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,34 @@ | ||
package commands | ||
|
||
import ( | ||
"path" | ||
|
||
"github.com/massdriver-cloud/mass/pkg/bundle" | ||
"github.com/massdriver-cloud/mass/pkg/provisioners" | ||
"github.com/massdriver-cloud/mass/pkg/templatecache" | ||
) | ||
|
||
func GenerateNewBundle(bundleCache templatecache.TemplateCache, templateData *templatecache.TemplateData) error { | ||
return bundleCache.RenderTemplate(templateData) | ||
renderErr := bundleCache.RenderTemplate(templateData) | ||
if renderErr != nil { | ||
return renderErr | ||
} | ||
|
||
// if we imported params from existing IaC, pass that to the provisioner in case more initialization should be done | ||
if templateData.ExistingParamsPath != "" { | ||
b, unmarshalErr := bundle.UnmarshalAndApplyDefaults(templateData.OutputDir) | ||
if unmarshalErr != nil { | ||
return unmarshalErr | ||
} | ||
|
||
for _, step := range b.Steps { | ||
prov := provisioners.NewProvisioner(step.Provisioner) | ||
initErr := prov.InitializeStep(path.Join(templateData.OutputDir, step.Path), templateData.ExistingParamsPath) | ||
if initErr != nil { | ||
return initErr | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package provisioners | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// CopyFile copies a single file from source to destination. | ||
func copyFile(src, dst string) error { | ||
sourceFileStat, err := os.Stat(src) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !sourceFileStat.Mode().IsRegular() { | ||
return fmt.Errorf("%s is not a regular file", src) | ||
} | ||
|
||
source, err := os.Open(src) | ||
if err != nil { | ||
return err | ||
} | ||
defer source.Close() | ||
|
||
destination, err := os.Create(dst) | ||
if err != nil { | ||
return err | ||
} | ||
defer destination.Close() | ||
|
||
_, err = io.Copy(destination, source) | ||
return err | ||
} | ||
|
||
// CopyDir copies a whole directory recursively from src to dst, | ||
// ignoring files and directories that match any of the ignore patterns. | ||
func copyDir(src string, dst string, ignorePatterns []string) error { | ||
entries, err := os.ReadDir(src) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, entry := range entries { | ||
srcPath := filepath.Join(src, entry.Name()) | ||
dstPath := filepath.Join(dst, entry.Name()) | ||
|
||
if shouldIgnore(srcPath, ignorePatterns) { | ||
continue | ||
} | ||
|
||
if entry.IsDir() { | ||
if err := os.MkdirAll(dstPath, entry.Type().Perm()); err != nil { | ||
return err | ||
} | ||
if err := copyDir(srcPath, dstPath, ignorePatterns); err != nil { | ||
return err | ||
} | ||
} else { | ||
if err := copyFile(srcPath, dstPath); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// shouldIgnore checks if a given path matches any of the glob patterns. | ||
func shouldIgnore(path string, patterns []string) bool { | ||
for _, pattern := range patterns { | ||
matched, err := filepath.Match(pattern, filepath.Base(path)) | ||
if err != nil { | ||
fmt.Printf("Error matching pattern: %v\n", err) | ||
continue | ||
} | ||
if matched { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
foobar |
1 change: 1 addition & 0 deletions
1
pkg/provisioners/testdata/opentofu/initializetest/.terraform.lock.hcl
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
pkg/provisioners/testdata/opentofu/initializetest/.terraform/terraform.tfstate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
foo |
Empty file.
1 change: 1 addition & 0 deletions
1
pkg/provisioners/testdata/opentofu/initializetest/terraform.tfstate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
foo |
1 change: 1 addition & 0 deletions
1
pkg/provisioners/testdata/opentofu/initializetest/terraform.tfstate.backup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
foo = bar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters