Skip to content

Commit

Permalink
Merge pull request #1859 from Automattic/update/lando-domain
Browse files Browse the repository at this point in the history
feat(dev-env): make `domain` configurable
  • Loading branch information
sjinks authored Jun 6, 2024
2 parents 264f082 + 48bc228 commit 1a81e25
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 25 deletions.
2 changes: 1 addition & 1 deletion __tests__/lib/dev-environment/dev-environment-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe( 'lib/dev-environment/dev-environment-core', () => {
const slug = 'foo';
jest.spyOn( fs, 'existsSync' ).mockReturnValueOnce( true );

const promise = createEnvironment( { siteSlug: slug } );
const promise = createEnvironment( {}, { siteSlug: slug } );

return expect( promise ).rejects.toEqual( new Error( 'Environment already exists.' ) );
} );
Expand Down
12 changes: 6 additions & 6 deletions assets/dev-env.lando.template.yml.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ env_file:
- .env
proxy:
nginx:
- <%= siteSlug %>.vipdev.lndo.site
- <%= siteSlug %>.<%= domain %>
<% if ( multisite ) { %>
- '*.<%= siteSlug %>.vipdev.lndo.site'
- '*.<%= siteSlug %>.<%= domain %>'
<% } %>
phpmyadmin:
- <%= siteSlug %>-pma.vipdev.lndo.site
- <%= siteSlug %>-pma.<%= domain %>
mailpit:
- <%= siteSlug %>-mailpit.vipdev.lndo.site:8025
- <%= siteSlug %>-mailpit.<%= domain %>:8025

services:
devtools:
Expand Down Expand Up @@ -79,9 +79,9 @@ services:
sh /dev-tools/setup.sh
--host database
--user root
--domain "http://<%= siteSlug %>.vipdev.lndo.site/"
--domain "http://<%= siteSlug %>.<%= domain %>/"
--title "<%= wpTitle %>"
<% if ( multisite ) { %>--ms-domain "<%= siteSlug %>.vipdev.lndo.site" <% if ( multisite === true || multisite === 'subdomain' ) { %>--subdomain <% } %> <% } %>
<% if ( multisite ) { %>--ms-domain "<%= siteSlug %>.<%= domain %>" <% if ( multisite === true || multisite === 'subdomain' ) { %>--subdomain <% } %> <% } %>
database:
type: compose
services:
Expand Down
2 changes: 1 addition & 1 deletion src/bin/vip-dev-env-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ cmd.argv( process.argv, async ( arg, opt ) => {
instanceData.siteSlug = slug;

try {
await createEnvironment( instanceData );
await createEnvironment( lando, instanceData );

await printEnvironmentInfo( lando, slug, { extended: false, suppressWarnings: true } );

Expand Down
2 changes: 1 addition & 1 deletion src/bin/vip-dev-env-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ cmd.argv( process.argv, async ( arg, opt ) => {
);
instanceData.siteSlug = slug;

await updateEnvironment( instanceData );
await updateEnvironment( lando, instanceData );

const message =
'\n' +
Expand Down
38 changes: 25 additions & 13 deletions src/lib/dev-environment/dev-environment-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ export async function startEnvironment(

let updated = false;
if ( ! options.skipWpVersionsCheck ) {
updated = await maybeUpdateWordPressImage( slug );
updated = await maybeUpdateWordPressImage( lando, slug );
}
updated = updated || ( await maybeUpdateVersion( slug ) );
updated = updated || ( await maybeUpdateVersion( lando, slug ) );

if ( options.skipRebuild && ! updated ) {
await landoStart( lando, instancePath );
Expand All @@ -142,7 +142,10 @@ export async function stopEnvironment( lando: Lando, slug: string ): Promise< vo
await landoStop( lando, instancePath );
}

export async function createEnvironment( instanceData: InstanceData ): Promise< void > {
export async function createEnvironment(
lando: Lando,
instanceData: InstanceData
): Promise< void > {
const slug = instanceData.siteSlug;
debug( 'Will process an environment', slug, 'with instanceData for creation: ', instanceData );

Expand All @@ -159,10 +162,13 @@ export async function createEnvironment( instanceData: InstanceData ): Promise<
const preProcessedInstanceData = preProcessInstanceData( instanceData );
debug( 'Will create an environment', slug, 'with instanceData: ', preProcessedInstanceData );

await prepareLandoEnv( preProcessedInstanceData, instancePath );
await prepareLandoEnv( lando, preProcessedInstanceData, instancePath );
}

export async function updateEnvironment( instanceData: InstanceData ): Promise< void > {
export async function updateEnvironment(
lando: Lando,
instanceData: InstanceData
): Promise< void > {
const slug = instanceData.siteSlug;
debug( 'Will process an environment', slug, 'with instanceData for updating: ', instanceData );

Expand All @@ -179,7 +185,7 @@ export async function updateEnvironment( instanceData: InstanceData ): Promise<
const preProcessedInstanceData = preProcessInstanceData( instanceData );
debug( 'Will create an environment', slug, 'with instanceData: ', preProcessedInstanceData );

await prepareLandoEnv( preProcessedInstanceData, instancePath );
await prepareLandoEnv( lando, preProcessedInstanceData, instancePath );
}

function preProcessInstanceData( instanceData: InstanceData ): InstanceData {
Expand Down Expand Up @@ -470,11 +476,17 @@ export function writeEnvironmentData( slug: string, data: InstanceData ): Promis
}

async function prepareLandoEnv(
lando: Lando,
instanceData: InstanceData,
instancePath: string
): Promise< void > {
const landoFile = await ejs.renderFile( landoFileTemplatePath, instanceData );
const nginxFile = await ejs.renderFile( nginxFileTemplatePath, instanceData );
const templateData = {
...instanceData,
domain: lando.config.domain,
};

const landoFile = await ejs.renderFile( landoFileTemplatePath, templateData );
const nginxFile = await ejs.renderFile( nginxFileTemplatePath, templateData );
const instanceDataFile = JSON.stringify( instanceData );

const landoFileTargetPath = path.join( instancePath, landoFileName );
Expand Down Expand Up @@ -697,7 +709,7 @@ export async function importMediaPath( slug: string, filePath: string ) {
* @param {string} slug slug
* @return {boolean} boolean
*/
async function maybeUpdateWordPressImage( slug: string ): Promise< boolean > {
async function maybeUpdateWordPressImage( lando: Lando, slug: string ): Promise< boolean > {
const versions = await getVersionList();
if ( ! versions.length ) {
return false;
Expand Down Expand Up @@ -785,7 +797,7 @@ async function maybeUpdateWordPressImage( slug: string ): Promise< boolean > {
envData.wordpress.tag = version?.tag ?? '';
envData.wordpress.ref = version?.ref;

await updateEnvironment( envData );
await updateEnvironment( lando, envData );

return true;
}
Expand All @@ -794,19 +806,19 @@ async function maybeUpdateWordPressImage( slug: string ): Promise< boolean > {
envData.wordpress.doNotUpgrade = true;
console.log( "We won't ask about upgrading this environment anymore." );
console.log( `To manually upgrade please run: ${ chalk.yellow( updateCommand ) }` );
await updateEnvironment( envData );
await updateEnvironment( lando, envData );
}

return false;
}

async function maybeUpdateVersion( slug: string ): Promise< boolean > {
async function maybeUpdateVersion( lando: Lando, slug: string ): Promise< boolean > {
const envData = readEnvironmentData( slug );
const currentVersion = envData.version;

console.log( 'Current local environment version is: ' + chalk.yellow( currentVersion ) );
if ( ! currentVersion || semver.lt( currentVersion, DEV_ENVIRONMENT_VERSION ) ) {
await updateEnvironment( envData );
await updateEnvironment( lando, envData );
console.log(
'Local environment version updated to: ' + chalk.green( DEV_ENVIRONMENT_VERSION )
);
Expand Down
6 changes: 3 additions & 3 deletions src/lib/dev-environment/dev-environment-lando.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ async function initLandoApplication( lando: Lando, instancePath: string ): Promi
return app;
}

async function regenerateLandofile( instancePath: string ): Promise< void > {
async function regenerateLandofile( lando: Lando, instancePath: string ): Promise< void > {
const landoFile = path.join( instancePath, '.lando.yml' );

try {
Expand All @@ -125,14 +125,14 @@ async function regenerateLandofile( instancePath: string ): Promise< void > {
const slug = path.basename( instancePath );
const currentInstanceData = readEnvironmentData( slug );
currentInstanceData.pullAfter = 0;
await updateEnvironment( currentInstanceData );
await updateEnvironment( lando, currentInstanceData );
}

async function landoRecovery( lando: Lando, instancePath: string, error: unknown ): Promise< App > {
debug( 'Error initializing Lando app', error );
console.warn( chalk.yellow( 'There was an error initializing Lando, trying to recover...' ) );
try {
await regenerateLandofile( instancePath );
await regenerateLandofile( lando, instancePath );
} catch ( err ) {
console.error(
`${ chalk.bold.red(
Expand Down

0 comments on commit 1a81e25

Please sign in to comment.