forked from apache/camel-k
/
local_run.go
294 lines (249 loc) · 8.94 KB
/
local_run.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
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 (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/apache/camel-k/pkg/util"
)
func newCmdLocalRun(rootCmdOptions *RootCmdOptions) (*cobra.Command, *localRunCmdOptions) {
options := localRunCmdOptions{
RootCmdOptions: rootCmdOptions,
}
cmd := cobra.Command{
Use: "run [integration files]",
Short: "Run integration locally.",
Long: `Run integration locally using the input integration files.`,
PreRunE: decode(&options),
RunE: func(cmd *cobra.Command, args []string) error {
if err := options.validate(cmd, args); err != nil {
return err
}
if err := options.init(); err != nil {
return err
}
// make sure cleanup is done when process is stopped externally
cs := make(chan os.Signal, 1)
signal.Notify(cs, os.Interrupt, syscall.SIGTERM)
go func() {
<-cs
if err := options.deinit(); err != nil {
fmt.Fprintln(cmd.ErrOrStderr(), err)
os.Exit(1)
}
os.Exit(0)
}()
if err := options.run(cmd, args); err != nil {
fmt.Fprintln(cmd.ErrOrStderr(), err.Error())
}
if err := options.deinit(); err != nil {
return err
}
return nil
},
Annotations: map[string]string{
offlineCommandLabel: "true",
},
}
cmd.Flags().Bool("containerize", false, "Run integration in a local container.")
cmd.Flags().String("image", "", "Full path to integration image including registry.")
cmd.Flags().String("network", "", "Custom network name to be used by the underlying Docker command.")
cmd.Flags().String("integration-directory", "", "Directory which holds the locally built integration and is the result of a local build action.")
cmd.Flags().StringArrayP("env", "e", nil, "Flag to specify an environment variable [--env VARIABLE=value].")
cmd.Flags().StringArray("property-file", nil, "Add a property file to the integration.")
cmd.Flags().StringArrayP("property", "p", nil, "Add a Camel property to the integration.")
cmd.Flags().StringArrayP("dependency", "d", nil, additionalDependencyUsageMessage)
cmd.Flags().StringArray("maven-repository", nil, "Use a maven repository")
// hidden flags for compatibility with kamel run
cmd.Flags().StringArrayP("trait", "t", nil, "")
if err := cmd.Flags().MarkHidden("trait"); err != nil {
fmt.Fprintln(cmd.ErrOrStderr(), err.Error())
}
return &cmd, &options
}
type localRunCmdOptions struct {
*RootCmdOptions
Containerize bool `mapstructure:"containerize"`
Image string `mapstructure:"image"`
Network string `mapstructure:"network"`
IntegrationDirectory string `mapstructure:"integration-directory"`
EnvironmentVariables []string `mapstructure:"envs"`
PropertyFiles []string `mapstructure:"property-files"`
Properties []string `mapstructure:"properties"`
AdditionalDependencies []string `mapstructure:"dependencies"`
MavenRepositories []string `mapstructure:"maven-repositories"`
Traits []string `mapstructure:"traits"`
}
func (command *localRunCmdOptions) validate(cmd *cobra.Command, args []string) error {
// Validate integration files when no image is provided and we are
// not running an already locally-built integration.
if command.Image == "" && command.IntegrationDirectory == "" {
err := validateIntegrationFiles(args)
if err != nil {
return err
}
}
// Validate additional dependencies specified by the user.
err := validateAdditionalDependencies(command.AdditionalDependencies)
if err != nil {
return err
}
// Validate properties file.
err = validatePropertyFiles(command.PropertyFiles)
if err != nil {
return err
}
// If containerize is set then docker image name must be set.
if command.Containerize && command.Image == "" {
return errors.New("containerization is active but no image name has been provided")
}
warnTraitUsages(cmd, command.Traits)
return nil
}
func (command *localRunCmdOptions) init() error {
if command.Containerize {
err := createDockerBaseWorkingDirectory()
if err != nil {
return err
}
err = createDockerWorkingDirectory()
if err != nil {
return err
}
}
setDockerNetworkName(command.Network)
setDockerEnvVars(command.EnvironmentVariables)
return createMavenWorkingDirectory()
}
func (command *localRunCmdOptions) run(cmd *cobra.Command, args []string) error {
// If local run is provided with an image name, it will just run the image locally and exit.
if command.Image != "" && !command.Containerize {
// Run image locally.
err := runIntegrationImage(command.Context, command.Image, cmd.OutOrStdout(), cmd.ErrOrStderr())
if err != nil {
return err
}
return nil
}
hasIntegrationDir := command.IntegrationDirectory != ""
var dependencies []string
if hasIntegrationDir {
// Fetch local dependencies
localBuildDependencies, err := getLocalBuildDependencies(command.IntegrationDirectory)
if err != nil {
return err
}
dependencies = localBuildDependencies
// Local dependencies directory
localDependenciesDirectory := getCustomDependenciesDir(command.IntegrationDirectory)
// The quarkus application files need to be at a specific location i.e.:
// <integration_directory>/../quarkus/quarkus-application.dat
// <integration_directory>/../quarkus/generated-bytecode.jar
localQuarkusDir := getCustomQuarkusDir(command.IntegrationDirectory)
err = util.CopyQuarkusAppFiles(localDependenciesDirectory, localQuarkusDir)
if err != nil {
return err
}
// The dependency jar files need to be at a specific location i.e.:
// <integration_directory>/../lib/main/*.jar
localLibDirectory := getCustomLibDir(command.IntegrationDirectory)
err = util.CopyLibFiles(localDependenciesDirectory, localLibDirectory)
if err != nil {
return err
}
// The Camel K jar file needs to be at a specific location i.e.:
// <integration_directory>/../app/camel-k-integration-X.X.X{-SNAPSHOT}.jar
localAppDirectory := getCustomAppDir(command.IntegrationDirectory)
err = util.CopyAppFile(localDependenciesDirectory, localAppDirectory)
if err != nil {
return err
}
} else {
computedDependencies, err := getDependencies(command.Context, args, command.AdditionalDependencies, command.MavenRepositories, true)
if err != nil {
return err
}
dependencies = computedDependencies
}
// Manage integration properties which may come from files or CLI.
propertyFiles := command.PropertyFiles
if hasIntegrationDir {
localBuildPropertyFiles, err := getLocalBuildProperties(command.IntegrationDirectory)
if err != nil {
return err
}
propertyFiles = localBuildPropertyFiles
}
updatedPropertyFiles, err := updateIntegrationProperties(command.Properties, propertyFiles, hasIntegrationDir)
if err != nil {
return err
}
propertyFiles = updatedPropertyFiles
routes := args
if hasIntegrationDir {
localBuildRoutes, err := getLocalBuildRoutes(command.IntegrationDirectory)
if err != nil {
return err
}
routes = localBuildRoutes
}
// If this is a containerized local run, create, build and run the container image.
if command.Containerize {
// Create and build integration image.
err := createAndBuildIntegrationImage(command.Context, "", false, command.Image, propertyFiles, dependencies, routes, hasIntegrationDir, cmd.OutOrStdout(), cmd.ErrOrStderr())
if err != nil {
return err
}
// Run integration image.
err = runIntegrationImage(command.Context, command.Image, cmd.OutOrStdout(), cmd.ErrOrStderr())
if err != nil {
return err
}
} else {
propertiesDir := util.GetLocalPropertiesDir()
if hasIntegrationDir {
propertiesDir = getCustomPropertiesDir(command.IntegrationDirectory)
}
// Run integration locally.
err := RunLocalIntegrationRunCommand(command.Context, propertyFiles, dependencies, routes, propertiesDir, cmd.OutOrStdout(), cmd.ErrOrStderr())
if err != nil {
return err
}
}
return nil
}
func (command *localRunCmdOptions) deinit() error {
if command.Containerize {
err := deleteDockerBaseWorkingDirectory()
if err != nil {
return err
}
err = deleteDockerWorkingDirectory()
if err != nil {
return err
}
}
if command.IntegrationDirectory != "" {
err := deleteLocalIntegrationDirs(command.IntegrationDirectory)
if err != nil {
return err
}
}
return deleteMavenWorkingDirectory()
}