Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(binaries): binary detection fixes & improvements #814

Merged
merged 49 commits into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
bc40d37
chore(prisma): upgrade to prisma 4.9.0
steebchen Feb 7, 2023
491e2a7
chore(prisma): upgrade to prisma 4.10.0
steebchen Feb 8, 2023
6146228
fix(bindata): always use platform constraint
steebchen Feb 8, 2023
195cccb
test(binaries): remove native option
steebchen Feb 8, 2023
7324b67
fix(binaries): remove introspection & fmt
steebchen Feb 8, 2023
2af555f
remove binary targets
steebchen Feb 8, 2023
374b59a
reformat build tags
steebchen Feb 8, 2023
646e225
fix(generator): remove introspection engine field
steebchen Feb 8, 2023
9e399f5
fix(generator): ignore native when darwin or windows
steebchen Feb 8, 2023
3f13a38
test(binaries): use openssl 3
steebchen Feb 8, 2023
5cb41b1
ci(all): use ubuntu-22.04
steebchen Feb 8, 2023
20c9a06
fix(unpack): use unpacked QE
steebchen Feb 8, 2023
aa3a76c
fix binaries schema
steebchen Feb 8, 2023
f09c4fb
chore(integration): run just on ubuntu-22.04
steebchen Feb 8, 2023
93aa27f
test(binaries): just check for requested binary
steebchen Feb 8, 2023
c22164c
Revert "chore(integration): run just on ubuntu-22.04"
steebchen Feb 8, 2023
f785f4a
Revert "ci(all): use ubuntu-22.04"
steebchen Feb 8, 2023
9e65348
fix(run): add more debug logs
steebchen Feb 9, 2023
9704096
fix(run): adapt for musl
steebchen Feb 9, 2023
3d709bf
fix(run): check for PRISMA_CLI_BINARY_TARGETS
steebchen Feb 9, 2023
352ab5d
fix(binaries): revert to 4.7.1 (#811)
steebchen Feb 21, 2023
e6be1cd
Merge branch 'main' into chore/prisma-upgrade
steebchen May 6, 2023
19328fa
Merge branch 'main' into chore/prisma-upgrade
steebchen Jun 12, 2023
e8df906
Update integration-test.yml
steebchen Jun 12, 2023
7255190
Update integration.dockerfile
steebchen Jun 12, 2023
da11fd3
Merge branch 'main' into chore/prisma-upgrade
steebchen Jun 12, 2023
1b25ab4
fix(binaries): adapt arch specific unpacking
steebchen Jun 18, 2023
80ca30b
Merge remote-tracking branch 'origin/chore/prisma-upgrade'
steebchen Jun 18, 2023
932139d
wip
steebchen Jun 18, 2023
6bf6148
fix platform build
steebchen Jun 18, 2023
6371fd6
debug
steebchen Jun 18, 2023
3835e01
add static binary name
steebchen Jun 18, 2023
9534c9d
use static
steebchen Jun 18, 2023
feb0334
fail fast
steebchen Jun 18, 2023
5205e38
logs
steebchen Jun 18, 2023
01a60e6
use static
steebchen Jun 18, 2023
b5c4be8
WIP no idea what i am doing at this point
steebchen Jun 18, 2023
eb82aeb
fix engine dir
steebchen Jun 18, 2023
3e4c4cf
consider cache path in path detection
steebchen Jun 18, 2023
c35eed1
Merge remote-tracking branch 'origin/main' into chore/prisma-upgrade
steebchen Jun 21, 2023
841c143
Update test.yml
steebchen Jun 21, 2023
adec5d7
minor fixes
steebchen Jun 21, 2023
a68a9ce
fix alpine
steebchen Jun 21, 2023
9004991
add integration tests for alpine
steebchen Jun 21, 2023
fd4d5ba
fix alpine checks
steebchen Jun 21, 2023
21f661c
fix integration tests
steebchen Jun 21, 2023
9cc3138
fix integration tests
steebchen Jun 21, 2023
f7b0b6a
bump prisma to v4.16.0
steebchen Jun 21, 2023
b97d9e9
set arch on linux
steebchen Jun 21, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ schema.graphql
**/*_gen.go
**/prisma-*
**/query-engine-*

Dockerfile
*.dockerfile
45 changes: 43 additions & 2 deletions .github/workflows/integration-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,47 @@ jobs:
restore-keys: ${{ runner.os }}-go-
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}

- name: integration
- name: test
if: steps.changes.outputs.go == 'true'
run: docker build . -f test/integration/integration.dockerfile -t integration && docker run integration
run: docker build . --build-arg IMAGE="golang:1" -f test/integration/integration.dockerfile -t integration && docker run integration

integration-alpine:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- uses: dorny/paths-filter@v2
id: changes
with:
filters: |
go:
- '.github/workflows/**/*.yml'
- '**/*.go'
- '**/*.gotpl'
- '**/*.mod'
- '**/*.sum'
- '**/*.work'

# fix for GitHub actions MacOS
- name: Setup docker
if: runner.os == 'macos' && steps.changes.outputs.go == 'true'
run: |
brew install docker
colima start

# For testcontainers to find the Colima socket
# https://github.com/abiosoft/colima/blob/main/docs/FAQ.md#cannot-connect-to-the-docker-daemon-at-unixvarrundockersock-is-the-docker-daemon-running
sudo ln -sf $HOME/.colima/default/docker.sock /var/run/docker.sock

- uses: actions/cache@v3
with:
path: |
~/go/pkg/mod
~/.cache
restore-keys: ${{ runner.os }}-go-
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}

- name: test on alpine
if: steps.changes.outputs.go == 'true'
run: docker build . --build-arg IMAGE="golang:1-alpine" -f test/integration/integration.dockerfile -t integration && docker run integration
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ jobs:

- name: test
if: steps.changes.outputs.go == 'true'
run: go test ./... -v
run: go test ./... -v -failfast
61 changes: 13 additions & 48 deletions binaries/binaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ import (
"os"
"path"
"path/filepath"
"time"

"github.com/steebchen/prisma-client-go/binaries/platform"
"github.com/steebchen/prisma-client-go/logger"
)

// PrismaVersion is a hardcoded version of the Prisma CLI.
const PrismaVersion = "4.15.0"
const PrismaVersion = "4.16.0"

// EngineVersion is a hardcoded version of the Prisma Engine.
// The versions can be found under https://github.com/prisma/prisma-engines/commits/main
const EngineVersion = "70a9adfd11f836696eca398396544fe07a8d8edb"
const EngineVersion = "b20ead4d3ab9e78ac112966e242ded703f4a052c"

// PrismaURL points to an S3 bucket URL where the CLI binaries are stored.
var PrismaURL = "https://packaged-cli.prisma.sh/%s-%s-%s-%s.gz"
Expand Down Expand Up @@ -86,26 +85,22 @@ func GlobalCacheDir() string {
return path.Join(cache, baseDirName, "cli", PrismaVersion)
}

func FetchEngine(toDir string, engineName string, binaryPlatformName string) error {
logger.Debug.Printf("checking %s...", engineName)
func FetchEngine(dir string, engineName string, binaryName string) error {
logger.Debug.Printf("checking %s %s...", engineName, binaryName)

to := platform.CheckForExtension(binaryPlatformName, path.Join(toDir, EngineVersion, fmt.Sprintf("prisma-%s-%s", engineName, binaryPlatformName)))

binaryPlatformRemoteName := binaryPlatformName
if binaryPlatformRemoteName == "linux" {
binaryPlatformRemoteName = "linux-static-x64"
}
url := platform.CheckForExtension(binaryPlatformName, fmt.Sprintf(EngineURL, EngineVersion, binaryPlatformRemoteName, engineName))

logger.Debug.Printf("download url %s", url)
to := GetEnginePath(dir, engineName, binaryName)

if _, err := os.Stat(to); !os.IsNotExist(err) {
logger.Debug.Printf("%s is cached", to)
logger.Debug.Printf("%s is cached at %s", engineName, to)
return nil
}

url := platform.CheckForExtension(binaryName, fmt.Sprintf(EngineURL, EngineVersion, binaryName, engineName))

logger.Debug.Printf("%s is missing, downloading...", engineName)

logger.Debug.Printf("downloading %s from %s to %s", engineName, url, to)

if err := download(url, to); err != nil {
return fmt.Errorf("could not download %s to %s: %w", url, to, err)
}
Expand All @@ -130,7 +125,7 @@ func FetchNative(toDir string) error {
}

for _, e := range Engines {
if _, err := DownloadEngine(e.Name, toDir); err != nil {
if err := FetchEngine(toDir, e.Name, platform.BinaryPlatformNameStatic()); err != nil {
return fmt.Errorf("could not download engines: %w", err)
}
}
Expand Down Expand Up @@ -160,38 +155,8 @@ func DownloadCLI(toDir string) error {
return nil
}

func GetEnginePath(dir, engine, binaryName string) string {
return platform.CheckForExtension(binaryName, path.Join(dir, EngineVersion, fmt.Sprintf("prisma-%s-%s", engine, binaryName)))
}

func DownloadEngine(name string, toDir string) (file string, err error) {
binaryName := platform.BinaryPlatformName()

logger.Debug.Printf("checking %s...", name)

to := platform.CheckForExtension(binaryName, path.Join(toDir, EngineVersion, fmt.Sprintf("prisma-%s-%s", name, binaryName)))

url := platform.CheckForExtension(binaryName, fmt.Sprintf(EngineURL, EngineVersion, binaryName, name))

logger.Debug.Printf("download url %s", url)

if _, err := os.Stat(to); !os.IsNotExist(err) {
logger.Debug.Printf("%s is cached", to)
return to, nil
}

logger.Debug.Printf("%s is missing, downloading...", name)

startDownload := time.Now()
if err := download(url, to); err != nil {
return "", fmt.Errorf("could not download %s to %s: %w", url, to, err)
}

logger.Debug.Printf("%s engine download took %s", name, time.Since(startDownload))

logger.Debug.Printf("%s done", name)

return to, nil
func GetEnginePath(dir, engineName, binaryName string) string {
return platform.CheckForExtension(binaryName, path.Join(dir, EngineVersion, fmt.Sprintf("prisma-%s-%s", engineName, binaryName)))
}

func download(url string, to string) error {
Expand Down
24 changes: 4 additions & 20 deletions binaries/bindata/bindata.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,11 @@ func WriteFile(name, pkg, platform, from, to string) error {
}

func writeHeader(w io.Writer, pkg, name, platform string) error {
var constraints string
if platform == "linux" {
if name == "linux" {
// TODO dynamically construct these with allTargets in run.go
// TODO only include these for engines, not for the CLI
constraints = `// +build !debian_openssl_1_0_x
// +build !debian_openssl_1_1_x
// +build !rhel_openssl_1_0_x
// +build !rhel_openssl_1_1_x`
constraints += "\n"
} else {
constraints = "// +build linux\n"
}
}

_, err := fmt.Fprintf(w, `// Code generated by Prisma Client Go. DO NOT EDIT.
//go:build !codeanalysis && !prisma_ignore && %s
// +build !codeanalysis,!prisma_ignore,%s

//nolint
// +build !codeanalysis
// +build %s
// +build !prisma_ignore
%s
package %s

import (
Expand All @@ -62,7 +46,7 @@ import (
func init() {
unpack.Unpack(data, "%s", "%s")
}
`, name, constraints, pkg, name, binaries.EngineVersion)
`, platform, platform, pkg, name, binaries.EngineVersion)
return err
}

Expand Down
29 changes: 22 additions & 7 deletions binaries/platform/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (

var binaryNameWithSSLCache string

// BinaryPlatformName returns the name of the prisma binary which should be used,
// for example "darwin" or "linux-openssl-1.1.x"
func BinaryPlatformName() string {
// BinaryPlatformNameDynamic returns the name of the prisma binary which should be used,
// for example "darwin" or "linux-openssl-1.1.x". This can include dynamically linked binaries.
func BinaryPlatformNameDynamic() string {
if binaryNameWithSSLCache != "" {
return binaryNameWithSSLCache
}
Expand All @@ -34,10 +34,6 @@ func BinaryPlatformName() string {

distro := getLinuxDistro()

if distro == "alpine" {
return fmt.Sprintf("linux-static-%s", arch)
}

ssl := getOpenSSL()

name := fmt.Sprintf("%s-openssl-%s", distro, ssl)
Expand All @@ -47,6 +43,25 @@ func BinaryPlatformName() string {
return name
}

// BinaryPlatformNameStatic returns the name of the prisma binary which should be used,
// for example "darwin" or "linux-static-x64". This only includes statically linked binaries.
func BinaryPlatformNameStatic() string {
platform := Name()
arch := Arch()

// other supported platforms are darwin and windows
if platform != "linux" {
// special case for darwin arm64
if platform == "darwin" && arch == "arm64" {
return "darwin-arm64"
}
// otherwise, return `darwin` or `windows`
return platform
}

return fmt.Sprintf("linux-static-%s", arch)
}

// Name returns the platform name
func Name() string {
return runtime.GOOS
Expand Down
11 changes: 10 additions & 1 deletion binaries/unpack/unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path"
"strings"
"time"

"github.com/steebchen/prisma-client-go/binaries"
Expand All @@ -13,10 +14,14 @@ import (

// TODO check checksum after expanding file

const FileEnv = "PRISMA_INTERNAL_QUERY_ENGINE_PATH"

// noinspection GoUnusedExportedFunction
func Unpack(data []byte, name string, version string) {
start := time.Now()

name = strings.ReplaceAll(name, "_", "-")

filename := fmt.Sprintf("prisma-query-engine-%s", name)

// TODO check if dev env/dev binary in ~/.prisma
Expand All @@ -31,7 +36,7 @@ func Unpack(data []byte, name string, version string) {
}

if _, err := os.Stat(file); err == nil {
logger.Debug.Printf("query engine exists, not unpacking. %s", time.Since(start))
logger.Debug.Printf("query engine exists, not unpacking. %s. at %s", time.Since(start), file)
return
}

Expand All @@ -53,4 +58,8 @@ func Unpack(data []byte, name string, version string) {
}

logger.Debug.Printf("unpacked at %s in %s", file, time.Since(start))

if err := os.Setenv(FileEnv, file); err != nil {
panic(err)
}
}
2 changes: 1 addition & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Run(arguments []string, output bool) error {
logger.Debug.Printf("running %s %+v", path.Join(dir, prisma), arguments)

cmd := exec.Command(path.Join(dir, prisma), arguments...) //nolint:gosec
binaryName := platform.CheckForExtension(platform.Name(), platform.BinaryPlatformName())
binaryName := platform.CheckForExtension(platform.Name(), platform.BinaryPlatformNameStatic())

cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "PRISMA_HIDE_UPDATE_MESSAGE=true")
Expand Down
Loading