forked from apache/camel-k
/
util_containerization.go
222 lines (176 loc) · 5.96 KB
/
util_containerization.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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 cmd
import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
"github.com/aanogueira/camel-k/pkg/util"
"github.com/aanogueira/camel-k/pkg/util/docker"
"github.com/pkg/errors"
)
/// Local Docker file system management functions.
func createDockerBaseWorkingDirectory() error {
// Create local docker base directory.
temporaryDirectory, err := ioutil.TempDir(os.TempDir(), "docker-base-")
if err != nil {
return err
}
// Set the Docker base directory to the default value.
docker.BaseWorkingDirectory = temporaryDirectory
return nil
}
func deleteDockerBaseWorkingDirectory() error {
// Remove directory used for computing the dependencies.
defer os.RemoveAll(docker.BaseWorkingDirectory)
return nil
}
func createDockerWorkingDirectory() error {
// Create local docker base directory.
temporaryDirectory, err := ioutil.TempDir(os.TempDir(), "docker-")
if err != nil {
return err
}
// Set the Docker base directory to the default value.
docker.IntegrationWorkingDirectory = temporaryDirectory
return nil
}
func deleteDockerWorkingDirectory() error {
// Remove directory used for computing the dependencies.
defer os.RemoveAll(docker.IntegrationWorkingDirectory)
return nil
}
func setDockerNetworkName(networkName string) {
if networkName != "" {
docker.NetworkName = networkName
}
}
func setDockerEnvVars(envVars []string) {
if envVars != nil && len(envVars) > 0 {
util.CLIEnvVars = envVars
}
}
func createAndBuildBaseImage(ctx context.Context) error {
// Create the base image Docker file.
err := docker.CreateBaseImageDockerFile()
if err != nil {
return err
}
// Get the Docker command arguments for building the base image and create the command.
args := docker.BuildBaseImageArgs()
cmd := exec.CommandContext(ctx, "docker", args...)
// Output executed command.
fmt.Printf("Executing: " + strings.Join(cmd.Args, " ") + "\n")
// Run the command.
if err := cmd.Run(); err != nil {
return errors.Errorf("base image containerization did not run successfully: %v", err)
}
return nil
}
func createAndBuildIntegrationImage(ctx context.Context, containerRegistry string, justBaseImage bool, image string,
propertyFiles []string, dependencies []string, routes []string, stdout, stderr io.Writer) error {
// This ensures the Dockerfile for the base image will not end up in an undesired location.
if docker.BaseWorkingDirectory == "" {
return errors.New("base directory that holds the base image Dockerfile has not been set correctly")
}
docker.RegistryName = containerRegistry
// If we build a normal image, i.e. not the base image, we need to parse
// the location where images will be pushed.
if !justBaseImage {
registryName, err := docker.ExtractRegistryName(image)
if err != nil {
return err
}
docker.RegistryName = registryName
}
// Create the Dockerfile and build the base image.
err := createAndBuildBaseImage(ctx)
if err != nil {
return err
}
if justBaseImage {
return nil
}
if docker.IntegrationWorkingDirectory == "" {
return errors.New("integration directory that holds the image Dockerfile has not been set correctly")
}
// Create integration image if integration files were provided.
// Copy dependencies to a dependencies folder under a local directory.
err = updateIntegrationDependencies(dependencies)
if err != nil {
return err
}
// Copy routes to a routes folder under a local directory.
err = updateIntegrationRoutes(routes)
if err != nil {
return err
}
// Get integration run command to be run inside the container. This means the command
// has to be created with the paths which will be valid inside the container.
containerCmd, err := GetContainerIntegrationRunCommand(ctx, propertyFiles, dependencies, routes, stdout, stderr)
if err != nil {
return err
}
// Create the integration image Docker file.
err = docker.CreateIntegrationImageDockerFile(containerCmd)
if err != nil {
return err
}
// Get the Docker command arguments for building the base image and create the command.
args := docker.BuildIntegrationImageArgs(image)
cmd := exec.CommandContext(ctx, "docker", args...)
// Set stdout and stderr.
cmd.Stderr = stderr
cmd.Stdout = stdout
// Output executed command.
fmt.Printf("Executing: " + strings.Join(cmd.Args, " ") + "\n")
// Run the command.
if err := cmd.Run(); err != nil {
return errors.Errorf("integration image containerization did not run successfully: %v", err)
}
return nil
}
func runIntegrationImage(ctx context.Context, image string, stdout, stderr io.Writer) error {
// Stop the child process before exiting
dockerCtx, cancel := context.WithCancel(ctx)
cs := make(chan os.Signal)
signal.Notify(cs, os.Interrupt, syscall.SIGTERM)
go func() {
<-cs
cancel()
}()
// Get the docker command line argument for running an image.
args, err := docker.RunIntegrationImageArgs(image)
if err != nil {
return err
}
cmd := exec.CommandContext(dockerCtx, "docker", args...)
// Set stdout and stderr.
cmd.Stderr = stderr
cmd.Stdout = stdout
// Output executed command.
fmt.Printf("Executing: " + strings.Join(cmd.Args, " ") + "\n")
// Run the command.
if err := cmd.Run(); err != nil {
return errors.Errorf("integration image did not run successfully: %v", err)
}
return nil
}