Skip to content

Commit

Permalink
[rescontainer] Solidify container podman/docker with rescontainerocibase
Browse files Browse the repository at this point in the history
  • Loading branch information
cgalibern committed Oct 22, 2024
1 parent a3e8ed2 commit 0d51b96
Show file tree
Hide file tree
Showing 13 changed files with 1,492 additions and 456 deletions.
3 changes: 2 additions & 1 deletion core/driverdb/drivers_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
_ "github.com/opensvc/om3/drivers/poolloop"
_ "github.com/opensvc/om3/drivers/poolvg"
_ "github.com/opensvc/om3/drivers/rescontainerdocker"
_ "github.com/opensvc/om3/drivers/rescontainerpodman"
_ "github.com/opensvc/om3/drivers/rescontainerdockercli"
_ "github.com/opensvc/om3/drivers/rescontainerkvm"
_ "github.com/opensvc/om3/drivers/rescontainerlxc"
_ "github.com/opensvc/om3/drivers/rescontainerpodman"
_ "github.com/opensvc/om3/drivers/rescontainervbox"
_ "github.com/opensvc/om3/drivers/resdiskcrypt"
_ "github.com/opensvc/om3/drivers/resdiskdrbd"
Expand Down
24 changes: 24 additions & 0 deletions drivers/rescontainerdockercli/caps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package rescontainerdockercli

import (
"os/exec"

"github.com/opensvc/om3/util/capabilities"
)

func init() {
capabilities.Register(capabilitiesScanner)
}

func capabilitiesScanner() ([]string, error) {
l := make([]string, 0)
drvCap := drvID.Cap()
if _, err := exec.LookPath("docker"); err != nil {
return l, nil
}
l = append(l, drvCap)
l = append(l, drvCap+".registry_creds")
l = append(l, drvCap+".signal")
l = append(l, altDrvID.Cap())
return l, nil
}
105 changes: 105 additions & 0 deletions drivers/rescontainerdockercli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package rescontainerdockercli

import (
"context"
"os/exec"
"strings"
"time"

"github.com/opensvc/om3/core/resource"
"github.com/opensvc/om3/drivers/rescontainerocibase"
)

type (
T struct {
*rescontainerocibase.BT
}

ExecutorArg struct {
*rescontainerocibase.ExecutorArg
exe string
inspectRefresher inspectRefresher
}

inspectRefresher interface {
InspectRefresh(context.Context) (rescontainerocibase.Inspecter, error)
}
)

func New() resource.Driver {
bt := &rescontainerocibase.BT{}
t := &T{BT: bt}
executorArg := &ExecutorArg{
ExecutorArg: &rescontainerocibase.ExecutorArg{
BT: bt,
RunArgsDNSOptionOption: "--dns-option",
},
exe: "docker",
}
executor := rescontainerocibase.NewExecutor("docker", executorArg, t)
executorArg.inspectRefresher = executor
_ = t.WithExecuter(executor)
return t
}

func (ea *ExecutorArg) WaitNotRunning(ctx context.Context) error {
var cmd *exec.Cmd
a := rescontainerocibase.Args{
{Option: "container"},
{Option: "wait"},
{Option: ea.BT.ContainerName()},
}
if ctx != nil {
select {
case <-ctx.Done():
return ctx.Err()
default:
cmd = exec.CommandContext(ctx, ea.exe, a.AsStrings()...)
}
} else {
cmd = exec.Command(ea.exe, a.AsStrings()...)
}
ea.Log().Infof("%s %s", ea.exe, strings.Join(a.Obfuscate(), " "))
if err := cmd.Run(); err != nil {
ea.Log().Infof("%s %s: %s", ea.exe, strings.Join(a.Obfuscate(), " "), err)
return err
}
return nil
}

func (ea *ExecutorArg) WaitRemoved(ctx context.Context) error {
if ctx != nil {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, 10*time.Second)
defer cancel()
}
if removed, err := ea.isRemoved(ctx); err != nil {
return err
} else if removed {
return nil
}
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
if removed, err := ea.isRemoved(ctx); err != nil {
return err
} else if removed {
return nil
}
case <-ctx.Done():
return ctx.Err()
}
}
}

func (ea *ExecutorArg) isRemoved(ctx context.Context) (bool, error) {
if inspect, err := ea.inspectRefresher.InspectRefresh(ctx); err == nil {
ea.BT.Log().Debugf("is removed: %v", inspect == nil)
return inspect == nil, nil
} else {
ea.BT.Log().Debugf("is removed: false")
return false, err
}
}
21 changes: 21 additions & 0 deletions drivers/rescontainerdockercli/manifest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package rescontainerdockercli

import (
"github.com/opensvc/om3/core/driver"
"github.com/opensvc/om3/core/manifest"
)

var (
drvID = driver.NewID(driver.GroupContainer, "dockercli")
altDrvID = driver.NewID(driver.GroupContainer, "oci")
)

func init() {
driver.Register(drvID, New)
driver.Register(altDrvID, New)
}

// Manifest exposes to the core the input expected by the driver.
func (t T) Manifest() *manifest.T {
return t.BT.ManifestWithID(drvID)
}
41 changes: 41 additions & 0 deletions drivers/rescontainerocibase/arg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package rescontainerocibase

type (
Arg struct {
Option string
Value string
Obfuscate bool
Multi bool
HasValue bool
}

Args []Arg
)

func (a *Args) AsStrings() []string {
var l []string
for _, v := range *a {
if v.HasValue {
l = append(l, v.Option, v.Value)
} else {
l = append(l, v.Option)
}
}
return l
}

func (a *Args) Obfuscate() []string {
var l []string
for _, v := range *a {
if v.HasValue {
if v.Obfuscate {
l = append(l, v.Option, "obfuscate")
} else {
l = append(l, v.Option, v.Value)
}
} else {
l = append(l, v.Option)
}
}
return l
}
Loading

0 comments on commit 0d51b96

Please sign in to comment.