Skip to content
Draft
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
55 changes: 54 additions & 1 deletion client/src/main/java/org/apache/cloudstack/ServerDaemon.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.net.URL;
import java.util.Properties;
Expand Down Expand Up @@ -309,9 +310,61 @@ private RequestLog createRequestLog() {
log.setAppend(true);
log.setLogTimeZone("GMT");
log.setLogLatency(true);
createRotateFile(logPath);
return log;
}

private static final String LOGROTATE_CONFIG_NAME = "access";
private static final String[] LOGROTATE_CONFIG_DIRS = {
"/etc/logrotate.d",
"/usr/local/etc/logrotate.d",
"/opt/local/etc/logrotate.d"
};

private void createRotateFile(File logPath) {
final File logrotateDir = findLogrotateConfigDir();
if (logrotateDir == null) {
logger.info("No logrotate.d directory found on this system, skipping access log rotation setup");
return;
}

final File rotateConfigFile = new File(logrotateDir, LOGROTATE_CONFIG_NAME);
if (rotateConfigFile.exists()) {
logger.debug(String.format("Logrotate config [%s] already exists, leaving it untouched", rotateConfigFile.getAbsolutePath()));
return;
}

final String fileContents = logPath.getAbsolutePath() + " {\n"
+ " copytruncate\n"
+ " daily\n"
+ " rotate 14\n"
+ " compress\n"
+ " missingok\n"
+ " create 0644 cloud cloud\n"
+ "}\n";
try (FileWriter fw = new FileWriter(rotateConfigFile)) {
fw.write(fileContents);
} catch (IOException e) {
logger.warn(String.format("Unable to create logrotate config [%s] for the access log, continuing without rotation", rotateConfigFile.getAbsolutePath()), e);
}
}

/**
* Finds the logrotate.d directory for this system. The path is the same across the
* Linux distributions CloudStack supports ({@code /etc/logrotate.d}), but is looked up
* rather than hardcoded so packagings that relocate it (e.g. under a custom prefix) are
* still honoured, and systems without logrotate installed are skipped gracefully.
*/
private File findLogrotateConfigDir() {
for (final String candidate : LOGROTATE_CONFIG_DIRS) {
final File dir = new File(candidate);
if (dir.isDirectory()) {
return dir;
}
}
return null;
}

private URL getResource(String aResource) {
return Thread.currentThread().getContextClassLoader().getResource(aResource);
}
Expand Down
Loading