2024-01-27 11:20:40 +01:00
|
|
|
package pods
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2024-02-02 18:47:45 +01:00
|
|
|
"time"
|
2024-01-27 11:20:40 +01:00
|
|
|
|
|
|
|
"github.com/containers/podman/v4/pkg/bindings/containers"
|
|
|
|
"github.com/containers/podman/v4/pkg/specgen"
|
2024-01-28 15:31:58 +01:00
|
|
|
runtime_spec "github.com/opencontainers/runtime-spec/specs-go"
|
|
|
|
"github.com/spf13/viper"
|
2024-01-27 11:20:40 +01:00
|
|
|
)
|
|
|
|
|
2024-02-03 11:11:10 +01:00
|
|
|
// CreateContainer connects to the Podman socket defined in the "Socket" variable in this scope
|
|
|
|
// and creates a new container using the Image defined in the config file
|
2024-01-27 11:20:40 +01:00
|
|
|
func CreateContainer() (string, error) {
|
2024-01-28 15:31:58 +01:00
|
|
|
image := viper.GetString("image")
|
2024-01-27 11:20:40 +01:00
|
|
|
conn := Socket
|
|
|
|
envmap := make(map[string]string)
|
2024-01-28 15:31:58 +01:00
|
|
|
driDevice := runtime_spec.LinuxDevice{
|
|
|
|
Path: viper.GetString("dri_node"),
|
|
|
|
}
|
|
|
|
for envvar, value := range viper.GetStringMap("envvars") {
|
|
|
|
envmap[envvar] = value.(string)
|
|
|
|
}
|
2024-01-27 11:20:40 +01:00
|
|
|
s := specgen.NewSpecGenerator(image, false)
|
|
|
|
s.Env = envmap
|
2024-01-28 15:31:58 +01:00
|
|
|
|
|
|
|
// Experimental DRI support for GPU acceleration in Container
|
|
|
|
if viper.GetBool("dri") == true {
|
|
|
|
s.Devices = []runtime_spec.LinuxDevice{driDevice}
|
|
|
|
}
|
2024-01-27 11:20:40 +01:00
|
|
|
createResponse, err := containers.CreateWithSpec(conn, s, nil)
|
2024-01-28 15:31:58 +01:00
|
|
|
log.Println("Created Container ", createResponse.ID, err, createResponse.Warnings)
|
2024-01-27 11:20:40 +01:00
|
|
|
|
|
|
|
return createResponse.ID, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func StartContainer(id string) error {
|
|
|
|
return containers.Start(Socket, id, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func DestroyContainer(id string) error {
|
|
|
|
conn := Socket
|
2024-01-28 15:31:58 +01:00
|
|
|
log.Println("Deleting Container", id)
|
2024-01-27 11:20:40 +01:00
|
|
|
if err := containers.Kill(conn, id, nil); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := containers.Remove(conn, id, nil); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-02 18:47:45 +01:00
|
|
|
// GetContainerIP returns a
|
2024-01-27 11:20:40 +01:00
|
|
|
func GetContainerIP(id string) (string, error) {
|
|
|
|
conn := Socket
|
2024-02-02 18:47:45 +01:00
|
|
|
var ip string
|
|
|
|
var err error
|
|
|
|
i := 0
|
2024-01-27 11:20:40 +01:00
|
|
|
|
2024-02-02 18:47:45 +01:00
|
|
|
for i < 50 {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
container, err := containers.Inspect(conn, id, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Could not get IP of container", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
ip = container.NetworkSettings.IPAddress
|
|
|
|
log.Println(ip)
|
|
|
|
if len(ip) > 5 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
if i > 50 {
|
|
|
|
log.Println("timed out waiting for IP")
|
2024-01-27 11:20:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ip, err
|
|
|
|
}
|