Run container locally.
diff --git a/pkg/cmd/local_create.go b/pkg/cmd/local_create.go
index fb6b479..7091b2b 100644
--- a/pkg/cmd/local_create.go
+++ b/pkg/cmd/local_create.go
@@ -110,15 +110,13 @@
func (command *localCreateCmdOptions) init(args []string) error {
// If base image construction is enabled create a directory for it.
- if command.BaseImage {
- err := createDockerBaseWorkingDirectory()
- if err != nil {
- return err
- }
+ err := createDockerBaseWorkingDirectory()
+ if err != nil {
+ return err
}
// If integration files are provided an integration image will be built.
- if len(args) > 0 {
+ if !command.BaseImage {
err := createDockerWorkingDirectory()
if err != nil {
return err
@@ -134,47 +132,23 @@
}
func (command *localCreateCmdOptions) run(args []string) error {
- // Create the Dockerfile and build the base image.
- if command.BaseImage {
- err := createAndBuildBaseImage(command.DockerRegistry)
- if err != nil {
- return err
- }
+ // Fetch dependencies.
+ dependencies, err := getDependencies(args, command.AdditionalDependencies, true)
+ if err != nil {
+ return err
}
- // Create integration image if integration files were provided.
- if len(args) > 0 {
- // Manage integration properties which may come from files or CLI.
- propertyFiles, err := updateIntegrationProperties(command.Properties, command.PropertyFiles)
- if err != nil {
- return nil
- }
+ // Manage integration properties which may come from files or CLI.
+ propertyFiles, err := updateIntegrationProperties(command.Properties, command.PropertyFiles)
+ if err != nil {
+ return err
+ }
- // Fetch dependencies.
- dependencies, err := getDependencies(args, command.AdditionalDependencies, true)
- if err != nil {
- return err
- }
-
- // 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(args)
- 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 := GetContainerIntegrationRunCommand(propertyFiles, dependencies, args)
- err = createAndBuildIntegrationImage(command.DockerRegistry, containerCmd, command.ImageName)
- if err != nil {
- return nil
- }
+ // Create and build integration image.
+ err = createAndBuildIntegrationImage(command.DockerRegistry, command.BaseImage,
+ command.ImageName, propertyFiles, dependencies, args)
+ if err != nil {
+ return err
}
return nil
@@ -182,12 +156,10 @@
func (command *localCreateCmdOptions) deinit(args []string) error {
// If base image construction is enabled delete the directory for it.
- if command.BaseImage {
- deleteDockerBaseWorkingDirectory()
- }
+ deleteDockerBaseWorkingDirectory()
// If integration files are provided delete the maven project folder.
- if len(args) > 0 {
+ if !command.BaseImage {
deleteDockerWorkingDirectory()
deleteMavenWorkingDirectory()
}
diff --git a/pkg/cmd/local_run.go b/pkg/cmd/local_run.go
index 99a6fd2..9425602 100644
--- a/pkg/cmd/local_run.go
+++ b/pkg/cmd/local_run.go
@@ -19,7 +19,6 @@
import (
"fmt"
- "strings"
"github.com/pkg/errors"
"github.com/spf13/cobra"
@@ -57,6 +56,7 @@
}
cmd.Flags().Bool("containerize", false, "Run integration in a local container.")
+ cmd.Flags().String("image-name", "", "Integration image name.")
cmd.Flags().String("docker-registry", "", "Docker registry to store intermediate images.")
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.")
@@ -68,6 +68,7 @@
type localRunCmdOptions struct {
*RootCmdOptions
Containerize bool `mapstructure:"containerize"`
+ ImageName string `mapstructure:"image-name"`
DockerRegistry string `mapstructure:"docker-registry"`
PropertyFiles []string `mapstructure:"property-files"`
Properties []string `mapstructure:"properties"`
@@ -76,13 +77,15 @@
func (command *localRunCmdOptions) validate(args []string) error {
// Validate integration files.
- err := validateIntegrationFiles(args)
- if err != nil {
- return err
+ if command.ImageName == "" {
+ err := validateIntegrationFiles(args)
+ if err != nil {
+ return err
+ }
}
// Validate additional dependencies specified by the user.
- err = validateAdditionalDependencies(command.AdditionalDependencies)
+ err := validateAdditionalDependencies(command.AdditionalDependencies)
if err != nil {
return err
}
@@ -90,7 +93,7 @@
// Validate properties file.
err = validatePropertyFiles(command.PropertyFiles)
if err != nil {
- return nil
+ return err
}
// If containerize is set then docker registry must be set.
@@ -98,6 +101,11 @@
return errors.New("containerization is active but no registry has been provided")
}
+ // If containerize is set then docker registry must be set.
+ if command.ImageName != "" && command.DockerRegistry == "" {
+ return errors.New("cannot get image as no registry has been provided")
+ }
+
return nil
}
@@ -106,6 +114,17 @@
}
func (command *localRunCmdOptions) run(args []string) error {
+ // If local run is provided with an image name, it will just run the image locally and exit.
+ if command.ImageName != "" {
+ // Run image locally.
+ err := runIntegrationImage(command.DockerRegistry, command.ImageName)
+ if err != nil {
+ return err
+ }
+
+ return nil
+ }
+
// Fetch dependencies.
dependencies, err := getDependencies(args, command.AdditionalDependencies, true)
if err != nil {
@@ -115,19 +134,29 @@
// Manage integration properties which may come from files or CLI.
propertyFiles, err := updateIntegrationProperties(command.Properties, command.PropertyFiles)
if err != nil {
- return nil
+ return err
}
- // Get integration run command.
- cmd := GetLocalIntegrationRunCommand(propertyFiles, dependencies, args)
+ // 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.DockerRegistry, false, command.ImageName,
+ propertyFiles, dependencies, args)
+ if err != nil {
+ return err
+ }
- // Output command we are about to run.
- fmt.Printf("Executing: %s", strings.Join(cmd.Args, " "))
-
- // Run integration locally.
- err = cmd.Run()
- if err != nil {
- return nil
+ // Run integratgion image.
+ err = runIntegrationImage(command.DockerRegistry, command.ImageName)
+ if err != nil {
+ return err
+ }
+ } else {
+ // Run integration locally.
+ err = RunLocalIntegrationRunCommand(propertyFiles, dependencies, args)
+ if err != nil {
+ return err
+ }
}
return nil
diff --git a/pkg/cmd/util_commands.go b/pkg/cmd/util_commands.go
index 4ddbf9e..0b82c4c 100644
--- a/pkg/cmd/util_commands.go
+++ b/pkg/cmd/util_commands.go
@@ -19,6 +19,7 @@
import (
"context"
+ "fmt"
"os"
"os/exec"
"strings"
@@ -83,9 +84,20 @@
return cmd
}
-// GetLocalIntegrationRunCommand --
-func GetLocalIntegrationRunCommand(properties []string, dependencies []string, routes []string) *exec.Cmd {
- return assembleIntegrationRunCommand(properties, dependencies, routes, util.GetLocalPropertiesDir())
+// RunLocalIntegrationRunCommand --
+func RunLocalIntegrationRunCommand(properties []string, dependencies []string, routes []string) error {
+ cmd := assembleIntegrationRunCommand(properties, dependencies, routes, util.GetLocalPropertiesDir())
+
+ // Output command we are about to run.
+ fmt.Printf("Executing: %s", strings.Join(cmd.Args, " "))
+
+ // Run integration locally.
+ err := cmd.Run()
+ if err != nil {
+ return err
+ }
+
+ return nil
}
// GetContainerIntegrationRunCommand --
diff --git a/pkg/cmd/util_containerization.go b/pkg/cmd/util_containerization.go
index 8def2b3..20c0719 100644
--- a/pkg/cmd/util_containerization.go
+++ b/pkg/cmd/util_containerization.go
@@ -95,17 +95,61 @@
return nil
}
-func createAndBuildIntegrationImage(dockerRegistry string, integrationRunCmd *exec.Cmd, imageName string) error {
+func createAndBuildIntegrationImage(dockerRegistry string, justBaseImage bool, imageName string,
+ propertyFiles []string, dependencies []string, routes []string) error {
docker.RegistryName = dockerRegistry
- // Create the integration image Docker file.
- err := docker.CreateIntegrationImageDockerFile(integrationRunCmd)
+ // Create the Dockerfile and build the base image.
+ err := createAndBuildBaseImage(dockerRegistry)
if err != nil {
return err
}
- // Get the Docker command arguments for building the base image and create the command.
- args := docker.BuildIntegrationImageArgs(imageName)
+ // Create integration image if integration files were provided.
+ if !justBaseImage {
+ // 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 := GetContainerIntegrationRunCommand(propertyFiles, dependencies, routes)
+
+ // 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(imageName)
+ 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 {
+ errors.Errorf("integration image containerization did not run successfully: %v", err)
+ }
+ }
+
+ return nil
+}
+
+func runIntegrationImage(dockerRegistry string, imageName string) error {
+ docker.RegistryName = dockerRegistry
+
+ // Get the docker command line argument for running an image.
+ args := docker.RunIntegrationImageArgs(imageName)
cmd := exec.CommandContext(ctx, "docker", args...)
// Output executed command.
@@ -113,7 +157,7 @@
// Run the command.
if err := cmd.Run(); err != nil {
- errors.Errorf("integration image containerization did not run successfully: %v", err)
+ errors.Errorf("integration image did not run successfully: %v", err)
}
return nil
diff --git a/pkg/util/docker/docker.go b/pkg/util/docker/docker.go
index d377533..2ffa14e 100644
--- a/pkg/util/docker/docker.go
+++ b/pkg/util/docker/docker.go
@@ -95,11 +95,20 @@
func BuildIntegrationImageArgs(imageName string) []string {
// Construct the docker command:
//
- // docker build -f <BaseWorkingDirectory>/Dockerfile -t <dockerRegistry>/<BaseImageName> <MavenWorkingDirectory>
+ // docker build -f <BaseWorkingDirectory>/Dockerfile -t <dockerRegistry>/<ImageName> <MavenWorkingDirectory>
//
return BuildImageArgs(IntegrationWorkingDirectory, imageName, util.MavenWorkingDirectory)
}
+// RunIntegrationImageArgs --
+func RunIntegrationImageArgs(imageName string) []string {
+ // Construct the docker command:
+ //
+ // docker run --network="host" <dockerRegistry>/<ImageName>
+ //
+ return RunImageArgs(imageName, latestTag)
+}
+
// GetContainerWorkspaceDir -- directory inside the container where all the integration files are copied.
func GetContainerWorkspaceDir() string {
return containerFileSeparator + util.DefaultWorkingDirectoryName