Add support for provisioning and rebuilding via interface

This commit is contained in:
Tom 2023-07-09 11:18:14 +02:00
parent f5c5999f44
commit ddb4bb3546
76 changed files with 1306 additions and 625 deletions

View file

@ -0,0 +1,41 @@
package extras
import (
"context"
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient/php"
_ "embed"
)
// Prefixes implements reading and writing prefix
type Adapters struct {
ingredient.Base
Dependencies struct {
PHP *php.PHP
}
}
//go:embed adapters.php
var adaptersPHP string
type DistilleryAdapter struct {
Label string
MachineName string
Description string
InstanceDomain string
GraphDBRepository string
GraphDBUsername string
GraphDBPassword string
}
func (wisski *Adapters) CreateDistilleryAdapter(ctx context.Context, server *phpx.Server, adapter DistilleryAdapter) error {
return wisski.Dependencies.PHP.ExecScript(
ctx, server, nil, adaptersPHP,
"create_distillery_adapter",
adapter.Label, adapter.MachineName, adapter.Description, adapter.InstanceDomain, adapter.GraphDBRepository, adapter.GraphDBUsername, adapter.GraphDBPassword,
)
}

View file

@ -0,0 +1,57 @@
<?php
/**
* Creates an adapter for the distillery
*/
function create_distillery_adapter(string $LABEL, string $MACHINE_NAME, string $DESCRIPTION, string $INSTANCE_DOMAIN, string $GRAPHDB_REPO, string $GRAPHDB_USER, string $GRAPHDB_PASSWORD) {
//
// PROPERTIES FOR THE ADAPTER
//
$id = 'default'; // id
$type = 'sparql11_with_pb'; // plugin
$machine_name = $MACHINE_NAME; // machine-name
$label = $LABEL;
$description = $DESCRIPTION; // description
$writable = TRUE; // writable
$is_preferred_local_store = TRUE; // is_preferred_local_store
$read_url = 'http://triplestore:7200/repositories/' . $GRAPHDB_REPO; // read_url
$write_url = 'http://triplestore:7200/repositories/' . $GRAPHDB_REPO . '/statements'; // write_url
$is_federatable = TRUE; // is_federatable
$default_graph_uri = 'https://' . $INSTANCE_DOMAIN . '/';
$same_as_properties = ['http://www.w3.org/2002/07/owl#sameAs']; // same_as_properties
$ontology_graphs = []; // ontology_graphs
// header
$header = "";
if ($GRAPHDB_USER !== "" && $GRAPHDB_PASSWORD !== "") {
$header = $GRAPHDB_USER . ":" . $GRAPHDB_PASSWORD;
$header = base64_encode($header);
}
//
// Do the creation!
//
$storage = \Drupal::entityTypeManager()->getStorage('wisski_salz_adapter');
$adapter = $storage->create([
"id" => $id,
"label" => $label,
"description" => $description,
]);
$adapter->setEngineConfig([
"id" => $type,
"machine-name" => $machine_name,
"header" => $header,
"writeable" => $writable,
"is_preferred_local_store" => $is_preferred_local_store,
"read_url" => $read_url,
"write_url" => $write_url,
"is_federatable" => $is_federatable,
"default_graph" => $default_graph_uri,
"same_as_properties" => $same_as_properties,
"ontology_graphs" => $ontology_graphs,
]);
$adapter->save();
}

View file

@ -3,6 +3,7 @@ package extras
import (
"context"
_ "embed"
"errors"
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
@ -24,6 +25,25 @@ func (settings *Settings) Get(ctx context.Context, server *phpx.Server, key stri
return
}
var errFailedToSetSetting = errors.New("failed to update setting")
func (settings *Settings) Set(ctx context.Context, server *phpx.Server, key string, value any) error {
return settings.Dependencies.PHP.ExecScript(ctx, server, nil, settingsPHP, "set_setting", key, value)
var ok bool
err := settings.Dependencies.PHP.ExecScript(ctx, server, &ok, settingsPHP, "set_setting", key, value)
if err == nil && !ok {
err = errFailedToSetSetting
}
return err
}
var errFailedToSetTrustedDomain = errors.New("failed to set trusted domain")
func (settings *Settings) SetTrustedDomain(ctx context.Context, server *phpx.Server, domain string) error {
var ok bool
err := settings.Dependencies.PHP.ExecScript(ctx, server, &ok, settingsPHP, "set_trusted_domain", domain)
if err == nil && !ok {
err = errFailedToSetTrustedDomain
}
return err
}

View file

@ -1,13 +1,28 @@
<?php
use \Drupal\Core\Site\Settings;
/** gets a setting from 'settings.php' */
function get_setting($name) {
use \Drupal\Core\Site\Settings;
return Settings::get($name);
}
/** sets a setting in 'settings.php' */
function set_setting($name, $value) {
function set_setting(string $name, mixed $value): bool {
// find settings.php
$filename = DRUPAL_ROOT . "/" . \Drupal::service("site.path") . "/settings.php";
// setup user write permissions for the file
$old = fileperms($filename);
if ($old === FALSE) {
return FALSE;
}
$new = 0777; // set all permissions
if (!chmod($filename, $new)) {
return FALSE;
}
// load install.inc
if(is_file(DRUPAL_ROOT . "/internal/")) {
include_once DRUPAL_ROOT . "/internal/core/includes/install.inc";
@ -21,9 +36,20 @@ function set_setting($name, $value) {
"required" => TRUE,
];
// find the filename
$filename = DRUPAL_ROOT . "/" . \Drupal::service("site.path") . "/settings.php";
drupal_rewrite_settings($settings, $filename);
// do the rewrite
try {
drupal_rewrite_settings($settings, $filename);
} catch(Throwable $t) {
throw $t; // DEBUG
return FALSE;
}
return True;
// reset the file mode
return chmod($filename, $old);
}
/** Sets the trusted host to the specified domain */
function set_trusted_domain(string $domain): bool {
return set_setting("trusted_host_patterns", [preg_quote($domain)]);
}

View file

@ -5,7 +5,6 @@ import (
_ "embed"
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
"github.com/alessio/shellescape"
"github.com/tkw1536/pkglib/stream"
)
@ -21,6 +20,6 @@ func (php *PHP) NewServer() *phpx.Server {
}
func (php *PHP) spawn(ctx context.Context, str stream.IOStream, code string) error {
php.Dependencies.Barrel.Shell(ctx, str, "-c", shellescape.QuoteCommand([]string{"drush", "php:eval", code}))()
php.Dependencies.Barrel.ShellScript(ctx, str, "drush", "php:eval", code)
return nil
}