Files
smartguide/service/build.gradle

99 lines
3.9 KiB
Groovy

plugins {
id 'base'
}
task createBaseImage(type: Exec) {
commandLine 'docker', 'build', '--platform', "${targetPlatform}", '-f', 'Dockerfile.base', '-t', "${imagePrefix}-base-static:latest", '.'
}
task buildBaseImageIfNeeded {
doLast {
def baseImage = "${imagePrefix}-base-static:latest"
def checkCmd = ["docker", "image", "inspect", baseImage]
def process = checkCmd.execute()
process.waitFor()
if (process.exitValue() != 0) {
println "Base image ${baseImage} not found. Building it..."
def buildCmd = ["docker", "build", "--platform", "${targetPlatform}", "-f", "Dockerfile.base", "-t", baseImage, "."]
def buildProcess = buildCmd.execute([], projectDir)
buildProcess.consumeProcessOutput(System.out, System.err)
if (buildProcess.waitFor() != 0) {
throw new GradleException("Failed to build base image.")
}
} else {
println "Base image ${baseImage} found. Skipping build."
}
}
}
task createImage(type: Exec) {
dependsOn buildBaseImageIfNeeded
commandLine 'docker', 'build', '--platform', "${targetPlatform}", '-t', "${imagePrefix}-service:${imageVersion}", '.'
}
task test(type: Exec) {
dependsOn createImage
commandLine 'docker', 'run', '--rm', '-e', "DOCKER_DEFAULT_PLATFORM='${targetPlatform}'", '--name', "${imageName}-service-test", "${imagePrefix}-service:${imageVersion}", 'test'
ignoreExitValue false
}
task exportOpenApiSpec(type: Exec) {
dependsOn createImage
def openApiDir = layout.buildDirectory.dir('openapi').get().asFile
def openApiFile = new File(openApiDir, 'inse-service.openapi.json')
inputs.dir file('src/main/inse')
outputs.file openApiFile
doFirst {
openApiDir.mkdirs()
}
commandLine 'docker', 'run', '--rm',
'--platform', "${targetPlatform}",
'-w', '/home',
//'-v', "${projectDir}/src/main:/home", the source code is already baked into the image at build time
'-v', "${openApiDir}:/mnt/openapi",
'-e', "DOCKER_DEFAULT_PLATFORM='${targetPlatform}'",
'--name', "${imageName}-service-openapi",
'--entrypoint', 'python',
"${imagePrefix}-service:${imageVersion}",
'-c',
"import json; from inse.main import app; open('/mnt/openapi/inse-service.openapi.json', 'w').write(json.dumps(app.openapi(), indent=2))"
}
task runServeContainer(type: Exec) {
dependsOn createImage
commandLine 'docker', 'run', '--rm', '-v', "${projectDir}/src/main:/home", '-v', "${projectDir}/src/data:/data", '-v', '/var/run/docker.sock:/var/run/docker.sock', '-p', '8000:8000', '-e', "DOCKER_DEFAULT_PLATFORM='${targetPlatform}'", '-e', "INSE_AUTH_STANDALONE=true", '-e', 'INSE_OPENALEX_WORKS_API_KEY', '--name', "${imageName}-service-serve", '--detach', "${imagePrefix}-service:${imageVersion}", 'run', '--reload', '--reload-dir', './inse'
}
task stopServeContainer(type: Exec) {
commandLine 'docker', 'stop', "${imageName}-service-serve"
ignoreExitValue true
}
task serve(type: Task) {
dependsOn runServeContainer
finalizedBy stopServeContainer
doLast {
ProcessBuilder processBuilder = new ProcessBuilder('docker', 'logs', '-f', "${imageName}-service-serve")
Process process = processBuilder.start();
Thread redirect = new Thread() {
public void run() {
BufferedReader r = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line;
while ((line = r.readLine()) != null) {
println(line);
}
}
};
redirect.start();
new Scanner(System.in).nextLine();
process.destroyForcibly();
int exitCode = process.waitFor();
redirect.join();
print("Exit code: " + exitCode);
}
}
build {
dependsOn createImage, exportOpenApiSpec, test
}