Skip to content

Commit

Permalink
Merge pull request #25093 from mmaslankaprv/bazel-ducktape
Browse files Browse the repository at this point in the history
Added directory mode package for ducktape
  • Loading branch information
mmaslankaprv authored Feb 17, 2025
2 parents e463cc3 + 176fa8a commit 52b5e2d
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 15 deletions.
9 changes: 9 additions & 0 deletions bazel/packaging/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ redpanda_package(
rpath_override = "$ORIGIN/../lib",
)

redpanda_package(
name = "ducktape",
out = "redpanda_ducktape",
include_sysroot_libs = True,
redpanda_binary = "//:redpanda",
rpath_override = "$ORIGIN/../lib",
rpk_binary = "//:rpk",
)

# This is the ID for the nonroot user in the distroless containers.
NONROOT_USER = 65532

Expand Down
11 changes: 7 additions & 4 deletions bazel/packaging/packaging.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ def _prepare_package_content(ctx, dynamic_loader_path = "/opt/redpanda/lib"):
)

def _impl(ctx):
use_dir = not ctx.attr.out.endswith(".tar.gz")
out = ctx.actions.declare_directory(ctx.attr.out) if use_dir else ctx.actions.declare_file(ctx.attr.out)
package_content = _prepare_package_content(ctx)

# Create the configuration file for the packaging tool
Expand All @@ -101,6 +103,7 @@ def _impl(ctx):
"shared_libraries": [solib.path for solib in package_content.shared_libraries],
"default_yaml_config": ctx.file.default_yaml_config.path if ctx.file.default_yaml_config else None,
"owner": ctx.attr.owner,
"directory_mode": use_dir,
}
ctx.actions.write(cfg_file, content = json.encode_indent(cfg))

Expand All @@ -113,20 +116,20 @@ def _impl(ctx):

# run the packaging tool
ctx.actions.run(
outputs = [ctx.outputs.out],
outputs = [out],
inputs = inputs,
tools = [ctx.executable._tool],
executable = ctx.executable._tool,
arguments = [
"-config",
cfg_file.path,
"-output",
ctx.outputs.out.path,
out.path,
],
mnemonic = "BuildingRedpandaPackage",
use_default_shell_env = False,
)
return [DefaultInfo(files = depset([ctx.outputs.out]))]
return [DefaultInfo(files = depset([out]))]

redpanda_package = rule(
implementation = _impl,
Expand All @@ -142,7 +145,7 @@ redpanda_package = rule(
allow_single_file = True,
),
"owner": attr.int(),
"out": attr.output(
"out": attr.string(
mandatory = True,
),
"include_sysroot_libs": attr.bool(),
Expand Down
99 changes: 88 additions & 11 deletions bazel/packaging/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type pkgConfig struct {
SharedLibraries []string `json:"shared_libraries"`
DefaultYAMLConfig *string `json:"default_yaml_config"`
Owner int `json:"owner"`
DirectoryMode bool `json:"directory_mode"`
}

func createTarball(cfg pkgConfig, w io.Writer) error {
Expand Down Expand Up @@ -113,6 +114,75 @@ func createTarball(cfg pkgConfig, w io.Writer) error {
}
return nil
}
func copyFile(src string, dst string) error {
srcFile, err := os.Open(src)
if err != nil {
return err
}
defer srcFile.Close()
dstFile, err := os.Create(dst)
if err != nil {
return err
}
defer dstFile.Close()
dstFile.ReadFrom(srcFile)

return nil
}

func createPackageDir(cfg pkgConfig, output string) error {
if err := os.MkdirAll(output, 0755); err != nil {
return err
}

dir := func(path string) error {
if err := os.Mkdir(filepath.Join(output, path), 0755); err != nil {
return fmt.Errorf("Error creating directory %s: %v", path, err)
}
return nil
}

file := func(src string, dir string, name string) error {
if err := copyFile(src, filepath.Join(output, dir, name)); err != nil {
return fmt.Errorf("Error copying file %s: %v", src, err)
}
return nil
}

if cfg.DefaultYAMLConfig != nil {
if err := dir("config"); err != nil {
return err
}
}
if err := dir("bin"); err != nil {
return err
}
if err := dir("lib"); err != nil {
return err
}

if err := dir("libexec"); err != nil {
return err
}

for _, so := range cfg.SharedLibraries {
if err := file(so, "lib", filepath.Base(so)); err != nil {
return err
}
}

if err := file(cfg.RedpandaBinary, "libexec", "redpanda"); err != nil {
return err
}

if cfg.RPKBinary != nil {
if err := file(*cfg.RPKBinary, "bin", "rpk"); err != nil {
return err
}
}

return nil
}

func runTool() error {
configPath := flag.String("config", "", "path to a configuration file to create the tarball")
Expand All @@ -124,17 +194,24 @@ func runTool() error {
} else if err := json.Unmarshal(b, &cfg); err != nil {
return fmt.Errorf("unable to parse config: %w", err)
}
file, err := os.OpenFile(*output, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644)
if err != nil {
return fmt.Errorf("unable to open output file: %w", err)
}
defer file.Close()
bw := bufio.NewWriter(file)
defer bw.Flush()
gw := gzip.NewWriter(bw)
defer gw.Close()
if err := createTarball(cfg, gw); err != nil {
return fmt.Errorf("unable to create tarball: %w", err)

if cfg.DirectoryMode {
if err := createPackageDir(cfg, *output); err != nil {
return fmt.Errorf("unable to create package directory: %w", err)
}
} else {
file, err := os.OpenFile(*output, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644)
if err != nil {
return fmt.Errorf("unable to open output file: %w", err)
}
defer file.Close()
bw := bufio.NewWriter(file)
defer bw.Flush()
gw := gzip.NewWriter(bw)
defer gw.Close()
if err := createTarball(cfg, gw); err != nil {
return fmt.Errorf("unable to create tarball: %w", err)
}
}
return nil
}
Expand Down

0 comments on commit 52b5e2d

Please sign in to comment.