-
Notifications
You must be signed in to change notification settings - Fork 50
/
upgrade.go
130 lines (117 loc) · 3.82 KB
/
upgrade.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package upgrade
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/jenkins-x-plugins/jx-gitops/pkg/plugins"
"github.com/jenkins-x-plugins/jx-gitops/pkg/rootcmd"
"github.com/jenkins-x/jx-helpers/v3/pkg/cmdrunner"
"github.com/jenkins-x/jx-helpers/v3/pkg/cobras/helper"
"github.com/jenkins-x/jx-helpers/v3/pkg/cobras/templates"
"github.com/jenkins-x/jx-helpers/v3/pkg/extensions"
"github.com/jenkins-x/jx-helpers/v3/pkg/files"
"github.com/jenkins-x/jx-helpers/v3/pkg/homedir"
"github.com/jenkins-x/jx-helpers/v3/pkg/termcolor"
"github.com/jenkins-x/jx-logging/v3/pkg/log"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
cmdPluginsLong = templates.LongDesc(`
Upgrades the binary plugins for this plugin
`)
cmdPluginsExample = templates.Examples(`
# upgrades your plugin binaries for gitops
%s plugins upgrade
`)
)
// UpgradeOptions the options for upgrading a cluster
type Options struct {
CommandRunner cmdrunner.CommandRunner
Path string
}
// NewCmdUpgrade creates a command object for the command
func NewCmdUpgradePlugins() (*cobra.Command, *Options) {
o := &Options{}
cmd := &cobra.Command{
Use: "upgrade",
Short: "Upgrades the binary plugins for this plugin",
Long: cmdPluginsLong,
Example: fmt.Sprintf(cmdPluginsExample, rootcmd.BinaryName),
Run: func(cmd *cobra.Command, args []string) {
err := o.Run()
helper.CheckErr(err)
},
}
cmd.Flags().StringVarP(&o.Path, "path", "", "", "creates a symlink to the binary plugins in this bin path dir")
return cmd, o
}
// Run implements the command
func (o *Options) Run() error {
pluginBinDir, err := homedir.DefaultPluginBinDir()
if err != nil {
return errors.Wrap(err, "failed to find plugin bin directory")
}
if o.CommandRunner == nil {
o.CommandRunner = cmdrunner.DefaultCommandRunner
}
if o.Path != "" {
err = os.MkdirAll(o.Path, files.DefaultDirWritePermissions)
if err != nil {
return errors.Wrapf(err, "failed to make bin directory %s", o.Path)
}
}
for k := range plugins.Plugins {
p := plugins.Plugins[k]
log.Logger().Infof("checking binary jx plugin %s version %s is installed", termcolor.ColorInfo(p.Name), termcolor.ColorInfo(p.Spec.Version))
fileName, err := extensions.EnsurePluginInstalled(p, pluginBinDir)
if err != nil {
return errors.Wrapf(err, "failed to ensure plugin is installed %s", p.Name)
}
if o.Path != "" {
binName := filepath.Join(o.Path, p.Name)
exists, err := files.FileExists(binName)
if err != nil {
return errors.Wrapf(err, "failed to check if file exists %s", binName)
}
if exists {
err = os.Remove(binName)
if err != nil {
return errors.Wrapf(err, "failed to remove existing file %s", binName)
}
}
err = os.Symlink(fileName, binName)
if err != nil {
return errors.Wrapf(err, "failed to create symlink from %s => %s", fileName, binName)
}
log.Logger().Infof("created symlink from %s => %s", fileName, binName)
}
goarch := strings.ToLower(runtime.GOARCH)
if p.Name == plugins.HelmPluginName && goarch != "arm" && !strings.HasPrefix(goarch, "arm") {
log.Logger().Infof("checking if we have installed the helm plugin helm-x on OS %s arch: %s", runtime.GOOS, goarch)
// we can't install helm-x yet on ARM
installHelmX := &cmdrunner.Command{
Name: fileName,
Args: []string{"plugin", "install", "https://github.com/mumoshu/helm-x"},
}
out, err := o.CommandRunner(installHelmX)
if err != nil {
if strings.Contains(out, "plugin already exists") {
updateHelmX := &cmdrunner.Command{
Name: fileName,
Args: []string{"plugin", "update", "x"},
}
out, err = o.CommandRunner(updateHelmX)
if err != nil {
return errors.Wrapf(err, "failed to update plugin helm-x")
}
} else {
return errors.Wrapf(err, "failed to install plugin helm-x")
}
}
}
}
return nil
}