Skip to content

Commit

Permalink
Fix Lint on Windows and enable the job
Browse files Browse the repository at this point in the history
[NO NEW TESTS NEEDED] Purely refactoring

Signed-off-by: Matt Heon <[email protected]>
  • Loading branch information
mheon committed Feb 20, 2024
1 parent d5a17ad commit ec68f07
Show file tree
Hide file tree
Showing 25 changed files with 203 additions and 142 deletions.
9 changes: 6 additions & 3 deletions cmd/podman-wslkerninst/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ import (
)

const (
//nolint:stylecheck
MB_ICONWARNING = 0x00000030
MB_OK = 0x00000000
MB_DEFBUTTON1 = 0x00000000
//nolint:stylecheck
MB_OK = 0x00000000
//nolint:stylecheck
MB_DEFBUTTON1 = 0x00000000
)

const KernelWarning = "WSL Kernel installation did not complete successfully. " +
Expand Down Expand Up @@ -85,7 +88,7 @@ func warn(title string, caption string) int {

func main() {
args := os.Args
setupLogging(path.Base(args[0]))
_, _ = setupLogging(path.Base(args[0]))
if wutil.IsWSLInstalled() {
// nothing to do
logrus.Info("WSL Kernel already installed")
Expand Down
23 changes: 16 additions & 7 deletions cmd/winpath/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@ import (
type operation int

const (
HWND_BROADCAST = 0xFFFF
WM_SETTINGCHANGE = 0x001A
SMTO_ABORTIFHUNG = 0x0002
ERR_BAD_ARGS = 0x000A
OPERATION_FAILED = 0x06AC
Environment = "Environment"
Add operation = iota
//nolint:stylecheck
HWND_BROADCAST = 0xFFFF
//nolint:stylecheck
WM_SETTINGCHANGE = 0x001A
//nolint:stylecheck
SMTO_ABORTIFHUNG = 0x0002
//nolint:stylecheck
ERR_BAD_ARGS = 0x000A
//nolint:stylecheck
OPERATION_FAILED = 0x06AC

Environment = "Environment"
Add operation = iota
Remove
Open
NotSpecified
Expand Down Expand Up @@ -143,6 +149,8 @@ func removePathFromRegistry(path string) error {
return err
}

// No point preallocating we can't know how big the array needs to be.
//nolint:prealloc
var elements []string
for _, element := range strings.Split(existing, ";") {
if strings.EqualFold(element, path) {
Expand Down Expand Up @@ -174,6 +182,7 @@ func broadcastEnvironmentChange() {
user32 := syscall.NewLazyDLL("user32")
proc := user32.NewProc("SendMessageTimeoutW")
millis := 3000
//nolint:dogsled
_, _, _ = proc.Call(HWND_BROADCAST, WM_SETTINGCHANGE, 0, uintptr(unsafe.Pointer(env)), SMTO_ABORTIFHUNG, uintptr(millis), 0)
}

Expand Down
3 changes: 2 additions & 1 deletion contrib/cirrus/runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ function _run_altbuild() {
rm -rf $context_dir
;;
*Windows*)
showrun make lint GOOS=windows || true # TODO: Enable when code passes check
showrun make .install.pre-commit
showrun make lint GOOS=windows CGO_ENABLED=0
showrun make podman-remote-release-windows_amd64.zip
;;
*RPM*)
Expand Down
4 changes: 2 additions & 2 deletions hack/golangci-lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ set -e
BUILD_TAGS_DEFAULT="apparmor,seccomp,selinux"
BUILD_TAGS_ABI="$BUILD_TAGS_DEFAULT,systemd"
BUILD_TAGS_TUNNEL="$BUILD_TAGS_DEFAULT,remote"
BUILD_TAGS_REMOTE="remote"
BUILD_TAGS_REMOTE="remote,containers_image_openpgp"

SKIP_DIRS_ABI=""
SKIP_DIRS_TUNNEL="pkg/api,pkg/domain/infra/abi"
Expand All @@ -40,6 +40,6 @@ for to_lint in "${to_lint[@]}"; do
# Make it really easy for a developer to copy-paste the command-line
# to focus or debug a single, specific linting category.
set -x
./bin/golangci-lint run --build-tags="${!tags_var}" --skip-dirs="${!skip_var}" "$@"
./bin/golangci-lint run --timeout=10m --build-tags="${!tags_var}" --skip-dirs="${!skip_var}" "$@"
)
done
1 change: 0 additions & 1 deletion pkg/bindings/containers/term_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func notifyWinChange(ctx context.Context, winChange chan os.Signal, stdin *os.Fi
}
}
}()

}

func getTermSize(stdin *os.File, stdout *os.File) (width, height int, err error) {
Expand Down
4 changes: 3 additions & 1 deletion pkg/machine/e2e/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,9 @@ func isWSL() bool {
return isVmtype(define.WSLVirt)
}

//nolint:unused
// Only used on Windows
//
//nolint:unparam,unused
func runSystemCommand(binary string, cmdArgs []string, timeout time.Duration, wait bool) (*machineSession, error) {
GinkgoWriter.Println(binary + " " + strings.Join(cmdArgs, " "))
c := exec.Command(binary, cmdArgs...)
Expand Down
5 changes: 3 additions & 2 deletions pkg/machine/e2e/init_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ var _ = Describe("podman machine init - windows only", func() {
Expect(session).To(Exit(0))

defer func() {
_, err := runSystemCommand(wutil.FindWSL(), []string{"--terminate", "podman-net-usermode"}, defaultTimeout, true)
_, err := runSystemCommand(wutil.FindWSL(), []string{"--terminate", "podman-net-usermode"}, defaultTimeout, true)
if err != nil {
fmt.Println("unable to terminate podman-net-usermode")
}

_, err = runSystemCommand(wutil.FindWSL(), []string{"--unregister", "podman-net-usermode"}, defaultTimeout, true)
_, err = runSystemCommand(wutil.FindWSL(), []string{"--unregister", "podman-net-usermode"}, defaultTimeout, true)
if err != nil {
fmt.Println("unable to unregister podman-net-usermode")
}
Expand Down Expand Up @@ -87,6 +87,7 @@ var _ = Describe("podman machine init - windows only", func() {
}()
i := new(initMachine)
session, err := mb.setName(name).setCmd(i.withImagePath(mb.imagePath)).run()
Expect(err).ToNot(HaveOccurred())
Expect(session).To(Exit(125))
Expect(session.errorToString()).To(ContainSubstring("already exists on hypervisor"))
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/machine/fedora_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func DetermineMachineArch() string {
current, _ := syscall.GetCurrentProcess()

if err := windows.IsWow64Process2(windows.Handle(current), &machine, &native); err != nil {
logrus.Warnf("Failure detecting native system architecture, %s: %w", fallbackMsg, err)
logrus.Warnf("Failure detecting native system architecture, %s: %v", fallbackMsg, err)
// Fall-back to binary arch
return runtime.GOARCH
}
Expand All @@ -26,7 +26,7 @@ func DetermineMachineArch() string {
case 0x8664:
return "amd64"
default:
logrus.Warnf("Unknown or unsupported native system architecture [%d], %s", fallbackMsg)
logrus.Warnf("Unknown or unsupported native system architecture [%d], %s", native, fallbackMsg)
return runtime.GOARCH
}
}
28 changes: 0 additions & 28 deletions pkg/machine/gvproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,10 @@ package machine
import (
"fmt"
"strconv"
"time"

"github.com/containers/podman/v5/pkg/machine/define"
psutil "github.com/shirou/gopsutil/v3/process"
)

const (
loops = 8
sleepTime = time.Millisecond * 1
)

// backoffForProcess checks if the process still exists, for something like
// sigterm. If the process still exists after loops and sleep time are exhausted,
// an error is returned
func backoffForProcess(p *psutil.Process) error {
sleepInterval := sleepTime
for i := 0; i < loops; i++ {
running, err := p.IsRunning()
if err != nil {
return fmt.Errorf("checking if process running: %w", err)
}
if !running {
return nil
}

time.Sleep(sleepInterval)
// double the time
sleepInterval += sleepInterval
}
return fmt.Errorf("process %d has not ended", p.Pid)
}

// CleanupGVProxy reads the --pid-file for gvproxy attempts to stop it
func CleanupGVProxy(f define.VMFile) error {
gvPid, err := f.Read()
Expand Down
27 changes: 27 additions & 0 deletions pkg/machine/gvproxy_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,38 @@ import (
"errors"
"fmt"
"syscall"
"time"

psutil "github.com/shirou/gopsutil/v3/process"
"github.com/sirupsen/logrus"
)

const (
loops = 8
sleepTime = time.Millisecond * 1
)

// backoffForProcess checks if the process still exists, for something like
// sigterm. If the process still exists after loops and sleep time are exhausted,
// an error is returned
func backoffForProcess(p *psutil.Process) error {
sleepInterval := sleepTime
for i := 0; i < loops; i++ {
running, err := p.IsRunning()
if err != nil {
return fmt.Errorf("checking if process running: %w", err)
}
if !running {
return nil
}

time.Sleep(sleepInterval)
// double the time
sleepInterval += sleepInterval
}
return fmt.Errorf("process %d has not ended", p.Pid)
}

// / waitOnProcess takes a pid and sends a sigterm to it. it then waits for the
// process to not exist. if the sigterm does not end the process after an interval,
// then sigkill is sent. it also waits for the process to exit after the sigkill too.
Expand Down
1 change: 1 addition & 0 deletions pkg/machine/gvproxy_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func waitOnProcess(processID int) error {
// FindProcess on Windows will return an error when the process is not found
// if a process can not be found then it has already exited and there is
// nothing left to do, so return without error
//nolint:nilerr
return nil
}

Expand Down
103 changes: 53 additions & 50 deletions pkg/machine/hyperv/stubber.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,66 +381,69 @@ func (h HyperVStubber) PostStartNetworking(mc *vmconfigs.MachineConfig, noInfo b
defer callbackFuncs.CleanIfErr(&err)
go callbackFuncs.CleanOnSignal()

if len(mc.Mounts) != 0 {
var (
dirs *define.MachineDirs
gvproxyPID int
)
dirs, err = machine.GetMachineDirs(h.VMType())
if err != nil {
return err
}
// GvProxy PID file path is now derived
gvproxyPIDFile, err := dirs.RuntimeDir.AppendToNewVMFile("gvproxy.pid", nil)
if err != nil {
return err
}
gvproxyPID, err = gvproxyPIDFile.ReadPIDFrom()
if err != nil {
return err
}
if len(mc.Mounts) == 0 {
return nil
}

executable, err = os.Executable()
if err != nil {
return err
}
// Start the 9p server in the background
p9ServerArgs := []string{}
if logrus.IsLevelEnabled(logrus.DebugLevel) {
p9ServerArgs = append(p9ServerArgs, "--log-level=debug")
}
p9ServerArgs = append(p9ServerArgs, "machine", "server9p")
var (
dirs *define.MachineDirs
gvproxyPID int
)
dirs, err = machine.GetMachineDirs(h.VMType())
if err != nil {
return err
}
// GvProxy PID file path is now derived
gvproxyPIDFile, err := dirs.RuntimeDir.AppendToNewVMFile("gvproxy.pid", nil)
if err != nil {
return err
}
gvproxyPID, err = gvproxyPIDFile.ReadPIDFrom()
if err != nil {
return err
}

for _, mount := range mc.Mounts {
if mount.VSockNumber == nil {
return fmt.Errorf("mount %s has not vsock port defined", mount.Source)
}
p9ServerArgs = append(p9ServerArgs, "--serve", fmt.Sprintf("%s:%s", mount.Source, winio.VsockServiceID(uint32(*mount.VSockNumber)).String()))
executable, err = os.Executable()
if err != nil {
return err
}
// Start the 9p server in the background
p9ServerArgs := []string{}
if logrus.IsLevelEnabled(logrus.DebugLevel) {
p9ServerArgs = append(p9ServerArgs, "--log-level=debug")
}
p9ServerArgs = append(p9ServerArgs, "machine", "server9p")

for _, mount := range mc.Mounts {
if mount.VSockNumber == nil {
return fmt.Errorf("mount %s has not vsock port defined", mount.Source)
}
p9ServerArgs = append(p9ServerArgs, fmt.Sprintf("%d", gvproxyPID))
p9ServerArgs = append(p9ServerArgs, "--serve", fmt.Sprintf("%s:%s", mount.Source, winio.VsockServiceID(uint32(*mount.VSockNumber)).String()))
}
p9ServerArgs = append(p9ServerArgs, fmt.Sprintf("%d", gvproxyPID))

logrus.Debugf("Going to start 9p server using command: %s %v", executable, p9ServerArgs)
logrus.Debugf("Going to start 9p server using command: %s %v", executable, p9ServerArgs)

fsCmd := exec.Command(executable, p9ServerArgs...)
fsCmd := exec.Command(executable, p9ServerArgs...)

if logrus.IsLevelEnabled(logrus.DebugLevel) {
err = logCommandToFile(fsCmd, "podman-machine-server9.log")
if err != nil {
return err
}
if logrus.IsLevelEnabled(logrus.DebugLevel) {
err = logCommandToFile(fsCmd, "podman-machine-server9.log")
if err != nil {
return err
}
}

err = fsCmd.Start()
if err == nil {
logrus.Infof("Started podman 9p server as PID %d", fsCmd.Process.Pid)
}
err = fsCmd.Start()
if err != nil {
return fmt.Errorf("unable to start 9p server: %v", err)
}
logrus.Infof("Started podman 9p server as PID %d", fsCmd.Process.Pid)

// Note: No callback is needed to stop the 9p server, because it will stop when
// gvproxy stops
// Note: No callback is needed to stop the 9p server, because it will stop when
// gvproxy stops

// Finalize starting shares after we are confident gvproxy is still alive.
err = startShares(mc)
}
// Finalize starting shares after we are confident gvproxy is still alive.
err = startShares(mc)
return err
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/machine/hyperv/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func removeShares(mc *vmconfigs.MachineConfig) error {

if err := vsockReg.Remove(); err != nil {
if removalErr != nil {
logrus.Errorf("Error removing vsock: %w", removalErr)
logrus.Errorf("Error removing vsock: %v", removalErr)
}
removalErr = fmt.Errorf("removing vsock %d for mountpoint %s: %w", *mount.VSockNumber, mount.Target, err)
}
Expand All @@ -44,7 +44,7 @@ func startShares(mc *vmconfigs.MachineConfig) error {
if logrus.IsLevelEnabled(logrus.DebugLevel) {
args = append(args, "--log-level=debug")
}
//just being protective here; in a perfect world, this cannot happen
// just being protective here; in a perfect world, this cannot happen
if mount.VSockNumber == nil {
return errors.New("cannot start 9p shares with undefined vsock number")
}
Expand Down
Loading

0 comments on commit ec68f07

Please sign in to comment.