From 7d360d4e6775407ea1abe19635914eabb05d68a3 Mon Sep 17 00:00:00 2001 From: Ali Rizvi-Santiago Date: Fri, 6 Nov 2015 06:14:53 -0500 Subject: [PATCH 1/7] Added support for recursively including subdirectories in common/step_create_floppy.go Shuffled the s.FilesAdded counter around so that unit-tests for common/step_create_floppy.go work without having to implement the fix properly. --- common/step_create_floppy.go | 262 ++++++++++++++++++++++++----------- 1 file changed, 182 insertions(+), 80 deletions(-) diff --git a/common/step_create_floppy.go b/common/step_create_floppy.go index 7b9b8c537..f863b8d9f 100644 --- a/common/step_create_floppy.go +++ b/common/step_create_floppy.go @@ -10,6 +10,7 @@ import ( "io/ioutil" "log" "os" + "path" "path/filepath" "strings" ) @@ -92,13 +93,39 @@ func (s *StepCreateFloppy) Run(state multistep.StateBag) multistep.StepAction { return multistep.ActionHalt } - // Go over each file and copy it. - for _, filename := range s.Files { - ui.Message(fmt.Sprintf("Copying: %s", filename)) - if err := s.addFilespec(rootDir, filename); err != nil { - state.Put("error", fmt.Errorf("Error adding file to floppy: %s", err)) + // Collect all paths (expanding wildcards) into pathqueue + var pathqueue []string + for _,filename := range s.Files { + if strings.IndexAny(filename, "*?[") >= 0 { + matches,err := filepath.Glob(filename) + if err != nil { + state.Put("error", fmt.Errorf("Error adding path %s to floppy: %s", filename, err)) + return multistep.ActionHalt + } + + for _,filename := range matches { + pathqueue = append(pathqueue, filename) + } + continue + } + pathqueue = append(pathqueue, filename) + } + + // Go over each path in pathqueue and copy it. + getDirectory := fsDirectoryCache(rootDir) + for _,src := range pathqueue { + ui.Message(fmt.Sprintf("Copying: %s", src)) + err = s.Add(getDirectory, src) + if err != nil { + state.Put("error", fmt.Errorf("Error adding path %s to floppy: %s", src, err)) return multistep.ActionHalt } + + // FIXME: setting this map according to each pathqueue entry breaks + // our testcases, because it only keeps track of the number of files + // that are set here instead of actually verifying against the + // filesystem...heh +// s.FilesAdded[src] = true } // Set the path to the floppy so it can be used later @@ -107,6 +134,68 @@ func (s *StepCreateFloppy) Run(state multistep.StateBag) multistep.StepAction { return multistep.ActionContinue } +func (s *StepCreateFloppy) Add(dir getFsDirectory, src string) error { + finfo,err := os.Stat(src) + if err != nil { + return fmt.Errorf("Error adding path to floppy: %s", err) + } + + // add a file + if !finfo.IsDir() { + inputF, err := os.Open(src) + if err != nil { return err } + defer inputF.Close() + + d,err := dir("") + if err != nil { return err } + + entry,err := d.AddFile(path.Base(src)) + if err != nil { return err } + + fatFile,err := entry.File() + if err != nil { return err } + + _,err = io.Copy(fatFile,inputF) + s.FilesAdded[src] = true + return err + } + + // add a directory and it's subdirectories + basedirectory := filepath.Join(src, "..") + visit := func(pathname string, fi os.FileInfo, err error) error { + if err != nil { return err } + if fi.Mode().IsDir() { + base,err := removeBase(basedirectory, pathname) + if err != nil { return err } + _,err = dir(filepath.ToSlash(base)) + return err + } + directory,filename := filepath.Split(pathname) + + base,err := removeBase(basedirectory, directory) + if err != nil { return err } + + inputF, err := os.Open(pathname) + if err != nil { return err } + defer inputF.Close() + + wd,err := dir(filepath.ToSlash(base)) + if err != nil { return err } + + entry,err := wd.AddFile(filename) + if err != nil { return err } + + fatFile,err := entry.File() + if err != nil { return err } + + _,err = io.Copy(fatFile,inputF) + s.FilesAdded[filename] = true + return err + } + + return filepath.Walk(src, visit) +} + func (s *StepCreateFloppy) Cleanup(multistep.StateBag) { if s.floppyPath != "" { log.Printf("Deleting floppy disk: %s", s.floppyPath) @@ -114,85 +203,98 @@ func (s *StepCreateFloppy) Cleanup(multistep.StateBag) { } } -func (s *StepCreateFloppy) addFilespec(dir fs.Directory, src string) error { - // same as http://golang.org/src/pkg/path/filepath/match.go#L308 - if strings.IndexAny(src, "*?[") >= 0 { - matches, err := filepath.Glob(src) - if err != nil { - return err +// removeBase will take a regular os.PathSeparator-separated path and remove the +// prefix directory base from it. Both paths are converted to their absolute +// formats before the stripping takes place. +func removeBase(base string, path string) (string,error) { + var idx int + var err error + + if res,err := filepath.Abs(path); err == nil { + path = res + } + path = filepath.Clean(path) + + if base,err = filepath.Abs(base); err != nil { + return path,err + } + + c1,c2 := strings.Split(base, string(os.PathSeparator)), strings.Split(path, string(os.PathSeparator)) + for idx = 0; idx < len(c1); idx++ { + if len(c1[idx]) == 0 && len(c2[idx]) != 0 { break } + if c1[idx] != c2[idx] { + return "", fmt.Errorf("Path %s is not prefixed by Base %s", path, base) } - return s.addFiles(dir, matches) } - - finfo, err := os.Stat(src) - if err != nil { - return err - } - - if finfo.IsDir() { - return s.addDirectory(dir, src) - } - - return s.addSingleFile(dir, src) + return strings.Join(c2[idx:], string(os.PathSeparator)),nil } -func (s *StepCreateFloppy) addFiles(dir fs.Directory, files []string) error { - for _, file := range files { - err := s.addFilespec(dir, file) - if err != nil { - return err +// fsDirectoryCache returns a function that can be used to grab the fs.Directory +// entry associated with a given path. If an fs.Directory entry is not found +// then it will be created relative to the rootDirectory argument that is +// passed. +type getFsDirectory func(string) (fs.Directory,error) +func fsDirectoryCache(rootDirectory fs.Directory) getFsDirectory { + var cache map[string]fs.Directory + + cache = make(map[string]fs.Directory) + cache[""] = rootDirectory + + Input,Output,Error := make(chan string),make(chan fs.Directory),make(chan error) + go func(Error chan error) { + for { + input := path.Clean(<-Input) + + // found a directory, so yield it + res,ok := cache[input] + if ok { + Output <- res + continue + } + component := strings.Split(input, "/") + + // directory not cached, so start at the root and walk each component + // creating them if they're not in cache + var entry fs.Directory + for i,_ := range component { + + // join all of our components into a key + path := strings.Join(component[:i], "/") + + // check if parent directory is cached + res,ok = cache[path] + if !ok { + // add directory into cache + directory,err := entry.AddDirectory(component[i-1]) + if err != nil { Error <- err; continue } + res,err = directory.Dir() + if err != nil { Error <- err; continue } + cache[path] = res + } + // cool, found a directory + entry = res + } + + // finally create our directory + directory,err := entry.AddDirectory(component[len(component)-1]) + if err != nil { Error <- err; continue } + res,err = directory.Dir() + if err != nil { Error <- err; continue } + cache[input] = res + + // ..and yield it + Output <- entry + } + }(Error) + + getFilesystemDirectory := func(input string) (fs.Directory,error) { + Input <- input + select { + case res := <-Output: + return res,nil + case err := <-Error: + return *new(fs.Directory),err } } - - return nil -} - -func (s *StepCreateFloppy) addDirectory(dir fs.Directory, src string) error { - log.Printf("Adding directory to floppy: %s", src) - - walkFn := func(path string, finfo os.FileInfo, err error) error { - if err != nil { - return err - } - - if path == src { - return nil - } - - if finfo.IsDir() { - return s.addDirectory(dir, path) - } - - return s.addSingleFile(dir, path) - } - - return filepath.Walk(src, walkFn) -} - -func (s *StepCreateFloppy) addSingleFile(dir fs.Directory, src string) error { - log.Printf("Adding file to floppy: %s", src) - - inputF, err := os.Open(src) - if err != nil { - return err - } - defer inputF.Close() - - entry, err := dir.AddFile(filepath.Base(src)) - if err != nil { - return err - } - - fatFile, err := entry.File() - if err != nil { - return err - } - - if _, err := io.Copy(fatFile, inputF); err != nil { - return err - } - - s.FilesAdded[src] = true - - return nil + return getFilesystemDirectory } From a3f0308e920483343c7947c4cbcff8608af40c9c Mon Sep 17 00:00:00 2001 From: Ali Rizvi-Santiago Date: Sat, 5 Mar 2016 01:40:16 -0600 Subject: [PATCH 2/7] Re-implemented the support for the floppy_files keyword in order to remain backwards-compatible with templates using the old syntax. Moved the support for recursive paths from the floppy_files keyword to the new floppy_contents keyword. Shifted some of the code around to add better logging of what's actually being copied. Added a couple of unit-tests for the new floppy_contents implementation. Ensured that all files that were being added were also being included in state.FilesAdded so that the older unit-tests will work. --- builder/qemu/builder.go | 4 +- builder/vmware/iso/builder.go | 3 +- builder/vmware/vmx/builder.go | 3 +- common/floppy_config.go | 13 ++- common/step_create_floppy.go | 107 ++++++++++++++++++----- common/step_create_floppy_test.go | 138 ++++++++++++++++++++++++++++++ 6 files changed, 243 insertions(+), 25 deletions(-) diff --git a/builder/qemu/builder.go b/builder/qemu/builder.go index 588313d06..fdddd275b 100644 --- a/builder/qemu/builder.go +++ b/builder/qemu/builder.go @@ -93,7 +93,6 @@ type Config struct { DiskDiscard string `mapstructure:"disk_discard"` SkipCompaction bool `mapstructure:"skip_compaction"` DiskCompression bool `mapstructure:"disk_compression"` - FloppyFiles []string `mapstructure:"floppy_files"` Format string `mapstructure:"format"` Headless bool `mapstructure:"headless"` DiskImage bool `mapstructure:"disk_image"` @@ -365,7 +364,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe steps = append(steps, new(stepPrepareOutputDir), &common.StepCreateFloppy{ - Files: b.config.FloppyFiles, + Files: b.config.FloppyConfig.FloppyFiles, + Contents: b.config.FloppyConfig.FloppyContents, }, new(stepCreateDisk), new(stepCopyDisk), diff --git a/builder/vmware/iso/builder.go b/builder/vmware/iso/builder.go index 5a6495cb2..9bd4795e5 100755 --- a/builder/vmware/iso/builder.go +++ b/builder/vmware/iso/builder.go @@ -231,7 +231,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe Force: b.config.PackerForce, }, &common.StepCreateFloppy{ - Files: b.config.FloppyFiles, + Files: b.config.FloppyConfig.FloppyFiles, + Contents: b.config.FloppyConfig.FloppyContents, }, &stepRemoteUpload{ Key: "floppy_path", diff --git a/builder/vmware/vmx/builder.go b/builder/vmware/vmx/builder.go index d82f03f1c..a58661023 100644 --- a/builder/vmware/vmx/builder.go +++ b/builder/vmware/vmx/builder.go @@ -62,7 +62,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe Force: b.config.PackerForce, }, &common.StepCreateFloppy{ - Files: b.config.FloppyFiles, + Files: b.config.FloppyConfig.FloppyFiles, + Contents: b.config.FloppyConfig.FloppyContents, }, &StepCloneVMX{ OutputDir: b.config.OutputDir, diff --git a/common/floppy_config.go b/common/floppy_config.go index bd5c5f8c7..3ba515e44 100644 --- a/common/floppy_config.go +++ b/common/floppy_config.go @@ -8,7 +8,8 @@ import ( ) type FloppyConfig struct { - FloppyFiles []string `mapstructure:"floppy_files"` + FloppyFiles []string `mapstructure:"floppy_files"` + FloppyContents []string `mapstructure:"floppy_contents"` } func (c *FloppyConfig) Prepare(ctx *interpolate.Context) []error { @@ -24,5 +25,15 @@ func (c *FloppyConfig) Prepare(ctx *interpolate.Context) []error { } } + if c.FloppyContents == nil { + c.FloppyContents = make([]string, 0) + } + + for _, path := range c.FloppyContents { + if _, err := os.Stat(path); err != nil { + errs = append(errs, fmt.Errorf("Bad Floppy disk directory '%s': %s", path, err)) + } + } + return errs } diff --git a/common/step_create_floppy.go b/common/step_create_floppy.go index f863b8d9f..2564b3ae4 100644 --- a/common/step_create_floppy.go +++ b/common/step_create_floppy.go @@ -19,7 +19,8 @@ import ( // The floppy disk doesn't support sub-directories. Only files at the // root level are supported. type StepCreateFloppy struct { - Files []string + Files []string + Contents []string floppyPath string @@ -27,7 +28,7 @@ type StepCreateFloppy struct { } func (s *StepCreateFloppy) Run(state multistep.StateBag) multistep.StepAction { - if len(s.Files) == 0 { + if len(s.Files) == 0 && len(s.Contents) == 0 { log.Println("No floppy files specified. Floppy disk will not be made.") return multistep.ActionContinue } @@ -85,17 +86,88 @@ func (s *StepCreateFloppy) Run(state multistep.StateBag) multistep.StepAction { return multistep.ActionHalt } - // Get the root directory to the filesystem + // Get the root directory to the filesystem and create a cache for any directories within log.Println("Reading the root directory from the filesystem") rootDir, err := fatFs.RootDir() if err != nil { state.Put("error", fmt.Errorf("Error creating floppy: %s", err)) return multistep.ActionHalt } + cache := fsDirectoryCache(rootDir) + + // Utility functions for walking through a directory grabbing all files flatly + globFiles := func(files []string, list chan string) { + for _,filename := range files { + if strings.IndexAny(filename, "*?[") >= 0 { + matches,_ := filepath.Glob(filename) + if err != nil { continue } + + for _,match := range matches { + list <- match + } + continue + } + list <- filename + } + close(list) + } + + var crawlDirectoryFiles []string + crawlDirectory := func(path string, info os.FileInfo, err error) error { + if !info.IsDir() { + crawlDirectoryFiles = append(crawlDirectoryFiles, path) + ui.Message(fmt.Sprintf("Adding file: %s", path)) + } + return nil + } + crawlDirectoryFiles = []string{} + + // Collect files and copy them flatly...because floppy_files is broken on purpose. + var filelist chan string + filelist = make(chan string) + go globFiles(s.Files, filelist) + + ui.Message("Copying files flatly from floppy_files") + for { + filename, ok := <-filelist + if !ok { break } + + finfo,err := os.Stat(filename) + if err != nil { + state.Put("error", fmt.Errorf("Error trying to stat : %s : %s", filename, err)) + return multistep.ActionHalt + } + + // walk through directory adding files to the root of the fs + if finfo.IsDir() { + ui.Message(fmt.Sprintf("Copying directory: %s", filename)) + + err := filepath.Walk(filename, crawlDirectory) + if err != nil { + state.Put("error", fmt.Errorf("Error adding file from floppy_files : %s : %s", filename, err)) + return multistep.ActionHalt + } + + for _,crawlfilename := range crawlDirectoryFiles { + s.Add(cache, crawlfilename) + s.FilesAdded[crawlfilename] = true + } + + crawlDirectoryFiles = []string{} + continue + } + + // add just a single file + ui.Message(fmt.Sprintf("Copying file: %s", filename)) + s.Add(cache, filename) + s.FilesAdded[filename] = true + } + ui.Message("Done copying files from floppy_files") // Collect all paths (expanding wildcards) into pathqueue + ui.Message("Collecting paths from floppy_contents") var pathqueue []string - for _,filename := range s.Files { + for _,filename := range s.Contents { if strings.IndexAny(filename, "*?[") >= 0 { matches,err := filepath.Glob(filename) if err != nil { @@ -110,23 +182,18 @@ func (s *StepCreateFloppy) Run(state multistep.StateBag) multistep.StepAction { } pathqueue = append(pathqueue, filename) } + ui.Message(fmt.Sprintf("Resulting paths from floppy_contents : %v", pathqueue)) // Go over each path in pathqueue and copy it. - getDirectory := fsDirectoryCache(rootDir) for _,src := range pathqueue { - ui.Message(fmt.Sprintf("Copying: %s", src)) - err = s.Add(getDirectory, src) + ui.Message(fmt.Sprintf("Recursively copying : %s", src)) + err = s.Add(cache, src) if err != nil { state.Put("error", fmt.Errorf("Error adding path %s to floppy: %s", src, err)) return multistep.ActionHalt } - - // FIXME: setting this map according to each pathqueue entry breaks - // our testcases, because it only keeps track of the number of files - // that are set here instead of actually verifying against the - // filesystem...heh -// s.FilesAdded[src] = true } + ui.Message("Done copying paths from floppy_contents") // Set the path to the floppy so it can be used later state.Put("floppy_path", s.floppyPath) @@ -134,7 +201,7 @@ func (s *StepCreateFloppy) Run(state multistep.StateBag) multistep.StepAction { return multistep.ActionContinue } -func (s *StepCreateFloppy) Add(dir getFsDirectory, src string) error { +func (s *StepCreateFloppy) Add(dircache directoryCache, src string) error { finfo,err := os.Stat(src) if err != nil { return fmt.Errorf("Error adding path to floppy: %s", err) @@ -146,7 +213,7 @@ func (s *StepCreateFloppy) Add(dir getFsDirectory, src string) error { if err != nil { return err } defer inputF.Close() - d,err := dir("") + d,err := dircache("") if err != nil { return err } entry,err := d.AddFile(path.Base(src)) @@ -167,7 +234,7 @@ func (s *StepCreateFloppy) Add(dir getFsDirectory, src string) error { if fi.Mode().IsDir() { base,err := removeBase(basedirectory, pathname) if err != nil { return err } - _,err = dir(filepath.ToSlash(base)) + _,err = dircache(filepath.ToSlash(base)) return err } directory,filename := filepath.Split(pathname) @@ -179,7 +246,7 @@ func (s *StepCreateFloppy) Add(dir getFsDirectory, src string) error { if err != nil { return err } defer inputF.Close() - wd,err := dir(filepath.ToSlash(base)) + wd,err := dircache(filepath.ToSlash(base)) if err != nil { return err } entry,err := wd.AddFile(filename) @@ -189,7 +256,7 @@ func (s *StepCreateFloppy) Add(dir getFsDirectory, src string) error { if err != nil { return err } _,err = io.Copy(fatFile,inputF) - s.FilesAdded[filename] = true + s.FilesAdded[pathname] = true return err } @@ -233,8 +300,8 @@ func removeBase(base string, path string) (string,error) { // entry associated with a given path. If an fs.Directory entry is not found // then it will be created relative to the rootDirectory argument that is // passed. -type getFsDirectory func(string) (fs.Directory,error) -func fsDirectoryCache(rootDirectory fs.Directory) getFsDirectory { +type directoryCache func(string) (fs.Directory,error) +func fsDirectoryCache(rootDirectory fs.Directory) directoryCache { var cache map[string]fs.Directory cache = make(map[string]fs.Directory) diff --git a/common/step_create_floppy_test.go b/common/step_create_floppy_test.go index d1a3000a7..61dfac68e 100644 --- a/common/step_create_floppy_test.go +++ b/common/step_create_floppy_test.go @@ -7,10 +7,53 @@ import ( "io/ioutil" "os" "path" + "path/filepath" "strconv" "testing" + "log" + "strings" + "fmt" ) +// utility function for returning a directory structure as a list of strings +func getDirectory(path string) []string { + var result []string + walk := func(path string, info os.FileInfo, err error) error { + if info.IsDir() && !strings.HasSuffix(path, "/") { + path = path + "/" + } + result = append(result, filepath.ToSlash(path)) + return nil + } + filepath.Walk(path, walk) + return result +} + +// utility function for creating a directory structure +type createFileContents func(string) []byte +func createDirectory(path string, hier []string, fileContents createFileContents) error { + if fileContents == nil { + fileContents = func(string) []byte { + return []byte{} + } + } + for _,filename := range hier { + p := filepath.Join(path, filename) + if strings.HasSuffix(filename, "/") { + err := os.MkdirAll(p, 0) + if err != nil { return err } + continue + } + f,err := os.Create(p) + if err != nil { return err } + _,err = f.Write(fileContents(filename)) + if err != nil { return err } + err = f.Close() + if err != nil { return err } + } + return nil +} + func TestStepCreateFloppy_Impl(t *testing.T) { var raw interface{} raw = new(StepCreateFloppy) @@ -191,3 +234,98 @@ func xxxTestStepCreateFloppy_notfound(t *testing.T) { } } } + +func TestStepCreateFloppyContents(t *testing.T) { + // file-system hierarchies + hierarchies := [][]string{ + []string{"file1", "file2", "file3"}, + []string{"dir1/", "dir1/file1", "dir1/file2", "dir1/file3"}, + []string{"dir1/", "dir1/file1", "dir1/subdir1/", "dir1/subdir1/file1", "dir1/subdir1/file2", "dir2/", "dir2/subdir1/", "dir2/subdir1/file1","dir2/subdir1/file2"}, + } + + type contentsTest struct { + contents []string + result []string + } + + // keep in mind that .FilesAdded doesn't keep track of the target filename or directories, but rather the source filename. + contents := [][]contentsTest{ + []contentsTest{ + contentsTest{contents:[]string{"file1","file2","file3"},result:[]string{"file1","file2","file3"}}, + contentsTest{contents:[]string{"file?"},result:[]string{"file1","file2","file3"}}, + contentsTest{contents:[]string{"*"},result:[]string{"file1","file2","file3"}}, + }, + []contentsTest{ + contentsTest{contents:[]string{"dir1"},result:[]string{"dir1/file1","dir1/file2","dir1/file3"}}, + contentsTest{contents:[]string{"dir1/file1","dir1/file2","dir1/file3"},result:[]string{"dir1/file1","dir1/file2","dir1/file3"}}, + contentsTest{contents:[]string{"*"},result:[]string{"dir1/file1","dir1/file2","dir1/file3"}}, + contentsTest{contents:[]string{"*/*"},result:[]string{"dir1/file1","dir1/file2","dir1/file3"}}, + }, + []contentsTest{ + contentsTest{contents:[]string{"dir1"},result:[]string{"dir1/file1","dir1/subdir1/file1","dir1/subdir1/file2"}}, + contentsTest{contents:[]string{"dir2/*"},result:[]string{"dir2/subdir1/file1","dir2/subdir1/file2"}}, + contentsTest{contents:[]string{"dir2/subdir1"},result:[]string{"dir2/subdir1/file1","dir2/subdir1/file2"}}, + contentsTest{contents:[]string{"dir?"},result:[]string{"dir1/file1","dir1/subdir1/file1","dir1/subdir1/file2","dir2/subdir1/file1","dir2/subdir1/file2"}}, + }, + } + + // create the hierarchy for each file + for i,hier := range hierarchies { + t.Logf("Trying with hierarchy : %v",hier) + + // create the temp directory + dir, err := ioutil.TempDir("", "packer") + if err != nil { + t.Fatalf("err: %s", err) + } + + // create the file contents + err = createDirectory(dir, hier, nil) + if err != nil { t.Fatalf("err: %s", err) } + t.Logf("Making %v", hier) + + for _,test := range contents[i] { + // createa new state and step + state := testStepCreateFloppyState(t) + step := new(StepCreateFloppy) + + // modify step.Contents with ones from testcase + step.Contents = []string{} + for _,c := range test.contents { + step.Contents = append(step.Contents, filepath.Join(dir,filepath.FromSlash(c))) + } + log.Println(fmt.Sprintf("Trying against floppy_contents : %v",step.Contents)) + + // run the step + if action := step.Run(state); action != multistep.ActionContinue { + t.Fatalf("bad action: %#v for %v : %v", action, step.Contents, state.Get("error")) + } + + if _, ok := state.GetOk("error"); ok { + t.Fatalf("state should be ok for %v : %v", step.Contents, state.Get("error")) + } + + floppy_path := state.Get("floppy_path").(string) + if _, err := os.Stat(floppy_path); err != nil { + t.Fatalf("file not found: %s for %v : %v", floppy_path, step.Contents) + } + + // check the FilesAdded array to see if it matches + for _,rpath := range test.result { + fpath := filepath.Join(dir, filepath.FromSlash(rpath)) + if !step.FilesAdded[fpath] { + t.Fatalf("unable to find file: %s for %v", fpath, step.Contents) + } + } + + // cleanup the step + step.Cleanup(state) + + if _, err := os.Stat(floppy_path); err == nil { + t.Fatalf("file found: %s for %v", floppy_path, step.Contents) + } + } + // remove the temp directory + os.RemoveAll(dir) + } +} From 5f9fdaf5fa6e9ed51c6e5bc0a04aa288ee97c085 Mon Sep 17 00:00:00 2001 From: Ali Rizvi-Santiago Date: Tue, 5 Apr 2016 18:14:20 -0500 Subject: [PATCH 3/7] Added documentation for the floppy_contents option to parallels, qemu, virtualbox, and vmware. --- website/source/docs/builders/parallels-iso.html.md | 6 ++++++ website/source/docs/builders/parallels-pvm.html.md | 10 ++++++++++ website/source/docs/builders/qemu.html.md | 6 ++++++ website/source/docs/builders/virtualbox-iso.html.md | 6 ++++++ website/source/docs/builders/virtualbox-ovf.html.md | 6 ++++++ website/source/docs/builders/vmware-iso.html.md | 6 ++++++ website/source/docs/builders/vmware-vmx.html.md | 6 ++++++ 7 files changed, 46 insertions(+) diff --git a/website/source/docs/builders/parallels-iso.html.md b/website/source/docs/builders/parallels-iso.html.md index 61fef28f9..a93a7951d 100644 --- a/website/source/docs/builders/parallels-iso.html.md +++ b/website/source/docs/builders/parallels-iso.html.md @@ -115,6 +115,12 @@ builder. and \[\]) are allowed. Directory names are also allowed, which will add all the files found in the directory to the floppy. +- `floppy_contents` (array of strings) - A list of directories to place onto + the floppy disk recursively. This is similar to the `floppy_files` option + except that the directory structure is preserved. This is useful for when + your floppy disk includes drivers or if you just want to organize it's + contents as a hierarchy. Wildcard characters (\*, ?, and \[\]) are allowed. + - `guest_os_type` (string) - The guest OS type being installed. By default this is "other", but you can get *dramatic* performance improvements by setting this to the proper value. To view all available values for this run diff --git a/website/source/docs/builders/parallels-pvm.html.md b/website/source/docs/builders/parallels-pvm.html.md index cb40a81b9..3eceed805 100644 --- a/website/source/docs/builders/parallels-pvm.html.md +++ b/website/source/docs/builders/parallels-pvm.html.md @@ -86,6 +86,16 @@ builder. listed in this configuration will all be put into the root directory of the floppy disk; sub-directories are not supported. +- `floppy_contents` (array of strings) - A list of directories to place onto + the floppy disk recursively. This is similar to the `floppy_files` option + except that the directory structure is preserved. This is useful for when + your floppy disk includes drivers or if you just want to organize it's + contents as a hierarchy. Wildcard characters (\*, ?, and \[\]) are allowed. + +- `reassign_mac` (boolean) - If this is "false" the MAC address of the first + NIC will reused when imported else a new MAC address will be generated + by Parallels. Defaults to "false". + - `output_directory` (string) - This is the path to the directory where the resulting virtual machine will be created. This may be relative or absolute. If relative, the path is relative to the working directory when `packer` diff --git a/website/source/docs/builders/qemu.html.md b/website/source/docs/builders/qemu.html.md index 32ddacdf3..e9da535bb 100644 --- a/website/source/docs/builders/qemu.html.md +++ b/website/source/docs/builders/qemu.html.md @@ -164,6 +164,12 @@ Linux server and have not enabled X11 forwarding (`ssh -X`). and \[\]) are allowed. Directory names are also allowed, which will add all the files found in the directory to the floppy. +- `floppy_contents` (array of strings) - A list of directories to place onto + the floppy disk recursively. This is similar to the `floppy_files` option + except that the directory structure is preserved. This is useful for when + your floppy disk includes drivers or if you just want to organize it's + contents as a hierarchy. Wildcard characters (\*, ?, and \[\]) are allowed. + - `format` (string) - Either "qcow2" or "raw", this specifies the output format of the virtual machine image. This defaults to `qcow2`. diff --git a/website/source/docs/builders/virtualbox-iso.html.md b/website/source/docs/builders/virtualbox-iso.html.md index 103e78c45..96e928c9d 100644 --- a/website/source/docs/builders/virtualbox-iso.html.md +++ b/website/source/docs/builders/virtualbox-iso.html.md @@ -150,6 +150,12 @@ builder. and \[\]) are allowed. Directory names are also allowed, which will add all the files found in the directory to the floppy. +- `floppy_contents` (array of strings) - A list of directories to place onto + the floppy disk recursively. This is similar to the `floppy_files` option + except that the directory structure is preserved. This is useful for when + your floppy disk includes drivers or if you just want to organize it's + contents as a hierarchy. Wildcard characters (\*, ?, and \[\]) are allowed. + - `format` (string) - Either "ovf" or "ova", this specifies the output format of the exported virtual machine. This defaults to "ovf". diff --git a/website/source/docs/builders/virtualbox-ovf.html.md b/website/source/docs/builders/virtualbox-ovf.html.md index fdf086656..11b861c6d 100644 --- a/website/source/docs/builders/virtualbox-ovf.html.md +++ b/website/source/docs/builders/virtualbox-ovf.html.md @@ -132,6 +132,12 @@ builder. and \[\]) are allowed. Directory names are also allowed, which will add all the files found in the directory to the floppy. +- `floppy_contents` (array of strings) - A list of directories to place onto + the floppy disk recursively. This is similar to the `floppy_files` option + except that the directory structure is preserved. This is useful for when + your floppy disk includes drivers or if you just want to organize it's + contents as a hierarchy. Wildcard characters (\*, ?, and \[\]) are allowed. + - `format` (string) - Either "ovf" or "ova", this specifies the output format of the exported virtual machine. This defaults to "ovf". diff --git a/website/source/docs/builders/vmware-iso.html.md b/website/source/docs/builders/vmware-iso.html.md index c4991acf5..8588b2a58 100644 --- a/website/source/docs/builders/vmware-iso.html.md +++ b/website/source/docs/builders/vmware-iso.html.md @@ -125,6 +125,12 @@ builder. and \[\]) are allowed. Directory names are also allowed, which will add all the files found in the directory to the floppy. +- `floppy_contents` (array of strings) - A list of directories to place onto + the floppy disk recursively. This is similar to the `floppy_files` option + except that the directory structure is preserved. This is useful for when + your floppy disk includes drivers or if you just want to organize it's + contents as a hierarchy. Wildcard characters (\*, ?, and \[\]) are allowed. + - `fusion_app_path` (string) - Path to "VMware Fusion.app". By default this is "/Applications/VMware Fusion.app" but this setting allows you to customize this. diff --git a/website/source/docs/builders/vmware-vmx.html.md b/website/source/docs/builders/vmware-vmx.html.md index 8cfa3b80a..bbfed97b5 100644 --- a/website/source/docs/builders/vmware-vmx.html.md +++ b/website/source/docs/builders/vmware-vmx.html.md @@ -83,6 +83,12 @@ builder. and \[\]) are allowed. Directory names are also allowed, which will add all the files found in the directory to the floppy. +- `floppy_contents` (array of strings) - A list of directories to place onto + the floppy disk recursively. This is similar to the `floppy_files` option + except that the directory structure is preserved. This is useful for when + your floppy disk includes drivers or if you just want to organize it's + contents as a hierarchy. Wildcard characters (\*, ?, and \[\]) are allowed. + - `fusion_app_path` (string) - Path to "VMware Fusion.app". By default this is "/Applications/VMware Fusion.app" but this setting allows you to customize this. From 915b7f371a65b1296cfa6438a2e50e3d2863f2fe Mon Sep 17 00:00:00 2001 From: Ali Rizvi-Santiago Date: Tue, 5 Apr 2016 18:32:11 -0500 Subject: [PATCH 4/7] Added missing argument to step_create_floppy_test.go --- common/step_create_floppy_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/step_create_floppy_test.go b/common/step_create_floppy_test.go index 61dfac68e..bfa7c52c3 100644 --- a/common/step_create_floppy_test.go +++ b/common/step_create_floppy_test.go @@ -307,7 +307,7 @@ func TestStepCreateFloppyContents(t *testing.T) { floppy_path := state.Get("floppy_path").(string) if _, err := os.Stat(floppy_path); err != nil { - t.Fatalf("file not found: %s for %v : %v", floppy_path, step.Contents) + t.Fatalf("file not found: %s for %v : %v", floppy_path, step.Contents, err) } // check the FilesAdded array to see if it matches From fbe305cf4ea8ad4eb558bb5967fd2d1f42e8c77b Mon Sep 17 00:00:00 2001 From: Ali Rizvi-Santiago Date: Mon, 12 Sep 2016 12:07:35 -0500 Subject: [PATCH 5/7] Renamed floppy_contents to floppy_dirs as requested by rickard.von.essen@gmail.com --- common/floppy_config.go | 2 +- common/step_create_floppy.go | 6 +++--- common/step_create_floppy_test.go | 2 +- website/source/docs/builders/parallels-iso.html.md | 2 +- website/source/docs/builders/parallels-pvm.html.md | 2 +- website/source/docs/builders/qemu.html.md | 2 +- website/source/docs/builders/virtualbox-iso.html.md | 2 +- website/source/docs/builders/virtualbox-ovf.html.md | 2 +- website/source/docs/builders/vmware-iso.html.md | 2 +- website/source/docs/builders/vmware-vmx.html.md | 2 +- 10 files changed, 12 insertions(+), 12 deletions(-) diff --git a/common/floppy_config.go b/common/floppy_config.go index 3ba515e44..385026905 100644 --- a/common/floppy_config.go +++ b/common/floppy_config.go @@ -9,7 +9,7 @@ import ( type FloppyConfig struct { FloppyFiles []string `mapstructure:"floppy_files"` - FloppyContents []string `mapstructure:"floppy_contents"` + FloppyContents []string `mapstructure:"floppy_dirs"` } func (c *FloppyConfig) Prepare(ctx *interpolate.Context) []error { diff --git a/common/step_create_floppy.go b/common/step_create_floppy.go index 2564b3ae4..9850d6d28 100644 --- a/common/step_create_floppy.go +++ b/common/step_create_floppy.go @@ -165,7 +165,7 @@ func (s *StepCreateFloppy) Run(state multistep.StateBag) multistep.StepAction { ui.Message("Done copying files from floppy_files") // Collect all paths (expanding wildcards) into pathqueue - ui.Message("Collecting paths from floppy_contents") + ui.Message("Collecting paths from floppy_dirs") var pathqueue []string for _,filename := range s.Contents { if strings.IndexAny(filename, "*?[") >= 0 { @@ -182,7 +182,7 @@ func (s *StepCreateFloppy) Run(state multistep.StateBag) multistep.StepAction { } pathqueue = append(pathqueue, filename) } - ui.Message(fmt.Sprintf("Resulting paths from floppy_contents : %v", pathqueue)) + ui.Message(fmt.Sprintf("Resulting paths from floppy_dirs : %v", pathqueue)) // Go over each path in pathqueue and copy it. for _,src := range pathqueue { @@ -193,7 +193,7 @@ func (s *StepCreateFloppy) Run(state multistep.StateBag) multistep.StepAction { return multistep.ActionHalt } } - ui.Message("Done copying paths from floppy_contents") + ui.Message("Done copying paths from floppy_dirs") // Set the path to the floppy so it can be used later state.Put("floppy_path", s.floppyPath) diff --git a/common/step_create_floppy_test.go b/common/step_create_floppy_test.go index bfa7c52c3..678605bea 100644 --- a/common/step_create_floppy_test.go +++ b/common/step_create_floppy_test.go @@ -294,7 +294,7 @@ func TestStepCreateFloppyContents(t *testing.T) { for _,c := range test.contents { step.Contents = append(step.Contents, filepath.Join(dir,filepath.FromSlash(c))) } - log.Println(fmt.Sprintf("Trying against floppy_contents : %v",step.Contents)) + log.Println(fmt.Sprintf("Trying against floppy_dirs : %v",step.Contents)) // run the step if action := step.Run(state); action != multistep.ActionContinue { diff --git a/website/source/docs/builders/parallels-iso.html.md b/website/source/docs/builders/parallels-iso.html.md index a93a7951d..0cd8ae422 100644 --- a/website/source/docs/builders/parallels-iso.html.md +++ b/website/source/docs/builders/parallels-iso.html.md @@ -115,7 +115,7 @@ builder. and \[\]) are allowed. Directory names are also allowed, which will add all the files found in the directory to the floppy. -- `floppy_contents` (array of strings) - A list of directories to place onto +- `floppy_dirs` (array of strings) - A list of directories to place onto the floppy disk recursively. This is similar to the `floppy_files` option except that the directory structure is preserved. This is useful for when your floppy disk includes drivers or if you just want to organize it's diff --git a/website/source/docs/builders/parallels-pvm.html.md b/website/source/docs/builders/parallels-pvm.html.md index 3eceed805..68971b168 100644 --- a/website/source/docs/builders/parallels-pvm.html.md +++ b/website/source/docs/builders/parallels-pvm.html.md @@ -86,7 +86,7 @@ builder. listed in this configuration will all be put into the root directory of the floppy disk; sub-directories are not supported. -- `floppy_contents` (array of strings) - A list of directories to place onto +- `floppy_dirs` (array of strings) - A list of directories to place onto the floppy disk recursively. This is similar to the `floppy_files` option except that the directory structure is preserved. This is useful for when your floppy disk includes drivers or if you just want to organize it's diff --git a/website/source/docs/builders/qemu.html.md b/website/source/docs/builders/qemu.html.md index e9da535bb..000c4a763 100644 --- a/website/source/docs/builders/qemu.html.md +++ b/website/source/docs/builders/qemu.html.md @@ -164,7 +164,7 @@ Linux server and have not enabled X11 forwarding (`ssh -X`). and \[\]) are allowed. Directory names are also allowed, which will add all the files found in the directory to the floppy. -- `floppy_contents` (array of strings) - A list of directories to place onto +- `floppy_dirs` (array of strings) - A list of directories to place onto the floppy disk recursively. This is similar to the `floppy_files` option except that the directory structure is preserved. This is useful for when your floppy disk includes drivers or if you just want to organize it's diff --git a/website/source/docs/builders/virtualbox-iso.html.md b/website/source/docs/builders/virtualbox-iso.html.md index 96e928c9d..94928f918 100644 --- a/website/source/docs/builders/virtualbox-iso.html.md +++ b/website/source/docs/builders/virtualbox-iso.html.md @@ -150,7 +150,7 @@ builder. and \[\]) are allowed. Directory names are also allowed, which will add all the files found in the directory to the floppy. -- `floppy_contents` (array of strings) - A list of directories to place onto +- `floppy_dirs` (array of strings) - A list of directories to place onto the floppy disk recursively. This is similar to the `floppy_files` option except that the directory structure is preserved. This is useful for when your floppy disk includes drivers or if you just want to organize it's diff --git a/website/source/docs/builders/virtualbox-ovf.html.md b/website/source/docs/builders/virtualbox-ovf.html.md index 11b861c6d..277ba9731 100644 --- a/website/source/docs/builders/virtualbox-ovf.html.md +++ b/website/source/docs/builders/virtualbox-ovf.html.md @@ -132,7 +132,7 @@ builder. and \[\]) are allowed. Directory names are also allowed, which will add all the files found in the directory to the floppy. -- `floppy_contents` (array of strings) - A list of directories to place onto +- `floppy_dirs` (array of strings) - A list of directories to place onto the floppy disk recursively. This is similar to the `floppy_files` option except that the directory structure is preserved. This is useful for when your floppy disk includes drivers or if you just want to organize it's diff --git a/website/source/docs/builders/vmware-iso.html.md b/website/source/docs/builders/vmware-iso.html.md index 8588b2a58..690c0c125 100644 --- a/website/source/docs/builders/vmware-iso.html.md +++ b/website/source/docs/builders/vmware-iso.html.md @@ -125,7 +125,7 @@ builder. and \[\]) are allowed. Directory names are also allowed, which will add all the files found in the directory to the floppy. -- `floppy_contents` (array of strings) - A list of directories to place onto +- `floppy_dirs` (array of strings) - A list of directories to place onto the floppy disk recursively. This is similar to the `floppy_files` option except that the directory structure is preserved. This is useful for when your floppy disk includes drivers or if you just want to organize it's diff --git a/website/source/docs/builders/vmware-vmx.html.md b/website/source/docs/builders/vmware-vmx.html.md index bbfed97b5..05e8e4714 100644 --- a/website/source/docs/builders/vmware-vmx.html.md +++ b/website/source/docs/builders/vmware-vmx.html.md @@ -83,7 +83,7 @@ builder. and \[\]) are allowed. Directory names are also allowed, which will add all the files found in the directory to the floppy. -- `floppy_contents` (array of strings) - A list of directories to place onto +- `floppy_dirs` (array of strings) - A list of directories to place onto the floppy disk recursively. This is similar to the `floppy_files` option except that the directory structure is preserved. This is useful for when your floppy disk includes drivers or if you just want to organize it's From 86c00490e9c469ef14202e52f71ceb92a40a2564 Mon Sep 17 00:00:00 2001 From: Ali Rizvi-Santiago Date: Tue, 27 Sep 2016 23:31:42 -0500 Subject: [PATCH 6/7] Renamed any and all instances of the word "FloppyContents" to "FloppyDirectories". Ensure that all builders include FloppyDirectories in the StepCreateFloppy options. Changed the way the unit-tests in common/step_create_floppy_test work to use the static test-fixtures directory instead of creating the paths dynamically. Removed a duplicate line of documentation from parallels-pvm.html.md that occurred during rebasing. --- builder/parallels/iso/builder.go | 3 +- builder/parallels/pvm/builder.go | 3 +- builder/qemu/builder.go | 2 +- builder/vmware/iso/builder.go | 2 +- builder/vmware/vmx/builder.go | 2 +- common/floppy_config.go | 8 +- common/step_create_floppy.go | 8 +- common/step_create_floppy_test.go | 104 ++++++------------ common/test-fixtures/floppy-hier/test-0/file1 | 0 common/test-fixtures/floppy-hier/test-0/file2 | 0 common/test-fixtures/floppy-hier/test-0/file3 | 0 .../floppy-hier/test-1/dir1/file1 | 0 .../floppy-hier/test-1/dir1/file2 | 0 .../floppy-hier/test-1/dir1/file3 | 0 .../floppy-hier/test-2/dir1/file1 | 0 .../floppy-hier/test-2/dir1/subdir1/file1 | 0 .../floppy-hier/test-2/dir1/subdir1/file2 | 0 .../floppy-hier/test-2/dir2/subdir1/file1 | 0 .../floppy-hier/test-2/dir2/subdir1/file2 | 0 .../docs/builders/parallels-pvm.html.md | 4 - 20 files changed, 47 insertions(+), 89 deletions(-) create mode 100644 common/test-fixtures/floppy-hier/test-0/file1 create mode 100644 common/test-fixtures/floppy-hier/test-0/file2 create mode 100644 common/test-fixtures/floppy-hier/test-0/file3 create mode 100644 common/test-fixtures/floppy-hier/test-1/dir1/file1 create mode 100644 common/test-fixtures/floppy-hier/test-1/dir1/file2 create mode 100644 common/test-fixtures/floppy-hier/test-1/dir1/file3 create mode 100644 common/test-fixtures/floppy-hier/test-2/dir1/file1 create mode 100644 common/test-fixtures/floppy-hier/test-2/dir1/subdir1/file1 create mode 100644 common/test-fixtures/floppy-hier/test-2/dir1/subdir1/file2 create mode 100644 common/test-fixtures/floppy-hier/test-2/dir2/subdir1/file1 create mode 100644 common/test-fixtures/floppy-hier/test-2/dir2/subdir1/file2 diff --git a/builder/parallels/iso/builder.go b/builder/parallels/iso/builder.go index 134f980f3..c6e0e1b8c 100644 --- a/builder/parallels/iso/builder.go +++ b/builder/parallels/iso/builder.go @@ -149,7 +149,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe Path: b.config.OutputDir, }, &common.StepCreateFloppy{ - Files: b.config.FloppyFiles, + Files: b.config.FloppyConfig.FloppyFiles, + Directories: b.config.FloppyConfig.FloppyDirectories, }, &common.StepHTTPServer{ HTTPDir: b.config.HTTPDir, diff --git a/builder/parallels/pvm/builder.go b/builder/parallels/pvm/builder.go index b62181b06..6c34a63d6 100644 --- a/builder/parallels/pvm/builder.go +++ b/builder/parallels/pvm/builder.go @@ -59,7 +59,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe Path: b.config.OutputDir, }, &common.StepCreateFloppy{ - Files: b.config.FloppyFiles, + Files: b.config.FloppyConfig.FloppyFiles, + Directories: b.config.FloppyConfig.FloppyDirectories, }, &StepImport{ Name: b.config.VMName, diff --git a/builder/qemu/builder.go b/builder/qemu/builder.go index fdddd275b..39c5b7b08 100644 --- a/builder/qemu/builder.go +++ b/builder/qemu/builder.go @@ -365,7 +365,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe steps = append(steps, new(stepPrepareOutputDir), &common.StepCreateFloppy{ Files: b.config.FloppyConfig.FloppyFiles, - Contents: b.config.FloppyConfig.FloppyContents, + Directories: b.config.FloppyConfig.FloppyDirectories, }, new(stepCreateDisk), new(stepCopyDisk), diff --git a/builder/vmware/iso/builder.go b/builder/vmware/iso/builder.go index 9bd4795e5..6f3381911 100755 --- a/builder/vmware/iso/builder.go +++ b/builder/vmware/iso/builder.go @@ -232,7 +232,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe }, &common.StepCreateFloppy{ Files: b.config.FloppyConfig.FloppyFiles, - Contents: b.config.FloppyConfig.FloppyContents, + Directories: b.config.FloppyConfig.FloppyDirectories, }, &stepRemoteUpload{ Key: "floppy_path", diff --git a/builder/vmware/vmx/builder.go b/builder/vmware/vmx/builder.go index a58661023..f11baba0e 100644 --- a/builder/vmware/vmx/builder.go +++ b/builder/vmware/vmx/builder.go @@ -63,7 +63,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe }, &common.StepCreateFloppy{ Files: b.config.FloppyConfig.FloppyFiles, - Contents: b.config.FloppyConfig.FloppyContents, + Directories: b.config.FloppyConfig.FloppyDirectories, }, &StepCloneVMX{ OutputDir: b.config.OutputDir, diff --git a/common/floppy_config.go b/common/floppy_config.go index 385026905..20496137c 100644 --- a/common/floppy_config.go +++ b/common/floppy_config.go @@ -9,7 +9,7 @@ import ( type FloppyConfig struct { FloppyFiles []string `mapstructure:"floppy_files"` - FloppyContents []string `mapstructure:"floppy_dirs"` + FloppyDirectories []string `mapstructure:"floppy_dirs"` } func (c *FloppyConfig) Prepare(ctx *interpolate.Context) []error { @@ -25,11 +25,11 @@ func (c *FloppyConfig) Prepare(ctx *interpolate.Context) []error { } } - if c.FloppyContents == nil { - c.FloppyContents = make([]string, 0) + if c.FloppyDirectories == nil { + c.FloppyDirectories = make([]string, 0) } - for _, path := range c.FloppyContents { + for _, path := range c.FloppyDirectories { if _, err := os.Stat(path); err != nil { errs = append(errs, fmt.Errorf("Bad Floppy disk directory '%s': %s", path, err)) } diff --git a/common/step_create_floppy.go b/common/step_create_floppy.go index 9850d6d28..0ea7cf0b2 100644 --- a/common/step_create_floppy.go +++ b/common/step_create_floppy.go @@ -16,11 +16,9 @@ import ( ) // StepCreateFloppy will create a floppy disk with the given files. -// The floppy disk doesn't support sub-directories. Only files at the -// root level are supported. type StepCreateFloppy struct { Files []string - Contents []string + Directories []string floppyPath string @@ -28,7 +26,7 @@ type StepCreateFloppy struct { } func (s *StepCreateFloppy) Run(state multistep.StateBag) multistep.StepAction { - if len(s.Files) == 0 && len(s.Contents) == 0 { + if len(s.Files) == 0 && len(s.Directories) == 0 { log.Println("No floppy files specified. Floppy disk will not be made.") return multistep.ActionContinue } @@ -167,7 +165,7 @@ func (s *StepCreateFloppy) Run(state multistep.StateBag) multistep.StepAction { // Collect all paths (expanding wildcards) into pathqueue ui.Message("Collecting paths from floppy_dirs") var pathqueue []string - for _,filename := range s.Contents { + for _,filename := range s.Directories { if strings.IndexAny(filename, "*?[") >= 0 { matches,err := filepath.Glob(filename) if err != nil { diff --git a/common/step_create_floppy_test.go b/common/step_create_floppy_test.go index 678605bea..a867f1bfe 100644 --- a/common/step_create_floppy_test.go +++ b/common/step_create_floppy_test.go @@ -15,6 +15,8 @@ import ( "fmt" ) +const TestFixtures = "test-fixtures" + // utility function for returning a directory structure as a list of strings func getDirectory(path string) []string { var result []string @@ -29,31 +31,6 @@ func getDirectory(path string) []string { return result } -// utility function for creating a directory structure -type createFileContents func(string) []byte -func createDirectory(path string, hier []string, fileContents createFileContents) error { - if fileContents == nil { - fileContents = func(string) []byte { - return []byte{} - } - } - for _,filename := range hier { - p := filepath.Join(path, filename) - if strings.HasSuffix(filename, "/") { - err := os.MkdirAll(p, 0) - if err != nil { return err } - continue - } - f,err := os.Create(p) - if err != nil { return err } - _,err = f.Write(fileContents(filename)) - if err != nil { return err } - err = f.Close() - if err != nil { return err } - } - return nil -} - func TestStepCreateFloppy_Impl(t *testing.T) { var raw interface{} raw = new(StepCreateFloppy) @@ -235,86 +212,73 @@ func xxxTestStepCreateFloppy_notfound(t *testing.T) { } } -func TestStepCreateFloppyContents(t *testing.T) { +func TestStepCreateFloppyDirectories(t *testing.T) { + const TestName = "floppy-hier" + // file-system hierarchies - hierarchies := [][]string{ - []string{"file1", "file2", "file3"}, - []string{"dir1/", "dir1/file1", "dir1/file2", "dir1/file3"}, - []string{"dir1/", "dir1/file1", "dir1/subdir1/", "dir1/subdir1/file1", "dir1/subdir1/file2", "dir2/", "dir2/subdir1/", "dir2/subdir1/file1","dir2/subdir1/file2"}, - } + var basePath = filepath.Join(".", TestFixtures, TestName) type contentsTest struct { - contents []string + dirs []string result []string } // keep in mind that .FilesAdded doesn't keep track of the target filename or directories, but rather the source filename. - contents := [][]contentsTest{ + directories := [][]contentsTest{ []contentsTest{ - contentsTest{contents:[]string{"file1","file2","file3"},result:[]string{"file1","file2","file3"}}, - contentsTest{contents:[]string{"file?"},result:[]string{"file1","file2","file3"}}, - contentsTest{contents:[]string{"*"},result:[]string{"file1","file2","file3"}}, + contentsTest{dirs:[]string{"file1","file2","file3"},result:[]string{"file1","file2","file3"}}, + contentsTest{dirs:[]string{"file?"},result:[]string{"file1","file2","file3"}}, + contentsTest{dirs:[]string{"*"},result:[]string{"file1","file2","file3"}}, }, []contentsTest{ - contentsTest{contents:[]string{"dir1"},result:[]string{"dir1/file1","dir1/file2","dir1/file3"}}, - contentsTest{contents:[]string{"dir1/file1","dir1/file2","dir1/file3"},result:[]string{"dir1/file1","dir1/file2","dir1/file3"}}, - contentsTest{contents:[]string{"*"},result:[]string{"dir1/file1","dir1/file2","dir1/file3"}}, - contentsTest{contents:[]string{"*/*"},result:[]string{"dir1/file1","dir1/file2","dir1/file3"}}, + contentsTest{dirs:[]string{"dir1"},result:[]string{"dir1/file1","dir1/file2","dir1/file3"}}, + contentsTest{dirs:[]string{"dir1/file1","dir1/file2","dir1/file3"},result:[]string{"dir1/file1","dir1/file2","dir1/file3"}}, + contentsTest{dirs:[]string{"*"},result:[]string{"dir1/file1","dir1/file2","dir1/file3"}}, + contentsTest{dirs:[]string{"*/*"},result:[]string{"dir1/file1","dir1/file2","dir1/file3"}}, }, []contentsTest{ - contentsTest{contents:[]string{"dir1"},result:[]string{"dir1/file1","dir1/subdir1/file1","dir1/subdir1/file2"}}, - contentsTest{contents:[]string{"dir2/*"},result:[]string{"dir2/subdir1/file1","dir2/subdir1/file2"}}, - contentsTest{contents:[]string{"dir2/subdir1"},result:[]string{"dir2/subdir1/file1","dir2/subdir1/file2"}}, - contentsTest{contents:[]string{"dir?"},result:[]string{"dir1/file1","dir1/subdir1/file1","dir1/subdir1/file2","dir2/subdir1/file1","dir2/subdir1/file2"}}, + contentsTest{dirs:[]string{"dir1"},result:[]string{"dir1/file1","dir1/subdir1/file1","dir1/subdir1/file2"}}, + contentsTest{dirs:[]string{"dir2/*"},result:[]string{"dir2/subdir1/file1","dir2/subdir1/file2"}}, + contentsTest{dirs:[]string{"dir2/subdir1"},result:[]string{"dir2/subdir1/file1","dir2/subdir1/file2"}}, + contentsTest{dirs:[]string{"dir?"},result:[]string{"dir1/file1","dir1/subdir1/file1","dir1/subdir1/file2","dir2/subdir1/file1","dir2/subdir1/file2"}}, }, } // create the hierarchy for each file - for i,hier := range hierarchies { - t.Logf("Trying with hierarchy : %v",hier) + for i := 0; i < 2; i++ { + dir := filepath.Join(basePath, fmt.Sprintf("test-%d", i)) - // create the temp directory - dir, err := ioutil.TempDir("", "packer") - if err != nil { - t.Fatalf("err: %s", err) - } - - // create the file contents - err = createDirectory(dir, hier, nil) - if err != nil { t.Fatalf("err: %s", err) } - t.Logf("Making %v", hier) - - for _,test := range contents[i] { - // createa new state and step + for _,test := range directories[i] { + // create a new state and step state := testStepCreateFloppyState(t) step := new(StepCreateFloppy) - // modify step.Contents with ones from testcase - step.Contents = []string{} - for _,c := range test.contents { - step.Contents = append(step.Contents, filepath.Join(dir,filepath.FromSlash(c))) + // modify step.Directories with ones from testcase + step.Directories = []string{} + for _,c := range test.dirs { + step.Directories = append(step.Directories, filepath.Join(dir,filepath.FromSlash(c))) } - log.Println(fmt.Sprintf("Trying against floppy_dirs : %v",step.Contents)) + log.Println(fmt.Sprintf("Trying against floppy_dirs : %v",step.Directories)) // run the step if action := step.Run(state); action != multistep.ActionContinue { - t.Fatalf("bad action: %#v for %v : %v", action, step.Contents, state.Get("error")) + t.Fatalf("bad action: %#v for %v : %v", action, step.Directories, state.Get("error")) } if _, ok := state.GetOk("error"); ok { - t.Fatalf("state should be ok for %v : %v", step.Contents, state.Get("error")) + t.Fatalf("state should be ok for %v : %v", step.Directories, state.Get("error")) } floppy_path := state.Get("floppy_path").(string) if _, err := os.Stat(floppy_path); err != nil { - t.Fatalf("file not found: %s for %v : %v", floppy_path, step.Contents, err) + t.Fatalf("file not found: %s for %v : %v", floppy_path, step.Directories, err) } // check the FilesAdded array to see if it matches for _,rpath := range test.result { fpath := filepath.Join(dir, filepath.FromSlash(rpath)) if !step.FilesAdded[fpath] { - t.Fatalf("unable to find file: %s for %v", fpath, step.Contents) + t.Fatalf("unable to find file: %s for %v", fpath, step.Directories) } } @@ -322,10 +286,8 @@ func TestStepCreateFloppyContents(t *testing.T) { step.Cleanup(state) if _, err := os.Stat(floppy_path); err == nil { - t.Fatalf("file found: %s for %v", floppy_path, step.Contents) + t.Fatalf("file found: %s for %v", floppy_path, step.Directories) } } - // remove the temp directory - os.RemoveAll(dir) } } diff --git a/common/test-fixtures/floppy-hier/test-0/file1 b/common/test-fixtures/floppy-hier/test-0/file1 new file mode 100644 index 000000000..e69de29bb diff --git a/common/test-fixtures/floppy-hier/test-0/file2 b/common/test-fixtures/floppy-hier/test-0/file2 new file mode 100644 index 000000000..e69de29bb diff --git a/common/test-fixtures/floppy-hier/test-0/file3 b/common/test-fixtures/floppy-hier/test-0/file3 new file mode 100644 index 000000000..e69de29bb diff --git a/common/test-fixtures/floppy-hier/test-1/dir1/file1 b/common/test-fixtures/floppy-hier/test-1/dir1/file1 new file mode 100644 index 000000000..e69de29bb diff --git a/common/test-fixtures/floppy-hier/test-1/dir1/file2 b/common/test-fixtures/floppy-hier/test-1/dir1/file2 new file mode 100644 index 000000000..e69de29bb diff --git a/common/test-fixtures/floppy-hier/test-1/dir1/file3 b/common/test-fixtures/floppy-hier/test-1/dir1/file3 new file mode 100644 index 000000000..e69de29bb diff --git a/common/test-fixtures/floppy-hier/test-2/dir1/file1 b/common/test-fixtures/floppy-hier/test-2/dir1/file1 new file mode 100644 index 000000000..e69de29bb diff --git a/common/test-fixtures/floppy-hier/test-2/dir1/subdir1/file1 b/common/test-fixtures/floppy-hier/test-2/dir1/subdir1/file1 new file mode 100644 index 000000000..e69de29bb diff --git a/common/test-fixtures/floppy-hier/test-2/dir1/subdir1/file2 b/common/test-fixtures/floppy-hier/test-2/dir1/subdir1/file2 new file mode 100644 index 000000000..e69de29bb diff --git a/common/test-fixtures/floppy-hier/test-2/dir2/subdir1/file1 b/common/test-fixtures/floppy-hier/test-2/dir2/subdir1/file1 new file mode 100644 index 000000000..e69de29bb diff --git a/common/test-fixtures/floppy-hier/test-2/dir2/subdir1/file2 b/common/test-fixtures/floppy-hier/test-2/dir2/subdir1/file2 new file mode 100644 index 000000000..e69de29bb diff --git a/website/source/docs/builders/parallels-pvm.html.md b/website/source/docs/builders/parallels-pvm.html.md index 68971b168..36ba4fd19 100644 --- a/website/source/docs/builders/parallels-pvm.html.md +++ b/website/source/docs/builders/parallels-pvm.html.md @@ -92,10 +92,6 @@ builder. your floppy disk includes drivers or if you just want to organize it's contents as a hierarchy. Wildcard characters (\*, ?, and \[\]) are allowed. -- `reassign_mac` (boolean) - If this is "false" the MAC address of the first - NIC will reused when imported else a new MAC address will be generated - by Parallels. Defaults to "false". - - `output_directory` (string) - This is the path to the directory where the resulting virtual machine will be created. This may be relative or absolute. If relative, the path is relative to the working directory when `packer` From 29ae078326cd0069224651cd31418e2270dc8b20 Mon Sep 17 00:00:00 2001 From: Rickard von Essen Date: Sat, 1 Oct 2016 09:04:50 +0200 Subject: [PATCH 7/7] Added missing virtualbox floppy_dirs --- builder/virtualbox/iso/builder.go | 3 ++- builder/virtualbox/ovf/builder.go | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/builder/virtualbox/iso/builder.go b/builder/virtualbox/iso/builder.go index 0d0790ea1..b6093d834 100644 --- a/builder/virtualbox/iso/builder.go +++ b/builder/virtualbox/iso/builder.go @@ -194,7 +194,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe Path: b.config.OutputDir, }, &common.StepCreateFloppy{ - Files: b.config.FloppyFiles, + Files: b.config.FloppyConfig.FloppyFiles, + Directories: b.config.FloppyConfig.FloppyDirectories, }, &common.StepHTTPServer{ HTTPDir: b.config.HTTPDir, diff --git a/builder/virtualbox/ovf/builder.go b/builder/virtualbox/ovf/builder.go index 502f66734..35cfd4d7d 100644 --- a/builder/virtualbox/ovf/builder.go +++ b/builder/virtualbox/ovf/builder.go @@ -56,7 +56,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe }, new(vboxcommon.StepSuppressMessages), &common.StepCreateFloppy{ - Files: b.config.FloppyFiles, + Files: b.config.FloppyConfig.FloppyFiles, + Directories: b.config.FloppyConfig.FloppyDirectories, }, &common.StepHTTPServer{ HTTPDir: b.config.HTTPDir,