From 0240783cfbebe574e2f68a40a5c511140917de65 Mon Sep 17 00:00:00 2001 From: MohamedSabthar Date: Tue, 3 Oct 2023 18:59:08 +0530 Subject: [PATCH] Update docker commands to use a named container --- .../ballerina/plugin/BallerinaPlugin.groovy | 64 +++++++++++++++++-- 1 file changed, 58 insertions(+), 6 deletions(-) diff --git a/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy b/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy index fc93bd4..01be1ee 100644 --- a/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy +++ b/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy @@ -38,6 +38,7 @@ class BallerinaExtension { } class BallerinaPlugin implements Plugin { + static final String CONTAINER_NAME = 'bal-container' @Override void apply(Project project) { @@ -295,10 +296,11 @@ class BallerinaPlugin implements Plugin { if (dockerTag != null && dockerTag != '') { ballerinaDockerTag = dockerTag } + startBallerinaContainer(project, ballerinaDockerTag) if (Os.isFamily(Os.FAMILY_WINDOWS)) { - commandLine 'cmd', '/c', "docker run --rm --net=host --user \$(id -u):\$(id -g) -v $project.projectDir/..:/home -v $project.projectDir:/home/ballerina ballerina/ballerina:$ballerinaDockerTag $balJavaDebugParam bal pack --target-dir ${balBuildTarget} ${debugParams}" + commandLine 'cmd', '/c', "docker exec $CONTAINER_NAME bal pack --target-dir ${balBuildTarget} ${debugParams}" } else { - commandLine 'sh', '-c', "docker run --rm --net=host --user \$(id -u):\$(id -g) -v $project.projectDir/..:/home -v $project.projectDir:/home/ballerina ballerina/ballerina:$ballerinaDockerTag $balJavaDebugParam bal pack --target-dir ${balBuildTarget} ${debugParams}" + commandLine 'sh', '-c', "docker exec $CONTAINER_NAME bal pack --target-dir ${balBuildTarget} ${debugParams}" } } else { String distributionBinPath = project.projectDir.absolutePath + "/build/jballerina-tools-${ballerinaExtension.langVersion}/bin" @@ -319,10 +321,11 @@ class BallerinaPlugin implements Plugin { if (dockerTag != null && dockerTag != '') { ballerinaDockerTag = dockerTag } + startBallerinaContainer(project, ballerinaDockerTag) if (Os.isFamily(Os.FAMILY_WINDOWS)) { - commandLine 'cmd', '/c', "docker run --rm --net=host --user \$(id -u):\$(id -g) -v $project.projectDir/..:/home -v $project.projectDir:/home/ballerina ballerina/ballerina:$ballerinaDockerTag $balJavaDebugParam bal test ${graalvmFlag} ${testCoverageParams} ${groupParams} ${disableGroups} ${debugParams}" + commandLine 'cmd', '/c', "docker exec $CONTAINER_NAME bal test ${graalvmFlag} ${testCoverageParams} ${groupParams} ${disableGroups} ${debugParams}" } else { - commandLine 'sh', '-c', "docker run --rm --net=host --user \$(id -u):\$(id -g) -v $project.projectDir/..:/home -v $project.projectDir:/home/ballerina ballerina/ballerina:$ballerinaDockerTag $balJavaDebugParam bal test ${graalvmFlag} ${testCoverageParams} ${groupParams} ${disableGroups} ${debugParams}" + commandLine 'sh', '-c', "docker exec $CONTAINER_NAME bal test ${graalvmFlag} ${testCoverageParams} ${groupParams} ${disableGroups} ${debugParams}" } } else if (Os.isFamily(Os.FAMILY_WINDOWS)) { commandLine 'cmd', '/c', "$balJavaDebugParam $distributionBinPath/bal.bat test --offline ${graalvmFlag} ${testCoverageParams} ${groupParams} ${disableGroups} ${debugParams} && exit %%ERRORLEVEL%%" @@ -395,10 +398,11 @@ class BallerinaPlugin implements Plugin { workingDir project.projectDir environment 'JAVA_OPTS', '-DBALLERINA_DEV_COMPILE_BALLERINA_ORG=true' if (buildOnDocker) { + startBallerinaContainer(project, ballerinaDockerTag) if (Os.isFamily(Os.FAMILY_WINDOWS)) { - commandLine 'cmd', '/c', "docker run --rm --net=host --user \$(id -u):\$(id -g) -v ${project.projectDir}/..:/home -v $project.projectDir:/home/ballerina ballerina/ballerina:$ballerinaDockerTag bal test ${graalvmFlag} ${testCoverageParams} ${groupParams} ${disableGroups} ${debugParams}" + commandLine 'cmd', '/c', "docker exec $CONTAINER_NAME bal test ${graalvmFlag} ${testCoverageParams} ${groupParams} ${disableGroups} ${debugParams}" } else { - commandLine 'sh', '-c', "docker run --rm --net=host --user \$(id -u):\$(id -g) -v ${project.projectDir}/..:/home -v $project.projectDir:/home/ballerina ballerina/ballerina:$ballerinaDockerTag bal test ${graalvmFlag} ${testCoverageParams} ${groupParams} ${disableGroups} ${debugParams}" + commandLine 'sh', '-c', "docker exec $CONTAINER_NAME bal test ${graalvmFlag} ${testCoverageParams} ${groupParams} ${disableGroups} ${debugParams}" } } else { String distributionBinPath = project.projectDir.absolutePath + "/build/jballerina-tools-${ballerinaExtension.langVersion}/bin" @@ -418,6 +422,54 @@ class BallerinaPlugin implements Plugin { delete "$project.projectDir/build" delete "$project.rootDir/target" } + + project.tasks.register('stopBalContainer') { + def stdOut = new ByteArrayOutputStream() + project.exec { + if (Os.isFamily(Os.FAMILY_WINDOWS)) { + commandLine 'cmd', '/c', "docker ps --filter name=$CONTAINER_NAME" + standardOutput = stdOut; + } else { + commandLine 'sh', '-c', "docker ps --filter name=$CONTAINER_NAME" + standardOutput = stdOut; + } + } + if (stdOut.toString().contains(CONTAINER_NAME)) { + project.exec { + println "Stopping Ballerina container." + if (Os.isFamily(Os.FAMILY_WINDOWS)) { + commandLine 'cmd', '/c', "docker stop $CONTAINER_NAME" + } else { + commandLine 'sh', '-c', "docker stop $CONTAINER_NAME" + } + } + } + } } + void startBallerinaContainer(Project project, String ballerinaDockerTag) { + def stdOut = new ByteArrayOutputStream() + project.exec { + println "Checking for Ballerina container." + if (Os.isFamily(Os.FAMILY_WINDOWS)) { + commandLine 'cmd', '/c', "docker ps --filter name=$CONTAINER_NAME" + standardOutput = stdOut; + } else { + commandLine 'sh', '-c', "docker ps --filter name=$CONTAINER_NAME" + standardOutput = stdOut; + } + } + if (!stdOut.toString().contains(CONTAINER_NAME)) { + project.exec { + println "Starting Ballerina container." + if (Os.isFamily(Os.FAMILY_WINDOWS)) { + commandLine 'cmd', '/c', "docker run --rm -dt --name $CONTAINER_NAME --net=host --user \$(id -u):\$(id -g) -v ${project.projectDir}/..:/home -v ${project.projectDir}:/home/ballerina ballerina/ballerina:$ballerinaDockerTag" + } else { + commandLine 'sh', '-c', "docker run --rm -dt --name $CONTAINER_NAME --net=host --user \$(id -u):\$(id -g) -v ${project.projectDir}/..:/home -v ${project.projectDir}:/home/ballerina ballerina/ballerina:$ballerinaDockerTag" + } + } + } else { + println "Ballerina container is already running." + } + } }