In #705026: Allow creation of example.com/foo and example.com/bar type of sites (Part I), we introduced subdirectory support. However, it won't allow a site at both example.com and example.com/foo. Part of the solution may be to #2020087: Use the <Location>-based approach from subdirs for all site vhosts.

Comments

anarcat’s picture

Assigned: Unassigned » anarcat
Status: Active » Needs work

i started looking into this - it can actually be fixed quite nicely.

diff --git a/http/Provision/Config/Apache/SubdirVhost.php b/http/Provision/Config/Apache/SubdirVhost.php
index bb7fe71..0420a1f 100644
--- a/http/Provision/Config/Apache/SubdirVhost.php
+++ b/http/Provision/Config/Apache/SubdirVhost.php
@@ -32,8 +32,15 @@ class Provision_Config_Apache_SubdirVhost extends Provision_Config_Http {
     foreach (d()->aliases as $alias) {
       if (strpos($alias, '/')) {
         $this->current_alias = $alias;
-        drush_log("Subdirectory alias `$alias` found. Creating vhost configuration file.", 'notice');
-        parent::write();
+        if (d($this->uri())) {
+          drush_log(dt('virtual host %vhost already exist for alias %alias, skipping', array('%vhost' => $this->uri(), '%alias' => $alias)), 'warning');
+          // XXX: we need to verify that vhost here, or at least
+          // generate its configuration file.
+        }
+        else {
+          drush_log("Subdirectory alias `$alias` found. Creating vhost configuration file.", 'notice');
+          parent::write();
+        }
       }
     }
   }
diff --git a/subdirs/subdirs.drush.inc b/subdirs/subdirs.drush.inc
index 02d1178..ee321ab 100644
--- a/subdirs/subdirs.drush.inc
+++ b/subdirs/subdirs.drush.inc
@@ -117,3 +117,18 @@ function _subdirs_remove_symlinks() {
     }
   }
 }
+
+/**
+ * Inject our includes into existing virtual host
+ *
+ * Implementation of hook_provision_apache_vhost_config().
+ *
+ * This is the case where example.com exists for the alias example.com/foo.
+ */
+function subdirs_provision_apache_vhost_config($uri, $data) {
+  $subdirs_path = $data['http_subdird_path'] . '/' . $uri . '/*.conf'; // wild guess
+  if (glob($subdirs_path)) {
+    return "# subdirectory sites for this virtual host
+Include $subdirs_path";
+  }
+}

This has been committed to the resurrected dev/subdirs branch in the backend. As the commitlog says:

we don't overwrite example.com if it exists - instead we hook into it using the hook_provision_apache_vhost_config()

unfotunately, this will not work right away - the example.com site will need to be verified.

also, if the subdir site is deleted, the apache config will be corrupt, we would need to generate (and not verify, because then it's too late) example.com then too.

This wasn't tested in any way.

ergonlogic’s picture

This results in the following warning and an inaccessible subdir site:

Webserver subdir configuration for domain path /var/aegir/config/server_master/apache/subdir.d/example.com is writable.
Template loaded: /var/aegir/.drush/provision/http/Provision/Config/Apache/subdir.tpl.php
Generated config subdirectory support
virtual host example.com already exist for alias example.com/site2, skipping
Template loaded: /var/aegir/.drush/provision/Provision/Config/Drupal/provision_drupal_settings.tpl.php
Generated config Drupal settings.php file
omega8cc’s picture

Plus, this patch introduces Apache-centric fix? The function subdirs_provision_apache_vhost_config doesn't belong in subdirs/subdirs.drush.inc

omega8cc’s picture

omega8cc’s picture

Just to summarize: now no vhost will be overwritten if the "parent" site already exists. Instead, the extra include line will be appended and the "parent" site auto-magically re-verified in the backend, once referenced by any other site in its subdir. We have also modified the alias check procedure in the frontend to allow it to work if the "parent" site already exists.

anarcat’s picture

that's... awesome! thanks!

omega8cc’s picture

Status: Fixed » Needs work

A few more commits are needed after extensive testing.

omega8cc’s picture

More improvements after extensive testing (and a few destroyed instances):

http://drupalcode.org/project/hosting.git/commit/5ab0e8c
http://drupalcode.org/project/hosting.git/commit/10f38a0
http://drupalcode.org/project/provision.git/commit/7acac7b
http://drupalcode.org/project/provision.git/commit/f4e4098
http://drupalcode.org/project/provision.git/commit/e4dab7b
http://drupalcode.org/project/provision.git/commit/8180c5d
http://drupalcode.org/project/provision.git/commit/dd95643

To quote the comment from latest commit:

 //
 //   drush_invoke_process('@none', 'cache-clear', array('drush'));
 //   provision_backend_invoke($site_name, 'provision-verify');
 //   drush_invoke_process('@none', 'cache-clear', array('drush'));
 //
 // Running automated re-verify for the parent site is currently
 // too dangerous. It will destroy/delete the parent site's database
 // if the parent and the subdir site use different installation
 // profiles, unless both profiles exist in the same platform.
 //
 // This is too serious limitation and we need to find a better
 // way to automate parent site re-verify when needed to add
 // required include line, which enables all subdir sites.
 //
 // With Drush 4 this could be done with separate task created like this:
 //
 //   drush_log(dt('Run parent site %vhost Verify via frontend', array('%vhost' => $site_name)), 'notice');
 //   provision_backend_invoke('@hostmaster', 'hosting-task', array($site_name, 'verify'), array('force' => TRUE));
 //
 // Unfortunatelly, it doesn't work with Drush 5 and current Aegir 2.x,
 // and is even more dangerous, because instead of creating separate
 // re-verify task for the parent site, it will run it "inline",
 // immediatelly, so in the wrong context, which, depending on other
 // conditions will destroy *hostmaster* database, so it is mentioned
 // here as a nostalgic reminiscence of good old Drush 4, which allowed
 // to create frontend tasks from the backend, safely.
 //
 // Without this re-verify it is fully possible to use any profile
 // with any platform for multiple subdir sites, under the same,
 // single, parent URL roof, with or without the parent site hosted
 // on the main URL (domain name). Which is good news!
 // 
omega8cc’s picture

Status: Needs work » Fixed

I tend to close this as fixed. We don't know how/when we will be able to safely automate the parent site re-verify, so for now here is improved built-in how-to: http://drupalcode.org/project/hosting.git/commit/37ce532

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.