Skip to content

Commit 221fb20

Browse files
Merge pull request #48 from T0MASD/ocm-addons
add ocm-addons utility
2 parents 960e553 + a928960 commit 221fb20

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

pkg/tools/ocmaddons/ocmaddons.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package ocmaddons
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
9+
gogithub "github.com/google/go-github/v51/github"
10+
"github.com/openshift/backplane-tools/pkg/sources/github"
11+
"github.com/openshift/backplane-tools/pkg/tools/base"
12+
"github.com/openshift/backplane-tools/pkg/utils"
13+
)
14+
15+
// Tool implements the interface to manage the 'ocm-addons' binary
16+
type Tool struct {
17+
base.Github
18+
}
19+
20+
func New() *Tool {
21+
t := &Tool{
22+
Github: base.Github{
23+
Default: base.NewDefaultWithExecutable("ocm-addons", "ocm-addons"),
24+
Source: github.NewSource("mt-sre", "ocm-addons"),
25+
},
26+
}
27+
return t
28+
}
29+
30+
func (t *Tool) Install() error {
31+
// Pull latest release from GH
32+
release, err := t.Source.FetchLatestRelease()
33+
if err != nil {
34+
return err
35+
}
36+
37+
matches := github.FindAssetsForArchAndOS(release.Assets)
38+
if len(matches) != 1 {
39+
return fmt.Errorf("unexpected number of assets found matching system spec: expected 1, got %d.\nMatching assets: %v", len(matches), matches)
40+
}
41+
ocmaddonsArchiveAsset := matches[0]
42+
43+
matches = github.FindAssetsContaining([]string{"checksums.txt"}, release.Assets)
44+
if len(matches) != 1 {
45+
return fmt.Errorf("unexpected number of checksum assets found: expected 1, got %d.\nMatching assets: %v", len(matches), matches)
46+
}
47+
checksumAsset := matches[0]
48+
49+
// Download the arch- & os-specific assets
50+
toolDir := t.ToolDir()
51+
versionedDir := filepath.Join(toolDir, release.GetTagName())
52+
err = os.MkdirAll(versionedDir, os.FileMode(0o755))
53+
if err != nil {
54+
return fmt.Errorf("failed to create version-specific directory '%s': %w", versionedDir, err)
55+
}
56+
57+
err = t.Source.DownloadReleaseAssets([]*gogithub.ReleaseAsset{checksumAsset, ocmaddonsArchiveAsset}, versionedDir)
58+
if err != nil {
59+
return fmt.Errorf("failed to download one or more assets: %w", err)
60+
}
61+
62+
// Verify checksum of downloaded assets
63+
ocmaddonsArchiveFilepath := filepath.Join(versionedDir, ocmaddonsArchiveAsset.GetName())
64+
binarySum, err := utils.Sha256sum(ocmaddonsArchiveFilepath)
65+
if err != nil {
66+
return fmt.Errorf("failed to calculate checksum for '%s': %w", ocmaddonsArchiveFilepath, err)
67+
}
68+
69+
checksumFilePath := filepath.Join(versionedDir, checksumAsset.GetName())
70+
checksumLine, err := utils.GetLineInFileMatchingKey(checksumFilePath, ocmaddonsArchiveAsset.GetName())
71+
if err != nil {
72+
return fmt.Errorf("failed to retrieve checksum from file '%s': %w", checksumFilePath, err)
73+
}
74+
checksumTokens := strings.Fields(checksumLine)
75+
if len(checksumTokens) != 2 {
76+
return fmt.Errorf("the checksum file '%s' is invalid: expected 2 fields, got %d", checksumFilePath, len(checksumTokens))
77+
}
78+
actual := checksumTokens[0]
79+
80+
toolExecutable := t.ExecutableName()
81+
if strings.TrimSpace(binarySum) != strings.TrimSpace(actual) {
82+
return fmt.Errorf("warning: Checksum for '%s' does not match the calculated value. Please retry installation. If issue persists, this tool can be downloaded manually at %s", toolExecutable, ocmaddonsArchiveAsset.GetBrowserDownloadURL())
83+
}
84+
85+
// Untar binary bundle
86+
err = utils.Unarchive(ocmaddonsArchiveFilepath, versionedDir)
87+
if err != nil {
88+
return fmt.Errorf("failed to unarchive the '%s' asset file '%s': %w", toolExecutable, ocmaddonsArchiveFilepath, err)
89+
}
90+
91+
// Link as latest
92+
latestFilePath := t.SymlinkPath()
93+
err = os.Remove(latestFilePath)
94+
if err != nil && !os.IsNotExist(err) {
95+
return fmt.Errorf("failed to remove existing '%s' binary at '%s': %w", toolExecutable, base.LatestDir, err)
96+
}
97+
98+
toolBinaryFilepath := filepath.Join(versionedDir, toolExecutable)
99+
err = os.Symlink(toolBinaryFilepath, latestFilePath)
100+
if err != nil {
101+
return fmt.Errorf("failed to link new '%s' binary to '%s': %w", toolExecutable, base.LatestDir, err)
102+
}
103+
return nil
104+
}

pkg/tools/tools.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/openshift/backplane-tools/pkg/tools/gcloud"
1313
"github.com/openshift/backplane-tools/pkg/tools/oc"
1414
"github.com/openshift/backplane-tools/pkg/tools/ocm"
15+
"github.com/openshift/backplane-tools/pkg/tools/ocmaddons"
1516
"github.com/openshift/backplane-tools/pkg/tools/osdctl"
1617
"github.com/openshift/backplane-tools/pkg/tools/rosa"
1718
"github.com/openshift/backplane-tools/pkg/tools/self"
@@ -68,6 +69,9 @@ func initMap() {
6869
ocm := ocm.New()
6970
toolMap[ocm.Name()] = ocm
7071

72+
ocmaddons := ocmaddons.New()
73+
toolMap[ocmaddons.Name()] = ocmaddons
74+
7175
osdctl := osdctl.New()
7276
toolMap[osdctl.Name()] = osdctl
7377

0 commit comments

Comments
 (0)