Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 16 additions & 1 deletion cmd/rpm2tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"os"
"sort"
"strings"
Expand All @@ -19,6 +21,7 @@ type rpm2tarOpts struct {
symlinks map[string]string
capabilities map[string]string
selinuxLabels map[string]string
compression string
}

var rpm2taropts = rpm2tarOpts{}
Expand All @@ -30,13 +33,24 @@ func NewRpm2TarCmd() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) (err error) {
sortSymlinkKeys()
rpmStream := os.Stdin
tarStream := os.Stdout
var tarStream io.Writer = os.Stdout
if rpm2taropts.output != "" {
tarStream, err = os.Create(rpm2taropts.output)
if err != nil {
return fmt.Errorf("could not create tar: %v", err)
}
}

if rpm2taropts.compression != "" {
switch rpm2taropts.compression {
case "gzip":
gw := gzip.NewWriter(tarStream)
defer gw.Close()
tarStream = gw
default:
return fmt.Errorf("unsupported compression algorithm: %s", rpm2taropts.compression)
}
}
cap := map[string][]string{}
for file, caps := range rpm2taropts.capabilities {
split := strings.Split(caps, ":")
Expand Down Expand Up @@ -105,6 +119,7 @@ func NewRpm2TarCmd() *cobra.Command {
rpm2tarCmd.Flags().StringToStringVarP(&rpm2taropts.symlinks, "symlinks", "s", map[string]string{}, "symlinks to add. Relative or absolute.")
rpm2tarCmd.Flags().StringToStringVarP(&rpm2taropts.capabilities, "capabilities", "c", map[string]string{}, "capabilities of files (--capabilities=/bin/ls=cap_net_bind_service)")
rpm2tarCmd.Flags().StringToStringVar(&rpm2taropts.selinuxLabels, "selinux-labels", map[string]string{}, "selinux labels of files (--selinux-labels=/bin/ls=unconfined_u:object_r:default_t:s0)")
rpm2tarCmd.Flags().StringVar(&rpm2taropts.compression, "compression", "", "compression determines the compression algorithm to be used, options include gzip (defaults to none)")
// deprecated options
rpm2tarCmd.Flags().StringToStringVar(&rpm2taropts.capabilities, "capabilties", map[string]string{}, "capabilities of files (-c=/bin/ls=cap_net_bind_service)")
rpm2tarCmd.Flags().MarkDeprecated("capabilties", "use --capabilities instead")
Expand Down
20 changes: 11 additions & 9 deletions cmd/rpmtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ import (
)

type rpmtreeOpts struct {
repofiles []string
workspace string
toMacro string
buildfile string
configname string
lockfile string
name string
public bool
repofiles []string
workspace string
toMacro string
buildfile string
configname string
lockfile string
name string
public bool
compression string
}

var rpmtreeopts = rpmtreeOpts{}
Expand Down Expand Up @@ -162,7 +163,7 @@ func NewRpmTreeCmd() *cobra.Command {
if err != nil {
return err
}
bazel.AddTree(rpmtreeopts.name, configname, build, install, rpmtreeopts.public)
bazel.AddTree(rpmtreeopts.name, configname, build, install, rpmtreeopts.public, rpmtreeopts.compression)

if err := handler.Process(install, build); err != nil {
return err
Expand Down Expand Up @@ -194,6 +195,7 @@ func NewRpmTreeCmd() *cobra.Command {
rpmtreeCmd.Flags().StringVar(&rpmtreeopts.configname, "configname", "rpms", "config name to use in lockfile")
rpmtreeCmd.Flags().StringVar(&rpmtreeopts.lockfile, "lockfile", "", "lockfile for RPMs")
rpmtreeCmd.Flags().StringVar(&rpmtreeopts.name, "name", "", "rpmtree rule name")
rpmtreeCmd.Flags().StringVar(&rpmtreeopts.compression, "compression", "", "Compression algorithm to use on resulting archive (e.g., gzip)")
rpmtreeCmd.MarkFlagRequired("name")

repo.AddCacheHelperFlags(rpmtreeCmd)
Expand Down
20 changes: 17 additions & 3 deletions internal/rpmtree.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ def _rpm2tar_impl(ctx):
selinux_labels.append(k + "=" + v)
args.add_joined("--selinux-labels", selinux_labels, join_with = ",")

if ctx.attr.compression:
args.add("--compression", ctx.attr.compression)

all_rpms = []

for target in ctx.attr.rpms:
Expand Down Expand Up @@ -110,6 +113,7 @@ _rpm2tar_attrs = {
"capabilities": attr.string_list_dict(),
"selinux_labels": attr.string_list_dict(),
"out": attr.output(mandatory = True),
"compression": attr.string(),
}

_tar2files_attrs = {
Expand All @@ -130,9 +134,19 @@ _tar2files = rule(
toolchains = [BAZELDNF_TOOLCHAIN],
)

def rpmtree(name, **kwargs):
"""Creates a tar file from a list of rpm files."""
tarname = name + ".tar"
def rpmtree(name, compression = None, **kwargs):
"""Creates a tar file from a list of rpm files.

Args:
name: The name of the target.
compression: The compression algorithm to use (e.g. 'gzip'). Defaults to None.
**kwargs: Additional keyword arguments to be passed to the _rpm2tar function (e.g. rpms, symlinks).
"""
extension = ".tar"
if compression == "gzip":
extension = ".tar.gz"

tarname = name + extension
_rpm2tar(
name = name,
out = tarname,
Expand Down
7 changes: 6 additions & 1 deletion pkg/bazel/bazel.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func AddTar2Files(name string, rpmtree string, buildfile *build.File, files []st
}
}

func AddTree(name, configname string, buildfile *build.File, pkgs []*api.Package, public bool) {
func AddTree(name, configname string, buildfile *build.File, pkgs []*api.Package, public bool, compression string) {
transform := func(n string) string {
return "@" + n + "//rpm"
}
Expand Down Expand Up @@ -302,6 +302,11 @@ func AddTree(name, configname string, buildfile *build.File, pkgs []*api.Package
}
rule.SetName(name)
rule.SetRPMs(rpms)

if compression != "" {
rule.SetAttr("compression", &build.StringExpr{Value: compression})
}

if public {
rule.SetAttr("visibility", &build.ListExpr{List: []build.Expr{&build.StringExpr{Value: "//visibility:public"}}})
}
Expand Down
Loading