Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/run, pkg/podman: Stop container once the last session finishes #1213

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
11 changes: 10 additions & 1 deletion src/cmd/run.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2019 – 2022 Red Hat Inc.
* Copyright © 2019 – 2023 Red Hat Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -285,6 +285,8 @@ func runCommand(container string,

logrus.Debugf("Container %s is initialized", container)

defer stopContainer(container)

if err := runCommandWithFallbacks(container,
preserveFDs,
command,
Expand Down Expand Up @@ -651,3 +653,10 @@ func startContainer(container string) error {

return nil
}

func stopContainer(container string) {
logrus.Debugf("Stopping container %s", container)
if err := podman.Stop(container); err != nil {
logrus.Debugf("Stopping container %s failed: %s", container, err)
}
}
15 changes: 14 additions & 1 deletion src/pkg/podman/podman.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2019 – 2022 Red Hat Inc.
* Copyright © 2019 – 2023 Red Hat Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -414,6 +414,19 @@ func Start(container string, stderr io.Writer) error {
return nil
}

func Stop(container string) error {
logrus.Debugf("Stopping container %s", container)

logLevelString := LogLevel.String()
args := []string{"--log-level", logLevelString, "stop", container}

if err := shell.Run("podman", nil, nil, nil, args...); err != nil {
return err
}

return nil
}

func SystemMigrate(ociRuntimeRequired string) error {
logLevelString := LogLevel.String()
args := []string{"--log-level", logLevelString, "system", "migrate"}
Expand Down