Skip to content

Commit 4e7ab31

Browse files
authored
Build zipped package (#550)
* Create stub for creating zipped package * Fix * Use z.Archive * Debug calls * os.Remove * Fix * CI: introduce build-zip goal * Implement clean * Fix * Don't update the stack * Remove build directories * Install zip packages * WIP * Add missing root directory in the zip * Fix * We need Kibana too * Address PR comments * Fix
1 parent a0c6664 commit 4e7ab31

File tree

10 files changed

+232
-18
lines changed

10 files changed

+232
-18
lines changed

.ci/Jenkinsfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ pipeline {
9393
parallel([
9494
'stack-command': generateTestCommandStage(command: 'test-stack-command', artifacts: ['build/elastic-stack-dump/stack/logs/*.log', 'build/elastic-stack-dump/stack/logs/fleet-server-internal/*']),
9595
'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),
96+
'build-zip': generateTestCommandStage(command: 'test-build-zip', artifacts: ['build/elastic-stack-dump/build-zip/logs/*.log']),
9697
'profiles-command': generateTestCommandStage(command: 'test-profiles-command'),
9798
])
9899
}

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,13 @@ test-stack-command:
4444
test-check-packages:
4545
./scripts/test-check-packages.sh
4646

47+
test-build-zip:
48+
./scripts/test-build-zip.sh
49+
4750
test-profiles-command:
4851
./scripts/test-profiles-command.sh
4952

50-
test: test-go test-stack-command test-check-packages test-profiles-command
53+
test: test-go test-stack-command test-check-packages test-profiles-command test-build-zip
5154

5255
check-git-clean:
5356
git update-index --really-refresh

cmd/build.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func setupBuildCommand() *cobraext.Command {
3434
Long: buildLongDescription,
3535
RunE: buildCommandAction,
3636
}
37+
cmd.Flags().Bool(cobraext.BuildZipFlagName, false, cobraext.BuildZipFlagDescription)
3738

3839
return cobraext.NewCommand(cmd, cobraext.ContextPackage)
3940
}
@@ -62,7 +63,11 @@ func buildCommandAction(cmd *cobra.Command, args []string) error {
6263
cmd.Printf("%s file rendered: %s\n", splitTarget[len(splitTarget)-1], target)
6364
}
6465

65-
target, err := builder.BuildPackage(packageRoot)
66+
createZip, _ := cmd.Flags().GetBool(cobraext.BuildZipFlagName)
67+
target, err := builder.BuildPackage(builder.BuildOptions{
68+
PackageRoot: packageRoot,
69+
CreateZip: createZip,
70+
})
6671
if err != nil {
6772
return errors.Wrap(err, "building package failed")
6873
}

go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ require (
2020
github.com/jedib0t/go-pretty v4.3.0+incompatible
2121
github.com/kylelemons/godebug v1.1.0
2222
github.com/magefile/mage v1.11.0
23+
github.com/mholt/archiver/v3 v3.5.0
2324
github.com/olekukonko/tablewriter v0.0.5
2425
github.com/pkg/errors v0.9.1
2526
github.com/spf13/cobra v1.2.1
@@ -47,9 +48,11 @@ require (
4748
github.com/PuerkitoBio/purell v1.1.1 // indirect
4849
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
4950
github.com/acomagu/bufpipe v1.0.3 // indirect
51+
github.com/andybalholm/brotli v1.0.0 // indirect
5052
github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535 // indirect
5153
github.com/creasty/defaults v1.5.2 // indirect
5254
github.com/davecgh/go-spew v1.1.1 // indirect
55+
github.com/dsnet/compress v0.0.1 // indirect
5356
github.com/emirpasic/gods v1.12.0 // indirect
5457
github.com/evanphx/json-patch v4.11.0+incompatible // indirect
5558
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect
@@ -64,6 +67,7 @@ require (
6467
github.com/go-stack/stack v1.8.0 // indirect
6568
github.com/gogo/protobuf v1.3.2 // indirect
6669
github.com/golang/protobuf v1.5.2 // indirect
70+
github.com/golang/snappy v0.0.1 // indirect
6771
github.com/google/btree v1.0.1 // indirect
6872
github.com/google/go-cmp v0.5.5 // indirect
6973
github.com/google/gofuzz v1.1.0 // indirect
@@ -79,6 +83,8 @@ require (
7983
github.com/json-iterator/go v1.1.11 // indirect
8084
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
8185
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
86+
github.com/klauspost/compress v1.11.13 // indirect
87+
github.com/klauspost/pgzip v1.2.4 // indirect
8288
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
8389
github.com/mailru/easyjson v0.7.6 // indirect
8490
github.com/mattn/go-colorable v0.1.9 // indirect
@@ -93,15 +99,19 @@ require (
9399
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
94100
github.com/modern-go/reflect2 v1.0.1 // indirect
95101
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
102+
github.com/nwaples/rardecode v1.1.0 // indirect
96103
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
104+
github.com/pierrec/lz4/v4 v4.0.3 // indirect
97105
github.com/pmezard/go-difflib v1.0.0 // indirect
98106
github.com/russross/blackfriday v1.5.2 // indirect
99107
github.com/sergi/go-diff v1.1.0 // indirect
100108
github.com/spf13/pflag v1.0.5 // indirect
109+
github.com/ulikunitz/xz v0.5.7 // indirect
101110
github.com/xanzy/ssh-agent v0.3.0 // indirect
102111
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
103112
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
104113
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
114+
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
105115
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca // indirect
106116
go.mongodb.org/mongo-driver v1.1.2 // indirect
107117
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect

go.sum

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRF
118118
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
119119
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
120120
github.com/alexflint/go-filemutex v0.0.0-20171022225611-72bdc8eae2ae/go.mod h1:CgnQgUtFrFz9mxFNtED3jI5tLDjKlOM+oUF/sTk6ps0=
121+
github.com/andybalholm/brotli v1.0.0 h1:7UCwP93aiSfvWpapti8g88vVVGp2qqtGyePsSuDafo4=
122+
github.com/andybalholm/brotli v1.0.0/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
121123
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo4JG6LR5AXSUEsOjtdm0kw0FtQtMJA=
122124
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
123125
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
320322
github.com/docker/libtrust v0.0.0-20150114040149-fa567046d9b1/go.mod h1:cyGadeNEkKy96OOhEzfZl+yxihPEzKnqJwvfuSUqbZE=
321323
github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM=
322324
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
325+
github.com/dsnet/compress v0.0.1 h1:PlZu0n3Tuv04TzpfPbrnI0HW/YwodEXDS+oPKahKF0Q=
326+
github.com/dsnet/compress v0.0.1/go.mod h1:Aw8dCMJ7RioblQeTqt88akK31OvO8Dhf5JflhBbQEHo=
327+
github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY=
323328
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
324329
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
325330
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
467472
github.com/golang/protobuf v1.5.1/go.mod h1:DopwsBzvsk0Fs44TXzsVbJyPhcCPeIwnvohx4u74HPM=
468473
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
469474
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
475+
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
476+
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
470477
github.com/golangplus/testing v0.0.0-20180327235837-af21d9c3145e/go.mod h1:0AA//k/eakGydO4jKRoRL2j92ZKSzTgj9tclaCrvXHk=
471478
github.com/gomodule/redigo v1.8.2/go.mod h1:P9dn9mFrCBvWhGE1wpxx6fgq7BAeLBk+UUUzlpkBYO0=
472479
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
612619
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
613620
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
614621
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
622+
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
623+
github.com/klauspost/compress v1.10.10/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
615624
github.com/klauspost/compress v1.11.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
625+
github.com/klauspost/compress v1.11.13 h1:eSvu8Tmq6j2psUJqJrLcWH6K3w5Dwc+qipbaA6eVEN4=
616626
github.com/klauspost/compress v1.11.13/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
627+
github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
628+
github.com/klauspost/pgzip v1.2.4 h1:TQ7CNpYKovDOmqzRHKxJh0BeaBI7UdQZYc6p7pMQh1A=
629+
github.com/klauspost/pgzip v1.2.4/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
617630
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
618631
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
619632
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
679692
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
680693
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQE9x6ikvDFZS2mDVS3drnohI=
681694
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
695+
github.com/mholt/archiver/v3 v3.5.0 h1:nE8gZIrw66cu4osS/U7UW7YDuGMHssxKutU8IfWxwWE=
696+
github.com/mholt/archiver/v3 v3.5.0/go.mod h1:qqTTPUK/HZPFgFQ/TJ3BzvTpF/dPtFVJXdQbCmeMxwc=
682697
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
683698
github.com/miekg/pkcs11 v1.0.3/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs=
684699
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
727742
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw=
728743
github.com/ncw/swift v1.0.47/go.mod h1:23YIA4yWVnGwv2dQlN4bB7egfYX6YLn0Yo/S6zZO/ZM=
729744
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
745+
github.com/nwaples/rardecode v1.1.0 h1:vSxaY8vQhOcVr4mm5e8XllHWTiM4JF507A0Katqw7MQ=
746+
github.com/nwaples/rardecode v1.1.0/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWkRpHbbfGS0=
730747
github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78=
731748
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
732749
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
781798
github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI=
782799
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
783800
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2/go.mod h1:iIss55rKnNBTvrwdmkUpLnDpZoAHvWaiq5+iMmen4AE=
801+
github.com/pierrec/lz4/v4 v4.0.3 h1:vNQKSVZNYUEAvRY9FaUXAF1XPbSOHJtDTiP41kzDz2E=
802+
github.com/pierrec/lz4/v4 v4.0.3/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
784803
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
785804
github.com/pkg/errors v0.8.1-0.20171018195549-f15c970de5b7/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
786805
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
905924
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
906925
github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
907926
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
927+
github.com/ulikunitz/xz v0.5.6/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8=
928+
github.com/ulikunitz/xz v0.5.7 h1:YvTNdFzX6+W5m9msiYg/zpkSURPPtOlzbqYjrFn7Yt4=
929+
github.com/ulikunitz/xz v0.5.7/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
908930
github.com/urfave/cli v0.0.0-20171014202726-7bc6a0acffa5/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
909931
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
910932
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:
927949
github.com/xeipuuv/gojsonschema v0.0.0-20180618132009-1d523034197f/go.mod h1:5yf86TLmAcydyeJq5YvxkGPE2fm/u4myDekKRoLuqhs=
928950
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
929951
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
952+
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 h1:nIPpBwaJSVYIxUFsDv3M8ofmx9yWTog9BfvIu0q41lo=
953+
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMxjDjgmT5uz5wzYJKVo23qUhYTos=
930954
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
931955
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca h1:1CFlNzQhALwjS9mBAUkycX616GzgsuYUOCHA5+HSlXI=
932956
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg=

internal/builder/packages.go

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ import (
1616
"github.com/pkg/errors"
1717
)
1818

19+
const buildIntegrationsFolder = "integrations"
20+
21+
type BuildOptions struct {
22+
PackageRoot string
23+
24+
CreateZip bool
25+
}
26+
1927
// BuildDirectory function locates the target build directory. If the directory doesn't exist, it will create it.
2028
func BuildDirectory() (string, error) {
2129
buildDir, found, err := findBuildDirectory()
@@ -55,25 +63,49 @@ func findBuildDirectory() (string, bool, error) {
5563
return "", false, nil
5664
}
5765

58-
// BuildPackagesDirectory function locates the target build directory for packages.
59-
// If the directories path doesn't exist, it will create it.
66+
// BuildPackagesDirectory function locates the target build directory for the package.
6067
func BuildPackagesDirectory(packageRoot string) (string, error) {
68+
buildDir, err := buildPackagesRootDirectory()
69+
if err != nil {
70+
return "", errors.Wrap(err, "can't locate build packages root directory")
71+
}
72+
m, err := packages.ReadPackageManifestFromPackageRoot(packageRoot)
73+
if err != nil {
74+
return "", errors.Wrapf(err, "reading package manifest failed (path: %s)", packageRoot)
75+
}
76+
return filepath.Join(buildDir, m.Name, m.Version), nil
77+
}
78+
79+
// buildPackagesZipPath function locates the target zipped package path.
80+
func buildPackagesZipPath(packageRoot string) (string, error) {
81+
buildDir, err := buildPackagesRootDirectory()
82+
if err != nil {
83+
return "", errors.Wrap(err, "can't locate build packages root directory")
84+
}
85+
m, err := packages.ReadPackageManifestFromPackageRoot(packageRoot)
86+
if err != nil {
87+
return "", errors.Wrapf(err, "reading package manifest failed (path: %s)", packageRoot)
88+
}
89+
return ZippedBuiltPackagePath(buildDir, *m), nil
90+
}
91+
92+
// ZippedBuiltPackagePath function returns the path to zipped built package.
93+
func ZippedBuiltPackagePath(buildDir string, m packages.PackageManifest) string {
94+
return filepath.Join(buildDir, fmt.Sprintf("%s-%s.zip", m.Name, m.Version))
95+
}
96+
97+
func buildPackagesRootDirectory() (string, error) {
6198
buildDir, found, err := FindBuildPackagesDirectory()
6299
if err != nil {
63100
return "", errors.Wrap(err, "can't locate build directory")
64101
}
65102
if !found {
66-
buildDir, err = createBuildDirectory("integrations") // TODO add support for other package types
103+
buildDir, err = createBuildDirectory(buildIntegrationsFolder) // TODO add support for other package types
67104
if err != nil {
68105
return "", errors.Wrap(err, "can't create new build directory")
69106
}
70107
}
71-
72-
m, err := packages.ReadPackageManifestFromPackageRoot(packageRoot)
73-
if err != nil {
74-
return "", errors.Wrapf(err, "reading package manifest failed (path: %s)", packageRoot)
75-
}
76-
return filepath.Join(buildDir, m.Name, m.Version), nil
108+
return buildDir, nil
77109
}
78110

79111
// FindBuildPackagesDirectory function locates the target build directory for packages.
@@ -84,7 +116,7 @@ func FindBuildPackagesDirectory() (string, bool, error) {
84116
}
85117

86118
if found {
87-
path := filepath.Join(buildDir, "integrations") // TODO add support for other package types
119+
path := filepath.Join(buildDir, buildIntegrationsFolder) // TODO add support for other package types
88120
fileInfo, err := os.Stat(path)
89121
if errors.Is(err, os.ErrNotExist) {
90122
return "", false, nil
@@ -102,8 +134,8 @@ func FindBuildPackagesDirectory() (string, bool, error) {
102134
}
103135

104136
// BuildPackage function builds the package.
105-
func BuildPackage(packageRoot string) (string, error) {
106-
destinationDir, err := BuildPackagesDirectory(packageRoot)
137+
func BuildPackage(options BuildOptions) (string, error) {
138+
destinationDir, err := BuildPackagesDirectory(options.PackageRoot)
107139
if err != nil {
108140
return "", errors.Wrap(err, "can't locate build directory")
109141
}
@@ -115,8 +147,8 @@ func BuildPackage(packageRoot string) (string, error) {
115147
return "", errors.Wrap(err, "clearing package contents failed")
116148
}
117149

118-
logger.Debugf("Copy package content (source: %s)", packageRoot)
119-
err = files.CopyWithoutDev(packageRoot, destinationDir)
150+
logger.Debugf("Copy package content (source: %s)", options.PackageRoot)
151+
err = files.CopyWithoutDev(options.PackageRoot, destinationDir)
120152
if err != nil {
121153
return "", errors.Wrap(err, "copying package contents failed")
122154
}
@@ -128,11 +160,26 @@ func BuildPackage(packageRoot string) (string, error) {
128160
}
129161

130162
logger.Debug("Resolve external fields")
131-
err = resolveExternalFields(packageRoot, destinationDir)
163+
err = resolveExternalFields(options.PackageRoot, destinationDir)
132164
if err != nil {
133165
return "", errors.Wrap(err, "resolving external fields failed")
134166
}
135-
return destinationDir, nil
167+
168+
if !options.CreateZip {
169+
return destinationDir, nil
170+
}
171+
172+
logger.Debug("Build zipped package")
173+
zippedPackagePath, err := buildPackagesZipPath(options.PackageRoot)
174+
if err != nil {
175+
return "", errors.Wrap(err, "can't evaluate path for the zipped package")
176+
}
177+
178+
err = files.Zip(destinationDir, zippedPackagePath)
179+
if err != nil {
180+
return "", errors.Wrapf(err, "can't compress the built package (compressed file path: %s)", zippedPackagePath)
181+
}
182+
return zippedPackagePath, nil
136183
}
137184

138185
func createBuildDirectory(dirs ...string) (string, error) {

internal/cleanup/build.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,12 @@ func Build() (string, error) {
5656
if err != nil {
5757
return "", errors.Wrapf(err, "can't remove directory (path: %s)", destinationDir)
5858
}
59+
60+
zippedBuildPackagePath := builder.ZippedBuiltPackagePath(buildDir, *m)
61+
logger.Debugf("Remove zipped built package (path: %s)", zippedBuildPackagePath)
62+
err = os.RemoveAll(zippedBuildPackagePath)
63+
if err != nil {
64+
return "", errors.Wrapf(err, "can't remove zipped built package (path: %s)", zippedBuildPackagePath)
65+
}
5966
return destinationDir, nil
6067
}

internal/cobraext/const.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ package cobraext
66

77
// Flag names and descriptions used by CLI commands.
88
const (
9+
BuildZipFlagName = "zip"
10+
BuildZipFlagDescription = "archive the built package"
11+
912
CheckConditionFlagName = "check-condition"
1013
CheckConditionFlagDescription = "check if the condition is met for the package, but don't install the package (e.g. kibana.version=7.10.0)"
1114

0 commit comments

Comments
 (0)