From 75a8f13c4a1cbe33c931c690eda4923770e1f5f2 Mon Sep 17 00:00:00 2001 From: Jake Correnti Date: Tue, 1 Aug 2023 16:00:31 -0400 Subject: [PATCH] Move `waitAPIAndPrintInfo` to common file Moves `waitAPIAndPrintInfo` into the common file `pkg/machine/machine_common.go` allowing applehv and qemu to share the code. Signed-off-by: Jake Correnti --- pkg/machine/applehv/machine.go | 76 ++++-------------------------- pkg/machine/machine_common.go | 68 +++++++++++++++++++++++++++ pkg/machine/qemu/machine.go | 86 +++++++--------------------------- 3 files changed, 95 insertions(+), 135 deletions(-) diff --git a/pkg/machine/applehv/machine.go b/pkg/machine/applehv/machine.go index 6677712c9f..68b93ae76f 100644 --- a/pkg/machine/applehv/machine.go +++ b/pkg/machine/applehv/machine.go @@ -688,7 +688,15 @@ func (m *MacMachine) Start(name string, opts machine.StartOptions) error { } logrus.Debug("ready notification received") - m.waitAPIAndPrintInfo(forwardState, forwardSock, opts.NoInfo) + machine.WaitAPIAndPrintInfo( + forwardState, + m.Name, + findClaimHelper(), + forwardSock, + opts.NoInfo, + m.isIncompatible(), + m.Rootful, + ) return nil } @@ -1074,72 +1082,6 @@ func (m *MacMachine) userGlobalSocketLink() (string, error) { return filepath.Join(filepath.Dir(path), "podman.sock"), err } -func (m *MacMachine) waitAPIAndPrintInfo(forwardState machine.APIForwardingState, forwardSock string, noInfo bool) { - suffix := "" - if m.Name != machine.DefaultMachineName { - suffix = " " + m.Name - } - - if m.isIncompatible() { - fmt.Fprintf(os.Stderr, "\n!!! ACTION REQUIRED: INCOMPATIBLE MACHINE !!!\n") - - fmt.Fprintf(os.Stderr, "\nThis machine was created by an older Podman release that is incompatible\n") - fmt.Fprintf(os.Stderr, "with this release of Podman. It has been started in a limited operational\n") - fmt.Fprintf(os.Stderr, "mode to allow you to copy any necessary files before recreating it. This\n") - fmt.Fprintf(os.Stderr, "can be accomplished with the following commands:\n\n") - fmt.Fprintf(os.Stderr, "\t# Login and copy desired files (Optional)\n") - fmt.Fprintf(os.Stderr, "\t# Podman machine ssh%s tar cvPf - /path/to/files > backup.tar\n\n", suffix) - fmt.Fprintf(os.Stderr, "\t# Recreate machine (DESTRUCTIVE!) \n") - fmt.Fprintf(os.Stderr, "\tpodman machine stop%s\n", suffix) - fmt.Fprintf(os.Stderr, "\tpodman machine rm -f%s\n", suffix) - fmt.Fprintf(os.Stderr, "\tpodman machine init --now%s\n\n", suffix) - fmt.Fprintf(os.Stderr, "\t# Copy back files (Optional)\n") - fmt.Fprintf(os.Stderr, "\t# cat backup.tar | podman machine ssh%s tar xvPf - \n\n", suffix) - } - - if forwardState == machine.NoForwarding { - return - } - - machine.WaitAndPingAPI(forwardSock) - - if !noInfo { - if !m.Rootful { - fmt.Printf("\nThis machine is currently configured in rootless mode. If your containers\n") - fmt.Printf("require root permissions (e.g. ports < 1024), or if you run into compatibility\n") - fmt.Printf("issues with non-Podman clients, you can switch using the following command: \n") - fmt.Printf("\n\tpodman machine set --rootful%s\n\n", suffix) - } - - fmt.Printf("API forwarding listening on: %s\n", forwardSock) - if forwardState == machine.DockerGlobal { - fmt.Printf("Docker API clients default to this address. You do not need to set DOCKER_HOST.\n\n") - } else { - stillString := "still " - switch forwardState { - case machine.NotInstalled: - fmt.Printf("\nThe system helper service is not installed; the default Docker API socket\n") - fmt.Printf("address can't be used by Podman. ") - if helper := findClaimHelper(); len(helper) > 0 { - fmt.Printf("If you would like to install it run the\nfollowing commands:\n") - fmt.Printf("\n\tsudo %s install\n", helper) - fmt.Printf("\tpodman machine stop%s; podman machine start%s\n\n", suffix, suffix) - } - case machine.MachineLocal: - fmt.Printf("\nAnother process was listening on the default Docker API socket address.\n") - case machine.ClaimUnsupported: - fallthrough - default: - stillString = "" - } - - fmt.Printf("You can %sconnect Docker API clients by setting DOCKER_HOST using the\n", stillString) - fmt.Printf("following command in your terminal session:\n") - fmt.Printf("\n\texport DOCKER_HOST='unix://%s'\n\n", forwardSock) - } - } -} - func (m *MacMachine) isIncompatible() bool { return m.UID == -1 } diff --git a/pkg/machine/machine_common.go b/pkg/machine/machine_common.go index ae34d771b6..10f76fe58d 100644 --- a/pkg/machine/machine_common.go +++ b/pkg/machine/machine_common.go @@ -54,3 +54,71 @@ func AddSSHConnectionsToPodmanSocket(uid, port int, identityPath, name, remoteUs } return nil } + +// WaitAPIAndPrintInfo prints info about the machine and does a ping test on the +// API socket +func WaitAPIAndPrintInfo(forwardState APIForwardingState, name, helper, forwardSock string, noInfo, isIncompatible, rootful bool) { + suffix := "" + if name != DefaultMachineName { + suffix = " " + name + } + + if isIncompatible { + fmt.Fprintf(os.Stderr, "\n!!! ACTION REQUIRED: INCOMPATIBLE MACHINE !!!\n") + + fmt.Fprintf(os.Stderr, "\nThis machine was created by an older podman release that is incompatible\n") + fmt.Fprintf(os.Stderr, "with this release of podman. It has been started in a limited operational\n") + fmt.Fprintf(os.Stderr, "mode to allow you to copy any necessary files before recreating it. This\n") + fmt.Fprintf(os.Stderr, "can be accomplished with the following commands:\n\n") + fmt.Fprintf(os.Stderr, "\t# Login and copy desired files (Optional)\n") + fmt.Fprintf(os.Stderr, "\t# podman machine ssh%s tar cvPf - /path/to/files > backup.tar\n\n", suffix) + fmt.Fprintf(os.Stderr, "\t# Recreate machine (DESTRUCTIVE!) \n") + fmt.Fprintf(os.Stderr, "\tpodman machine stop%s\n", suffix) + fmt.Fprintf(os.Stderr, "\tpodman machine rm -f%s\n", suffix) + fmt.Fprintf(os.Stderr, "\tpodman machine init --now%s\n\n", suffix) + fmt.Fprintf(os.Stderr, "\t# Copy back files (Optional)\n") + fmt.Fprintf(os.Stderr, "\t# cat backup.tar | podman machine ssh%s tar xvPf - \n\n", suffix) + } + + if forwardState == NoForwarding { + return + } + + WaitAndPingAPI(forwardSock) + + if !noInfo { + if !rootful { + fmt.Printf("\nThis machine is currently configured in rootless mode. If your containers\n") + fmt.Printf("require root permissions (e.g. ports < 1024), or if you run into compatibility\n") + fmt.Printf("issues with non-podman clients, you can switch using the following command: \n") + fmt.Printf("\n\tpodman machine set --rootful%s\n\n", suffix) + } + + fmt.Printf("API forwarding listening on: %s\n", forwardSock) + if forwardState == DockerGlobal { + fmt.Printf("Docker API clients default to this address. You do not need to set DOCKER_HOST.\n\n") + } else { + stillString := "still " + switch forwardState { + case NotInstalled: + fmt.Printf("\nThe system helper service is not installed; the default Docker API socket\n") + fmt.Printf("address can't be used by podman. ") + if len(helper) > 0 { + fmt.Printf("If you would like to install it run the\nfollowing commands:\n") + fmt.Printf("\n\tsudo %s install\n", helper) + fmt.Printf("\tpodman machine stop%s; podman machine start%s\n\n", suffix, suffix) + } + case MachineLocal: + fmt.Printf("\nAnother process was listening on the default Docker API socket address.\n") + case ClaimUnsupported: + fallthrough + default: + stillString = "" + } + + fmt.Printf("You can %sconnect Docker API clients by setting DOCKER_HOST using the\n", stillString) + fmt.Printf("following command in your terminal session:\n") + fmt.Printf("\n\texport DOCKER_HOST='unix://%s'\n\n", forwardSock) + } + } +} diff --git a/pkg/machine/qemu/machine.go b/pkg/machine/qemu/machine.go index bc23298a3c..953b4ce302 100644 --- a/pkg/machine/qemu/machine.go +++ b/pkg/machine/qemu/machine.go @@ -755,7 +755,15 @@ func (v *MachineVM) Start(name string, opts machine.StartOptions) error { } if len(v.Mounts) == 0 { - v.waitAPIAndPrintInfo(forwardState, forwardSock, opts.NoInfo) + machine.WaitAPIAndPrintInfo( + forwardState, + v.Name, + findClaimHelper(), + forwardSock, + opts.NoInfo, + v.isIncompatible(), + v.Rootful, + ) return nil } @@ -777,7 +785,15 @@ func (v *MachineVM) Start(name string, opts machine.StartOptions) error { return err } - v.waitAPIAndPrintInfo(forwardState, forwardSock, opts.NoInfo) + machine.WaitAPIAndPrintInfo( + forwardState, + v.Name, + findClaimHelper(), + forwardSock, + opts.NoInfo, + v.isIncompatible(), + v.Rootful, + ) return nil } @@ -1487,72 +1503,6 @@ func alreadyLinked(target string, link string) bool { return err == nil && read == target } -func (v *MachineVM) waitAPIAndPrintInfo(forwardState machine.APIForwardingState, forwardSock string, noInfo bool) { - suffix := "" - if v.Name != machine.DefaultMachineName { - suffix = " " + v.Name - } - - if v.isIncompatible() { - fmt.Fprintf(os.Stderr, "\n!!! ACTION REQUIRED: INCOMPATIBLE MACHINE !!!\n") - - fmt.Fprintf(os.Stderr, "\nThis machine was created by an older podman release that is incompatible\n") - fmt.Fprintf(os.Stderr, "with this release of podman. It has been started in a limited operational\n") - fmt.Fprintf(os.Stderr, "mode to allow you to copy any necessary files before recreating it. This\n") - fmt.Fprintf(os.Stderr, "can be accomplished with the following commands:\n\n") - fmt.Fprintf(os.Stderr, "\t# Login and copy desired files (Optional)\n") - fmt.Fprintf(os.Stderr, "\t# podman machine ssh%s tar cvPf - /path/to/files > backup.tar\n\n", suffix) - fmt.Fprintf(os.Stderr, "\t# Recreate machine (DESTRUCTIVE!) \n") - fmt.Fprintf(os.Stderr, "\tpodman machine stop%s\n", suffix) - fmt.Fprintf(os.Stderr, "\tpodman machine rm -f%s\n", suffix) - fmt.Fprintf(os.Stderr, "\tpodman machine init --now%s\n\n", suffix) - fmt.Fprintf(os.Stderr, "\t# Copy back files (Optional)\n") - fmt.Fprintf(os.Stderr, "\t# cat backup.tar | podman machine ssh%s tar xvPf - \n\n", suffix) - } - - if forwardState == machine.NoForwarding { - return - } - - machine.WaitAndPingAPI(forwardSock) - - if !noInfo { - if !v.Rootful { - fmt.Printf("\nThis machine is currently configured in rootless mode. If your containers\n") - fmt.Printf("require root permissions (e.g. ports < 1024), or if you run into compatibility\n") - fmt.Printf("issues with non-podman clients, you can switch using the following command: \n") - fmt.Printf("\n\tpodman machine set --rootful%s\n\n", suffix) - } - - fmt.Printf("API forwarding listening on: %s\n", forwardSock) - if forwardState == machine.DockerGlobal { - fmt.Printf("Docker API clients default to this address. You do not need to set DOCKER_HOST.\n\n") - } else { - stillString := "still " - switch forwardState { - case machine.NotInstalled: - fmt.Printf("\nThe system helper service is not installed; the default Docker API socket\n") - fmt.Printf("address can't be used by podman. ") - if helper := findClaimHelper(); len(helper) > 0 { - fmt.Printf("If you would like to install it run the\nfollowing commands:\n") - fmt.Printf("\n\tsudo %s install\n", helper) - fmt.Printf("\tpodman machine stop%s; podman machine start%s\n\n", suffix, suffix) - } - case machine.MachineLocal: - fmt.Printf("\nAnother process was listening on the default Docker API socket address.\n") - case machine.ClaimUnsupported: - fallthrough - default: - stillString = "" - } - - fmt.Printf("You can %sconnect Docker API clients by setting DOCKER_HOST using the\n", stillString) - fmt.Printf("following command in your terminal session:\n") - fmt.Printf("\n\texport DOCKER_HOST='unix://%s'\n\n", forwardSock) - } - } -} - // update returns the content of the VM's // configuration file in json func (v *MachineVM) update() error {