227 lines
5.4 KiB
Go
227 lines
5.4 KiB
Go
// +build !remoteclient
|
|
|
|
package adapter
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"github.com/containers/libpod/cmd/podman/cliconfig"
|
|
"github.com/containers/libpod/cmd/podman/shared"
|
|
"github.com/containers/libpod/libpod"
|
|
"github.com/containers/libpod/pkg/adapter/shortcuts"
|
|
)
|
|
|
|
// Pod ...
|
|
type Pod struct {
|
|
*libpod.Pod
|
|
}
|
|
|
|
// RemovePods ...
|
|
func (r *LocalRuntime) RemovePods(ctx context.Context, cli *cliconfig.PodRmValues) ([]string, []error) {
|
|
var (
|
|
errs []error
|
|
podids []string
|
|
)
|
|
pods, err := shortcuts.GetPodsByContext(cli.All, cli.Latest, cli.InputArgs, r.Runtime)
|
|
if err != nil {
|
|
errs = append(errs, err)
|
|
return nil, errs
|
|
}
|
|
|
|
for _, p := range pods {
|
|
if err := r.RemovePod(ctx, p, cli.Force, cli.Force); err != nil {
|
|
errs = append(errs, err)
|
|
} else {
|
|
podids = append(podids, p.ID())
|
|
}
|
|
}
|
|
return podids, errs
|
|
}
|
|
|
|
// GetLatestPod gets the latest pod and wraps it in an adapter pod
|
|
func (r *LocalRuntime) GetLatestPod() (*Pod, error) {
|
|
pod := Pod{}
|
|
p, err := r.Runtime.GetLatestPod()
|
|
pod.Pod = p
|
|
return &pod, err
|
|
}
|
|
|
|
// GetAllPods gets all pods and wraps it in an adapter pod
|
|
func (r *LocalRuntime) GetAllPods() ([]*Pod, error) {
|
|
var pods []*Pod
|
|
allPods, err := r.Runtime.GetAllPods()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, p := range allPods {
|
|
pod := Pod{}
|
|
pod.Pod = p
|
|
pods = append(pods, &pod)
|
|
}
|
|
return pods, nil
|
|
}
|
|
|
|
// LookupPod gets a pod by name or id and wraps it in an adapter pod
|
|
func (r *LocalRuntime) LookupPod(nameOrID string) (*Pod, error) {
|
|
pod := Pod{}
|
|
p, err := r.Runtime.LookupPod(nameOrID)
|
|
pod.Pod = p
|
|
return &pod, err
|
|
}
|
|
|
|
// StopPods is a wrapper to libpod to stop pods based on a cli context
|
|
func (r *LocalRuntime) StopPods(ctx context.Context, cli *cliconfig.PodStopValues) ([]string, []error) {
|
|
timeout := -1
|
|
if cli.Flags().Changed("timeout") {
|
|
timeout = int(cli.Timeout)
|
|
}
|
|
var (
|
|
errs []error
|
|
podids []string
|
|
)
|
|
pods, err := shortcuts.GetPodsByContext(cli.All, cli.Latest, cli.InputArgs, r.Runtime)
|
|
if err != nil {
|
|
errs = append(errs, err)
|
|
return nil, errs
|
|
}
|
|
|
|
for _, p := range pods {
|
|
stopped := true
|
|
conErrs, stopErr := p.StopWithTimeout(ctx, true, int(timeout))
|
|
if stopErr != nil {
|
|
errs = append(errs, stopErr)
|
|
stopped = false
|
|
}
|
|
if conErrs != nil {
|
|
stopped = false
|
|
for _, err := range conErrs {
|
|
errs = append(errs, err)
|
|
}
|
|
}
|
|
if stopped {
|
|
podids = append(podids, p.ID())
|
|
}
|
|
}
|
|
return podids, errs
|
|
}
|
|
|
|
// KillPods is a wrapper to libpod to start pods based on the cli context
|
|
func (r *LocalRuntime) KillPods(ctx context.Context, cli *cliconfig.PodKillValues, signal uint) ([]string, []error) {
|
|
var (
|
|
errs []error
|
|
podids []string
|
|
)
|
|
pods, err := shortcuts.GetPodsByContext(cli.All, cli.Latest, cli.InputArgs, r.Runtime)
|
|
if err != nil {
|
|
errs = append(errs, err)
|
|
return nil, errs
|
|
}
|
|
for _, p := range pods {
|
|
killed := true
|
|
conErrs, killErr := p.Kill(signal)
|
|
if killErr != nil {
|
|
errs = append(errs, killErr)
|
|
killed = false
|
|
}
|
|
if conErrs != nil {
|
|
killed = false
|
|
for _, err := range conErrs {
|
|
errs = append(errs, err)
|
|
}
|
|
}
|
|
if killed {
|
|
podids = append(podids, p.ID())
|
|
}
|
|
}
|
|
return podids, errs
|
|
}
|
|
|
|
// StartPods is a wrapper to start pods based on the cli context
|
|
func (r *LocalRuntime) StartPods(ctx context.Context, cli *cliconfig.PodStartValues) ([]string, []error) {
|
|
var (
|
|
errs []error
|
|
podids []string
|
|
)
|
|
pods, err := shortcuts.GetPodsByContext(cli.All, cli.Latest, cli.InputArgs, r.Runtime)
|
|
if err != nil {
|
|
errs = append(errs, err)
|
|
return nil, errs
|
|
}
|
|
for _, p := range pods {
|
|
started := true
|
|
conErrs, startErr := p.Start(ctx)
|
|
if startErr != nil {
|
|
errs = append(errs, startErr)
|
|
started = false
|
|
}
|
|
if conErrs != nil {
|
|
started = false
|
|
for _, err := range conErrs {
|
|
errs = append(errs, err)
|
|
}
|
|
}
|
|
if started {
|
|
podids = append(podids, p.ID())
|
|
}
|
|
}
|
|
return podids, errs
|
|
}
|
|
|
|
// CreatePod is a wrapper for libpod and creating a new pod from the cli context
|
|
func (r *LocalRuntime) CreatePod(ctx context.Context, cli *cliconfig.PodCreateValues, labels map[string]string) (string, error) {
|
|
var (
|
|
options []libpod.PodCreateOption
|
|
err error
|
|
)
|
|
|
|
if cli.Flag("cgroup-parent").Changed {
|
|
options = append(options, libpod.WithPodCgroupParent(cli.CgroupParent))
|
|
}
|
|
|
|
if len(labels) != 0 {
|
|
options = append(options, libpod.WithPodLabels(labels))
|
|
}
|
|
|
|
if cli.Flag("name").Changed {
|
|
options = append(options, libpod.WithPodName(cli.Name))
|
|
}
|
|
|
|
if cli.Infra {
|
|
options = append(options, libpod.WithInfraContainer())
|
|
nsOptions, err := shared.GetNamespaceOptions(strings.Split(cli.Share, ","))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
options = append(options, nsOptions...)
|
|
}
|
|
|
|
if len(cli.Publish) > 0 {
|
|
portBindings, err := shared.CreatePortBindings(cli.Publish)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
options = append(options, libpod.WithInfraContainerPorts(portBindings))
|
|
|
|
}
|
|
// always have containers use pod cgroups
|
|
// User Opt out is not yet supported
|
|
options = append(options, libpod.WithPodCgroups())
|
|
|
|
pod, err := r.NewPod(ctx, options...)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return pod.ID(), nil
|
|
}
|
|
|
|
// GetPodStatus is a wrapper to get the status of a local libpod pod
|
|
func (p *Pod) GetPodStatus() (string, error) {
|
|
return shared.GetPodStatus(p.Pod)
|
|
}
|
|
|
|
// BatchContainerOp is a wrapper for the shared function of the same name
|
|
func BatchContainerOp(ctr *libpod.Container, opts shared.PsOptions) (shared.BatchContainerStruct, error) {
|
|
return shared.BatchContainerOp(ctr, opts)
|
|
}
|