forked from srl-labs/containerlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.go
91 lines (78 loc) · 2.8 KB
/
runtime.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2020 Nokia
// Licensed under the BSD 3-Clause License.
// SPDX-License-Identifier: BSD-3-Clause
package runtime
import (
"context"
"time"
"github.com/srl-labs/containerlab/types"
)
const (
DockerRuntime = "docker"
IgniteRuntime = "ignite"
)
type ContainerRuntime interface {
// Initializes the Container runtime struct
Init(...RuntimeOption) error
// Mgmt return management network configuration of a runtime
Mgmt() *types.MgmtNet
// Adds custom configuration items to the container runtime struct
WithConfig(*RuntimeConfig)
// Set the network management details (generated by the config.go)
WithMgmtNet(*types.MgmtNet)
// Instructs the runtime not to delete the mgmt network on destroy
WithKeepMgmtNet()
// Create container (bridge) network
CreateNet(context.Context) error
// Delete container (bridge) network
DeleteNet(context.Context) error
// Pull container image if not present
PullImageIfRequired(context.Context, string) error
// CreateContainer creates a container, but does not start it
CreateContainer(context.Context, *types.NodeConfig) (string, error)
// Start pre-created container by its name. Returns an extra interface that can be used to receive signals
// about the container life-cycle after it was created, e.g. for post-deploy tasks
StartContainer(context.Context, string, *types.NodeConfig) (interface{}, error)
// Stop running container by its name
StopContainer(context.Context, string) error
// List all containers matching labels
ListContainers(context.Context, []*types.GenericFilter) ([]types.GenericContainer, error)
// Get a netns path using the pid of a container
GetNSPath(context.Context, string) (string, error)
// Executes cmd on container identified with id and returns stdout, stderr bytes and an error
Exec(context.Context, string, []string) ([]byte, []byte, error)
// ExecNotWait executes cmd on container identified with id but doesn't wait for output nor attaches stdout/err
ExecNotWait(context.Context, string, []string) error
// Delete container by its name
DeleteContainer(context.Context, string) error
// Getter for runtime config options
Config() RuntimeConfig
GetName() string
}
type Initializer func() ContainerRuntime
type RuntimeOption func(ContainerRuntime)
type RuntimeConfig struct {
Timeout time.Duration
GracefulShutdown bool
Debug bool
KeepMgmtNet bool
}
var ContainerRuntimes = map[string]Initializer{}
func Register(name string, initFn Initializer) {
ContainerRuntimes[name] = initFn
}
func WithConfig(cfg *RuntimeConfig) RuntimeOption {
return func(r ContainerRuntime) {
r.WithConfig(cfg)
}
}
func WithMgmtNet(mgmt *types.MgmtNet) RuntimeOption {
return func(r ContainerRuntime) {
r.WithMgmtNet(mgmt)
}
}
func WithKeepMgmtNet() RuntimeOption {
return func(r ContainerRuntime) {
r.WithKeepMgmtNet()
}
}