Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/maven/fixes/7.0' into maven/rele…
Browse files Browse the repository at this point in the history
…ase/7.0
  • Loading branch information
metaventis-build committed Oct 17, 2023
2 parents ac25dfa + b14a0ca commit 72281c7
Show file tree
Hide file tree
Showing 18 changed files with 333 additions and 37 deletions.
8 changes: 8 additions & 0 deletions deploy/docker/compose/src/main/compose/2_connector-common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ services:
DATABASE_NAME: "${SERVICES_EDU_CONNECTOR_DATABASE_NAME:-connector}"
DATABASE_USER: "${SERVICES_EDU_CONNECTOR_DATABASE_USER:-connector}"
DATABASE_PASSWORD: "${SERVICES_EDU_CONNECTOR_DATABASE_PASS:-connector}"
ONLYOFFICE_DOCUMENT_SERVER: "${SERVICES_EDU_CONNECTOR_ONLYOFFICE_DOCUMENT_SERVER:-}"
ONLYOFFICE_PLUGIN_URL: "${SERVICES_EDU_CONNECTOR_ONLYOFFICE_PLUGIN_URL:-}"
ONLYOFFICE_JWT_SECRET: "${SERVICES_EDU_CONNECTOR_ONLYOFFICE_JWT_SECRET:-}"
UPLOAD_FILE_SIZE: "${SERVICES_EDU_CONNECTOR_UPLOAD_FILE_SIZE:-512M}"
POST_MAX_SIZE: "${SERVICES_EDU_CONNECTOR_POST_MAX_SIZE:-513M}"
MEMORY_LIMIT: "${SERVICES_EDU_CONNECTOR_MEMORY_LIMIT:-1024M}"
MOODLE_BASE_DIR: "${SERVICES_EDU_CONNECTOR_MOODLE_BASE_DIR:-}"
MOODLE_TOKEN: "${SERVICES_EDU_CONNECTOR_MOODLE_TOKEN:-}"
volumes:
- "services-edu-connector-data:/var/data"
depends_on:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: '3.7'
services:

services-edu-connector-database:
image: "${docker.registry}/${docker.repository}/${docker.prefix}-deploy-docker-build-postgres:${docker.tag}"
image: "${docker.registry}/${docker.repository}/${docker.prefix}-deploy-docker-build-postgresql:${docker.tag}"

services-edu-connector-service:
image: "${docker.registry}/community/${docker.prefix}-service:${docker.tag}"
Expand Down
2 changes: 1 addition & 1 deletion deploy/docker/helm/src/main/chart/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ apiVersion: v2
name: edu-sharing-services-connector
description: Helm chart for edu-sharing connector app
type: application
version: 0.1.0
version: 8.0.9998
appVersion: 0.1.0
14 changes: 14 additions & 0 deletions deploy/docker/helm/src/main/chart/templates/statefulset.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ spec:
value: {{ required "A valid .Values.passwordDB is required!" .Values.passwordDB }}
- name: ONLYOFFICE_DOCUMENT_SERVER
value: {{ .Values.onlyofficeDocumentServer | default "" }}
- name: ONLYOFFICE_PLUGIN_URL
value: {{ .Values.onlyofficePluginUrl | default "" }}
- name: ONLYOFFICE_JWT_SECRET
value: {{ .Values.onlyofficeJwtSecret | default "" }}
- name: MOODLE_BASE_DIR
value: {{ .Values.moodleBaseDir | default "" }}
- name: MOODLE_TOKEN
value: {{ .Values.moodleToken | default "" }}
- name: UPLOAD_FILE_SIZE
value: {{ .Values.uploadFileSize | default "512M" }}
- name: POST_MAX_SIZE
value: {{ .Values.postMaxSize | default "513M" }}
- name: MEMORY_LIMIT
value: {{ .Values.memoryLimit | "1024M" }}
livenessProbe:
httpGet:
path: /metadata
Expand Down
2 changes: 1 addition & 1 deletion deploy/docker/helm/src/main/chart/values.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
baseUrl:
storageClassName:
clusterIssuer: letsencrypt
version: maven-fixes-7.0-SNAPSHOT
version: maven-fixes-8.0-SNAPSHOT
3 changes: 2 additions & 1 deletion service/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ RUN set -eux \
&& adduser www-data appuser

RUN set -eux \
&& chown -R appuser:appuser /etc/apache2 $ROOT $DATA
&& chown -R appuser:appuser /etc/apache2 $ROOT $DATA \
&& chown -R appuser:appuser $PHP_INI_DIR

USER appuser

Expand Down
10 changes: 10 additions & 0 deletions service/src/main/docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ connector_database_name=${DATABASE_NAME//\/&/\\&}

# OPTIONALS

upload_max_filesize="${UPLOAD_FILE_SIZE:-512M}"
post_max_size="${POST_MAX_SIZE:-513M}"
memory_limit="${MEMORY_LIMIT:-1024M}"

only_office_document_server="${ONLYOFFICE_DOCUMENT_SERVER:-}"
only_office_plugin_url="${ONLYOFFICE_PLUGIN_URL:-}"
only_office_jwt_secret="${ONLYOFFICE_JWT_SECRET:-}"
Expand All @@ -32,6 +36,8 @@ moodle_base_dir="${MOODLE_BASE_DIR:-}"
# shellcheck disable=SC2153
moodle_token="${MOODLE_TOKEN:-}"

php_ini=$PHP_INI_DIR/php.ini

sed -i "s|define('WWWURL', '.*')|define('WWWURL', '${connector_url}')|g" "${conf}"
sed -i "s|define('DOCROOT', '.*')|define('DOCROOT', '${ROOT}')|g" "${conf}"
sed -i "s|define('DATA', '.*')|define('DATA', '${DATA}')|g" "${conf}"
Expand All @@ -43,6 +49,10 @@ sed -i "s|define('DBUSER', '.*')|define('DBUSER', '${connector_database_user}')|
sed -i "s|define('DBPASSWORD', '.*')|define('DBPASSWORD', '${connector_database_password}')|g" "${conf}"
sed -i "s|define('DBNAME', '.*')|define('DBNAME', '${connector_database_name}')|g" "${conf}"

sed -i -r "s|upload_max_filesize.*|upload_max_filesize = ${upload_max_filesize}|" "${php_ini}"
sed -i -r "s|post_max_size.*|post_max_size = ${post_max_size}|" "${php_ini}"
sed -i -r "s|memory_limit.*|memory_limit = ${memory_limit}|" "${php_ini}"

sed -i "s|define('ONLYOFFICE_DOCUMENT_SERVER', '.*')|define('ONLYOFFICE_DOCUMENT_SERVER', '${only_office_document_server}')|g" "${conf}"
sed -i "s|define('ONLYOFFICE_PLUGIN_URL', '.*')|define('ONLYOFFICE_PLUGIN_URL', '${only_office_plugin_url}')|g" "${conf}"
sed -i "s|define('ONLYOFFICE_JWT_SECRET', '.*')|define('ONLYOFFICE_JWT_SECRET', '${only_office_jwt_secret}')|g" "${conf}"
Expand Down
9 changes: 7 additions & 2 deletions service/src/main/php/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
<?php

use connector\lib\BlockingMiddleware;
use connector\lib\Connector;
use connector\lib\EduRestClient;
use connector\lib\Logger;
use connector\lib\MetadataGenerator;
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use Slim\App;
use Slim\Container;
use Slim\Views\Twig;

error_reporting(0); //do not change, can cause ajax problems

$container = new \Slim\Container;
$app = new \Slim\App([$container, 'settings' => [
$container = new Container;
$app = new App([$container, 'settings' => [
'displayErrorDetails' => true,
'debug' => true,
'whoops.editor' => 'sublime',
Expand All @@ -31,6 +34,8 @@
return $view;
};

$app->add(new BlockingMiddleware());

$app->get('/', function (Request $request, Response $response) {
$this->get('log')->info($request->getUri());
$connector = new Connector($this->get('log'), $this, $response);
Expand Down
8 changes: 8 additions & 0 deletions service/src/main/php/cacheCleaner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

use connector\lib\CacheCleaner;

require_once 'src/lib/CacheCleaner.php';

$cacheCleaner = new CacheCleaner();
$cacheCleaner->run();
41 changes: 41 additions & 0 deletions service/src/main/php/src/lib/BlockingMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types=1);

namespace connector\lib;

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

/**
* Class BlockingMiddleware
*
* includes logic to block the connector service for the
* duration of the cache cleaning process.
* This is needed in order to prevent potential invalid state creation.
*
* @author Marian Ziegler <[email protected]>
*/
class BlockingMiddleware
{
/**
* Function __invoke
*
* Function to run the middleware as per the Slim PHP documentation
*
* @param Request $request
* @param Response $response
* @param callable $next
* @return Response
*/
public function __invoke(Request $request, Response $response, callable $next): Response
{
$retryCount = 0;
while (file_exists(__DIR__ . '/' . CacheCleaner::LOCK_FILE_NAME) && $retryCount <= 10) {
sleep(1);
$retryCount += 1;
}
if (file_exists(__DIR__ . '/' . CacheCleaner::LOCK_FILE_NAME)) {
return $response->withStatus(503);
}
return $next($request, $response);
}
}
136 changes: 136 additions & 0 deletions service/src/main/php/src/lib/CacheCleaner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php declare(strict_types=1);

namespace connector\lib;

require_once __DIR__ . '/../../vendor/autoload.php';

use connector\tools\h5p\H5P;
use Exception;
use Monolog\Logger as MonoLogger;

/**
* Class CacheCleaner
*
* encapsulates logic for cleaning the connector service H5P cache.
* There is only one public function to be called: run(). Invoke it
* to perform the cleaning process and behold its might!
*
* @author Marian Ziegler <[email protected]>
*/
class CacheCleaner
{
public const LOCK_FILE_NAME = 'LOCK';
private MonoLogger $logger;
private Database $database;
private H5P $h5p;

/**
* CacheCleaner constructor
*/
public function __construct() {
$this->init();
}

/**
* Function init
*
* sets up dependencies
*/
private function init(): void {
$logInitializer = new Logger();
$this->logger = $logInitializer->getLog();
$this->database = new Database();
$this->h5p = new H5P();
}

/**
* Function run
*
* This is the main workhorse of the class
*
* @return void
*/
public function run(): void {
$this->logger->info('### Cache cleaner started ###');
try {
$this->lock();
$this->database->beginTransaction();
$this->clearH5pTables();
$this->clearH5pDirectories();
! $this->database->commit() && throw new Exception('### Database commit failed. Script terminated ###');
} catch (Exception $exception) {
$this->logger->error($exception->getMessage());
$this->logger->error('### Database transaction rollback started. ###');
$this->database->rollBack();
} finally {
try {
$this->unlock();
$this->logger->info('### Cache cleaner ran successfully ###');
} catch (Exception $exception) {
$this->logger->error($exception->getMessage());
}
}
}

/**
* Function clearH5pTables
*
* clears H5P-related tables
*
* @throws Exception
*/
private function clearH5pTables(): void {
$libraryLanguageRes = $this->database->query('TRUNCATE TABLE h5p_libraries_languages');
$contentsLibrariesRes = $this->database->query('TRUNCATE TABLE h5p_contents_libraries');
$contentsRes = $this->database->query('TRUNCATE TABLE h5p_contents');
$librariesRes = $this->database->query('TRUNCATE TABLE h5p_libraries');
$librariesLanguagesRes = $this->database->query('TRUNCATE TABLE h5p_libraries_languages');
$librariesLibrariesRes = $this->database->query('TRUNCATE TABLE h5p_libraries_libraries');
if (in_array(false, [$libraryLanguageRes, $contentsLibrariesRes, $contentsRes, $librariesRes, $librariesLanguagesRes, $librariesLibrariesRes], true)) {
throw new Exception('### Cache cleaner error: Database operation failed. Script will be terminated. ###');
}
}

/**
* Function clearH5pDirectories
*
* deletes all files and folders from the H5P-related directories within the cache directory
*
* @throws Exception
*/
private function clearH5pDirectories(): void {
$directories = ['content', 'exports', 'libraries', 'editor', 'temp'];
try {
foreach ($directories as $directory) {
$this->h5p->rrmdir($this->h5p->H5PFramework->get_h5p_path() . '/' . $directory, true);
}
} catch (Exception $exception) {
throw new Exception('### Cache cleaner error: ' . $exception->getMessage() . '. Script will be terminated. ###');
}
}

/**
* Function lock
*
* creates a lock file in order to lock the connector service
* for the duration of the cache cleaning process
*
* @throws Exception
*/
private function lock(): void {
$lockFile = fopen(static::LOCK_FILE_NAME, "w");
$lockFile === false && throw new Exception('### Cache cleaner error: Cannot create lock file. Script will be terminated. ###');
}

/**
* Function unlock
*
* deletes the lock file in order to unlock the connector service
*
* @throws Exception
*/
private function unlock(): void {
$isFileDeleted = unlink(static::LOCK_FILE_NAME);
! $isFileDeleted && throw new Exception('### Cache cleaner error: Cannot delete lock file. Script will be terminated. ###');
}
}
16 changes: 14 additions & 2 deletions service/src/main/php/src/lib/EduRestClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace connector\lib;

use connector\tools\h5p\H5PFramework;

define('APPID', 'educonnector');

class EduRestClient
Expand Down Expand Up @@ -93,7 +95,7 @@ public function unlockNode($nodeId) {
throw new \Exception('Error unlocking node ' . $nodeId, $httpcode);
}

public function getContent($node, $downloadUrl=NULL){
public function getContent($node, $downloadUrl = null, $isH5p = false){
if ($node->node->contentUrl){
$contentUrl = $node->node->contentUrl; //repo-version 5.0 or older
}else{
Expand Down Expand Up @@ -122,14 +124,24 @@ public function getContent($node, $downloadUrl=NULL){
$url = $contentUrl . '&ticket=' . $_SESSION[$this->connectorId]['ticket'] . '&params=display%3Ddownload';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
if (! $isH5p) {
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
} else {
$h5pFramework = new H5PFramework();
$path = $h5pFramework->getUploadedH5pPath();
$filePath = fopen($path, 'wb');
curl_setopt($curl,CURLOPT_FILE, $filePath);
}
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, $curlHeader);
$data = curl_exec($curl);
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($isH5p) {
fclose($filePath);
}
if ($httpcode >= 200 && $httpcode < 308) {
return $data;
}else{
Expand Down
Loading

0 comments on commit 72281c7

Please sign in to comment.