Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix VM creation to use parent domain for subdomains #522

Closed
wants to merge 1 commit into from

Conversation

jasperf
Copy link

@jasperf jasperf commented Mar 7, 2025

This pull request introduces several enhancements and new features to the trellis package, focusing on domain management and site identification. The changes include adding new utility functions, updating existing methods for better site handling, and adding comprehensive tests for the new functionalities.

This pull request still needs testing!

Enhancements and New Features:

  • Utility Functions:

    • Added GetParentDomain function to return the parent domain of a given site name.
    • Added MainSiteName method to determine the main site name for VM naming, prioritizing non-subdomain sites.
  • Method Updates:

    • Updated MainSiteFromEnvironment method to use the new MainSiteName method for determining the main site name.
  • Testing:

    • Added tests for GetParentDomain to verify correct parent domain extraction from various site names.
    • Added tests for MainSiteName to ensure it correctly identifies the main site in different configurations.
    • Updated TestMainSiteFromEnvironment to include various scenarios for site selection and error handling.

These changes aim to improve the accuracy and reliability of domain and site management within the trellis package.

How These Changes Help Run Multiple Sites on a Single VM

The changes we've made to the Trellis CLI ensure that blocks.imagewize.com and imagewize.com will run on the same virtual machine rather than creating separate VMs. Here's how it works:

Problem Solved

Previously, Trellis CLI would create a separate VM for each site defined in your wordpress_sites.yml file, even when one was a subdomain of another. This caused:

  • Resource inefficiency with multiple VMs
  • Network conflicts since both VMs tried to handle similar domains
  • Confusion when running commands against what should be related sites

Solution Implemented

The core of our solution involves these key components:

  1. Parent Domain Detection

We added the GetParentDomain() function that extracts the parent domain from any subdomain:

  • For imagewize.com → returns imagewize.com
  • For blocks.imagewize.com → returns imagewize.com
  1. Smart VM Naming

The new MainSiteName() method determines which site name should be used for VM naming:

  • Prioritizes non-subdomain sites (e.g., imagewize.com over blocks.imagewize.com)
  • Falls back to alphabetical order if only subdomains exist
  1. VM Command Integration

We updated MainSiteFromEnvironment() to use this domain grouping logic, which is called by all VM commands:

  • trellis vm start
  • trellis vm stop
  • trellis vm delete

Practical Result

When you run trellis vm start in your project containing both imagewize.com and blocks.imagewize.com:

  • The CLI determines that imagewize.com is the parent domain
  • A single VM named after imagewize.com is created
  • Both sites are configured on this VM
  • All mounts are properly set up for both sites within the same VM
  • Both sites work correctly, avoiding the resource waste and conflicts

These changes ensure a more intuitive experience where related sites are properly grouped together on the same VM, which is particularly important for projects with multiple subdomains.

@jasperf
Copy link
Author

jasperf commented Mar 7, 2025

Other PRs seem to in the works based on #514 but perhaps still useful as contribution to new code.

@jasperf jasperf closed this Mar 7, 2025
@jasperf jasperf reopened this Mar 7, 2025
@jasperf jasperf closed this Mar 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant