diff --git a/cmd/wwbuild/vnfs-local.go b/cmd/wwbuild/vnfs-local.go new file mode 100644 index 00000000..4a0fdb0c --- /dev/null +++ b/cmd/wwbuild/vnfs-local.go @@ -0,0 +1,36 @@ +package main + +import ( + "fmt" + "os" + "path" + "sync" +) + +func vnfsLocalBuild(vnfsPath string, wg *sync.WaitGroup) { + defer wg.Done() + + if _, err := os.Stat(vnfsPath); err == nil { + // TODO: Build VNFS to temporary file and move to real location when complete atomically + // TODO: Check time stamps of sourcedir and build file to see if we need to rebuild or skip + + vnfsDestination := fmt.Sprintf("%s/provision/vnfs/%s.img.gz", LocalStateDir, path.Base(vnfsPath)) + + fmt.Printf("Building local Container: %s\n", vnfsPath) + + err := os.MkdirAll(path.Dir(vnfsDestination), 0755) + if err != nil { + fmt.Printf("ERROR: %s\n", err) + return + } + + err = buildVnfs(vnfsPath, vnfsDestination) + if err != nil { + fmt.Printf("ERROR: %s\n", err) + os.Exit(1) + } + + } else { + fmt.Printf("SKIPPING VNFS: (bad path) %s\n", vnfsPath) + } +} diff --git a/cmd/wwbuild/vnfs-oci.go b/cmd/wwbuild/vnfs-oci.go new file mode 100644 index 00000000..4c35793f --- /dev/null +++ b/cmd/wwbuild/vnfs-oci.go @@ -0,0 +1,66 @@ +package main + +import ( + "context" + "fmt" + "github.com/hpcng/warewulf/internal/pkg/oci" + "log" + "os" + "path" + "sync" +) + +func vnfsOciBuild(OciPath string, wg *sync.WaitGroup) { + vnfsDestination := fmt.Sprintf("%s/provision/vnfs/%s.img.gz", LocalStateDir, path.Base(OciPath)) + defer wg.Done() + + log.Printf("Building OCI Container: %s\n", OciPath) + c, err := oci.NewCache(oci.OptSetCachePath("/var/warewulf/oci")) + if err != nil { + fmt.Printf("ERROR: %s\n", err) + os.Exit(1) + } + sourcePath, err := c.Pull(context.Background(), OciPath, nil) + if err != nil { + fmt.Printf("ERROR: %s\n", err) + os.Exit(1) + } + + ociDestination := fmt.Sprintf("%s/oci/vnfs/%s", LocalStateDir, path.Base(sourcePath)) + + name, err := os.Readlink(vnfsDestination) + if err == nil { + if name == ociDestination { + log.Printf("Container already built, no changes from upstream\n") + return + } + } + + err = os.MkdirAll(path.Dir(ociDestination), 0755) + if err != nil { + fmt.Printf("ERROR: %s\n", err) + return + } + err = os.MkdirAll(path.Dir(vnfsDestination), 0755) + if err != nil { + fmt.Printf("ERROR: %s\n", err) + return + } + + err = buildVnfs(sourcePath, ociDestination) + if err != nil { + fmt.Printf("ERROR: %s\n", err) + os.Exit(1) + } + + err = os.Symlink(ociDestination, vnfsDestination+"-link") + if err != nil { + fmt.Printf("ERROR: %s\n", err) + os.Exit(1) + } + err = os.Rename(vnfsDestination+"-link", vnfsDestination) + + log.Printf("Completed building VNFS: %s\n", path.Base(OciPath)) + + return +} diff --git a/cmd/wwbuild/vnfs.go b/cmd/wwbuild/vnfs.go new file mode 100644 index 00000000..d113b0d2 --- /dev/null +++ b/cmd/wwbuild/vnfs.go @@ -0,0 +1,14 @@ +package main + +import ( + "fmt" + "os/exec" +) + +func buildVnfs(source string, dest string) error { + cmd := fmt.Sprintf("cd %s; find . | cpio --quiet -o -H newc | gzip -c > \"%s\"", source, dest) + + err := exec.Command("/bin/sh", "-c", cmd).Run() + + return err +} diff --git a/cmd/wwbuild/wwbuild.go b/cmd/wwbuild/wwbuild.go index dc336d02..533f1db8 100644 --- a/cmd/wwbuild/wwbuild.go +++ b/cmd/wwbuild/wwbuild.go @@ -1,45 +1,18 @@ package main import ( - "fmt" - "github.com/hpcng/warewulf/internal/pkg/assets" - "os" - "os/exec" - "path" - "strings" - "sync" + "fmt" + "github.com/hpcng/warewulf/internal/pkg/assets" + "os" + "os/exec" + "path" + "strings" + "sync" "time" ) const LocalStateDir = "/var/warewulf" -func vnfsBuild(vnfsPath string, wg *sync.WaitGroup) { - defer wg.Done() - if _, err := os.Stat(vnfsPath); err == nil { - // TODO: Build VNFS to temporary file and move to real location when complete atomically - // TODO: Check time stamps of sourcedir and build file to see if we need to rebuild or skip - vnfsDestination := fmt.Sprintf("%s/provision/vnfs/%s.img.gz", LocalStateDir, path.Base(vnfsPath)) - - err := os.MkdirAll(path.Dir(vnfsDestination), 0755) - if err != nil { - fmt.Printf("ERROR: %s\n", err) - return - } - - fmt.Printf("BUILDING VNFS: %s\n", vnfsPath) - - cmd := fmt.Sprintf("cd %s; find . | cpio --quiet -o -H newc | gzip -c > \"%s\"", vnfsPath, vnfsDestination) - err = exec.Command("/bin/sh", "-c", cmd).Run() - if err != nil { - fmt.Printf("ERROR: %s\n", err) - os.Exit(1) - } - - } else { - fmt.Printf("SKIPPING VNFS: (bad path) %s\n", vnfsPath) - } -} - func main() { if len(os.Args) < 2 { @@ -68,10 +41,16 @@ func main() { set[node.Vnfs] = true } } + for entry := range set { wg.Add(1) - go vnfsBuild(entry, &wg) + if strings.HasPrefix(entry, "/") { + vnfsLocalBuild(entry, &wg) + } else { + vnfsOciBuild(entry, &wg) + } } + time.Sleep(1000 * time.Millisecond) fmt.Printf("Waiting for build(s) to complete...\n") wg.Wait() @@ -161,9 +140,9 @@ func main() { wg.Add(2) overlayRuntime(node, replace, &wg) - overlaySystem(node, replace, &wg) + overlaySystem(node, replace, &wg) } - wg.Wait() + wg.Wait() - } + } } diff --git a/internal/pkg/oci/cache.go b/internal/pkg/oci/cache.go index 60810454..6aaa0828 100644 --- a/internal/pkg/oci/cache.go +++ b/internal/pkg/oci/cache.go @@ -53,7 +53,7 @@ func (c *Cache) checkEntry(id string) (string, error) { func (c *Cache) createEntry(id string) (string, error) { path := filepath.Join(c.rootfsDir(), id) - if err := os.MkdirAll(path, 700); err != nil { + if err := os.MkdirAll(path, 755); err != nil { return "", err } @@ -71,7 +71,7 @@ func NewCache(opts ...CacheOpt) (*Cache, error) { } } - if err := os.MkdirAll(s.path, 0700); err != nil { + if err := os.MkdirAll(s.path, 0755); err != nil { return nil, err }