Skip to content

Commit b768fca

Browse files
committed
Move execute() function to a common location
1 parent 2f8d547 commit b768fca

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

pkg/asset/agent/common.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package agent
22

33
import (
4+
"github.com/sirupsen/logrus"
5+
46
hiveext "github.com/openshift/assisted-service/api/hiveextension/v1beta1"
57
"github.com/openshift/installer/pkg/types"
68
"github.com/openshift/installer/pkg/types/baremetal"
@@ -56,3 +58,25 @@ func IsSupportedPlatform(platform hiveext.PlatformType) bool {
5658
}
5759
return false
5860
}
61+
62+
// DetermineReleaseImageArch returns the arch of the release image.
63+
func DetermineReleaseImageArch(pullSecret, pullSpec string) (string, error) {
64+
templateFilter := "-o=go-template={{if and .metadata.metadata (index . \"metadata\" \"metadata\" \"release.openshift.io/architecture\")}}{{index . \"metadata\" \"metadata\" \"release.openshift.io/architecture\"}}{{else}}{{.config.architecture}}{{end}}"
65+
66+
var getReleaseArch = []string{
67+
"oc",
68+
"adm",
69+
"release",
70+
"info",
71+
pullSpec,
72+
templateFilter,
73+
}
74+
75+
releaseArch, err := ExecuteOC(pullSecret, getReleaseArch)
76+
if err != nil {
77+
logrus.Errorf("Release Image arch could not be found: %s", err)
78+
return "", err
79+
}
80+
logrus.Debugf("Release Image arch is: %s", releaseArch)
81+
return releaseArch, nil
82+
}

pkg/asset/agent/oc.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package agent
2+
3+
import (
4+
"bytes"
5+
"errors"
6+
"fmt"
7+
"os"
8+
"os/exec"
9+
"strings"
10+
)
11+
12+
// ExecuteOC will execute an oc command.
13+
func ExecuteOC(pullSecret string, command []string) (string, error) {
14+
// create registry config
15+
ps, err := os.CreateTemp("", "registry-config")
16+
if err != nil {
17+
return "", err
18+
}
19+
defer func() {
20+
ps.Close()
21+
os.Remove(ps.Name())
22+
}()
23+
_, err = ps.Write([]byte(pullSecret))
24+
if err != nil {
25+
return "", err
26+
}
27+
// flush the buffer to ensure the file can be read
28+
ps.Close()
29+
registryConfig := "--registry-config=" + ps.Name()
30+
command = append(command, registryConfig)
31+
var stdoutBytes, stderrBytes bytes.Buffer
32+
cmd := exec.Command(command[0], command[1:]...) // #nosec G204
33+
cmd.Stdout = &stdoutBytes
34+
cmd.Stderr = &stderrBytes
35+
36+
err = cmd.Run()
37+
if err == nil {
38+
return strings.TrimSpace(stdoutBytes.String()), nil
39+
}
40+
41+
var exitErr *exec.ExitError
42+
if errors.As(err, &exitErr) {
43+
err = fmt.Errorf("command '%s' exited with non-zero exit code %d: %s\n%s", command, exitErr.ExitCode(), stdoutBytes.String(), stderrBytes.String())
44+
} else {
45+
err = fmt.Errorf("command '%s' failed: %w", command, err)
46+
}
47+
return "", err
48+
}

0 commit comments

Comments
 (0)