Add initial setup global settings

This commit is contained in:
Tom Wiesing 2024-04-01 15:10:35 +02:00
parent a7af0f64d2
commit 6eab3ac311
No known key found for this signature in database
7 changed files with 113 additions and 11 deletions

View file

@ -11,6 +11,7 @@ import (
)
// Apply applies the given profile to this existing instance.
// The instance must be running
func (manager *Manager) Apply(ctx context.Context, progress io.Writer, flags Profile) error {
// Update drupal
if flags.Drupal != "" {

View file

@ -127,6 +127,14 @@ func (provision *Manager) bootstrap(ctx context.Context, progress io.Writer, fla
}
}
// Rebuild the settings file
logging.LogMessage(progress, "Rebuilding Settings")
{
if err := provision.dependencies.SystemManager.RebuildSettings(ctx, progress); err != nil {
return err
}
}
// Create directory for ontologies
logging.LogMessage(progress, fmt.Sprintf("Creating %q", barrel.OntologyDirectory))
{
@ -168,13 +176,6 @@ func (provision *Manager) bootstrap(ctx context.Context, progress io.Writer, fla
}
}
logging.LogMessage(progress, "Updating TRUSTED_HOST_PATTERNS in settings.php")
{
if err := provision.dependencies.Settings.SetTrustedDomain(ctx, nil, provision.Domain()); err != nil {
return err
}
}
logging.LogMessage(progress, "Running initial cron")
{
if err := provision.dependencies.Drush.Exec(ctx, progress, "core-cron"); err != nil {

View file

@ -0,0 +1,27 @@
package system
import (
"context"
"io"
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
)
// RebuildSettings (re-)configures settings.php for the provided running instance
func (smanager *SystemManager) RebuildSettings(ctx context.Context, progress io.Writer) (err error) {
logging.LogMessage(progress, "Updating TRUSTED_HOST_PATTERNS in settings.php")
{
if err := smanager.dependencies.Settings.SetTrustedDomain(ctx, nil, smanager.Domain()); err != nil {
return err
}
}
logging.LogMessage(progress, "Adding distillery settings to settings.php")
{
if err := smanager.dependencies.Settings.InstallDistillerySettings(ctx, nil); err != nil {
return err
}
}
return nil
}

View file

@ -36,7 +36,10 @@ func (settings *Settings) Set(ctx context.Context, server *phpx.Server, key stri
return err
}
var errFailedToSetTrustedDomain = errors.New("failed to set trusted domain")
var (
errFailedToSetTrustedDomain = errors.New("failed to set trusted domain")
errFailedInstallDistillerySettings = errors.New("failed to install distillery settings")
)
func (settings *Settings) SetTrustedDomain(ctx context.Context, server *phpx.Server, domain string) error {
var ok bool
@ -47,3 +50,18 @@ func (settings *Settings) SetTrustedDomain(ctx context.Context, server *phpx.Ser
}
return err
}
// GlobalSettingsPath is the global path to distillery settings
const GlobalSettingsPath = "/distillery_settings.php"
func (settings *Settings) InstallDistillerySettings(ctx context.Context, server *phpx.Server) error {
var ok bool
err := settings.dependencies.PHP.ExecScript(ctx, server, &ok, settingsPHP, "install_settings_include", []string{
GlobalSettingsPath,
})
if err == nil && !ok {
err = errFailedInstallDistillerySettings
}
return err
}

View file

@ -40,7 +40,6 @@ function set_setting(string $name, mixed $value): bool {
try {
drupal_rewrite_settings($settings, $filename);
} catch(Throwable $t) {
throw $t; // DEBUG
return FALSE;
}
@ -52,4 +51,43 @@ function set_setting(string $name, mixed $value): bool {
/** Sets the trusted host to the specified domain */
function set_trusted_domain(string $domain): bool {
return set_setting("trusted_host_patterns", [preg_quote($domain)]);
}
/** Sets up including a settings.php file from the given path */
function install_settings_include(array $paths): bool {
// find the original filename
$filename = DRUPAL_ROOT . "/" . \Drupal::getContainer()->getParameter("site.path") . "/settings.php";
// read the original file
$original_content = file_get_contents($filename);
if ($original_content === FALSE) {
return FALSE;
}
// remove any old <distillery-settings-includes>
$pattern = '/\/\/(\s*)<distillery-settings-include>(.*?)\/\/(\s*)<\/distillery-settings-include>/s';
$new_content = preg_replace($pattern, '', $originalContent);
$code = "// <distillery-settings-include>>\n//\n// DO NOT MODIFY THIS BLOCK AND KEEP IT AT THE END OF THE FILE.\n// DO NOT REMOVE CONFIG TAGS\n";
foreach ($paths as $path) {
// escape the path to be included
$the_path = "'" . addslashes($path) . "'";
// resolve it (if it isn't absolute)
if (!str_starts_with($path, '/')) {
$the_path = '$app_root . \'/\' . $site_path . \'/\' . ' . $the_path;
}
// add code to include the file if it exists
$code = $code . 'if (file_exists(' . $the_path . ')) { include_once ' . $the_path . '; }' . "\n";
}
$code = $code . "// </distillery-settings-include>";
// and store the settings
try {
file_put_contents($filename, $original_content . $code);
} catch(Throwable $t) {
return FALSE;
}
return TRUE;
}