Skip to content

Commit c58f0a2

Browse files
committed
Merge commit '116f22083cdc2eb7f715104a0d4f32998ee43bb0' into e2e-tests
2 parents 2339141 + 116f220 commit c58f0a2

File tree

3 files changed

+60
-43
lines changed

3 files changed

+60
-43
lines changed

cmd/plugins/plugin.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616
package plugins
1717

1818
import (
19+
"errors"
1920
"fmt"
2021
"log/slog"
2122
"os"
@@ -27,7 +28,6 @@ import (
2728

2829
dkplog "github.com/deckhouse/deckhouse/pkg/log"
2930

30-
"github.com/deckhouse/deckhouse-cli/cmd/plugins/flags"
3131
"github.com/deckhouse/deckhouse-cli/pkg/registry/service"
3232
)
3333

@@ -40,7 +40,7 @@ const (
4040
func NewPluginCommand(commandName string, description string, aliases []string, logger *dkplog.Logger) *cobra.Command {
4141
pc := NewPluginsCommand(logger.Named("plugins-command"))
4242

43-
pluginContractFilePath := path.Join(flags.DeckhousePluginsDir, "cache", "contracts", "system.json")
43+
pluginContractFilePath := path.Join(pc.pluginDirectory, "cache", "contracts", "system.json")
4444
pluginContract, err := service.GetPluginContractFromFile(pluginContractFilePath)
4545
if err != nil {
4646
logger.Debug("failed to get plugin contract from cache", slog.String("error", err.Error()))
@@ -50,6 +50,27 @@ func NewPluginCommand(commandName string, description string, aliases []string,
5050
description = pluginContract.Description
5151
}
5252

53+
// to check we can create directories here
54+
// we try to create root plugins folder
55+
err = os.MkdirAll(pc.pluginDirectory+"/plugins", 0755)
56+
// if permission failed
57+
if errors.Is(err, os.ErrPermission) {
58+
pc.logger.Warn("use homedir instead of default d8 plugins path in '/opt/deckhouse/lib/deckhouse-cli'", slog.String("new_path", pc.pluginDirectory), dkplog.Err(err))
59+
60+
pc.pluginDirectory, err = os.UserHomeDir()
61+
if err != nil {
62+
logger.Warn("failed to receive home dir to create plugins dir", slog.String("error", err.Error()))
63+
return nil
64+
}
65+
66+
pc.pluginDirectory = path.Join(pc.pluginDirectory, ".deckhouse-cli")
67+
}
68+
69+
if err != nil {
70+
logger.Warn("failed to create plugin root directory", slog.String("error", err.Error()))
71+
return nil
72+
}
73+
5374
systemCmd := &cobra.Command{
5475
Use: commandName,
5576
Short: description,
@@ -61,11 +82,12 @@ func NewPluginCommand(commandName string, description string, aliases []string,
6182
pc.InitPluginServices()
6283
},
6384
Run: func(cmd *cobra.Command, args []string) {
64-
installed, err := checkInstalled(commandName)
85+
installed, err := pc.checkInstalled(commandName)
6586
if err != nil {
6687
fmt.Println("Error checking installed:", err)
6788
return
6889
}
90+
6991
if !installed {
7092
fmt.Println("Not installed, installing...")
7193
err = pc.InstallPlugin(cmd.Context(), commandName, "", -1)
@@ -76,15 +98,15 @@ func NewPluginCommand(commandName string, description string, aliases []string,
7698
fmt.Println("Installed successfully")
7799
}
78100

79-
pluginPath := path.Join(flags.DeckhousePluginsDir, "plugins", commandName)
101+
pluginPath := path.Join(pc.pluginDirectory, "plugins", commandName)
80102
pluginBinaryPath := path.Join(pluginPath, "current")
81103
absPath, err := filepath.Abs(pluginBinaryPath)
82104
if err != nil {
83105
logger.Warn("failed to compute absolute path", slog.String("error", err.Error()))
84106
return
85107
}
86108

87-
logger.Info("Executing plugin", slog.Any("args", args))
109+
logger.Debug("Executing plugin", slog.Any("args", args))
88110

89111
command := exec.CommandContext(cmd.Context(), absPath, args...)
90112
command.Stdout = os.Stdout
@@ -100,8 +122,8 @@ func NewPluginCommand(commandName string, description string, aliases []string,
100122
return systemCmd
101123
}
102124

103-
func checkInstalled(commandName string) (bool, error) {
104-
installedFile := path.Join(flags.DeckhousePluginsDir, "plugins", commandName, "current")
125+
func (pc *PluginsCommand) checkInstalled(commandName string) (bool, error) {
126+
installedFile := path.Join(pc.pluginDirectory, "plugins", commandName, "current")
105127
absPath, err := filepath.Abs(installedFile)
106128
if err != nil {
107129
return false, fmt.Errorf("failed to compute absolute path: %w", err)

cmd/plugins/plugins.go

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import (
4343
type PluginsCommand struct {
4444
service *service.PluginService
4545
pluginRegistryClient registry.Client
46+
pluginDirectory string
4647

4748
logger *dkplog.Logger
4849
}
@@ -63,7 +64,8 @@ type pluginsListData struct {
6364

6465
func NewPluginsCommand(logger *dkplog.Logger) *PluginsCommand {
6566
return &PluginsCommand{
66-
logger: logger,
67+
pluginDirectory: flags.DeckhousePluginsDir,
68+
logger: logger,
6769
}
6870
}
6971

@@ -77,6 +79,20 @@ func NewCommand(logger *dkplog.Logger) *cobra.Command {
7779
PersistentPreRun: func(_ *cobra.Command, _ []string) {
7880
// init plugin services for subcommands after flags are parsed
7981
pc.InitPluginServices()
82+
83+
err := os.MkdirAll(flags.DeckhousePluginsDir+"/plugins", 0755)
84+
// if permission failed
85+
if errors.Is(err, os.ErrPermission) {
86+
pc.logger.Warn("use homedir instead of default d8 plugins path in '/opt/deckhouse/lib/deckhouse-cli'", slog.String("new_path", flags.DeckhousePluginsDir), dkplog.Err(err))
87+
88+
newPluginDirectory, err := os.UserHomeDir()
89+
if err != nil {
90+
logger.Warn("failed to receive home dir to create plugins dir", slog.String("error", err.Error()))
91+
return
92+
}
93+
94+
pc.pluginDirectory = path.Join(newPluginDirectory, ".deckhouse-cli")
95+
}
8096
},
8197
}
8298

@@ -151,15 +167,15 @@ func (pc *PluginsCommand) preparePluginsListData(ctx context.Context, showInstal
151167

152168
// fetchInstalledPlugins retrieves installed plugins from filesystem
153169
func (pc *PluginsCommand) fetchInstalledPlugins() ([]pluginDisplayInfo, error) {
154-
plugins, err := os.ReadDir(path.Join(flags.DeckhousePluginsDir, "plugins"))
170+
plugins, err := os.ReadDir(path.Join(pc.pluginDirectory, "plugins"))
155171
if err != nil {
156172
return nil, fmt.Errorf("failed to read plugins directory: %w", err)
157173
}
158174

159175
res := make([]pluginDisplayInfo, 0, len(plugins))
160176

161177
for _, plugin := range plugins {
162-
pluginBinaryPath := path.Join(flags.DeckhousePluginsDir, "plugins", plugin.Name(), "current")
178+
pluginBinaryPath := path.Join(pc.pluginDirectory, "plugins", plugin.Name(), "current")
163179
cmd := exec.Command(pluginBinaryPath, "--version")
164180

165181
output, err := cmd.Output()
@@ -203,7 +219,7 @@ func (pc *PluginsCommand) fetchInstalledPlugins() ([]pluginDisplayInfo, error) {
203219
}
204220

205221
func (pc *PluginsCommand) getInstalledPluginContract(pluginName string) (*internal.Plugin, error) {
206-
contractFile := path.Join(flags.DeckhousePluginsDir, "cache", "contracts", pluginName+".json")
222+
contractFile := path.Join(pc.pluginDirectory, "cache", "contracts", pluginName+".json")
207223

208224
file, err := os.Open(contractFile)
209225
if err != nil {
@@ -519,29 +535,10 @@ func (pc *PluginsCommand) InstallPlugin(ctx context.Context, pluginName, version
519535
}
520536

521537
func (pc *PluginsCommand) installPlugin(ctx context.Context, pluginName string, version *semver.Version, useMajor int) error {
522-
// to check we can create directories here
523-
// we try to create root plugins folder
524-
err := os.MkdirAll(flags.DeckhousePluginsDir+"/plugins", 0755)
525-
// if permission failed
526-
if errors.Is(err, os.ErrPermission) {
527-
pc.logger.Warn("use homedir instead of default d8 plugins path in '/opt/deckhouse/lib/deckhouse-cli'", slog.String("new_path", flags.DeckhousePluginsDir), dkplog.Err(err))
528-
529-
flags.DeckhousePluginsDir, err = os.UserHomeDir()
530-
if err != nil {
531-
return fmt.Errorf("failed to receive home dir to create plugins dir: %w", err)
532-
}
533-
534-
flags.DeckhousePluginsDir = path.Join(flags.DeckhousePluginsDir, ".deckhouse-cli")
535-
}
536-
537-
if err != nil {
538-
return fmt.Errorf("failed to create plugin root directory: %w", err)
539-
}
540-
541538
// create plugin directory if it doesn't exist
542539
// example path: /opt/deckhouse/lib/deckhouse-cli/plugins/example-plugin
543-
pluginDir := path.Join(flags.DeckhousePluginsDir, "plugins", pluginName)
544-
err = os.MkdirAll(pluginDir, 0755)
540+
pluginDir := path.Join(pc.pluginDirectory, "plugins", pluginName)
541+
err := os.MkdirAll(pluginDir, 0755)
545542
if err != nil {
546543
return fmt.Errorf("failed to create plugin directory: %w", err)
547544
}
@@ -635,7 +632,7 @@ func (pc *PluginsCommand) installPlugin(ctx context.Context, pluginName string,
635632

636633
// cache contract
637634
// example path: /opt/deckhouse/lib/deckhouse-cli/cache/contracts
638-
contractDir := path.Join(flags.DeckhousePluginsDir, "cache", "contracts")
635+
contractDir := path.Join(pc.pluginDirectory, "cache", "contracts")
639636
err = os.MkdirAll(contractDir, 0755)
640637
if err != nil {
641638
return fmt.Errorf("failed to create contract directory: %w", err)
@@ -727,7 +724,7 @@ func (pc *PluginsCommand) pluginsUpdateAllCommand() *cobra.Command {
727724

728725
fmt.Println("Updating all installed plugins...")
729726

730-
plugins, err := os.ReadDir(path.Join(flags.DeckhousePluginsDir, "plugins"))
727+
plugins, err := os.ReadDir(path.Join(pc.pluginDirectory, "plugins"))
731728
if err != nil {
732729
return fmt.Errorf("failed to read plugins directory: %w", err)
733730
}
@@ -759,7 +756,7 @@ func (pc *PluginsCommand) pluginsRemoveCommand() *cobra.Command {
759756
pluginName := args[0]
760757
fmt.Printf("Removing plugin: %s\n", pluginName)
761758

762-
pluginDir := path.Join(flags.DeckhousePluginsDir, "plugins", pluginName)
759+
pluginDir := path.Join(pc.pluginDirectory, "plugins", pluginName)
763760
fmt.Printf("Removing plugin from: %s\n", pluginDir)
764761

765762
err := os.RemoveAll(pluginDir)
@@ -769,7 +766,7 @@ func (pc *PluginsCommand) pluginsRemoveCommand() *cobra.Command {
769766

770767
fmt.Println("Cleaning up plugin files...")
771768

772-
os.Remove(path.Join(flags.DeckhousePluginsDir, "cache", "contracts", pluginName+".json"))
769+
os.Remove(path.Join(pc.pluginDirectory, "cache", "contracts", pluginName+".json"))
773770

774771
fmt.Printf("✓ Plugin '%s' successfully removed!\n", pluginName)
775772

@@ -791,15 +788,15 @@ func (pc *PluginsCommand) pluginsRemoveAllCommand() *cobra.Command {
791788
RunE: func(_ *cobra.Command, _ []string) error {
792789
fmt.Println("Removing all installed plugins...")
793790

794-
plugins, err := os.ReadDir(path.Join(flags.DeckhousePluginsDir, "plugins"))
791+
plugins, err := os.ReadDir(path.Join(pc.pluginDirectory, "plugins"))
795792
if err != nil {
796793
return fmt.Errorf("failed to read plugins directory: %w", err)
797794
}
798795

799796
fmt.Println("Found", len(plugins), "plugins to remove:")
800797

801798
for _, plugin := range plugins {
802-
pluginDir := path.Join(flags.DeckhousePluginsDir, "plugins", plugin.Name())
799+
pluginDir := path.Join(pc.pluginDirectory, "plugins", plugin.Name())
803800
fmt.Printf("Removing plugin from: %s\n", pluginDir)
804801

805802
err := os.RemoveAll(pluginDir)
@@ -809,7 +806,7 @@ func (pc *PluginsCommand) pluginsRemoveAllCommand() *cobra.Command {
809806

810807
fmt.Printf("Cleaning up plugin files for '%s'...\n", plugin.Name())
811808

812-
os.Remove(path.Join(flags.DeckhousePluginsDir, "cache", "contracts", plugin.Name()+".json"))
809+
os.Remove(path.Join(pc.pluginDirectory, "cache", "contracts", plugin.Name()+".json"))
813810

814811
fmt.Printf("✓ Plugin '%s' successfully removed!\n", plugin.Name())
815812
}

pkg/registry/service/plugin_service.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,13 @@ func (s *PluginService) GetPluginContract(ctx context.Context, pluginName, tag s
8181
return nil, fmt.Errorf("no manifests found in index manifest")
8282
}
8383

84-
// hardcoded first
84+
// hardcoded first manifest (all contracts must be the same for all manifests)
8585
digest := indexManifest.GetManifests()[0].GetDigest()
8686
if digest.String() == "" {
8787
return nil, fmt.Errorf("no digest found in manifest")
8888
}
8989

90-
digestClient := pluginClient.WithSegment("meta")
91-
92-
digestManifestResult, err = digestClient.GetManifest(ctx, "@"+digest.String())
90+
digestManifestResult, err = pluginClient.GetManifest(ctx, "@"+digest.String())
9391
if err != nil {
9492
return nil, fmt.Errorf("failed to get manifest: %w", err)
9593
}

0 commit comments

Comments
 (0)