Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance error handling for tasks #851

Merged
merged 2 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,20 @@ abstract class AbstractServerTask extends AbstractLibertyTask {
}
}

// Use this method to copy over the server.xml file from the defaultServer template.
// Returns true if the server.xml file does not exist and the defaultServer template server.xml file is copied over, false otherwise.
protected boolean copyDefaultServerTemplate(File installDir, File serverDir) {
File serverXmlFile = new File(serverDir, "server.xml")
if (!serverXmlFile.exists()) {
File defaultServerTemplate = new File(installDir, "templates/servers/defaultServer/server.xml")
if (defaultServerTemplate.exists()) {
Files.copy(defaultServerTemplate.toPath(), serverXmlFile.toPath(), StandardCopyOption.REPLACE_EXISTING)
return true;
}
}
return false;
}

protected void copyConfigDirectory() {
//merge default server.env with one in config directory
File configDirServerEnv = new File(server.configDirectory, "server.env")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* (C) Copyright IBM Corporation 2014, 2022.
* (C) Copyright IBM Corporation 2014, 2023.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,7 +33,7 @@ class CreateTask extends AbstractServerTask {
group 'Liberty'
})
outputs.upToDateWhen {
getServerDir(project).exists() && new File(getServerDir(project), 'server.xml')
getServerDir(project).exists() && (new File(getServerDir(project), 'server.xml')).exists()
}
}

Expand Down Expand Up @@ -76,7 +76,9 @@ class CreateTask extends AbstractServerTask {
void create() {
//Checking etc/server.env for outputDirs
Liberty.checkEtcServerEnvProperties(project)
if(!getServerDir(project).exists()){
File serverDir = getServerDir(project)
File serverXmlFile = new File(serverDir, "server.xml")
if(!serverDir.exists()){
def params = buildLibertyMap(project);
if (server.template != null && server.template.length() != 0) {
params.put('template', server.template)
Expand All @@ -85,6 +87,10 @@ class CreateTask extends AbstractServerTask {
params.put('noPassword', server.noPassword)
}
executeServerCommand(project, 'create', params)
} else if (copyDefaultServerTemplate(getInstallDir(project), serverDir)) {
// copied defaultServer template server.xml over for rare case that the server.xml disappears from an existing Liberty server (issue 850)
// if the project contains its own server.xml file, it will get copied over in copyConfigFiles() next
logger.warn("The " + serverXmlFile.getAbsolutePath() + " does not exist. Copying over the defaultServer template server.xml file.")
}
copyConfigFiles()
writeServerPropertiesToXml(project)
Expand Down
21 changes: 15 additions & 6 deletions src/main/groovy/io/openliberty/tools/gradle/tasks/RunTask.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* (C) Copyright IBM Corporation 2014, 2019.
* (C) Copyright IBM Corporation 2014, 2023.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,10 +32,19 @@ class RunTask extends AbstractServerTask {
void run() {
addShutdownHook {
if (isLibertyInstalledAndValid(project)) {
if (getServerDir(project).exists()) {
ServerTask serverTaskStop = createServerTask(project, "stop");
serverTaskStop.setUseEmbeddedServer(server.embedded)
serverTaskStop.execute()
File serverDir = getServerDir(project)
if (serverDir.exists()) {
// copy default server template server.xml file over if the server.xml file does not exist so that the server can be stopped
boolean defaultServerTemplateUsed = copyDefaultServerTemplate(getInstallDir(project), serverDir)
File serverXmlFile = new File(getServerDir(project),"server.xml")
if (serverXmlFile.exists()) {
ServerTask serverTaskStop = createServerTask(project, "stop");
serverTaskStop.setUseEmbeddedServer(server.embedded)
serverTaskStop.execute()
}
if (defaultServerTemplateUsed) {
serverXmlFile.delete() // delete the temporary copy of the server.xml file
}
}
}
}
Expand All @@ -58,6 +67,6 @@ class RunTask extends AbstractServerTask {
run_process.inputStream.eachLine {
println it
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* (C) Copyright IBM Corporation 2014, 2019.
* (C) Copyright IBM Corporation 2014, 2023.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,9 +29,23 @@ class StatusTask extends AbstractServerTask {

@TaskAction
void status() {
def status_process = new ProcessBuilder(buildCommand("status")).redirectErrorStream(true).start()
status_process.inputStream.eachLine {
println it
if (isLibertyInstalledAndValid(project)) {
File serverDir = getServerDir(project)
if (serverDir.exists()) {
File serverXmlFile = new File(serverDir,"server.xml")
if (serverXmlFile.exists()) {
def status_process = new ProcessBuilder(buildCommand("status")).redirectErrorStream(true).start()
status_process.inputStream.eachLine {
println it
}
} else {
logger.error ('The server status cannot be checked. There is no server.xml file in the server.')
}
} else {
logger.error ('The server status cannot be checked. The server has not been created.')
}
} else {
logger.error ('The server status cannot be checked. The runtime has not been installed.')
}
}

Expand Down
26 changes: 20 additions & 6 deletions src/main/groovy/io/openliberty/tools/gradle/tasks/StopTask.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* (C) Copyright IBM Corporation 2014, 2020.
* (C) Copyright IBM Corporation 2014, 2023.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,12 +31,26 @@ class StopTask extends AbstractServerTask {
@TaskAction
void stop() {
if (isLibertyInstalledAndValid(project)) {
if (getServerDir(project).exists()) {
ServerTask serverTaskStop = createServerTask(project, "stop");
serverTaskStop.setUseEmbeddedServer(server.embedded)
serverTaskStop.execute()
File serverDir = getServerDir(project)
if (serverDir.exists()) {
File serverXmlFile = new File(serverDir,"server.xml")
boolean defaultServerTemplateUsed = copyDefaultServerTemplate(getInstallDir(project),serverDir)
if (serverXmlFile.exists()) {
ServerTask serverTaskStop = createServerTask(project, "stop");
serverTaskStop.setUseEmbeddedServer(server.embedded)
serverTaskStop.execute()
} else {
logger.error ('The server cannot be stopped. There is no server.xml file in the server.')
}

if (defaultServerTemplateUsed) {
logger.warn ('The server.xml file was missing in the server during the stop task. Copied the defaultServer template server.xml file into the server temporarily so the stop task could be completed.')
if (!serverXmlFile.delete()) {
logger.error('Could not delete the server.xml file copied from the defaultServer template after stopping the server.')
}
}
} else {
logger.error ('There is no server to stop. The server has not been created.')
logger.error ('There is no server to stop. The server has not been created.')
}
} else {
logger.error ('There is no server to stop. The runtime has not been installed.')
Expand Down
23 changes: 22 additions & 1 deletion src/test/groovy/io/openliberty/tools/gradle/LibertyTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class LibertyTest extends AbstractIntegrationTest{
static File resourceDir = new File("build/resources/test/liberty-test")
static File buildDir = new File(integTestDir, "/liberty-test")
static String buildFilename = "build.gradle"
static File serverXmlFile = new File(buildDir, "/build/wlp/usr/servers/LibertyProjectServer/server.xml")

@BeforeClass
public static void setup() {
Expand Down Expand Up @@ -180,10 +181,30 @@ class LibertyTest extends AbstractIntegrationTest{
throw new AssertionError ("Fail on task libertyStart after second clean.", e)
}

// try deleting the server.xml and see if we can recover
assert serverXmlFile.exists() : 'server.xml file does not exist in LibertyProjectServer'
assert serverXmlFile.delete() : 'server.xml could not be deleted in LibertyProjectServer'

try{
runTasks(buildDir, 'libertyStop')
} catch (Exception e) {
throw new AssertionError ("Fail on task libertyStop after libertyStart.", e)
throw new AssertionError ("Fail on task libertyStop after deleting server.xml.", e)
}

assert !serverXmlFile.exists() : 'server.xml file unexpectedly exists in LibertyProjectServer after libertyStop'

try{
runTasks(buildDir, 'libertyStatus')
} catch (Exception e) {
throw new AssertionError ("Fail on task libertyStatus after deleting server.xml.", e)
}

assert serverXmlFile.exists() : 'server.xml file does not exist in LibertyProjectServer after libertyStatus'

try{
runTasks(buildDir, 'clean')
} catch (Exception e) {
throw new AssertionError ("Fail on task clean after deleting server.xml.", e)
}
}

Expand Down
Loading