Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions src/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"time"

"github.com/briandowns/spinner"
"github.com/containers/toolbox/pkg/architecture"
"github.com/containers/toolbox/pkg/podman"
"github.com/containers/toolbox/pkg/shell"
"github.com/containers/toolbox/pkg/skopeo"
Expand Down Expand Up @@ -174,7 +175,8 @@ func create(cmd *cobra.Command, args []string) error {
containerArg,
createFlags.distro,
createFlags.image,
createFlags.release)
createFlags.release,
architecture.HostArchID)

if err != nil {
return err
Expand Down Expand Up @@ -486,19 +488,15 @@ func createContainer(container, image, release, authFile string, showCommandToEn
logrus.Debugf("%s", arg)
}

s := spinner.New(spinner.CharSets[9], 500*time.Millisecond, spinner.WithWriterFile(os.Stdout))
if logLevel := logrus.GetLevel(); logLevel < logrus.DebugLevel {
s.Prefix = fmt.Sprintf("Creating container %s: ", container)
s.Start()
defer s.Stop()
}
s := startSpinner(fmt.Sprintf("Creating container %s: ", container))
defer stopSpinner(s)

if err := shell.Run("podman", nil, nil, nil, createArgs...); err != nil {
return fmt.Errorf("failed to create container %s", container)
}

// The spinner must be stopped before showing the 'enter' hint below.
s.Stop()
stopSpinner(s)

if showCommandToEnter {
fmt.Printf("Created container: %s\n", container)
Expand Down Expand Up @@ -568,7 +566,7 @@ func getEnterCommand(container string) string {
}

func getImageSizeFromRegistry(ctx context.Context, imageFull string) (string, error) {
image, err := skopeo.Inspect(ctx, imageFull)
image, err := skopeo.Inspect(ctx, imageFull, architecture.HostArchID, "")
if err != nil {
return "", err
}
Expand Down Expand Up @@ -735,12 +733,8 @@ func pullImage(image, release, authFile string) (bool, error) {

logrus.Debugf("Pulling image %s", imageFull)

if logLevel := logrus.GetLevel(); logLevel < logrus.DebugLevel {
s := spinner.New(spinner.CharSets[9], 500*time.Millisecond, spinner.WithWriterFile(os.Stdout))
s.Prefix = fmt.Sprintf("Pulling %s: ", imageFull)
s.Start()
defer s.Stop()
}
s := startSpinner(fmt.Sprintf("Pulling %s: ", imageFull))
defer stopSpinner(s)

if err := podman.Pull(imageFull, authFile); err != nil {
var builder strings.Builder
Expand Down Expand Up @@ -963,6 +957,22 @@ func showPromptForDownload(imageFull string) bool {
return shouldPullImage
}

func startSpinner(message string) *spinner.Spinner {
if logLevel := logrus.GetLevel(); logLevel < logrus.DebugLevel {
s := spinner.New(spinner.CharSets[9], 500*time.Millisecond, spinner.WithWriterFile(os.Stdout))
s.Prefix = message
s.Start()
return s
}
return nil
}

func stopSpinner(s *spinner.Spinner) {
if s != nil {
s.Stop()
}
}

// systemdNeedsEscape checks whether a byte in a potential dbus ObjectPath needs to be escaped
func systemdNeedsEscape(i int, b byte) bool {
// Escape everything that is not a-z-A-Z-0-9
Expand Down
4 changes: 3 additions & 1 deletion src/cmd/enter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"os"

"github.com/containers/toolbox/pkg/architecture"
"github.com/containers/toolbox/pkg/utils"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -108,7 +109,8 @@ func enter(cmd *cobra.Command, args []string) error {
containerArg,
enterFlags.distro,
"",
enterFlags.release)
enterFlags.release,
architecture.HostArchID)

if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/rootMigrationPath.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"os"
"strings"

"github.com/containers/toolbox/pkg/architecture"
"github.com/containers/toolbox/pkg/utils"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -56,7 +57,7 @@ func rootRunImpl(cmd *cobra.Command, args []string) error {
return &exitError{exitCode, err}
}

container, image, release, err := resolveContainerAndImageNames("", "", "", "", "")
container, image, release, err := resolveContainerAndImageNames("", "", "", "", "", architecture.HostArchID)
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion src/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"syscall"
"time"

"github.com/containers/toolbox/pkg/architecture"
"github.com/containers/toolbox/pkg/nvidia"
"github.com/containers/toolbox/pkg/podman"
"github.com/containers/toolbox/pkg/shell"
Expand Down Expand Up @@ -145,7 +146,8 @@ func run(cmd *cobra.Command, args []string) error {
"--container",
runFlags.distro,
"",
runFlags.release)
runFlags.release,
architecture.HostArchID)

if err != nil {
return err
Expand Down
72 changes: 71 additions & 1 deletion src/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"strings"
"syscall"

"github.com/containers/toolbox/pkg/architecture"
"github.com/containers/toolbox/pkg/shell"
"github.com/containers/toolbox/pkg/utils"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -261,6 +262,18 @@ func discardInputAsync(ctx context.Context) (<-chan int, <-chan error) {
return retValCh, errCh
}

func createErrorConflictingArchSpecs(archCLI, archTag int) error {
var builder strings.Builder
fmt.Fprintf(&builder, "conflicting architecture specifications\n")
fmt.Fprintf(&builder, "--arch=%s but image tag specifies %s\n",
architecture.GetArchNameOCI(archCLI),
architecture.GetArchNameOCI(archTag))
fmt.Fprintf(&builder, "Run '%s --help' for usage.", executableBase)

errMsg := builder.String()
return errors.New(errMsg)
}

func createErrorContainerNotFound(container string) error {
var builder strings.Builder
fmt.Fprintf(&builder, "container %s not found\n", container)
Expand Down Expand Up @@ -483,9 +496,40 @@ func poll(pollFn pollFunc, eventFD int32, fds ...int32) error {
}
}

func resolveContainerAndImageNames(container, containerArg, distroCLI, imageCLI, releaseCLI string) (
func resolveArchitectureID(arch string, image string) (int, error) {
archID := architecture.NotSpecified
if arch != "" {
archIDParsed, err := architecture.ParseArgArchValue(arch)
if err != nil {
return architecture.NotSpecified, err
}
archID = archIDParsed
}

if image != "" && utils.IsSupportedDistroImage(image) {
archIDFromTag := architecture.ImageReferenceGetArchFromTag(image)

if archID == architecture.NotSpecified && archIDFromTag != architecture.NotSpecified {
logrus.Debug("non-native architecture was detected in the image tag -> cross-architecture approach is going to be used")

archID = archIDFromTag
} else if archID != archIDFromTag && archIDFromTag != architecture.NotSpecified {
return architecture.NotSpecified, createErrorConflictingArchSpecs(archID, archIDFromTag)
}
}

if archID == architecture.NotSpecified {
archID = architecture.HostArchID
}

return archID, nil
}

func resolveContainerAndImageNames(container, containerArg, distroCLI, imageCLI, releaseCLI string, archID int) (
string, string, string, error,
) {
containerWasEmpty := container == ""

container, image, release, err := utils.ResolveContainerAndImageNames(container,
distroCLI,
imageCLI,
Expand Down Expand Up @@ -540,9 +584,35 @@ func resolveContainerAndImageNames(container, containerArg, distroCLI, imageCLI,
}
}

if containerWasEmpty && !architecture.HasContainerNativeArch(archID) {
archIDFromTag := architecture.ImageReferenceGetArchFromTag(image)

if archIDFromTag == architecture.NotSpecified {
archName := architecture.GetArchNameOCI(archID)
if archName != "" {
container = container + "-" + archName
}
}
}

return container, image, release, nil
}

func resolveImageNameWithArchitectureSuffix(image string, archID int) string {
if architecture.HasContainerNativeArch(archID) {
return image
}

archIDFromTag := architecture.ImageReferenceGetArchFromTag(image)
isSupportedDistroImage := utils.IsSupportedDistroImage(image)

if isSupportedDistroImage && archIDFromTag == architecture.NotSpecified {
return image + "-" + architecture.GetArchNameOCI(archID)
}

return image
}

// showManual tries to open the specified manual page using man on stdout
func showManual(manual string) error {
manBinary, err := exec.LookPath("man")
Expand Down
Loading
Loading