Skip to content

Commit

Permalink
5804 - Rollback griffin libreoffice daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Baptiste Petit authored and croftophile committed Oct 15, 2019
1 parent e52549c commit d7f42b4
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 109 deletions.
4 changes: 3 additions & 1 deletion libreoffice/griffin
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

java -Dfile.encoding=UTF-8 -jar /vitam/bin/worker/griffins/libreoffice-griffin/libreoffice-jar-with-dependencies.jar $1
LIBREOFFICE_HOME=$(dirname $(dirname $(readlink -f $(which $(compgen -c | grep libreoffice | head -1)))))

java -Doffice.home="$LIBREOFFICE_HOME" -Dfile.encoding=UTF-8 -jar /vitam/bin/worker/griffins/libreoffice-griffin/libreoffice-jar-with-dependencies.jar $1
3 changes: 0 additions & 3 deletions libreoffice/libreoffice

This file was deleted.

43 changes: 0 additions & 43 deletions libreoffice/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,28 +99,6 @@
<prefix>/vitam/bin/worker/griffins/${unix.name}</prefix>
</mapper>
</data>
<data>
<src>${basedir}/libreoffice</src>
<type>file</type>
<mapper>
<type>perm</type>
<user>${unix.user}</user>
<group>${unix.group}</group>
<filemode>750</filemode>
<prefix>/vitam/bin/worker/griffins/${unix.name}</prefix>
</mapper>
</data>
<data>
<src>${basedir}/soffice.service</src>
<type>file</type>
<mapper>
<type>perm</type>
<user>${unix.user}</user>
<group>${unix.group}</group>
<filemode>0755</filemode>
<prefix>/lib/systemd/system</prefix>
</mapper>
</data>
</dataSet>
</configuration>
</plugin>
Expand Down Expand Up @@ -190,33 +168,12 @@
<source>
<location>${project.basedir}/griffin</location>
</source>
<source>
<location>${project.basedir}/libreoffice</location>
</source>
<source>
<location>${project.build.directory}/libreoffice-jar-with-dependencies.jar</location>
</source>
</sources>
</mapping>
<mapping>
<directory>/usr/lib/systemd/system/</directory>
<filemode>644</filemode>
<username>root</username>
<groupname>root</groupname>
<directoryIncluded>false</directoryIncluded>
<sources>
<source>
<location>${project.basedir}/soffice.service</location>
</source>
</sources>
</mapping>
</mappings>
<postinstallScriptlet>
<script>%systemd_post soffice.service;systemctl enable soffice.service;systemctl start soffice.service</script>
</postinstallScriptlet>
<preremoveScriptlet>
<script>%systemd_preun soffice.service</script>
</preremoveScriptlet>
</configuration>
</plugin>
</plugins>
Expand Down
13 changes: 0 additions & 13 deletions libreoffice/soffice.service

This file was deleted.

7 changes: 0 additions & 7 deletions libreoffice/src/deb/control/postinst

This file was deleted.

6 changes: 0 additions & 6 deletions libreoffice/src/deb/control/prerm

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright French Prime minister Office/SGMAP/DINSIC/Vitam Program (2015-2019)
*
* [email protected]
*
* This software is a computer program whose purpose is to implement a digital archiving back-office system managing
* high volumetry securely and efficiently.
*
* This software is governed by the CeCILL 2.1 license under French law and abiding by the rules of distribution of free
* software. You can use, modify and/ or redistribute the software under the terms of the CeCILL 2.1 license as
* circulated by CEA, CNRS and INRIA at the following URL "http://www.cecill.info".
*
* As a counterpart to the access to the source code and rights to copy, modify and redistribute granted by the license,
* users are provided only with a limited warranty and the software's author, the holder of the economic rights, and the
* successive licensors have only limited liability.
*
* In this respect, the user's attention is drawn to the risks associated with loading, using, modifying and/or
* developing or reproducing the software by the user in light of its specific status of free software, that may mean
* that it is complicated to manipulate, and that also therefore means that it is reserved for developers and
* experienced professionals having in-depth computer knowledge. Users are therefore encouraged to load and test the
* software's suitability as regards their requirements in conditions enabling the security of their systems and/or data
* to be ensured and, more generally, to use and operate it in the same conditions as regards security.
*
* The fact that you are presently reading this means that you have had knowledge of the CeCILL 2.1 license and that you
* accept its terms.
*/
package fr.gouv.vitam.griffins.libreoffice;

import org.jodconverter.office.LocalOfficeManager;
import org.jodconverter.office.OfficeException;
import org.jodconverter.office.OfficeManager;
import org.jodconverter.task.OfficeTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.ServerSocket;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.Paths;

public class AutoClosableOfficeManager implements AutoCloseable, OfficeManager {

private final OfficeManager manager;

public AutoClosableOfficeManager(int libreOffice) throws IOException {
this.manager = LocalOfficeManager.builder()
.portNumbers(libreOffice)
.workingDir(Files.createTempDirectory("griffin-libreoffice-").toFile())
.install()
.build();
}

@Override
public void close() throws Exception {
this.stop();
}

@Override
public void execute(OfficeTask task) throws OfficeException {
manager.execute(task);
}

@Override
public boolean isRunning() {
return manager.isRunning();
}

@Override
public void start() throws OfficeException {
manager.start();
}

@Override
public void stop() throws OfficeException {
manager.stop();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
import fr.gouv.vitam.griffins.libreoffice.pojo.Result;
import fr.gouv.vitam.griffins.libreoffice.status.GriffinStatus;
import org.jodconverter.LocalConverter;
import org.jodconverter.office.ExternalOfficeManagerBuilder;
import org.jodconverter.office.OfficeException;
import org.jodconverter.office.OfficeManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -65,7 +63,6 @@ public class BatchProcessor {
public static final String parametersFileName = "parameters.json";
public static final String resultFileName = "result.json";
public static final String inputFilesDirName = "input-files";

private static final String FILTER_DATA = "FilterData";
private static final String FILTER_NAME = "FilterName";
private static final String FILTER_OPTIONS = "FilterOptions";
Expand All @@ -83,9 +80,9 @@ public BatchStatus execute() {
long startTime = System.currentTimeMillis();
String batchProcessingId = batchDirectory.getFileName().toString();

OfficeManager officeManager = new ExternalOfficeManagerBuilder().build();
try (PortReserver portReserver = new PortReserver();
AutoClosableOfficeManager officeManager = new AutoClosableOfficeManager(portReserver.getLibreOfficePort())) {

try {
File file = batchDirectory.resolve(parametersFileName).toFile();
parameters = mapper.readValue(file, Parameters.class);

Expand All @@ -112,8 +109,6 @@ public BatchStatus execute() {
} catch (Exception e) {
logger.error("{}", e);
return BatchStatus.error(batchProcessingId, startTime, e);
} finally {
stopOfficeManager(officeManager);
}
}

Expand Down Expand Up @@ -215,12 +210,4 @@ private boolean isInteger(String value) {
return false;
}
}

private void stopOfficeManager(OfficeManager manager) {
try {
manager.stop();
} catch (OfficeException ex) {
logger.error("{}", ex);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright French Prime minister Office/SGMAP/DINSIC/Vitam Program (2015-2019)
*
* [email protected]
*
* This software is a computer program whose purpose is to implement a digital archiving back-office system managing
* high volumetry securely and efficiently.
*
* This software is governed by the CeCILL 2.1 license under French law and abiding by the rules of distribution of free
* software. You can use, modify and/ or redistribute the software under the terms of the CeCILL 2.1 license as
* circulated by CEA, CNRS and INRIA at the following URL "http://www.cecill.info".
*
* As a counterpart to the access to the source code and rights to copy, modify and redistribute granted by the license,
* users are provided only with a limited warranty and the software's author, the holder of the economic rights, and the
* successive licensors have only limited liability.
*
* In this respect, the user's attention is drawn to the risks associated with loading, using, modifying and/or
* developing or reproducing the software by the user in light of its specific status of free software, that may mean
* that it is complicated to manipulate, and that also therefore means that it is reserved for developers and
* experienced professionals having in-depth computer knowledge. Users are therefore encouraged to load and test the
* software's suitability as regards their requirements in conditions enabling the security of their systems and/or data
* to be ensured and, more generally, to use and operate it in the same conditions as regards security.
*
* The fact that you are presently reading this means that you have had knowledge of the CeCILL 2.1 license and that you
* accept its terms.
*/
package fr.gouv.vitam.griffins.libreoffice;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class PortReserver implements AutoCloseable {
private static final Logger LOGGER = LoggerFactory.getLogger(PortReserver.class);
private static final Path LOCK_DIRECTORY = Paths.get(System.getProperty("java.io.tmpdir"), "griffin-libre-office-locks");
private static final int MAX_ATTEMPT_NUMBER = 4;

private final int libreOfficePort;

public PortReserver() throws IOException {
createLockDirectory();
this.libreOfficePort = findAvailablePort(MAX_ATTEMPT_NUMBER);
}

private void createLockDirectory() throws IOException {
if (Files.notExists(LOCK_DIRECTORY)) {
try {
Files.createDirectory(LOCK_DIRECTORY);
} catch (FileAlreadyExistsException e) {
LOGGER.info("ignore error {}", e);
}
}
}

private int findAvailablePort(int numberOfAttempt) throws IOException {
try (ServerSocket socket = new ServerSocket(0, 1, InetAddress.getLocalHost())) {
int localPort = socket.getLocalPort();
Files.createFile(LOCK_DIRECTORY.resolve(String.format("%d.lock", localPort)));
return localPort;
} catch (FileAlreadyExistsException e) {
LOGGER.warn("{}", e);

if (numberOfAttempt > MAX_ATTEMPT_NUMBER) {
throw new RuntimeException("Reach max retries for find available port and yet there is still:", e);
}

return findAvailablePort(numberOfAttempt + 1);
}
}

@Override
public void close() throws Exception {
Files.delete(LOCK_DIRECTORY.resolve(String.format("%d.lock", this.libreOfficePort)));
}

public int getLibreOfficePort() {
return libreOfficePort;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@
import fr.gouv.vitam.griffins.libreoffice.pojo.Result;
import fr.gouv.vitam.griffins.libreoffice.pojo.Values;
import fr.gouv.vitam.griffins.libreoffice.status.GriffinStatus;
import org.jodconverter.office.LocalOfficeManager;
import org.jodconverter.office.OfficeException;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
Expand Down Expand Up @@ -78,19 +74,6 @@ public class MainTest {
@Rule
public TemporaryFolder tmpGriffinFolder = new TemporaryFolder();

private static LocalOfficeManager officeManager;

@BeforeClass
public static void setup() throws OfficeException {
officeManager = LocalOfficeManager.install();
officeManager.start();
}

@AfterClass
public static void clean() throws OfficeException {
officeManager.stop();
}

@Test
public void should_GENERATE_DOC_file_from_odt() {
// Given
Expand Down Expand Up @@ -174,13 +157,14 @@ public void should_GENERATE_for_multiple_instance() {
assertThat(actual).hasSize(4);
assertThat(actual).extracting(Output::getAction).allMatch(a -> a.equals(GENERATE));
assertThat(actual).extracting(Output::getStatus).allMatch(s -> s.equals(OK));
assertThat(actual).extracting(Output::getOutputName).containsExactlyElementsOf(names.stream().map(s -> String.format("%s.pdf", s)).collect(Collectors.toList()));
assertThat(actual).extracting(Output::getOutputName)
.containsExactlyElementsOf(names.stream().map(s -> String.format("%s.pdf", s)).collect(Collectors.toList()));
}

@Test
public void should_GENERATE_for_big_number_of_file() {
String sourceName = "test.odt";
List<String> names = IntStream.range(0, 400)
List<String> names = IntStream.range(0, 30)
.mapToObj(i -> String.format("%d_%s", i, sourceName))
.collect(Collectors.toList());

Expand All @@ -207,7 +191,7 @@ public void should_GENERATE_for_big_number_of_file() {
// Then
assertThat(actual).extracting(Output::getAction).allMatch(a -> a.equals(GENERATE));
assertThat(actual).extracting(Output::getStatus).allMatch(s -> s.equals(OK));
assertThat(actual).hasSize(400);
assertThat(actual).hasSize(names.size());
}

@Test
Expand Down Expand Up @@ -336,7 +320,8 @@ private Parameters generateBatch(Action action, Input input, String sourceName)
parameters.setActions(Collections.singletonList(action));
parameters.setInputs(Collections.singletonList(input));

FileAttribute<Set<PosixFilePermission>> setFileAttribute = PosixFilePermissions.asFileAttribute(Files.getPosixFilePermissions(batchFolder, NOFOLLOW_LINKS));
FileAttribute<Set<PosixFilePermission>> setFileAttribute =
PosixFilePermissions.asFileAttribute(Files.getPosixFilePermissions(batchFolder, NOFOLLOW_LINKS));
File parametersFile = Files.createFile(Paths.get(batchFolder.toString(), parametersFileName), setFileAttribute).toFile();

objectMapper.writer().writeValue(parametersFile, parameters);
Expand Down

0 comments on commit d7f42b4

Please sign in to comment.