Skip to content

Commit 116f220

Browse files
[deckhouse-cli] fix: Plugins fallback to use homedir (#258)
Signed-off-by: Smyslov Maxim <[email protected]> Signed-off-by: Pavel Okhlopkov <[email protected]> Co-authored-by: Pavel Okhlopkov <[email protected]>
1 parent da83be6 commit 116f220

File tree

3 files changed

+42
-27
lines changed

3 files changed

+42
-27
lines changed

cmd/plugins/plugin.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828

2929
dkplog "github.com/deckhouse/deckhouse/pkg/log"
3030

31-
"github.com/deckhouse/deckhouse-cli/cmd/plugins/flags"
3231
"github.com/deckhouse/deckhouse-cli/pkg/registry/service"
3332
)
3433

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

44-
pluginContractFilePath := path.Join(flags.DeckhousePluginsDir, "cache", "contracts", "system.json")
43+
pluginContractFilePath := path.Join(pc.pluginDirectory, "cache", "contracts", "system.json")
4544
pluginContract, err := service.GetPluginContractFromFile(pluginContractFilePath)
4645
if err != nil {
4746
logger.Debug("failed to get plugin contract from cache", slog.String("error", err.Error()))
@@ -53,18 +52,18 @@ func NewPluginCommand(commandName string, description string, aliases []string,
5352

5453
// to check we can create directories here
5554
// we try to create root plugins folder
56-
err = os.MkdirAll(flags.DeckhousePluginsDir+"/plugins", 0755)
55+
err = os.MkdirAll(pc.pluginDirectory+"/plugins", 0755)
5756
// if permission failed
5857
if errors.Is(err, os.ErrPermission) {
59-
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))
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))
6059

61-
flags.DeckhousePluginsDir, err = os.UserHomeDir()
60+
pc.pluginDirectory, err = os.UserHomeDir()
6261
if err != nil {
6362
logger.Warn("failed to receive home dir to create plugins dir", slog.String("error", err.Error()))
6463
return nil
6564
}
6665

67-
flags.DeckhousePluginsDir = path.Join(flags.DeckhousePluginsDir, ".deckhouse-cli")
66+
pc.pluginDirectory = path.Join(pc.pluginDirectory, ".deckhouse-cli")
6867
}
6968

7069
if err != nil {
@@ -83,11 +82,12 @@ func NewPluginCommand(commandName string, description string, aliases []string,
8382
pc.InitPluginServices()
8483
},
8584
Run: func(cmd *cobra.Command, args []string) {
86-
installed, err := checkInstalled(commandName)
85+
installed, err := pc.checkInstalled(commandName)
8786
if err != nil {
8887
fmt.Println("Error checking installed:", err)
8988
return
9089
}
90+
9191
if !installed {
9292
fmt.Println("Not installed, installing...")
9393
err = pc.InstallPlugin(cmd.Context(), commandName, "", -1)
@@ -98,15 +98,15 @@ func NewPluginCommand(commandName string, description string, aliases []string,
9898
fmt.Println("Installed successfully")
9999
}
100100

101-
pluginPath := path.Join(flags.DeckhousePluginsDir, "plugins", commandName)
101+
pluginPath := path.Join(pc.pluginDirectory, "plugins", commandName)
102102
pluginBinaryPath := path.Join(pluginPath, "current")
103103
absPath, err := filepath.Abs(pluginBinaryPath)
104104
if err != nil {
105105
logger.Warn("failed to compute absolute path", slog.String("error", err.Error()))
106106
return
107107
}
108108

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

111111
command := exec.CommandContext(cmd.Context(), absPath, args...)
112112
command.Stdout = os.Stdout
@@ -122,8 +122,8 @@ func NewPluginCommand(commandName string, description string, aliases []string,
122122
return systemCmd
123123
}
124124

125-
func checkInstalled(commandName string) (bool, error) {
126-
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")
127127
absPath, err := filepath.Abs(installedFile)
128128
if err != nil {
129129
return false, fmt.Errorf("failed to compute absolute path: %w", err)

cmd/plugins/plugins.go

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package plugins
1919
import (
2020
"context"
2121
"encoding/json"
22+
"errors"
2223
"fmt"
2324
"log/slog"
2425
"os"
@@ -42,6 +43,7 @@ import (
4243
type PluginsCommand struct {
4344
service *service.PluginService
4445
pluginRegistryClient registry.Client
46+
pluginDirectory string
4547

4648
logger *dkplog.Logger
4749
}
@@ -62,7 +64,8 @@ type pluginsListData struct {
6264

6365
func NewPluginsCommand(logger *dkplog.Logger) *PluginsCommand {
6466
return &PluginsCommand{
65-
logger: logger,
67+
pluginDirectory: flags.DeckhousePluginsDir,
68+
logger: logger,
6669
}
6770
}
6871

@@ -76,6 +79,20 @@ func NewCommand(logger *dkplog.Logger) *cobra.Command {
7679
PersistentPreRun: func(_ *cobra.Command, _ []string) {
7780
// init plugin services for subcommands after flags are parsed
7881
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+
}
7996
},
8097
}
8198

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

151168
// fetchInstalledPlugins retrieves installed plugins from filesystem
152169
func (pc *PluginsCommand) fetchInstalledPlugins() ([]pluginDisplayInfo, error) {
153-
plugins, err := os.ReadDir(path.Join(flags.DeckhousePluginsDir, "plugins"))
170+
plugins, err := os.ReadDir(path.Join(pc.pluginDirectory, "plugins"))
154171
if err != nil {
155172
return nil, fmt.Errorf("failed to read plugins directory: %w", err)
156173
}
157174

158175
res := make([]pluginDisplayInfo, 0, len(plugins))
159176

160177
for _, plugin := range plugins {
161-
pluginBinaryPath := path.Join(flags.DeckhousePluginsDir, "plugins", plugin.Name(), "current")
178+
pluginBinaryPath := path.Join(pc.pluginDirectory, "plugins", plugin.Name(), "current")
162179
cmd := exec.Command(pluginBinaryPath, "--version")
163180

164181
output, err := cmd.Output()
@@ -202,7 +219,7 @@ func (pc *PluginsCommand) fetchInstalledPlugins() ([]pluginDisplayInfo, error) {
202219
}
203220

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

207224
file, err := os.Open(contractFile)
208225
if err != nil {
@@ -520,7 +537,7 @@ func (pc *PluginsCommand) InstallPlugin(ctx context.Context, pluginName, version
520537
func (pc *PluginsCommand) installPlugin(ctx context.Context, pluginName string, version *semver.Version, useMajor int) error {
521538
// create plugin directory if it doesn't exist
522539
// example path: /opt/deckhouse/lib/deckhouse-cli/plugins/example-plugin
523-
pluginDir := path.Join(flags.DeckhousePluginsDir, "plugins", pluginName)
540+
pluginDir := path.Join(pc.pluginDirectory, "plugins", pluginName)
524541
err := os.MkdirAll(pluginDir, 0755)
525542
if err != nil {
526543
return fmt.Errorf("failed to create plugin directory: %w", err)
@@ -615,7 +632,7 @@ func (pc *PluginsCommand) installPlugin(ctx context.Context, pluginName string,
615632

616633
// cache contract
617634
// example path: /opt/deckhouse/lib/deckhouse-cli/cache/contracts
618-
contractDir := path.Join(flags.DeckhousePluginsDir, "cache", "contracts")
635+
contractDir := path.Join(pc.pluginDirectory, "cache", "contracts")
619636
err = os.MkdirAll(contractDir, 0755)
620637
if err != nil {
621638
return fmt.Errorf("failed to create contract directory: %w", err)
@@ -707,7 +724,7 @@ func (pc *PluginsCommand) pluginsUpdateAllCommand() *cobra.Command {
707724

708725
fmt.Println("Updating all installed plugins...")
709726

710-
plugins, err := os.ReadDir(path.Join(flags.DeckhousePluginsDir, "plugins"))
727+
plugins, err := os.ReadDir(path.Join(pc.pluginDirectory, "plugins"))
711728
if err != nil {
712729
return fmt.Errorf("failed to read plugins directory: %w", err)
713730
}
@@ -739,7 +756,7 @@ func (pc *PluginsCommand) pluginsRemoveCommand() *cobra.Command {
739756
pluginName := args[0]
740757
fmt.Printf("Removing plugin: %s\n", pluginName)
741758

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

745762
err := os.RemoveAll(pluginDir)
@@ -749,7 +766,7 @@ func (pc *PluginsCommand) pluginsRemoveCommand() *cobra.Command {
749766

750767
fmt.Println("Cleaning up plugin files...")
751768

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

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

@@ -771,15 +788,15 @@ func (pc *PluginsCommand) pluginsRemoveAllCommand() *cobra.Command {
771788
RunE: func(_ *cobra.Command, _ []string) error {
772789
fmt.Println("Removing all installed plugins...")
773790

774-
plugins, err := os.ReadDir(path.Join(flags.DeckhousePluginsDir, "plugins"))
791+
plugins, err := os.ReadDir(path.Join(pc.pluginDirectory, "plugins"))
775792
if err != nil {
776793
return fmt.Errorf("failed to read plugins directory: %w", err)
777794
}
778795

779796
fmt.Println("Found", len(plugins), "plugins to remove:")
780797

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

785802
err := os.RemoveAll(pluginDir)
@@ -789,7 +806,7 @@ func (pc *PluginsCommand) pluginsRemoveAllCommand() *cobra.Command {
789806

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

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

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

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)