-
Notifications
You must be signed in to change notification settings - Fork 52
Added studio site set-domain and studio site set-https commands
#2151
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
Merged
+1,161
−79
Merged
Changes from 15 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
fd24a5f
Implement studio site stop CLI command with graceful shutdown
bcotrim 25bd8ce
Fix types and remove sendStopMessage
fredrikekelund e2bc082
stop proxy if needed
bcotrim fc01870
use getSiteByFolder
bcotrim 57a05ba
Fix issues from my previous commits
fredrikekelund 1a81ea8
Clean up test file
fredrikekelund a4b15b7
fix lint and unit tests
bcotrim cb20e2d
CLI: Implement `studio site set-domain` and `studio site set-https`
fredrikekelund 1cdde21
Harden `sendMessage` cleanup logic
fredrikekelund 7c935bb
Cleanup
fredrikekelund 8bdb8c8
Disallow `set-https` command on sites without custom domains
fredrikekelund 97b6f20
Finish early if HTTPS config already matches
fredrikekelund b985d9b
Simplify
fredrikekelund 41d19d6
Merge branch 'trunk' into f26d/cli-site-set-domain-https
fredrikekelund b9e007e
Fix yargs option config
fredrikekelund e8028ab
Address review comments
fredrikekelund 864c9b4
Disallow setting the same domain as before
fredrikekelund 1c3af77
Merge branch 'trunk' into f26d/cli-site-set-domain-https
fredrikekelund 78ec8f1
Update tests to follow new paradigm
fredrikekelund File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { __, _n } from '@wordpress/i18n'; | ||
| import { getDomainNameValidationError } from 'common/lib/domains'; | ||
| import { arePathsEqual } from 'common/lib/fs-utils'; | ||
| import { SiteCommandLoggerAction as LoggerAction } from 'common/logger-actions'; | ||
| import { | ||
| getSiteByFolder, | ||
| lockAppdata, | ||
| readAppdata, | ||
| saveAppdata, | ||
| unlockAppdata, | ||
| } from 'cli/lib/appdata'; | ||
| import { removeDomainFromHosts } from 'cli/lib/hosts-file'; | ||
| import { connect, disconnect } from 'cli/lib/pm2-manager'; | ||
| import { setupCustomDomain } from 'cli/lib/site-utils'; | ||
| import { | ||
| isServerRunning, | ||
| startWordPressServer, | ||
| stopWordPressServer, | ||
| } from 'cli/lib/wordpress-server-manager'; | ||
| import { Logger, LoggerError } from 'cli/logger'; | ||
| import { StudioArgv } from 'cli/types'; | ||
|
|
||
| export async function runCommand( sitePath: string, domainName: string ): Promise< void > { | ||
| const logger = new Logger< LoggerAction >(); | ||
|
|
||
| try { | ||
| logger.reportStart( LoggerAction.LOAD_SITES, __( 'Loading site…' ) ); | ||
| const site = await getSiteByFolder( sitePath, false ); | ||
| logger.reportSuccess( __( 'Site loaded' ) ); | ||
|
|
||
| try { | ||
| await lockAppdata(); | ||
| const appdata = await readAppdata(); | ||
|
|
||
| const existingDomains = appdata.sites | ||
| .map( ( site ) => site.customDomain ) | ||
| .filter( ( domain ): domain is string => Boolean( domain ) ); | ||
| const domainError = getDomainNameValidationError( true, domainName, existingDomains ); | ||
|
|
||
| if ( domainError ) { | ||
| throw new LoggerError( domainError ); | ||
| } | ||
|
|
||
| const site = appdata.sites.find( ( site ) => arePathsEqual( site.path, sitePath ) ); | ||
| if ( ! site ) { | ||
| throw new LoggerError( __( 'The specified folder is not added to Studio.' ) ); | ||
| } | ||
| const oldDomainName = site.customDomain; | ||
| site.customDomain = domainName; | ||
| await saveAppdata( appdata ); | ||
|
|
||
| if ( oldDomainName ) { | ||
| logger.reportStart( | ||
| LoggerAction.REMOVE_DOMAIN_FROM_HOSTS, | ||
| __( 'Removing domain from hosts file...' ) | ||
| ); | ||
| await removeDomainFromHosts( oldDomainName ); | ||
| logger.reportSuccess( __( 'Domain removed from hosts file' ) ); | ||
| } | ||
| } finally { | ||
| await unlockAppdata(); | ||
| } | ||
|
|
||
| logger.reportStart( LoggerAction.START_DAEMON, __( 'Starting process daemon...' ) ); | ||
| await connect(); | ||
| logger.reportSuccess( __( 'Process daemon started' ) ); | ||
|
|
||
| const runningProcess = await isServerRunning( site.id ); | ||
|
|
||
| if ( runningProcess ) { | ||
| await stopWordPressServer( site.id ); | ||
| await setupCustomDomain( site, logger ); | ||
| logger.reportStart( LoggerAction.START_SITE, __( 'Restarting site...' ) ); | ||
| await startWordPressServer( site ); | ||
| logger.reportSuccess( __( 'Site restarted' ) ); | ||
| } | ||
| } catch ( error ) { | ||
| if ( error instanceof LoggerError ) { | ||
| logger.reportError( error ); | ||
| } else { | ||
| const loggerError = new LoggerError( __( 'Failed to start site infrastructure' ), error ); | ||
| logger.reportError( loggerError ); | ||
| } | ||
| } finally { | ||
| disconnect(); | ||
| } | ||
| } | ||
|
|
||
| export const registerCommand = ( yargs: StudioArgv ) => { | ||
| return yargs.command( { | ||
| command: 'set-domain <domain>', | ||
| describe: __( 'Set domain for a local site' ), | ||
| builder: ( yargs ) => { | ||
| return yargs.positional( 'domain', { | ||
| type: 'string', | ||
| description: __( 'Domain name' ), | ||
| demandOption: true, | ||
| } ); | ||
| }, | ||
| handler: async ( argv ) => { | ||
| await runCommand( argv.path, argv.domain ); | ||
| }, | ||
| } ); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import { __, _n } from '@wordpress/i18n'; | ||
| import { arePathsEqual } from 'common/lib/fs-utils'; | ||
| import { SiteCommandLoggerAction as LoggerAction } from 'common/logger-actions'; | ||
| import { | ||
| getSiteByFolder, | ||
| lockAppdata, | ||
| readAppdata, | ||
| saveAppdata, | ||
| unlockAppdata, | ||
| } from 'cli/lib/appdata'; | ||
| import { connect, disconnect } from 'cli/lib/pm2-manager'; | ||
| import { | ||
| isServerRunning, | ||
| startWordPressServer, | ||
| stopWordPressServer, | ||
| } from 'cli/lib/wordpress-server-manager'; | ||
| import { Logger, LoggerError } from 'cli/logger'; | ||
| import { StudioArgv } from 'cli/types'; | ||
|
|
||
| export async function runCommand( sitePath: string, enableHttps: boolean ): Promise< void > { | ||
| const logger = new Logger< LoggerAction >(); | ||
|
|
||
| try { | ||
| logger.reportStart( LoggerAction.LOAD_SITES, __( 'Loading site…' ) ); | ||
| const site = await getSiteByFolder( sitePath, false ); | ||
| logger.reportSuccess( __( 'Site loaded' ) ); | ||
|
|
||
| if ( ! site.customDomain ) { | ||
| throw new LoggerError( __( 'Site does not have a custom domain.' ) ); | ||
| } | ||
|
|
||
| if ( site.enableHttps === enableHttps ) { | ||
| if ( enableHttps ) { | ||
| throw new LoggerError( __( 'HTTPS is already enabled for this site.' ) ); | ||
| } else { | ||
| throw new LoggerError( __( 'HTTPS is already disabled for this site.' ) ); | ||
| } | ||
| } | ||
|
|
||
| logger.reportStart( LoggerAction.START_DAEMON, __( 'Starting process daemon...' ) ); | ||
| await connect(); | ||
| logger.reportSuccess( __( 'Process daemon started' ) ); | ||
|
|
||
| try { | ||
| await lockAppdata(); | ||
| const appdata = await readAppdata(); | ||
| const site = appdata.sites.find( ( site ) => arePathsEqual( site.path, sitePath ) ); | ||
|
||
| if ( ! site ) { | ||
| throw new LoggerError( __( 'The specified folder is not added to Studio.' ) ); | ||
| } | ||
| site.enableHttps = enableHttps; | ||
| await saveAppdata( appdata ); | ||
| } finally { | ||
| await unlockAppdata(); | ||
| } | ||
|
|
||
| const runningProcess = await isServerRunning( site.id ); | ||
|
|
||
| if ( runningProcess ) { | ||
| logger.reportStart( LoggerAction.START_SITE, __( 'Restarting site...' ) ); | ||
| await stopWordPressServer( site.id ); | ||
| await startWordPressServer( site ); | ||
| logger.reportSuccess( __( 'Site restarted' ) ); | ||
| } | ||
| } catch ( error ) { | ||
| if ( error instanceof LoggerError ) { | ||
| logger.reportError( error ); | ||
| } else { | ||
| const loggerError = new LoggerError( __( 'Failed to start site infrastructure' ), error ); | ||
| logger.reportError( loggerError ); | ||
| } | ||
| } finally { | ||
| disconnect(); | ||
| } | ||
| } | ||
|
|
||
| export const registerCommand = ( yargs: StudioArgv ) => { | ||
| return yargs.command( { | ||
| command: 'set-https <enable>', | ||
| describe: __( 'Set HTTPS for a local site' ), | ||
| builder: ( yargs ) => { | ||
| return yargs.positional( 'enable', { | ||
| type: 'boolean', | ||
| description: __( 'Enable HTTPS' ), | ||
| demandOption: true, | ||
| } ); | ||
| }, | ||
| handler: async ( argv ) => { | ||
| await runCommand( argv.path, argv.enable ); | ||
| }, | ||
| } ); | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need both
getSiteByFolderand this validation, I understand we want to search the site in appdata to make the changes, so we could removegetSiteByFolder?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call 👍