/
helpers.go
186 lines (166 loc) · 5.47 KB
/
helpers.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package plugins
import (
"fmt"
"io"
"net/http"
"runtime"
"strings"
jenkinsv1 "github.com/jenkins-x/jx-api/v4/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx-helpers/v3/pkg/extensions"
"github.com/jenkins-x/jx-helpers/v3/pkg/homedir"
"github.com/jenkins-x/jx-helpers/v3/pkg/httphelpers"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/json"
)
const (
jenkinsxOrganisation = "jenkins-x"
jenkinsxPluginsOrganisation = "jenkins-x-plugins"
// OctantPluginName the default name of the octant plugin
OctantPluginName = "octant"
// OctantJXPluginName the name of the octant-jx plugin
OctantJXPluginName = "octant-jx"
// OctantJXOPluginName the name of the octant-jxo plugin
OctantJXOPluginName = "octant-jxo"
)
// GetJXPlugin returns the path to the locally installed jx plugin
func GetJXPlugin(name, version string) (string, error) {
pluginBinDir, err := homedir.DefaultPluginBinDir()
if err != nil {
return "", err
}
plugin := extensions.CreateJXPlugin(jenkinsxOrganisation, name, version)
return extensions.EnsurePluginInstalled(plugin, pluginBinDir)
}
// GetOctantBinary returns the path to the locally installed octant plugin
func GetOctantBinary(version string) (string, error) {
if version == "" {
version = OctantVersion
}
pluginBinDir, err := homedir.DefaultPluginBinDir()
if err != nil {
return "", err
}
plugin := CreateOctantPlugin(version)
return extensions.EnsurePluginInstalled(plugin, pluginBinDir)
}
// CreateOctantPlugin creates the helm 3 plugin
func CreateOctantPlugin(version string) jenkinsv1.Plugin {
binaries := extensions.CreateBinaries(func(p extensions.Platform) string {
kind := strings.ToLower(p.Goarch)
if strings.HasSuffix(kind, "64") {
kind = "64bit"
}
goos := p.Goos
if goos == "Darwin" {
goos = "macOS"
}
return fmt.Sprintf("https://github.com/vmware-tanzu/octant/releases/download/v%s/octant_%s_%s-%s.%s", version, version, goos, kind, p.Extension())
})
plugin := jenkinsv1.Plugin{
ObjectMeta: metav1.ObjectMeta{
Name: OctantPluginName,
},
Spec: jenkinsv1.PluginSpec{
SubCommand: "octant",
Binaries: binaries,
Description: "octant binary",
Name: OctantPluginName,
Version: version,
},
}
return plugin
}
// GetOctantJXBinary returns the path to the locally installed octant-jx extension
func GetOctantJXBinary(version string) (string, error) {
if version == "" {
version = OctantJXVersion
}
pluginBinDir, err := homedir.DefaultPluginBinDir()
if err != nil {
return "", err
}
plugin := CreateOctantJXPlugin(version)
return extensions.EnsurePluginInstalled(plugin, pluginBinDir)
}
// CreateOctantJXPlugin creates the helm 3 plugin
func CreateOctantJXPlugin(version string) jenkinsv1.Plugin {
binaries := extensions.CreateBinaries(func(p extensions.Platform) string {
return fmt.Sprintf("https://github.com/jenkins-x-plugins/octant-jx/releases/download/v%s/octant-jx-%s-%s.%s", version, strings.ToLower(p.Goos), strings.ToLower(p.Goarch), p.Extension())
})
plugin := jenkinsv1.Plugin{
ObjectMeta: metav1.ObjectMeta{
Name: OctantJXPluginName,
},
Spec: jenkinsv1.PluginSpec{
SubCommand: "octant-jx",
Binaries: binaries,
Description: "octant plugin for Jenkins X",
Name: OctantJXPluginName,
Version: version,
},
}
return plugin
}
// GetOctantJXOBinary returns the path to the locally installed helmAnnotate extension
func GetOctantJXOBinary(version string) (string, error) {
if version == "" {
version = OctantJXVersion
}
pluginBinDir, err := homedir.DefaultPluginBinDir()
if err != nil {
return "", err
}
plugin := CreateOctantJXOPlugin(version)
aliasFileName := "ha.tar.gz"
if runtime.GOOS == "windows" {
aliasFileName = "ha.zip"
}
return extensions.EnsurePluginInstalledForAliasFile(plugin, pluginBinDir, aliasFileName)
}
// CreateOctantJXOPlugin creates the octant-ojx plugin
func CreateOctantJXOPlugin(version string) jenkinsv1.Plugin {
plugin := CreateOctantJXPlugin(version)
plugin.Name = OctantJXOPluginName
plugin.Spec.Name = OctantJXOPluginName
plugin.Spec.SubCommand = OctantJXOPluginName
return plugin
}
type githubRelease struct {
TagName string `json:"tag_name"`
}
// InstallStandardPlugin makes sure that latest version of plugin is installed and returns the path to the binary
func InstallStandardPlugin(dir, name string) (string, error) {
u := "https://api.github.com/repos/jenkins-x-plugins/" + name + "/releases/latest"
client := httphelpers.GetClient()
req, err := http.NewRequest("GET", u, http.NoBody)
if err != nil {
return "", errors.Wrapf(err, "failed to create http request for %s", u)
}
resp, err := client.Do(req)
if err != nil {
if resp != nil {
return "", errors.Wrapf(err, "failed to GET endpoint %s with status %s", u, resp.Status)
}
return "", errors.Wrapf(err, "failed to GET endpoint %s", u)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", errors.Wrapf(err, "failed to read response from %s", u)
}
release := &githubRelease{}
err = json.Unmarshal(body, release)
if err != nil {
return "", errors.Wrapf(err, "failed to unmarshal release from %s", u)
}
latestVersion := strings.TrimPrefix(release.TagName, "v")
if latestVersion == "" {
return "", fmt.Errorf("can't find latest version of plugin: %s", body)
}
plugin := extensions.CreateJXPlugin("jenkins-x-plugins", strings.TrimPrefix(name, "jx-"), latestVersion)
if err != nil {
return "", err
}
return extensions.EnsurePluginInstalled(plugin, dir)
}