Skip to content

Commit

Permalink
feat: adds support for starting in debug mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
outofcoffee committed May 1, 2024
1 parent e6c2afe commit f9b2df0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 19 deletions.
4 changes: 4 additions & 0 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package cmd

import (
"fmt"
"gatehill.io/imposter/config"
"gatehill.io/imposter/engine"
"gatehill.io/imposter/fileutil"
Expand Down Expand Up @@ -46,6 +47,7 @@ var upFlags = struct {
environment []string
dirMounts []string
recursiveConfigScan bool
debugMode bool
}{}

// upCmd represents the up command
Expand Down Expand Up @@ -107,6 +109,7 @@ If CONFIG_DIR is not specified, the current working directory is used.`,
EnableFileCache: upFlags.enableFileCache,
Environment: buildStartEnvironment(upFlags.environment),
DirMounts: upFlags.dirMounts,
DebugMode: upFlags.debugMode,
}
start(&lib, startOptions, configDir, upFlags.restartOnChange)
},
Expand All @@ -126,6 +129,7 @@ func init() {
upCmd.Flags().StringArrayVarP(&upFlags.environment, "env", "e", []string{}, "Explicit environment variables to set")
upCmd.Flags().StringArrayVar(&upFlags.dirMounts, "mount-dir", []string{}, "(Docker engine type only) Extra directory bind-mounts in the form HOST_PATH:CONTAINER_PATH (e.g. $HOME/somedir:/opt/imposter/somedir) or simply HOST_PATH, which will mount the directory at /opt/imposter/<dir>")
upCmd.Flags().BoolVarP(&upFlags.recursiveConfigScan, "recursive-config-scan", "r", false, "Scan for config files in subdirectories")
upCmd.Flags().BoolVar(&upFlags.debugMode, "debug-mode", false, fmt.Sprintf("Enable JVM debug mode and listen on port %v", engine.DefaultDebugPort))
registerEngineTypeCompletions(upCmd)
rootCmd.AddCommand(upCmd)
}
Expand Down
3 changes: 3 additions & 0 deletions engine/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type StartOptions struct {
EnableFileCache bool
Environment []string
DirMounts []string
DebugMode bool
}

type PullPolicy int
Expand Down Expand Up @@ -95,3 +96,5 @@ type ManagedMock struct {
Port int
Health MockHealth
}

const DefaultDebugPort = 8000
6 changes: 5 additions & 1 deletion engine/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,11 @@ func SanitiseVersionOutput(s string) string {
}

func BuildEnv(options StartOptions, includeHome bool) []string {
return buildEnvFromParent(os.Environ(), options, includeHome)
env := buildEnvFromParent(os.Environ(), options, includeHome)
if options.DebugMode {
env = append(env, fmt.Sprintf("JAVA_TOOL_OPTIONS=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=0.0.0.0:%v", DefaultDebugPort))
}
return env
}

func buildEnvFromParent(parentEnv []string, options StartOptions, includeHome bool) []string {
Expand Down
50 changes: 32 additions & 18 deletions engine/docker/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,35 +70,24 @@ func (d *DockerMockEngine) startWithOptions(wg *sync.WaitGroup, options engine.S
stopDuplicateContainers(d, cli, ctx, mockHash)
}

containerPort := nat.Port(fmt.Sprintf("%d/tcp", options.Port))
hostPort := fmt.Sprintf("%d", options.Port)

// if not specified, falls back to default in container image
containerUser := viper.GetString("docker.containerUser")
logger.Tracef("container user: %s", containerUser)

exposedPorts, portBindings := buildPorts(options)
resp, err := cli.ContainerCreate(ctx, &container.Config{
Image: d.provider.imageAndTag,
Cmd: []string{
"--configDir=" + containerConfigDir,
fmt.Sprintf("--listenPort=%d", options.Port),
},
Env: buildEnv(options),
ExposedPorts: nat.PortSet{
containerPort: {},
},
Labels: containerLabels,
User: containerUser,
Env: buildEnv(options),
ExposedPorts: exposedPorts,
Labels: containerLabels,
User: containerUser,
}, &container.HostConfig{
Binds: buildBinds(d, options),
PortBindings: nat.PortMap{
containerPort: []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: hostPort,
},
},
},
Binds: buildBinds(d, options),
PortBindings: portBindings,
}, nil, nil, "")
if err != nil {
logger.Fatal(err)
Expand All @@ -125,6 +114,31 @@ func (d *DockerMockEngine) startWithOptions(wg *sync.WaitGroup, options engine.S
return up
}

func buildPorts(options engine.StartOptions) (nat.PortSet, nat.PortMap) {
ports := map[int]int{
options.Port: options.Port,
}
if options.DebugMode {
ports[engine.DefaultDebugPort] = engine.DefaultDebugPort
}

exposedPorts := nat.PortSet{}
portBindings := nat.PortMap{}
for hp, cp := range ports {
containerPort := nat.Port(fmt.Sprintf("%d/tcp", cp))
hostPort := fmt.Sprintf("%d", hp)

exposedPorts[containerPort] = struct{}{}
portBindings[containerPort] = []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: hostPort,
},
}
}
return exposedPorts, portBindings
}

func buildEnv(options engine.StartOptions) []string {
env := engine.BuildEnv(options, false)
if options.EnableFileCache {
Expand Down

0 comments on commit f9b2df0

Please sign in to comment.