forked from konveyor/move2kube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peercontainer.go
183 lines (166 loc) · 5.91 KB
/
peercontainer.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Copyright IBM Corporation 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 environment
import (
"fmt"
"io/fs"
"net"
"os"
"path/filepath"
"strings"
"github.com/dchest/uniuri"
"github.com/konveyor/move2kube/environment/container"
"github.com/konveyor/move2kube/types"
environmenttypes "github.com/konveyor/move2kube/types/environment"
"github.com/sirupsen/logrus"
"github.com/spf13/cast"
)
const (
// DefaultWorkspaceDir is the default workspace directory
DefaultWorkspaceDir = "workspace"
)
// PeerContainer is supports spawning peer containers to run the environment
type PeerContainer struct {
EnvInfo
WorkspaceSource string
WorkspaceContext string
GRPCQAReceiver net.Addr
ImageName string
ImageWithData string
CID string // A started instance of ImageWithData
}
// NewPeerContainer creates an instance of peer container based environment
func NewPeerContainer(envInfo EnvInfo, grpcQAReceiver net.Addr, c environmenttypes.Container) (ei EnvironmentInstance, err error) {
peerContainer := &PeerContainer{
EnvInfo: envInfo,
ImageName: c.Image,
GRPCQAReceiver: grpcQAReceiver,
}
if c.WorkingDir != "" {
peerContainer.WorkspaceContext = c.WorkingDir
} else {
peerContainer.WorkspaceContext = filepath.Join(string(filepath.Separator), types.AppNameShort)
}
peerContainer.WorkspaceSource = filepath.Join(string(filepath.Separator), DefaultWorkspaceDir)
cengine := container.GetContainerEngine()
if cengine == nil {
return ei, fmt.Errorf("no working container runtime found")
}
newImageName := peerContainer.ImageName + strings.ToLower(envInfo.Name+uniuri.NewLen(5))
err = cengine.CopyDirsIntoImage(peerContainer.ImageName, newImageName, map[string]string{envInfo.Source: peerContainer.WorkspaceSource})
if err != nil {
logrus.Debugf("Unable to create new container image with new data")
if c.ContainerBuild.Context != "" {
err = cengine.BuildImage(c.Image, filepath.Join(envInfo.Context, c.ContainerBuild.Context), c.ContainerBuild.Dockerfile)
if err != nil {
logrus.Errorf("Unable to build new container image for %s : %s", c.Image, err)
return ei, err
}
err = cengine.CopyDirsIntoImage(c.Image, newImageName, map[string]string{envInfo.Source: peerContainer.WorkspaceSource})
if err != nil {
logrus.Errorf("Unable to copy paths to new container image : %s", err)
}
} else {
return ei, err
}
}
peerContainer.ImageWithData = newImageName
cid, err := cengine.CreateContainer(newImageName)
if err != nil {
logrus.Errorf("Unable to start container with image %s : %s", newImageName, cid)
return ei, err
}
peerContainer.CID = cid
return peerContainer, nil
}
// Reset resets the PeerContainer environment
func (e *PeerContainer) Reset() error {
cengine := container.GetContainerEngine()
err := cengine.StopAndRemoveContainer(e.CID)
if err != nil {
logrus.Errorf("Unable to delete image %s : %s", e.ImageWithData, err)
}
cid, err := cengine.CreateContainer(e.ImageWithData)
if err != nil {
logrus.Errorf("Unable to start container with image %s : %s", e.ImageWithData, cid)
return err
}
e.CID = cid
return nil
}
// Stat returns stat info of the file/dir in the env
func (e *PeerContainer) Stat(name string) (fs.FileInfo, error) {
cengine := container.GetContainerEngine()
return cengine.Stat(e.CID, name)
}
// Exec executes a command in the container
func (e *PeerContainer) Exec(cmd environmenttypes.Command) (stdout string, stderr string, exitcode int, err error) {
cengine := container.GetContainerEngine()
envs := []string{}
if e.GRPCQAReceiver != nil {
hostname := getIP()
port := cast.ToString(e.GRPCQAReceiver.(*net.TCPAddr).Port)
envs = append(envs, GRPCEnvName+"="+hostname+":"+port)
}
return cengine.RunCmdInContainer(e.CID, cmd, e.WorkspaceContext, envs)
}
// Destroy destroys the container instance
func (e *PeerContainer) Destroy() error {
cengine := container.GetContainerEngine()
err := cengine.StopAndRemoveContainer(e.CID)
if err != nil {
logrus.Errorf("Unable to stop and remove container %s : %s", e.CID, err)
}
err = cengine.RemoveImage(e.ImageWithData)
if err != nil {
logrus.Errorf("Unable to delete image %s : %s", e.ImageWithData, err)
}
return nil
}
// Download downloads the path to outside the environment
func (e *PeerContainer) Download(path string) (string, error) {
output, err := os.MkdirTemp(e.TempPath, "*")
if err != nil {
logrus.Errorf("Unable to create temp dir : %s", err)
return path, err
}
cengine := container.GetContainerEngine()
err = cengine.CopyDirsFromContainer(e.CID, map[string]string{path: output})
if err != nil {
logrus.Errorf("Unable to copy data from container : %s", err)
return path, err
}
return output, nil
}
// Upload uploads the path from outside the environment into it
func (e *PeerContainer) Upload(outpath string) (envpath string, err error) {
envpath = "/var/tmp/" + uniuri.NewLen(5) + "/" + filepath.Base(outpath)
cengine := container.GetContainerEngine()
err = cengine.CopyDirsIntoContainer(e.CID, map[string]string{outpath: envpath})
if err != nil {
logrus.Errorf("Unable to copy data from container : %s", err)
return outpath, err
}
return envpath, nil
}
// GetContext returns the context within the PeerContainer environment
func (e *PeerContainer) GetContext() string {
return e.WorkspaceContext
}
// GetSource returns the source path within the PeerContainer environment
func (e *PeerContainer) GetSource() string {
return e.WorkspaceSource
}