-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[rescontainer] Solidify container podman/docker with rescontainerocibase
- Loading branch information
Showing
13 changed files
with
1,492 additions
and
456 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
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,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 | ||
} |
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,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 | ||
} | ||
} |
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,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) | ||
} |
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,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 | ||
} |
Oops, something went wrong.