69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
package Podman
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path"
|
|
)
|
|
|
|
type Volume struct {
|
|
Src, Dest string
|
|
}
|
|
|
|
type Sysctl struct {
|
|
Key, Value string
|
|
}
|
|
|
|
type Container struct {
|
|
Name string
|
|
Replace bool
|
|
Executable string // defaults to podman
|
|
Devices []string // globs of devices to map in container
|
|
Environ []string
|
|
Volumes []Volume // volume mounts
|
|
Sysctls []Sysctl
|
|
}
|
|
|
|
func (c Container) Args() []string {
|
|
args := make([]string, 0, 4+len(c.Devices)+len(c.Environ)+len(c.Volumes)+len(c.Sysctls))
|
|
args = append(args, "run", "--rm")
|
|
if c.Replace {
|
|
args = append(args, "--replace")
|
|
if c.Name == "" {
|
|
c.Name = path.Base(os.Args[0])
|
|
}
|
|
}
|
|
if c.Name != "" {
|
|
args = append(args, "--name="+c.Name)
|
|
}
|
|
for _, env := range c.Environ {
|
|
args = append(args, "--env="+env)
|
|
}
|
|
for _, vol := range c.Volumes {
|
|
os.MkdirAll(os.ExpandEnv(vol.Src), 0o755)
|
|
args = append(args, os.ExpandEnv(fmt.Sprintf("--volume=%s:%s", vol.Src, vol.Dest)))
|
|
}
|
|
for _, sysctl := range c.Sysctls {
|
|
args = append(args, os.ExpandEnv(fmt.Sprintf("--sysctl=%s=%s", sysctl.Key, sysctl.Value)))
|
|
}
|
|
args = append(args, Devices(c.Devices...)...)
|
|
return args
|
|
}
|
|
|
|
func (c Container) Run(extraArgs ...string) error {
|
|
if len(extraArgs) < 1 {
|
|
return fmt.Errorf("must provide at least an image to run")
|
|
}
|
|
args := append(c.Args(), extraArgs...)
|
|
return Run(c.Executable, args...)
|
|
}
|
|
|
|
func (c Container) Start(extraArgs ...string) (chan os.Signal, *exec.Cmd, error) {
|
|
if len(extraArgs) < 1 {
|
|
return nil, nil, fmt.Errorf("must provide at least an image to run")
|
|
}
|
|
args := append(c.Args(), extraArgs...)
|
|
return Start(c.Executable, args...)
|
|
}
|