From c1cb8bc919438646493fa0540ad761aa805f87c8 Mon Sep 17 00:00:00 2001 From: mtojek Date: Thu, 21 Oct 2021 15:00:42 +0200 Subject: [PATCH 01/18] Create stub for creating zipped package --- cmd/build.go | 7 +++- internal/builder/packages.go | 67 +++++++++++++++++++++++++++--------- internal/cobraext/const.go | 3 ++ internal/files/compress.go | 9 +++++ 4 files changed, 69 insertions(+), 17 deletions(-) create mode 100644 internal/files/compress.go diff --git a/cmd/build.go b/cmd/build.go index cc08e3430f..0007d90b39 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -34,6 +34,7 @@ func setupBuildCommand() *cobraext.Command { Long: buildLongDescription, RunE: buildCommandAction, } + cmd.Flags().Bool(cobraext.BuildZipFlagName, false, cobraext.BuildZipFlagDescription) return cobraext.NewCommand(cmd, cobraext.ContextPackage) } @@ -62,7 +63,11 @@ func buildCommandAction(cmd *cobra.Command, args []string) error { cmd.Printf("%s file rendered: %s\n", splitTarget[len(splitTarget)-1], target) } - target, err := builder.BuildPackage(packageRoot) + createZip, _ := cmd.Flags().GetBool(cobraext.BuildZipFlagName) + target, err := builder.BuildPackage(builder.BuildOptions{ + PackageRoot: packageRoot, + CreateZip: createZip, + }) if err != nil { return errors.Wrap(err, "building package failed") } diff --git a/internal/builder/packages.go b/internal/builder/packages.go index 2a7b23efcf..cefabd31f7 100644 --- a/internal/builder/packages.go +++ b/internal/builder/packages.go @@ -16,6 +16,14 @@ import ( "github.com/pkg/errors" ) +const buildIntegrationsFolder = "integrations" + +type BuildOptions struct { + PackageRoot string + + CreateZip bool +} + // BuildDirectory function locates the target build directory. If the directory doesn't exist, it will create it. func BuildDirectory() (string, error) { buildDir, found, err := findBuildDirectory() @@ -55,25 +63,38 @@ func findBuildDirectory() (string, bool, error) { return "", false, nil } -// BuildPackagesDirectory function locates the target build directory for packages. -// If the directories path doesn't exist, it will create it. +// BuildPackagesDirectory function locates the target build directory for the package. func BuildPackagesDirectory(packageRoot string) (string, error) { + buildDir, err := buildPackagesRootDirectory() + m, err := packages.ReadPackageManifestFromPackageRoot(packageRoot) + if err != nil { + return "", errors.Wrapf(err, "reading package manifest failed (path: %s)", packageRoot) + } + return filepath.Join(buildDir, m.Name, m.Version), nil +} + +// buildPackagesZipPath function locates the target zipped package path. +func buildPackagesZipPath(packageRoot string) (string, error) { + buildDir, err := buildPackagesRootDirectory() + m, err := packages.ReadPackageManifestFromPackageRoot(packageRoot) + if err != nil { + return "", errors.Wrapf(err, "reading package manifest failed (path: %s)", packageRoot) + } + return filepath.Join(buildDir, fmt.Sprintf("%s-%s.zip", m.Name, m.Version)), nil +} + +func buildPackagesRootDirectory() (string, error) { buildDir, found, err := FindBuildPackagesDirectory() if err != nil { return "", errors.Wrap(err, "can't locate build directory") } if !found { - buildDir, err = createBuildDirectory("integrations") // TODO add support for other package types + buildDir, err = createBuildDirectory(buildIntegrationsFolder) // TODO add support for other package types if err != nil { return "", errors.Wrap(err, "can't create new build directory") } } - - m, err := packages.ReadPackageManifestFromPackageRoot(packageRoot) - if err != nil { - return "", errors.Wrapf(err, "reading package manifest failed (path: %s)", packageRoot) - } - return filepath.Join(buildDir, m.Name, m.Version), nil + return buildDir, nil } // FindBuildPackagesDirectory function locates the target build directory for packages. @@ -84,7 +105,7 @@ func FindBuildPackagesDirectory() (string, bool, error) { } if found { - path := filepath.Join(buildDir, "integrations") // TODO add support for other package types + path := filepath.Join(buildDir, buildIntegrationsFolder) // TODO add support for other package types fileInfo, err := os.Stat(path) if errors.Is(err, os.ErrNotExist) { return "", false, nil @@ -102,8 +123,8 @@ func FindBuildPackagesDirectory() (string, bool, error) { } // BuildPackage function builds the package. -func BuildPackage(packageRoot string) (string, error) { - destinationDir, err := BuildPackagesDirectory(packageRoot) +func BuildPackage(options BuildOptions) (string, error) { + destinationDir, err := BuildPackagesDirectory(options.PackageRoot) if err != nil { return "", errors.Wrap(err, "can't locate build directory") } @@ -115,8 +136,8 @@ func BuildPackage(packageRoot string) (string, error) { return "", errors.Wrap(err, "clearing package contents failed") } - logger.Debugf("Copy package content (source: %s)", packageRoot) - err = files.CopyWithoutDev(packageRoot, destinationDir) + logger.Debugf("Copy package content (source: %s)", options.PackageRoot) + err = files.CopyWithoutDev(options.PackageRoot, destinationDir) if err != nil { return "", errors.Wrap(err, "copying package contents failed") } @@ -128,11 +149,25 @@ func BuildPackage(packageRoot string) (string, error) { } logger.Debug("Resolve external fields") - err = resolveExternalFields(packageRoot, destinationDir) + err = resolveExternalFields(options.PackageRoot, destinationDir) if err != nil { return "", errors.Wrap(err, "resolving external fields failed") } - return destinationDir, nil + + if !options.CreateZip { + return destinationDir, nil + } + + zippedPackagePath, err := buildPackagesZipPath(options.PackageRoot) + if err != nil { + return "", errors.Wrap(err, "can't evaluate path for the zipped package") + } + + err = files.Zip(destinationDir, zippedPackagePath) + if err != nil { + return "", errors.Wrapf(err, "can't compress the built package (compressed file path: %s)", zippedPackagePath) + } + return zippedPackagePath, nil } func createBuildDirectory(dirs ...string) (string, error) { diff --git a/internal/cobraext/const.go b/internal/cobraext/const.go index e9ee5795a7..5aba2768a1 100644 --- a/internal/cobraext/const.go +++ b/internal/cobraext/const.go @@ -6,6 +6,9 @@ package cobraext // Flag names and descriptions used by CLI commands. const ( + BuildZipFlagName = "zip" + BuildZipFlagDescription = "archive the built package" + CheckConditionFlagName = "check-condition" CheckConditionFlagDescription = "check if the condition is met for the package, but don't install the package (e.g. kibana.version=7.10.0)" diff --git a/internal/files/compress.go b/internal/files/compress.go new file mode 100644 index 0000000000..0a0e96f7c6 --- /dev/null +++ b/internal/files/compress.go @@ -0,0 +1,9 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + +package files + +func Zip(sourcePath string, destinationFile string) error { + return nil +} From ecc1cc0b89b2d2ff57f42d7dd551fe6172957353 Mon Sep 17 00:00:00 2001 From: mtojek Date: Thu, 21 Oct 2021 15:09:05 +0200 Subject: [PATCH 02/18] Fix --- internal/files/compress.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/files/compress.go b/internal/files/compress.go index 0a0e96f7c6..ddde11005f 100644 --- a/internal/files/compress.go +++ b/internal/files/compress.go @@ -4,6 +4,6 @@ package files -func Zip(sourcePath string, destinationFile string) error { +func Zip(sourcePath, destinationFile string) error { return nil } From d450981728ea651a8283af9dce7b41b77be93881 Mon Sep 17 00:00:00 2001 From: mtojek Date: Thu, 21 Oct 2021 15:23:38 +0200 Subject: [PATCH 03/18] Use z.Archive --- go.mod | 10 ++++++++++ go.sum | 24 ++++++++++++++++++++++++ internal/files/compress.go | 21 +++++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/go.mod b/go.mod index 9797acaf12..96016adefc 100644 --- a/go.mod +++ b/go.mod @@ -20,6 +20,7 @@ require ( github.com/jedib0t/go-pretty v4.3.0+incompatible github.com/kylelemons/godebug v1.1.0 github.com/magefile/mage v1.11.0 + github.com/mholt/archiver/v3 v3.5.0 github.com/olekukonko/tablewriter v0.0.5 github.com/pkg/errors v0.9.1 github.com/spf13/cobra v1.2.1 @@ -47,9 +48,11 @@ require ( github.com/PuerkitoBio/purell v1.1.1 // indirect github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect github.com/acomagu/bufpipe v1.0.3 // indirect + github.com/andybalholm/brotli v1.0.0 // indirect github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535 // indirect github.com/creasty/defaults v1.5.2 // indirect github.com/davecgh/go-spew v1.1.1 // indirect + github.com/dsnet/compress v0.0.1 // indirect github.com/emirpasic/gods v1.12.0 // indirect github.com/evanphx/json-patch v4.11.0+incompatible // indirect github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect @@ -64,6 +67,7 @@ require ( github.com/go-stack/stack v1.8.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.5.2 // indirect + github.com/golang/snappy v0.0.1 // indirect github.com/google/btree v1.0.1 // indirect github.com/google/go-cmp v0.5.5 // indirect github.com/google/gofuzz v1.1.0 // indirect @@ -79,6 +83,8 @@ require ( github.com/json-iterator/go v1.1.11 // indirect github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect + github.com/klauspost/compress v1.11.13 // indirect + github.com/klauspost/pgzip v1.2.4 // indirect github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect github.com/mailru/easyjson v0.7.6 // indirect github.com/mattn/go-colorable v0.1.9 // indirect @@ -93,15 +99,19 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.1 // indirect github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect + github.com/nwaples/rardecode v1.1.0 // indirect github.com/peterbourgon/diskv v2.0.1+incompatible // indirect + github.com/pierrec/lz4/v4 v4.0.3 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/russross/blackfriday v1.5.2 // indirect github.com/sergi/go-diff v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect + github.com/ulikunitz/xz v0.5.7 // indirect github.com/xanzy/ssh-agent v0.3.0 // indirect github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect github.com/xeipuuv/gojsonschema v1.2.0 // indirect + github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca // indirect go.mongodb.org/mongo-driver v1.1.2 // indirect go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect diff --git a/go.sum b/go.sum index ddb312b0d6..3068cc53c7 100644 --- a/go.sum +++ b/go.sum @@ -118,6 +118,8 @@ github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRF github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= github.com/alexflint/go-filemutex v0.0.0-20171022225611-72bdc8eae2ae/go.mod h1:CgnQgUtFrFz9mxFNtED3jI5tLDjKlOM+oUF/sTk6ps0= +github.com/andybalholm/brotli v1.0.0 h1:7UCwP93aiSfvWpapti8g88vVVGp2qqtGyePsSuDafo4= +github.com/andybalholm/brotli v1.0.0/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y= github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo4JG6LR5AXSUEsOjtdm0kw0FtQtMJA= github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= @@ -320,6 +322,9 @@ github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDD github.com/docker/libtrust v0.0.0-20150114040149-fa567046d9b1/go.mod h1:cyGadeNEkKy96OOhEzfZl+yxihPEzKnqJwvfuSUqbZE= github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM= github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE= +github.com/dsnet/compress v0.0.1 h1:PlZu0n3Tuv04TzpfPbrnI0HW/YwodEXDS+oPKahKF0Q= +github.com/dsnet/compress v0.0.1/go.mod h1:Aw8dCMJ7RioblQeTqt88akK31OvO8Dhf5JflhBbQEHo= +github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY= github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/elastic/go-elasticsearch/v7 v7.15.1 h1:Wd8RLHb5D8xPBU8vGlnLXyflkso9G+rCmsXjqH8LLQQ= @@ -467,6 +472,8 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS github.com/golang/protobuf v1.5.1/go.mod h1:DopwsBzvsk0Fs44TXzsVbJyPhcCPeIwnvohx4u74HPM= github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= +github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golangplus/testing v0.0.0-20180327235837-af21d9c3145e/go.mod h1:0AA//k/eakGydO4jKRoRL2j92ZKSzTgj9tclaCrvXHk= github.com/gomodule/redigo v1.8.2/go.mod h1:P9dn9mFrCBvWhGE1wpxx6fgq7BAeLBk+UUUzlpkBYO0= github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= @@ -612,8 +619,14 @@ github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvW github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= +github.com/klauspost/compress v1.10.10/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= github.com/klauspost/compress v1.11.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= +github.com/klauspost/compress v1.11.13 h1:eSvu8Tmq6j2psUJqJrLcWH6K3w5Dwc+qipbaA6eVEN4= github.com/klauspost/compress v1.11.13/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= +github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= +github.com/klauspost/pgzip v1.2.4 h1:TQ7CNpYKovDOmqzRHKxJh0BeaBI7UdQZYc6p7pMQh1A= +github.com/klauspost/pgzip v1.2.4/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= @@ -679,6 +692,8 @@ github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182aff github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQE9x6ikvDFZS2mDVS3drnohI= github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= +github.com/mholt/archiver/v3 v3.5.0 h1:nE8gZIrw66cu4osS/U7UW7YDuGMHssxKutU8IfWxwWE= +github.com/mholt/archiver/v3 v3.5.0/go.mod h1:qqTTPUK/HZPFgFQ/TJ3BzvTpF/dPtFVJXdQbCmeMxwc= github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= github.com/miekg/pkcs11 v1.0.3/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs= github.com/mistifyio/go-zfs v2.1.2-0.20190413222219-f784269be439+incompatible/go.mod h1:8AuVvqP/mXw1px98n46wfvcGfQ4ci2FwoAjKYxuo3Z4= @@ -727,6 +742,8 @@ github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRW github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw= github.com/ncw/swift v1.0.47/go.mod h1:23YIA4yWVnGwv2dQlN4bB7egfYX6YLn0Yo/S6zZO/ZM= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= +github.com/nwaples/rardecode v1.1.0 h1:vSxaY8vQhOcVr4mm5e8XllHWTiM4JF507A0Katqw7MQ= +github.com/nwaples/rardecode v1.1.0/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWkRpHbbfGS0= github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= @@ -781,6 +798,8 @@ github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCko github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI= github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU= github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2/go.mod h1:iIss55rKnNBTvrwdmkUpLnDpZoAHvWaiq5+iMmen4AE= +github.com/pierrec/lz4/v4 v4.0.3 h1:vNQKSVZNYUEAvRY9FaUXAF1XPbSOHJtDTiP41kzDz2E= +github.com/pierrec/lz4/v4 v4.0.3/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1-0.20171018195549-f15c970de5b7/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -905,6 +924,9 @@ github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1 github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= +github.com/ulikunitz/xz v0.5.6/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8= +github.com/ulikunitz/xz v0.5.7 h1:YvTNdFzX6+W5m9msiYg/zpkSURPPtOlzbqYjrFn7Yt4= +github.com/ulikunitz/xz v0.5.7/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= github.com/urfave/cli v0.0.0-20171014202726-7bc6a0acffa5/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= @@ -927,6 +949,8 @@ github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1: github.com/xeipuuv/gojsonschema v0.0.0-20180618132009-1d523034197f/go.mod h1:5yf86TLmAcydyeJq5YvxkGPE2fm/u4myDekKRoLuqhs= github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74= github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y= +github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 h1:nIPpBwaJSVYIxUFsDv3M8ofmx9yWTog9BfvIu0q41lo= +github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMxjDjgmT5uz5wzYJKVo23qUhYTos= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca h1:1CFlNzQhALwjS9mBAUkycX616GzgsuYUOCHA5+HSlXI= github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg= diff --git a/internal/files/compress.go b/internal/files/compress.go index ddde11005f..84b84cef05 100644 --- a/internal/files/compress.go +++ b/internal/files/compress.go @@ -4,6 +4,27 @@ package files +import ( + "compress/flate" + + "github.com/mholt/archiver/v3" + "github.com/pkg/errors" +) + +// Zip function creates the .zip archive from the source path (built package content). func Zip(sourcePath, destinationFile string) error { + z := archiver.Zip{ + CompressionLevel: flate.DefaultCompression, + MkdirAll: true, + SelectiveCompression: true, + ContinueOnError: false, + OverwriteExisting: false, + ImplicitTopLevelFolder: false, + } + + err := z.Archive([]string{sourcePath}, destinationFile) + if err != nil { + return errors.Wrapf(err, "can't archive source directory (source path: %s)", sourcePath) + } return nil } From 90d3b1511bb5f314a6c9c7dfdcde3a0fd2befc48 Mon Sep 17 00:00:00 2001 From: mtojek Date: Thu, 21 Oct 2021 15:27:17 +0200 Subject: [PATCH 04/18] Debug calls --- internal/builder/packages.go | 1 + internal/files/compress.go | 3 +++ 2 files changed, 4 insertions(+) diff --git a/internal/builder/packages.go b/internal/builder/packages.go index cefabd31f7..69c81bcf8b 100644 --- a/internal/builder/packages.go +++ b/internal/builder/packages.go @@ -158,6 +158,7 @@ func BuildPackage(options BuildOptions) (string, error) { return destinationDir, nil } + logger.Debug("Build zipped package") zippedPackagePath, err := buildPackagesZipPath(options.PackageRoot) if err != nil { return "", errors.Wrap(err, "can't evaluate path for the zipped package") diff --git a/internal/files/compress.go b/internal/files/compress.go index 84b84cef05..f3e0693d69 100644 --- a/internal/files/compress.go +++ b/internal/files/compress.go @@ -7,6 +7,8 @@ package files import ( "compress/flate" + "github.com/elastic/elastic-package/internal/logger" + "github.com/mholt/archiver/v3" "github.com/pkg/errors" ) @@ -22,6 +24,7 @@ func Zip(sourcePath, destinationFile string) error { ImplicitTopLevelFolder: false, } + logger.Debug("Compress using archiver.Zip (destination: %s)", destinationFile) err := z.Archive([]string{sourcePath}, destinationFile) if err != nil { return errors.Wrapf(err, "can't archive source directory (source path: %s)", sourcePath) From 3dd4b2ef97cf8dc1865f89e0e20e906ab999bcf8 Mon Sep 17 00:00:00 2001 From: mtojek Date: Thu, 21 Oct 2021 15:31:22 +0200 Subject: [PATCH 05/18] os.Remove --- internal/files/compress.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/internal/files/compress.go b/internal/files/compress.go index f3e0693d69..b3f8be5ef1 100644 --- a/internal/files/compress.go +++ b/internal/files/compress.go @@ -6,6 +6,7 @@ package files import ( "compress/flate" + "os" "github.com/elastic/elastic-package/internal/logger" @@ -15,6 +16,14 @@ import ( // Zip function creates the .zip archive from the source path (built package content). func Zip(sourcePath, destinationFile string) error { + logger.Debugf("Compress using archiver.Zip (destination: %s)", destinationFile) + + logger.Debugf("Remove old .zip artifact first (destination: %s)", destinationFile) + err := os.Remove(destinationFile) + if err != nil && !errors.Is(err, os.ErrNotExist) { + return errors.Wrap(err, "can't remove old .zip artifact") + } + z := archiver.Zip{ CompressionLevel: flate.DefaultCompression, MkdirAll: true, @@ -23,9 +32,7 @@ func Zip(sourcePath, destinationFile string) error { OverwriteExisting: false, ImplicitTopLevelFolder: false, } - - logger.Debug("Compress using archiver.Zip (destination: %s)", destinationFile) - err := z.Archive([]string{sourcePath}, destinationFile) + err = z.Archive([]string{sourcePath}, destinationFile) if err != nil { return errors.Wrapf(err, "can't archive source directory (source path: %s)", sourcePath) } From 51d698fc64a70924c7748efa33b2f8fdd8e0cf07 Mon Sep 17 00:00:00 2001 From: mtojek Date: Thu, 21 Oct 2021 16:02:48 +0200 Subject: [PATCH 06/18] Fix --- internal/files/compress.go | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/internal/files/compress.go b/internal/files/compress.go index b3f8be5ef1..f0f8ec3ac3 100644 --- a/internal/files/compress.go +++ b/internal/files/compress.go @@ -7,6 +7,7 @@ package files import ( "compress/flate" "os" + "path/filepath" "github.com/elastic/elastic-package/internal/logger" @@ -18,23 +19,38 @@ import ( func Zip(sourcePath, destinationFile string) error { logger.Debugf("Compress using archiver.Zip (destination: %s)", destinationFile) - logger.Debugf("Remove old .zip artifact first (destination: %s)", destinationFile) - err := os.Remove(destinationFile) - if err != nil && !errors.Is(err, os.ErrNotExist) { - return errors.Wrap(err, "can't remove old .zip artifact") - } - z := archiver.Zip{ CompressionLevel: flate.DefaultCompression, - MkdirAll: true, + MkdirAll: false, SelectiveCompression: true, ContinueOnError: false, - OverwriteExisting: false, + OverwriteExisting: true, ImplicitTopLevelFolder: false, } - err = z.Archive([]string{sourcePath}, destinationFile) + + // It's impossible to select the root directory with archiver's options, so to prevent creating a common + // root directory ("1.0.1" for "build/integrations/aws/1.0.1"), we need to list all items in the package root. + listed, err := listFilesInRoot(sourcePath) + if err != nil { + return errors.Wrapf(err, "can't list files in root (path: %s)", sourcePath) + } + + err = z.Archive(listed, destinationFile) if err != nil { return errors.Wrapf(err, "can't archive source directory (source path: %s)", sourcePath) } return nil } + +func listFilesInRoot(sourcePath string) ([]string, error) { + dirEntries, err := os.ReadDir(sourcePath) + if err != nil { + return nil, errors.Wrap(err, "can't list source path") + } + + var paths []string + for _, de := range dirEntries { + paths = append(paths, filepath.Join(sourcePath, de.Name())) + } + return paths, nil +} From 27fa9c93e0ced5c86f823bc8109699060611e466 Mon Sep 17 00:00:00 2001 From: mtojek Date: Thu, 21 Oct 2021 18:07:31 +0200 Subject: [PATCH 07/18] CI: introduce build-zip goal --- .ci/Jenkinsfile | 1 + Makefile | 5 ++++- scripts/test-build-zip.sh | 44 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100755 scripts/test-build-zip.sh diff --git a/.ci/Jenkinsfile b/.ci/Jenkinsfile index 12dd6a8ef2..22e2a9b6f1 100644 --- a/.ci/Jenkinsfile +++ b/.ci/Jenkinsfile @@ -93,6 +93,7 @@ pipeline { parallel([ 'stack-command': generateTestCommandStage(command: 'test-stack-command', artifacts: ['build/elastic-stack-dump/stack/logs/*.log', 'build/elastic-stack-dump/stack/logs/fleet-server-internal/*']), 'check-packages': generateTestCommandStage(command: 'test-check-packages', artifacts: ['build/test-results/*.xml', 'build/kubectl-dump.txt', 'build/elastic-stack-dump/check/logs/*.log', 'build/elastic-stack-dump/check/logs/fleet-server-internal/*'], junitArtifacts: true, publishCoverage: true), + 'build-zip': generateTestCommandStage(command: 'test-build-zip', artifacts: ['build/elastic-stack-dump/build-zip/logs/*.log']), 'profiles-command': generateTestCommandStage(command: 'test-profiles-command'), ]) } diff --git a/Makefile b/Makefile index c20fff8eed..f86268ab07 100644 --- a/Makefile +++ b/Makefile @@ -44,10 +44,13 @@ test-stack-command: test-check-packages: ./scripts/test-check-packages.sh +test-build-zip: + ./scripts/test-build-zip.sh + test-profiles-command: ./scripts/test-profiles-command.sh -test: test-go test-stack-command test-check-packages test-profiles-command +test: test-go test-stack-command test-check-packages test-profiles-command test-build-zip check-git-clean: git update-index --really-refresh diff --git a/scripts/test-build-zip.sh b/scripts/test-build-zip.sh new file mode 100755 index 0000000000..22e9cb856a --- /dev/null +++ b/scripts/test-build-zip.sh @@ -0,0 +1,44 @@ +#!/bin/bash + +set -euxo pipefail + +cleanup() { + r=$? + + # Dump stack logs + elastic-package stack dump -v --output build/elastic-stack-dump/build-zip + + # Take down the stack + elastic-package stack down -v + + # Clean used resources + for d in test/packages/*/; do + ( + cd $d + elastic-package clean -v + ) + done + + exit $r +} + +trap cleanup EXIT + +OLDPWD=$PWD +# Build packages +for d in test/packages/*/; do + ( + cd $d + elastic-package build --zip -v + ) +done +cd - + +# Update the stack +elastic-package stack update -v --services package-registry + +# Boot up the stack +elastic-package stack up -d -v --services package-registry + +cd - +done From 255b64e31b09304bd8466e8818c9d37fd780d94f Mon Sep 17 00:00:00 2001 From: mtojek Date: Thu, 21 Oct 2021 18:15:35 +0200 Subject: [PATCH 08/18] Implement clean --- internal/builder/packages.go | 7 ++++++- internal/cleanup/build.go | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/internal/builder/packages.go b/internal/builder/packages.go index 69c81bcf8b..d7d95d4e13 100644 --- a/internal/builder/packages.go +++ b/internal/builder/packages.go @@ -80,7 +80,12 @@ func buildPackagesZipPath(packageRoot string) (string, error) { if err != nil { return "", errors.Wrapf(err, "reading package manifest failed (path: %s)", packageRoot) } - return filepath.Join(buildDir, fmt.Sprintf("%s-%s.zip", m.Name, m.Version)), nil + return ZippedBuiltPackagePath(buildDir, *m), nil +} + +// ZippedBuiltPackagePath function returns the path to zipped built package. +func ZippedBuiltPackagePath(buildDir string, m packages.PackageManifest) string { + return filepath.Join(buildDir, fmt.Sprintf("%s-%s.zip", m.Name, m.Version)) } func buildPackagesRootDirectory() (string, error) { diff --git a/internal/cleanup/build.go b/internal/cleanup/build.go index 3221237dde..bddfd7a97f 100644 --- a/internal/cleanup/build.go +++ b/internal/cleanup/build.go @@ -56,5 +56,12 @@ func Build() (string, error) { if err != nil { return "", errors.Wrapf(err, "can't remove directory (path: %s)", destinationDir) } + + zippedBuildPackagePath := builder.ZippedBuiltPackagePath(buildDir, *m) + logger.Debugf("Remove zipped built package (path: %s)", zippedBuildPackagePath) + err = os.RemoveAll(zippedBuildPackagePath) + if err != nil { + return "", errors.Wrapf(err, "can't remove zipped built package (path: %s)", zippedBuildPackagePath) + } return destinationDir, nil } From 44fa6afd9be2352e7a7327a65a9c04a69e7f5d72 Mon Sep 17 00:00:00 2001 From: mtojek Date: Thu, 21 Oct 2021 18:26:00 +0200 Subject: [PATCH 09/18] Fix --- internal/builder/packages.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/builder/packages.go b/internal/builder/packages.go index d7d95d4e13..fc2d881bec 100644 --- a/internal/builder/packages.go +++ b/internal/builder/packages.go @@ -66,6 +66,9 @@ func findBuildDirectory() (string, bool, error) { // BuildPackagesDirectory function locates the target build directory for the package. func BuildPackagesDirectory(packageRoot string) (string, error) { buildDir, err := buildPackagesRootDirectory() + if err != nil { + return "", errors.Wrap(err, "can't locate build packages root directory") + } m, err := packages.ReadPackageManifestFromPackageRoot(packageRoot) if err != nil { return "", errors.Wrapf(err, "reading package manifest failed (path: %s)", packageRoot) @@ -76,6 +79,9 @@ func BuildPackagesDirectory(packageRoot string) (string, error) { // buildPackagesZipPath function locates the target zipped package path. func buildPackagesZipPath(packageRoot string) (string, error) { buildDir, err := buildPackagesRootDirectory() + if err != nil { + return "", errors.Wrap(err, "can't locate build packages root directory") + } m, err := packages.ReadPackageManifestFromPackageRoot(packageRoot) if err != nil { return "", errors.Wrapf(err, "reading package manifest failed (path: %s)", packageRoot) From e65293652ebff42458e4051e32551215058cc4fa Mon Sep 17 00:00:00 2001 From: mtojek Date: Thu, 21 Oct 2021 18:41:56 +0200 Subject: [PATCH 10/18] Don't update the stack --- scripts/test-build-zip.sh | 3 --- 1 file changed, 3 deletions(-) diff --git a/scripts/test-build-zip.sh b/scripts/test-build-zip.sh index 22e9cb856a..176499cca3 100755 --- a/scripts/test-build-zip.sh +++ b/scripts/test-build-zip.sh @@ -34,9 +34,6 @@ for d in test/packages/*/; do done cd - -# Update the stack -elastic-package stack update -v --services package-registry - # Boot up the stack elastic-package stack up -d -v --services package-registry From 85d4a4ea4874801ffef4abfe835c43c59fb7b176 Mon Sep 17 00:00:00 2001 From: mtojek Date: Thu, 21 Oct 2021 18:44:23 +0200 Subject: [PATCH 11/18] Remove build directories --- scripts/test-build-zip.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scripts/test-build-zip.sh b/scripts/test-build-zip.sh index 176499cca3..756980e32e 100755 --- a/scripts/test-build-zip.sh +++ b/scripts/test-build-zip.sh @@ -34,6 +34,9 @@ for d in test/packages/*/; do done cd - +# Remove unzipped built packages, leave .zip files +rm -r build/integrations/*/ + # Boot up the stack elastic-package stack up -d -v --services package-registry From 1bd04c7339a231d5cb1a938240847635e3a54a2b Mon Sep 17 00:00:00 2001 From: mtojek Date: Thu, 21 Oct 2021 18:45:43 +0200 Subject: [PATCH 12/18] Install zip packages --- scripts/test-build-zip.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/scripts/test-build-zip.sh b/scripts/test-build-zip.sh index 756980e32e..f20ff4f83e 100755 --- a/scripts/test-build-zip.sh +++ b/scripts/test-build-zip.sh @@ -40,5 +40,11 @@ rm -r build/integrations/*/ # Boot up the stack elastic-package stack up -d -v --services package-registry +# Install zipped packages +for d in test/packages/*/; do + ( + cd $d + elastic-package install -v + ) cd - -done +done \ No newline at end of file From 89222b09fbabb79b7d650a8c6eec73014c87697a Mon Sep 17 00:00:00 2001 From: mtojek Date: Mon, 25 Oct 2021 12:20:02 +0200 Subject: [PATCH 13/18] WIP --- internal/files/compress.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/files/compress.go b/internal/files/compress.go index f0f8ec3ac3..efec10b2d6 100644 --- a/internal/files/compress.go +++ b/internal/files/compress.go @@ -25,7 +25,7 @@ func Zip(sourcePath, destinationFile string) error { SelectiveCompression: true, ContinueOnError: false, OverwriteExisting: true, - ImplicitTopLevelFolder: false, + ImplicitTopLevelFolder: true, } // It's impossible to select the root directory with archiver's options, so to prevent creating a common From a01337603a58a00e05e8cbcdcabff6d3c2f2d03a Mon Sep 17 00:00:00 2001 From: mtojek Date: Mon, 25 Oct 2021 13:16:47 +0200 Subject: [PATCH 14/18] Add missing root directory in the zip --- internal/files/compress.go | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/internal/files/compress.go b/internal/files/compress.go index efec10b2d6..ab45375bca 100644 --- a/internal/files/compress.go +++ b/internal/files/compress.go @@ -8,6 +8,7 @@ import ( "compress/flate" "os" "path/filepath" + "strings" "github.com/elastic/elastic-package/internal/logger" @@ -25,32 +26,34 @@ func Zip(sourcePath, destinationFile string) error { SelectiveCompression: true, ContinueOnError: false, OverwriteExisting: true, - ImplicitTopLevelFolder: true, + ImplicitTopLevelFolder: false, } - // It's impossible to select the root directory with archiver's options, so to prevent creating a common - // root directory ("1.0.1" for "build/integrations/aws/1.0.1"), we need to list all items in the package root. - listed, err := listFilesInRoot(sourcePath) + // Create a temporary work directory to properly name the root directory in the archive, e.g. aws-1.0.1 + tempDir := filepath.Join(os.TempDir(), folderNameFromFileName(destinationFile)) + os.MkdirAll(tempDir, 0755) + defer os.RemoveAll(tempDir) + + logger.Debugf("Create temporary directory for archiving: %s", tempDir) + err := CopyAll(sourcePath, tempDir) if err != nil { - return errors.Wrapf(err, "can't list files in root (path: %s)", sourcePath) + return errors.Wrapf(err, "can't create a temporary work directory (path: %s)", tempDir) } - err = z.Archive(listed, destinationFile) + err = z.Archive([]string{tempDir}, destinationFile) if err != nil { return errors.Wrapf(err, "can't archive source directory (source path: %s)", sourcePath) } return nil } -func listFilesInRoot(sourcePath string) ([]string, error) { - dirEntries, err := os.ReadDir(sourcePath) - if err != nil { - return nil, errors.Wrap(err, "can't list source path") - } - - var paths []string - for _, de := range dirEntries { - paths = append(paths, filepath.Join(sourcePath, de.Name())) +// folderNameFromFileName returns the folder name from the destination file. +// Based on mholt/archiver: https://github.com/mholt/archiver/blob/d35d4ce7c5b2411973fb7bd96ca1741eb011011b/archiver.go#L397 +func folderNameFromFileName(filename string) string { + base := filepath.Base(filename) + firstDot := strings.LastIndex(base, ".") + if firstDot > -1 { + return base[:firstDot] } - return paths, nil + return base } From 0ccb162b3cf65e4d499331e6e5d002e093f3eddf Mon Sep 17 00:00:00 2001 From: mtojek Date: Mon, 25 Oct 2021 13:35:26 +0200 Subject: [PATCH 15/18] Fix --- scripts/test-build-zip.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/test-build-zip.sh b/scripts/test-build-zip.sh index f20ff4f83e..d10465f832 100755 --- a/scripts/test-build-zip.sh +++ b/scripts/test-build-zip.sh @@ -38,6 +38,7 @@ cd - rm -r build/integrations/*/ # Boot up the stack +eval "$(elastic-package stack shellinit)" elastic-package stack up -d -v --services package-registry # Install zipped packages From 5c4ca2bc0c36f04931591c2de63262c13158025c Mon Sep 17 00:00:00 2001 From: mtojek Date: Mon, 25 Oct 2021 14:28:58 +0200 Subject: [PATCH 16/18] We need Kibana too --- scripts/test-build-zip.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/test-build-zip.sh b/scripts/test-build-zip.sh index d10465f832..0bc714aa4a 100755 --- a/scripts/test-build-zip.sh +++ b/scripts/test-build-zip.sh @@ -39,7 +39,7 @@ rm -r build/integrations/*/ # Boot up the stack eval "$(elastic-package stack shellinit)" -elastic-package stack up -d -v --services package-registry +elastic-package stack up -d -v # Install zipped packages for d in test/packages/*/; do From a4e8466887ce5dbe67602b11945bd3dae3424b8a Mon Sep 17 00:00:00 2001 From: mtojek Date: Mon, 25 Oct 2021 15:19:39 +0200 Subject: [PATCH 17/18] Address PR comments --- internal/files/compress.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/internal/files/compress.go b/internal/files/compress.go index ab45375bca..cf7727c853 100644 --- a/internal/files/compress.go +++ b/internal/files/compress.go @@ -30,17 +30,21 @@ func Zip(sourcePath, destinationFile string) error { } // Create a temporary work directory to properly name the root directory in the archive, e.g. aws-1.0.1 - tempDir := filepath.Join(os.TempDir(), folderNameFromFileName(destinationFile)) - os.MkdirAll(tempDir, 0755) + tempDir, err := os.MkdirTemp("", "elastic-package-") + if err != nil { + return errors.Wrap(err, "can't prepare a temporary directory") + } + workDir := filepath.Join(tempDir, folderNameFromFileName(destinationFile)) + os.MkdirAll(workDir, 0755) defer os.RemoveAll(tempDir) - logger.Debugf("Create temporary directory for archiving: %s", tempDir) - err := CopyAll(sourcePath, tempDir) + logger.Debugf("Create work directory for archiving: %s", workDir) + err = CopyAll(sourcePath, workDir) if err != nil { - return errors.Wrapf(err, "can't create a temporary work directory (path: %s)", tempDir) + return errors.Wrapf(err, "can't create a work directory (path: %s)", workDir) } - err = z.Archive([]string{tempDir}, destinationFile) + err = z.Archive([]string{workDir}, destinationFile) if err != nil { return errors.Wrapf(err, "can't archive source directory (source path: %s)", sourcePath) } From 92ef5d76a7f0c186e076af1bf1ad01d79faebf0a Mon Sep 17 00:00:00 2001 From: mtojek Date: Mon, 25 Oct 2021 15:29:24 +0200 Subject: [PATCH 18/18] Fix --- internal/files/compress.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/files/compress.go b/internal/files/compress.go index cf7727c853..3325016cff 100644 --- a/internal/files/compress.go +++ b/internal/files/compress.go @@ -34,9 +34,9 @@ func Zip(sourcePath, destinationFile string) error { if err != nil { return errors.Wrap(err, "can't prepare a temporary directory") } + defer os.RemoveAll(tempDir) workDir := filepath.Join(tempDir, folderNameFromFileName(destinationFile)) os.MkdirAll(workDir, 0755) - defer os.RemoveAll(tempDir) logger.Debugf("Create work directory for archiving: %s", workDir) err = CopyAll(sourcePath, workDir)