release/dist/qnap: use tmp file directory for qpkg building

This change allows for the release/dist/qnap package to be used
outside of the tailscale repo (notably, will be used from corp),
by using an embedded file system for build files which gets
temporarily written to a new folder during qnap build runs.

Without this change, when used from corp, the release/dist/qnap
folder will fail to be found within the corp repo, causing
various steps of the build to fail.

The file renames in this change are to combine the build files
into a /files folder, separated into /scripts and /Tailscale.

Updates tailscale/tailscale-qpkg#135

Signed-off-by: Sonia Appasamy <sonia@tailscale.com>
pull/11862/head
Sonia Appasamy 7 months ago committed by Sonia Appasamy
parent c8e912896e
commit 0cce456ee5

Before

Width:  |  Height:  |  Size: 600 B

After

Width:  |  Height:  |  Size: 600 B

Before

Width:  |  Height:  |  Size: 741 B

After

Width:  |  Height:  |  Size: 741 B

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

@ -7,8 +7,9 @@
package qnap package qnap
import ( import (
"errors" "embed"
"fmt" "fmt"
"io/fs"
"log" "log"
"os" "os"
"os/exec" "os/exec"
@ -39,13 +40,7 @@ func (t *target) Build(b *dist.Build) ([]string, error) {
return nil, fmt.Errorf("docker not found, cannot build: %w", err) return nil, fmt.Errorf("docker not found, cannot build: %w", err)
} }
if t.signer != nil { qnapBuilds := getQnapBuilds(b, t.signer)
if err := t.setUpSignatureFiles(b); err != nil {
return nil, err
}
}
qnapBuilds := getQnapBuilds(b)
inner, err := qnapBuilds.buildInnerPackage(b, t.goenv) inner, err := qnapBuilds.buildInnerPackage(b, t.goenv)
if err != nil { if err != nil {
return nil, err return nil, err
@ -77,8 +72,8 @@ func (t *target) buildQPKG(b *dist.Build, qnapBuilds *qnapBuilds, inner *innerPk
"-v", fmt.Sprintf("%s:/tailscale", inner.tailscalePath), "-v", fmt.Sprintf("%s:/tailscale", inner.tailscalePath),
"-v", fmt.Sprintf("%s:/tailscaled", inner.tailscaledPath), "-v", fmt.Sprintf("%s:/tailscaled", inner.tailscaledPath),
// Tailscale folder has QNAP package setup files needed for building. // Tailscale folder has QNAP package setup files needed for building.
"-v", fmt.Sprintf("%s:/Tailscale", filepath.Join(b.Repo, "release/dist/qnap/Tailscale")), "-v", fmt.Sprintf("%s:/Tailscale", filepath.Join(qnapBuilds.tmpDir, "files/Tailscale")),
"-v", fmt.Sprintf("%s:/build-qpkg.sh", filepath.Join(b.Repo, "release/dist/qnap/build-qpkg.sh")), "-v", fmt.Sprintf("%s:/build-qpkg.sh", filepath.Join(qnapBuilds.tmpDir, "files/scripts/build-qpkg.sh")),
"-v", fmt.Sprintf("%s:/out", b.Out), "-v", fmt.Sprintf("%s:/out", b.Out),
"build.tailscale.io/qdk:latest", "build.tailscale.io/qdk:latest",
"/build-qpkg.sh", "/build-qpkg.sh",
@ -101,58 +96,112 @@ func (t *target) buildQPKG(b *dist.Build, qnapBuilds *qnapBuilds, inner *innerPk
return []string{filePath, filePath + ".md5"}, nil return []string{filePath, filePath + ".md5"}, nil
} }
func (t *target) setUpSignatureFiles(b *dist.Build) error { type qnapBuildsMemoizeKey struct{}
return b.Once(fmt.Sprintf("qnap-signature-%s-%s", t.signer.privateKeyPath, t.signer.certificatePath), func() error {
log.Print("Setting up qnap signature files") type innerPkg struct {
tailscalePath string
tailscaledPath string
}
// qnapBuilds holds extra build context shared by all qnap builds.
type qnapBuilds struct {
// innerPkgs contains per-goenv compiled binary paths.
// It is used to avoid repeated compilations for the same architecture.
innerPkgs dist.Memoize[*innerPkg]
dockerImageMu sync.Mutex
// tmpDir is a temp directory used for building qpkgs.
// It gets cleaned up when the dist.Build is closed.
tmpDir string
}
key, err := os.ReadFile(t.signer.privateKeyPath) // getQnapBuilds returns the qnapBuilds for b, creating one if needed.
func getQnapBuilds(b *dist.Build, signer *signer) *qnapBuilds {
return b.Extra(qnapBuildsMemoizeKey{}, func() any {
builds, err := newQNAPBuilds(b, signer)
if err != nil {
panic(fmt.Errorf("setUpTmpDir: %v", err))
}
return builds
}).(*qnapBuilds)
}
//go:embed all:files
var buildFiles embed.FS
// newQNAPBuilds creates a new qnapBuilds instance to hold context shared by
// all qnap targets, and sets up its local temp directory used for building.
//
// The qnapBuilds.tmpDir is filled with the contents of the buildFiles embedded
// FS for building.
//
// We do this to allow for this tailscale.com/release/dist/qnap package to be
// used from both the corp and OSS repos. When built from OSS source directly,
// this is a superfluous extra step, but when imported as a go module to another
// repo (such as corp), we must do this to allow for the module's build files
// to be reachable and editable from docker.
//
// This runs only once per dist.Build instance, is shared by all qnap targets,
// and gets cleaned up upon close of the dist.Build.
//
// When a signer is provided, newQNAPBuilds also sets up the qpkg signature
// files in qbuild's expected location within m.tmpDir.
func newQNAPBuilds(b *dist.Build, signer *signer) (*qnapBuilds, error) {
m := new(qnapBuilds)
log.Print("Setting up qnap tmp build directory")
m.tmpDir = filepath.Join(b.Repo, "tmp-qnap-build")
b.AddOnCloseFunc(func() error {
return os.RemoveAll(m.tmpDir)
})
if err := fs.WalkDir(buildFiles, "files", func(path string, d fs.DirEntry, err error) error {
if err != nil { if err != nil {
return err return err
} }
cert, err := os.ReadFile(t.signer.certificatePath) outPath := filepath.Join(m.tmpDir, path)
if d.IsDir() {
return os.MkdirAll(outPath, 0700)
}
file, err := fs.ReadFile(buildFiles, path)
if err != nil { if err != nil {
return err return err
} }
perm := fs.FileMode(0600)
if filepath.Ext(path) == ".sh" {
perm = 0700
}
return os.WriteFile(outPath, file, perm)
}); err != nil {
return nil, err
}
if signer != nil {
log.Print("Setting up qnap signing files")
key, err := os.ReadFile(signer.privateKeyPath)
if err != nil {
return nil, err
}
cert, err := os.ReadFile(signer.certificatePath)
if err != nil {
return nil, err
}
// QNAP's qbuild command expects key and cert files to be in the root // QNAP's qbuild command expects key and cert files to be in the root
// of the project directory (in our case release/dist/qnap/Tailscale). // of the project directory (in our case release/dist/qnap/Tailscale).
// So here, we copy the key and cert over to the project folder for the // So here, we copy the key and cert over to the project folder for the
// duration of qnap package building and then delete them on close. // duration of qnap package building and then delete them on close.
keyPath := filepath.Join(b.Repo, "release/dist/qnap/Tailscale/private_key") keyPath := filepath.Join(m.tmpDir, "files/Tailscale/private_key")
if err := os.WriteFile(keyPath, key, 0400); err != nil { if err := os.WriteFile(keyPath, key, 0400); err != nil {
return err return nil, err
} }
certPath := filepath.Join(b.Repo, "release/dist/qnap/Tailscale/certificate") certPath := filepath.Join(m.tmpDir, "files/Tailscale/certificate")
if err := os.WriteFile(certPath, cert, 0400); err != nil { if err := os.WriteFile(certPath, cert, 0400); err != nil {
return err return nil, err
} }
}
b.AddOnCloseFunc(func() error { return m, nil
return errors.Join(os.Remove(keyPath), os.Remove(certPath))
})
return nil
})
}
type qnapBuildsMemoizeKey struct{}
type innerPkg struct {
tailscalePath string
tailscaledPath string
}
// qnapBuilds holds extra build context shared by all qnap builds.
type qnapBuilds struct {
// innerPkgs contains per-goenv compiled binary paths.
// It is used to avoid repeated compilations for the same architecture.
innerPkgs dist.Memoize[*innerPkg]
dockerImageMu sync.Mutex
}
// getQnapBuilds returns the qnapBuilds for b, creating one if needed.
func getQnapBuilds(b *dist.Build) *qnapBuilds {
return b.Extra(qnapBuildsMemoizeKey{}, func() any { return new(qnapBuilds) }).(*qnapBuilds)
} }
// buildInnerPackage builds the go binaries used for qnap packages. // buildInnerPackage builds the go binaries used for qnap packages.
@ -181,7 +230,7 @@ func (m *qnapBuilds) buildInnerPackage(b *dist.Build, goenv map[string]string) (
// So here, we move the binaries into a directory within the b.Repo // So here, we move the binaries into a directory within the b.Repo
// path and clean it up when the builder closes. // path and clean it up when the builder closes.
tmpDir := filepath.Join(b.Repo, fmt.Sprintf("/tmp-qnap-%s-%s-%s", b.Version.Short, goenv["GOOS"], goenv["GOARCH"])) tmpDir := filepath.Join(m.tmpDir, fmt.Sprintf("/binaries-%s-%s-%s", b.Version.Short, goenv["GOOS"], goenv["GOARCH"]))
if err = os.MkdirAll(tmpDir, 0755); err != nil { if err = os.MkdirAll(tmpDir, 0755); err != nil {
return nil, err return nil, err
} }
@ -216,9 +265,9 @@ func (m *qnapBuilds) makeDockerImage(b *dist.Build) error {
log.Printf("Building qnapbuilder docker image") log.Printf("Building qnapbuilder docker image")
cmd := b.Command(b.Repo, "docker", "build", cmd := b.Command(b.Repo, "docker", "build",
"-f", filepath.Join(b.Repo, "release/dist/qnap/Dockerfile.qpkg"), "-f", filepath.Join(m.tmpDir, "files/scripts/Dockerfile.qpkg"),
"-t", "build.tailscale.io/qdk:latest", "-t", "build.tailscale.io/qdk:latest",
filepath.Join(b.Repo, "release/dist/qnap/"), filepath.Join(m.tmpDir, "files/scripts"),
) )
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil { if err != nil {

Loading…
Cancel
Save