55package cmd
66
77import (
8- "fmt"
9-
10- "github.com/Masterminds/semver/v3"
118 "github.com/pkg/errors"
129 "github.com/spf13/cobra"
1310
14- "github.com/elastic/package-spec/v2/code/go/pkg/validator"
15-
16- "github.com/elastic/elastic-package/internal/builder"
1711 "github.com/elastic/elastic-package/internal/cobraext"
1812 "github.com/elastic/elastic-package/internal/kibana"
19- "github.com/elastic/elastic-package/internal/logger"
2013 "github.com/elastic/elastic-package/internal/packages"
2114 "github.com/elastic/elastic-package/internal/packages/installer"
2215)
@@ -25,8 +18,6 @@ const installLongDescription = `Use this command to install the package in Kiban
2518
2619The command uses Kibana API to install the package in Kibana. The package must be exposed via the Package Registry or built locally in zip format so they can be installed using --zip parameter. Zip packages can be installed directly in Kibana >= 8.7.0. More details in this [HOWTO guide](https://github.com/elastic/elastic-package/blob/main/docs/howto/install_package.md).`
2720
28- var semver8_7_0 = semver .MustParse ("8.7.0" )
29-
3021func setupInstallCommand () * cobraext.Command {
3122 cmd := & cobra.Command {
3223 Use : "install" ,
@@ -60,23 +51,27 @@ func installCommandAction(cmd *cobra.Command, _ []string) error {
6051 if err != nil {
6152 return errors .Wrap (err , "could not create kibana client" )
6253 }
63- kibanaVersion , err := kibanaClient .Version ()
64- if err != nil {
65- return err
66- }
67- v , err := semver .NewVersion (kibanaVersion .Number )
68- if err != nil {
69- return fmt .Errorf ("invalid Kibana version" )
70- }
7154
72- installer , err := newInstaller (zipPathFile , packageRootPath , v , skipValidation )
73- if err != nil {
74- return err
55+ if zipPathFile == "" && packageRootPath == "" {
56+ var found bool
57+ var err error
58+ packageRootPath , found , err = packages .FindPackageRoot ()
59+ if ! found {
60+ return errors .New ("package root not found" )
61+ }
62+ if err != nil {
63+ return errors .Wrap (err , "locating package root failed" )
64+ }
7565 }
7666
77- manifest , err := installer .manifest ()
67+ installer , err := installer .NewForPackage (installer.Options {
68+ Kibana : kibanaClient ,
69+ RootPath : packageRootPath ,
70+ SkipValidation : skipValidation ,
71+ ZipPath : zipPathFile ,
72+ })
7873 if err != nil {
79- return err
74+ return errors . Wrap ( err , "package installation failed" )
8075 }
8176
8277 // Check conditions
@@ -85,6 +80,11 @@ func installCommandAction(cmd *cobra.Command, _ []string) error {
8580 return errors .Wrap (err , "can't process check-condition flag" )
8681 }
8782 if len (keyValuePairs ) > 0 {
83+ manifest , err := installer .Manifest ()
84+ if err != nil {
85+ return err
86+ }
87+
8888 cmd .Println ("Check conditions for package" )
8989 err = packages .CheckConditions (* manifest , keyValuePairs )
9090 if err != nil {
@@ -95,116 +95,6 @@ func installCommandAction(cmd *cobra.Command, _ []string) error {
9595 return nil
9696 }
9797
98- return installer .install (cmd , manifest .Name , manifest .Version )
99- }
100-
101- type packageInstaller interface {
102- manifest () (* packages.PackageManifest , error )
103- install (cmd * cobra.Command , name , version string ) error
104- }
105-
106- func newInstaller (zipPath , packageRootPath string , kibanaVersion * semver.Version , skipValidation bool ) (packageInstaller , error ) {
107- if zipPath != "" {
108- if kibanaVersion .LessThan (semver8_7_0 ) {
109- return nil , fmt .Errorf ("not supported uploading zip packages in Kibana %s" , kibanaVersion )
110- }
111- if ! skipValidation {
112- logger .Debugf ("Validating built .zip package (path: %s)" , zipPath )
113- err := validator .ValidateFromZip (zipPath )
114- if err != nil {
115- return nil , errors .Wrapf (err , "invalid content found in built zip package" )
116- }
117- }
118- logger .Debug ("Skip validation of the built .zip package" )
119- return zipPackage {zipPath : zipPath }, nil
120- }
121- if packageRootPath == "" {
122- var found bool
123- var err error
124- packageRootPath , found , err = packages .FindPackageRoot ()
125- if ! found {
126- return nil , errors .New ("package root not found" )
127- }
128- if err != nil {
129- return nil , errors .Wrap (err , "locating package root failed" )
130- }
131- }
132-
133- if kibanaVersion .LessThan (semver8_7_0 ) {
134- return localPackage {rootPath : packageRootPath }, nil
135- }
136-
137- // build and install
138- target , err := builder .BuildPackage (builder.BuildOptions {
139- PackageRoot : packageRootPath ,
140- CreateZip : true ,
141- SignPackage : false ,
142- SkipValidation : skipValidation ,
143- })
144- if err != nil {
145- return nil , errors .Wrap (err , "building package failed" )
146- }
147- logger .Infof ("Built package path: %s" , target )
148-
149- return zipPackage {zipPath : target }, nil
150- }
151-
152- type localPackage struct {
153- rootPath string
154- }
155-
156- func (l localPackage ) manifest () (* packages.PackageManifest , error ) {
157- logger .Debugf ("Reading package manifest from %s" , l .rootPath )
158- manifest , err := packages .ReadPackageManifestFromPackageRoot (l .rootPath )
159- if err != nil {
160- return nil , errors .Wrapf (err , "reading package manifest failed (path: %s)" , l .rootPath )
161- }
162- return manifest , nil
163- }
164-
165- func (l localPackage ) install (cmd * cobra.Command , name , version string ) error {
166- aInstaller , err := installer .CreateForManifest (name , version )
167- if err != nil {
168- return errors .Wrap (err , "can't create the package installer" )
169- }
170-
171- cmd .Println ("Install the package" )
172- return installPackage (cmd , aInstaller )
173- }
174-
175- type zipPackage struct {
176- zipPath string
177- }
178-
179- func (z zipPackage ) manifest () (* packages.PackageManifest , error ) {
180- logger .Debugf ("Reading package manifest from %s" , z .zipPath )
181- manifest , err := packages .ReadPackageManifestFromZipPackage (z .zipPath )
182- if err != nil {
183- return nil , errors .Wrapf (err , "reading package manifest failed (path: %s)" , z .zipPath )
184- }
185- return manifest , nil
186- }
187-
188- func (z zipPackage ) install (cmd * cobra.Command , name , version string ) error {
189- aInstaller , err := installer .CreateForZip (z .zipPath , name , version )
190- if err != nil {
191- return errors .Wrap (err , "can't create the package installer" )
192- }
193-
194- cmd .Printf ("Install zip package: %s\n " , z .zipPath )
195- return installPackage (cmd , aInstaller )
196- }
197-
198- func installPackage (cmd * cobra.Command , packageInstaller installer.Installer ) error {
199- installedPackage , err := packageInstaller .Install ()
200- if err != nil {
201- return errors .Wrap (err , "can't install the package" )
202- }
203-
204- cmd .Println ("Installed assets:" )
205- for _ , asset := range installedPackage .Assets {
206- cmd .Printf ("- %s (type: %s)\n " , asset .ID , asset .Type )
207- }
208- cmd .Println ("Done" )
209- return nil
98+ _ , err = installer .Install ()
99+ return err
210100}
0 commit comments