Skip to content

Commit

Permalink
Symlink app archive to apps instead of expanding
Browse files Browse the repository at this point in the history
  • Loading branch information
hibell authored and Hank W Ibell committed Dec 9, 2022
1 parent d3ac57a commit 1dffc41
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 24 deletions.
30 changes: 12 additions & 18 deletions liberty/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"
"github.com/paketo-buildpacks/liberty/internal/core"
"github.com/paketo-buildpacks/liberty/internal/server"
"github.com/paketo-buildpacks/libpak/crush"
"github.com/paketo-buildpacks/libpak/sherpa"
"io/fs"
"os"
Expand Down Expand Up @@ -272,29 +271,24 @@ func (b Base) contributeApp(layer libcnb.Layer, config server.Config) error {

serverPath := filepath.Join(layer.Path, "wlp", "usr", "servers", b.ServerName)

linkPath := filepath.Join(serverPath, "apps", "app")
if err := os.Remove(linkPath); err != nil && !errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("unable to remove app\n%w", err)
}

// Expand app if needed
isDir, err := sherpa.DirExists(appPath)
if err != nil {
return fmt.Errorf("unable to check if app path is a directory\n%w", err)
}

var linkPath string
if isDir {
if err := os.Symlink(appPath, linkPath); err != nil {
return fmt.Errorf("unable to symlink application to '%s'\n%w", linkPath, err)
}
linkPath = filepath.Join(serverPath, "apps", "app")
} else {
compiledArtifact, err := os.Open(appPath)
if err != nil {
return fmt.Errorf("unable to open compiled artifact\n%w", err)
}
err = crush.Extract(compiledArtifact, linkPath, 0)
if err != nil {
return fmt.Errorf("unable to extract compiled artifact\n%w", err)
}
linkPath = filepath.Join(serverPath, "apps", filepath.Base(appPath))
}

if err := os.Remove(linkPath); err != nil && !errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("unable to remove app\n%w", err)
}

if err := os.Symlink(appPath, linkPath); err != nil {
return fmt.Errorf("unable to symlink application to '%s'\n%w", linkPath, err)
}

if config.Application.Name == "app" {
Expand Down
8 changes: 2 additions & 6 deletions liberty/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package liberty_test

import (
"github.com/paketo-buildpacks/libpak/sherpa"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -187,9 +186,7 @@ func testBase(t *testing.T, _ spec.G, it spec.S) {

it("contributes server.xml and compiled artifact", func() {
// Set up app and server config
file, err := os.Open(filepath.Join("testdata", "test.war"))
Expect(err).NotTo(HaveOccurred())
Expect(sherpa.CopyFile(file, filepath.Join(ctx.Application.Path, "test.war"))).To(Succeed())
Expect(os.WriteFile(filepath.Join(ctx.Application.Path, "test.war"), []byte{}, 0644)).To(Succeed())
Expect(os.WriteFile(filepath.Join(ctx.Application.Path, "server.xml"), []byte("<server/>"), 0644)).To(Succeed())
Expect(os.WriteFile(filepath.Join(ctx.Application.Path, "server.env"), []byte("TEST_ENV=foo"), 0644)).To(Succeed())
Expect(os.WriteFile(filepath.Join(ctx.Application.Path, "bootstrap.properties"), []byte("test.property=foo"), 0644)).To(Succeed())
Expand All @@ -210,8 +207,7 @@ func testBase(t *testing.T, _ spec.G, it spec.S) {
layer, err = base.Contribute(layer)
Expect(err).ToNot(HaveOccurred())

Expect(filepath.Join(layer.Path, "wlp", "usr", "servers", "defaultServer", "apps", "app")).To(BeADirectory())
Expect(filepath.Join(layer.Path, "wlp", "usr", "servers", "defaultServer", "apps", "app", "index.html")).To(BeAnExistingFile())
Expect(filepath.Join(layer.Path, "wlp", "usr", "servers", "defaultServer", "apps", "test.war")).To(BeAnExistingFile())

serverDir := filepath.Join(layer.Path, "wlp", "usr", "servers", "defaultServer")
for _, file := range []string{
Expand Down
Binary file removed liberty/testdata/test.war
Binary file not shown.

0 comments on commit 1dffc41

Please sign in to comment.