Updated build to be able to build from OCI repositories and some housecleaning

This commit is contained in:
Gregory Kurtzer
2020-11-07 21:34:07 -08:00
parent badd10b838
commit 4e0a5bea48
5 changed files with 135 additions and 40 deletions

36
cmd/wwbuild/vnfs-local.go Normal file
View File

@@ -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)
}
}

66
cmd/wwbuild/vnfs-oci.go Normal file
View File

@@ -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
}

14
cmd/wwbuild/vnfs.go Normal file
View File

@@ -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
}

View File

@@ -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()
}
}
}