-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
598 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/containers/podman/v5/pkg/bindings" | ||
"github.com/containers/podman/v5/pkg/bindings/containers" | ||
"github.com/containers/podman/v5/pkg/bindings/images" | ||
"github.com/containers/podman/v5/pkg/specgen" | ||
) | ||
|
||
func stopAndRemoveAllContainer(conn context.Context) { | ||
// List and delete images with name obdb-* | ||
containerSummary, err := containers.List(conn, new(containers.ListOptions).WithAll(true)) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
fmt.Println("Deleting images with name obdb-*", containerSummary) | ||
|
||
for _, container := range containerSummary { | ||
for _, repoTag := range container.Names { | ||
if strings.HasPrefix(repoTag, "obdb-") { | ||
// stop first | ||
err := containers.Stop(conn, container.ID, nil) | ||
if err != nil { | ||
fmt.Printf("Error stopping image %s: %v\n", repoTag, err) | ||
} else { | ||
fmt.Printf("Stopped image: %s\n", repoTag) | ||
} | ||
|
||
_, err = containers.Remove(conn, container.ID, nil) | ||
if err != nil { | ||
fmt.Printf("Error deleting image %s: %v\n", repoTag, err) | ||
} else { | ||
fmt.Printf("Deleted image: %s\n", repoTag) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
conn, err := bindings.NewConnection(context.Background(), "unix:///run/podman/podman.sock") | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
stopAndRemoveAllContainer(conn) | ||
defer stopAndRemoveAllContainer(conn) | ||
|
||
// Pull and run hello-world container | ||
_, err = images.Pull(conn, "hello-world", nil) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
s := specgen.NewSpecGenerator("hello-world", false) | ||
s.Name = "obdb-hello-world-container" | ||
createResponse, err := containers.CreateWithSpec(conn, s, nil) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
fmt.Println("Container created.") | ||
if err := containers.Start(conn, createResponse.ID, nil); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
fmt.Println("Container started.") | ||
|
||
stdoutChan := make(chan string) | ||
stderrChan := make(chan string) | ||
go func() { | ||
for log := range stdoutChan { | ||
fmt.Print(log) | ||
} | ||
}() | ||
go func() { | ||
for log := range stderrChan { | ||
fmt.Print(log) | ||
} | ||
}() | ||
|
||
logsOptions := new(containers.LogOptions).WithStdout(true).WithStderr(true) | ||
err = containers.Logs(conn, createResponse.ID, logsOptions, stdoutChan, stderrChan) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.