/
container.go
100 lines (92 loc) · 3.52 KB
/
container.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
92
93
94
95
96
97
98
99
100
/*
* Copyright IBM Corporation 2020, 2021
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package container
import (
"errors"
"fmt"
"io/fs"
dockertypes "github.com/docker/docker/api/types"
"github.com/konveyor/move2kube/common"
"github.com/konveyor/move2kube/qaengine"
environmenttypes "github.com/konveyor/move2kube/types/environment"
"github.com/sirupsen/logrus"
)
var (
inited bool
enabled bool
workingEngine ContainerEngine
// ErrNoContainerRuntime is an error that indicates that no container runtime was found (Docker, Podman, etc.).
ErrNoContainerRuntime = errors.New("no working container runtime found")
)
// ContainerEngine defines interface to manage containers
type ContainerEngine interface {
// RunCmdInContainer runs a container
RunCmdInContainer(image string, cmd environmenttypes.Command, workingdir string, env []string) (stdout, stderr string, exitcode int, err error)
// InspectImage gets Inspect output for a container
InspectImage(image string) (dockertypes.ImageInspect, error)
// TODO: Change paths from map to array
CopyDirsIntoImage(image, newImageName string, paths map[string]string) (err error)
CopyDirsIntoContainer(containerID string, paths map[string]string) (err error)
CopyDirsFromContainer(containerID string, paths map[string]string) (err error)
BuildImage(image, context, dockerfile string) (err error)
RemoveImage(image string) (err error)
CreateContainer(container environmenttypes.Container) (containerid string, err error)
StopAndRemoveContainer(containerID string) (err error)
// RunContainer runs a container from an image
RunContainer(image string, cmd environmenttypes.Command, volsrc string, voldest string) (output string, containerStarted bool, err error)
Stat(containerID, name string) (fs.FileInfo, error)
}
func initContainerEngine() (err error) {
logrus.Trace("initContainerEngine start")
defer logrus.Trace("initContainerEngine end")
workingEngine, err = newDockerEngine()
if err != nil {
return fmt.Errorf("failed to use docker as the container engine. Error: %w", err)
}
//TODO: Add Support for podman
if workingEngine == nil {
return fmt.Errorf("no working container runtime available")
}
return nil
}
// GetContainerEngine gets a working container engine
func GetContainerEngine(spawnContainers bool) (ContainerEngine, error) {
logrus.Trace("GetContainerEngine start")
defer logrus.Trace("GetContainerEngine end")
if !inited {
enabled = qaengine.FetchBoolAnswer(
common.ConfigSpawnContainersKey,
"Allow spawning containers?",
[]string{"If this setting is set to false, those transformers that rely on containers will not work."},
spawnContainers,
nil,
)
if enabled {
if err := initContainerEngine(); err != nil {
return nil, fmt.Errorf("failed to initialize the container engine. Error: %w", err)
}
}
inited = true
}
if workingEngine == nil {
return nil, ErrNoContainerRuntime
}
return workingEngine, nil
}
// IsDisabled returns whether the container environment is disabled
func IsDisabled() bool {
return !enabled
}