generators: Make more robust

Signed-off-by: Stéphane Graber <stgraber@ubuntu.com>
This commit is contained in:
Stéphane Graber 2018-03-13 17:59:15 -04:00
parent cd307cdec4
commit d248b6782d
No known key found for this signature in database
GPG Key ID: C638974D64792D67
2 changed files with 28 additions and 10 deletions

View File

@ -63,7 +63,7 @@ func (c *cmdLXC) run(cmd *cobra.Command, args []string) error {
err := generator.RunLXC(c.global.flagCacheDir, c.global.sourceDir, img,
file)
if err != nil {
continue
return err
}
}

View File

@ -4,7 +4,6 @@ import (
"os"
p "path"
"path/filepath"
"strings"
lxd "github.com/lxc/lxd/shared"
@ -37,30 +36,49 @@ func Get(generator string) Generator {
return nil
}
var storedFiles = map[string]string{}
// StoreFile caches a file which can be restored with the RestoreFiles function.
func StoreFile(cacheDir, sourceDir, path string) error {
// Record newly created files
if !lxd.PathExists(filepath.Join(sourceDir, path)) {
storedFiles[filepath.Join(sourceDir, path)] = ""
return nil
}
// create temporary directory containing old files
err := os.MkdirAll(filepath.Join(cacheDir, "tmp", p.Dir(path)), 0755)
if err != nil {
return err
}
storedFiles[filepath.Join(sourceDir, path)] = filepath.Join(cacheDir, "tmp", path)
return lxd.FileCopy(filepath.Join(sourceDir, path),
filepath.Join(cacheDir, "tmp", path))
}
// RestoreFiles restores original files which were cached by StoreFile.
func RestoreFiles(cacheDir, sourceDir string) error {
f := func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
// We don't care about directories. They should be present so there's
// no need to create them.
return nil
for origPath, tmpPath := range storedFiles {
// Deal with newly created files
if tmpPath == "" {
err := os.Remove(origPath)
if err != nil {
return err
}
continue
}
return lxd.FileCopy(path, filepath.Join(sourceDir,
strings.TrimPrefix(path, filepath.Join(cacheDir, "tmp"))))
err := lxd.FileCopy(tmpPath, origPath)
if err != nil {
return err
}
}
return filepath.Walk(filepath.Join(cacheDir, "tmp"), f)
// Reset the list of stored files
storedFiles = map[string]string{}
return nil
}