Skip to content

Commit

Permalink
Update docker commands to use a named container
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedSabthar committed Oct 3, 2023
1 parent 5b3f0b8 commit 0240783
Showing 1 changed file with 58 additions and 6 deletions.
64 changes: 58 additions & 6 deletions src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class BallerinaExtension {
}

class BallerinaPlugin implements Plugin<Project> {
static final String CONTAINER_NAME = 'bal-container'

@Override
void apply(Project project) {
Expand Down Expand Up @@ -295,10 +296,11 @@ class BallerinaPlugin implements Plugin<Project> {
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"
Expand All @@ -319,10 +321,11 @@ class BallerinaPlugin implements Plugin<Project> {
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%%"
Expand Down Expand Up @@ -395,10 +398,11 @@ class BallerinaPlugin implements Plugin<Project> {
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"
Expand All @@ -418,6 +422,54 @@ class BallerinaPlugin implements Plugin<Project> {
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."
}
}
}

0 comments on commit 0240783

Please sign in to comment.