-
Notifications
You must be signed in to change notification settings - Fork 6
/
factory.go
87 lines (74 loc) · 2.1 KB
/
factory.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
package podman
import (
"fmt"
"math/rand"
"os"
"os/exec"
"path/filepath"
"sync"
"time"
log "go.arcalot.io/log/v2"
"go.flow.arcalot.io/deployer"
"go.flow.arcalot.io/pluginsdk/schema"
"go.flow.arcalot.io/podmandeployer/internal/cliwrapper"
)
// NewFactory creates a new factory for the Docker deployer.
func NewFactory() deployer.ConnectorFactory[*Config] {
return &factory{}
}
type factory struct {
}
func (f factory) Name() string {
return "podman"
}
func (f factory) DeploymentType() deployer.DeploymentType {
return "image"
}
func (f factory) ConfigurationSchema() *schema.TypedScopeSchema[*Config] {
return Schema
}
func (f factory) Create(config *Config, logger log.Logger) (deployer.Connector, error) {
podmanPath, err := binaryCheck(config.Podman.Path)
if err != nil {
return &Connector{}, fmt.Errorf("podman binary check failed with error: %w", err)
}
podman := cliwrapper.NewCliWrapper(podmanPath, logger, config.Podman.ConnectionName)
var rngSeed int64
if config.Podman.RngSeed == 0 {
rngSeed = time.Now().UnixNano()
} else {
rngSeed = config.Podman.RngSeed
}
rng := rand.New(rand.NewSource(rngSeed)) //nolint:gosec // random number is not a security credential
var containerNamePrefix string
if config.Podman.ContainerNamePrefix == "" {
containerNamePrefix = "arcaflow_podman"
} else {
containerNamePrefix = config.Podman.ContainerNamePrefix
}
return &Connector{
config: config,
logger: logger,
podmanCliWrapper: podman,
containerNamePrefix: containerNamePrefix,
rng: rng,
rngSeed: rngSeed,
lock: &sync.Mutex{},
}, nil
}
func binaryCheck(podmanPath string) (string, error) {
if podmanPath == "" {
podmanPath = "podman"
}
if !filepath.IsAbs(podmanPath) {
podmanPathAbs, err := exec.LookPath(podmanPath)
if err != nil {
return "", fmt.Errorf("podman executable not found in a valid path with error: %w", err)
}
podmanPath = podmanPathAbs
}
if _, err := os.Stat(podmanPath); err != nil {
return "", fmt.Errorf("podman binary not found with error: %w", err)
}
return podmanPath, nil
}