Skip to content

Commit

Permalink
replace packr with embed for the bosh assets
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonskie authored and rkoster committed Mar 14, 2023
1 parent e95db43 commit 1333c80
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 38 deletions.
1 change: 1 addition & 0 deletions bosh/assets/bosh-deployment/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
my-license
1 change: 1 addition & 0 deletions bosh/assets/bosh-deployment/vsphere/cpi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vsphere-cpi
1 change: 1 addition & 0 deletions bosh/assets/jumpbox-deployment/aws/cpi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aws-cpi
1 change: 1 addition & 0 deletions bosh/assets/jumpbox-deployment/azure/cpi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
azure-cpi
1 change: 1 addition & 0 deletions bosh/assets/jumpbox-deployment/no-external-ip.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
no-ip
2 changes: 0 additions & 2 deletions bosh/deployments/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
## Why?

This directory is now here to help with the usage of [packr2](https://godoc.org/github.com/gobuffalo/packr/v2)

When building the actual bbl binary, this is where your jumpbox / bosh
deployment repos should end up so they can be included in the final build.

Expand Down
68 changes: 52 additions & 16 deletions bosh/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package bosh

import (
"bytes"
"embed"
"errors"
"fmt"
"io"
"io/fs"
"os"
"os/exec"
"path/filepath"
Expand All @@ -13,7 +15,6 @@ import (

"github.com/cloudfoundry/bosh-bootloader/fileio"
"github.com/cloudfoundry/bosh-bootloader/storage"
"github.com/gobuffalo/packr/v2"
)

type executorFs interface {
Expand All @@ -23,9 +24,10 @@ type executorFs interface {
}

type Executor struct {
CLI cli
FS executorFs
Box *packr.Box
CLI cli
FS executorFs
EmbedData embed.FS
EmbedDataPrefix string
}

type DirInput struct {
Expand All @@ -50,30 +52,63 @@ const (
boshDeploymentRepo = "bosh-deployment"
)

//go:embed deployments/jumpbox-deployment
//go:embed deployments/bosh-deployment
var content embed.FS

func NewExecutor(cmd cli, fs executorFs) Executor {
return Executor{
CLI: cmd,
FS: fs,
Box: packr.New("setup-files", "./deployments"),
CLI: cmd,
FS: fs,
EmbedData: content,
EmbedDataPrefix: "deployments/",
}
}

func extractNestedFiles(fs embed.FS, source_entry fs.DirEntry, fileList []setupFile, path string, trimPrefix string, destPath string) []setupFile {
subDir, err := fs.ReadDir(path)
if err != nil {
panic(err)
}
for _, entry := range subDir {
if entry.IsDir() {
fileList = extractNestedFiles(fs, entry, fileList, filepath.Join(path, entry.Name()), trimPrefix, destPath)
} else {
contents, err := fs.ReadFile(filepath.Join(path, entry.Name()))
if err != nil {
panic(err)
}
fileList = append(fileList, setupFile{
source: entry.Name(),
dest: filepath.Join(destPath, strings.TrimPrefix(path, trimPrefix), entry.Name()),
contents: contents,
})
}
}
return fileList
}
func (e Executor) getSetupFiles(sourcePath, destPath string) []setupFile {
files := []setupFile{}
fullPath := filepath.Join(e.EmbedDataPrefix, sourcePath)

assetNames := e.Box.List()

assetNames, err := e.EmbedData.ReadDir(fullPath)
if err != nil {
panic(err)
}
for _, asset := range assetNames {
if strings.Contains(asset, sourcePath) {
fileContents, err := e.Box.Find(asset)

if asset.IsDir() {
prefix := filepath.Join(e.EmbedDataPrefix, sourcePath)
files = extractNestedFiles(e.EmbedData, asset, files, filepath.Join(fullPath, asset.Name()), prefix, destPath)
} else {
contents, err := e.EmbedData.ReadFile(filepath.Join(fullPath, asset.Name()))
if err != nil {

panic(err) // this panic is intentional as it was exactly the same way MustAsset worked previously in go-bindata
}

files = append(files, setupFile{
source: strings.TrimPrefix(asset, sourcePath),
dest: filepath.Join(destPath, strings.TrimPrefix(asset, sourcePath)),
contents: fileContents,
source: asset.Name(),
dest: filepath.Join(destPath, strings.TrimPrefix(asset.Name(), e.EmbedDataPrefix)),
contents: contents,
})
}
}
Expand All @@ -85,6 +120,7 @@ func (e Executor) PlanJumpbox(input DirInput, deploymentDir, iaas string) error
setupFiles := e.getSetupFiles(jumpboxDeploymentRepo, deploymentDir)

for _, f := range setupFiles {
// ignore error if dir already exists
os.MkdirAll(filepath.Dir(f.dest), os.ModePerm)
err := e.FS.WriteFile(f.dest, f.contents, storage.StateMode)
if err != nil {
Expand Down
39 changes: 19 additions & 20 deletions bosh/executor_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bosh_test

import (
"embed"
"errors"
"fmt"
"io"
Expand All @@ -11,13 +12,15 @@ import (
"github.com/cloudfoundry/bosh-bootloader/fakes"
"github.com/cloudfoundry/bosh-bootloader/fileio"
"github.com/cloudfoundry/bosh-bootloader/storage"
"github.com/gobuffalo/packr/v2"
"github.com/spf13/afero"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

//go:embed assets/*
var content embed.FS

var _ = Describe("Executor", func() {
var (
fs *afero.Afero
Expand Down Expand Up @@ -63,14 +66,12 @@ var _ = Describe("Executor", func() {
StateDir: stateDir,
}

box := packr.New("some-name", "./deployments")
box.AddString("jumpbox-deployment/no-external-ip.yml", "no-ip")
box.AddString("jumpbox-deployment/aws/cpi.yml", "vsphere-cpi")
box.AddString("jumpbox-deployment/azure/cpi.yml", "azure-cpi")
box.AddString("bosh-deployment/vsphere/cpi.yml", "vsphere-cpi")
box.AddString("bosh-deployment/LICENSE", "my-license")

executor = bosh.Executor{CLI: cli, FS: fs, Box: box}
executor = bosh.Executor{
CLI: cli,
FS: fs,
EmbedData: content,
EmbedDataPrefix: "assets/",
}
})

Describe("PlanJumpbox", func() {
Expand All @@ -89,7 +90,7 @@ var _ = Describe("Executor", func() {

contents, err = fs.ReadFile(nestedPath)
Expect(err).NotTo(HaveOccurred())
Expect(string(contents)).To(Equal("vsphere-cpi"))
Expect(string(contents)).To(Equal("aws-cpi"))
})

By("writing create-env and delete-env scripts", func() {
Expand Down Expand Up @@ -468,12 +469,11 @@ var _ = Describe("Executor", func() {
stateDir, err = fs.TempDir("", "")
Expect(err).NotTo(HaveOccurred())

box := packr.New("another-test", "./deployments")

executor = bosh.Executor{
CLI: cli,
FS: fs,
Box: box,
CLI: cli,
FS: fs,
EmbedData: content,
EmbedDataPrefix: "assets/",
}

dirInput = bosh.DirInput{
Expand Down Expand Up @@ -674,12 +674,11 @@ var _ = Describe("Executor", func() {
stateDir, err = fs.TempDir("", "")
Expect(err).NotTo(HaveOccurred())

box := packr.New("best-test-box", "./deployments")

executor = bosh.Executor{
CLI: cli,
FS: fs,
Box: box,
CLI: cli,
FS: fs,
EmbedData: content,
EmbedDataPrefix: "assets/",
}

dirInput = bosh.DirInput{
Expand Down

0 comments on commit 1333c80

Please sign in to comment.