-
Notifications
You must be signed in to change notification settings - Fork 0
Add sync-local commands for local-server development
#38
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
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
c0a30c5
Add stack sync commands for local-server development
jerico b7f11d2
Add --latest flag to skip prompt and auto-confirm
jerico 6d86bec
Update user-facing strings to say "Altis Dashboard" instead of "Vantage"
jerico ff8dd39
Rename sync to sync-local and update command strings to use app
jerico b468759
Add partial database sync with --table and --site-id options
jerico e470177
Handle site ID 1 as main site for partial sync
jerico 2914452
Revert site ID 1 special case for database sync
jerico fa086d3
Always create a new partial backup when --site-id or --table is set
jerico 5f4f79a
Fix --site-id handling in sync-local all command
jerico 4477dc2
Catch comma-separated --site-id values in single-site guards
jerico dbfeeb6
Warn when --site-id 1 is used for DB sync
jerico 16415de
Probe for s3 subcommand before falling back to import-uploads
jerico b284db0
Normalise siteId once at the top of the all handler
jerico 2a89beb
Fix misnumbered step comments in database.js and all.js
jerico 784083f
Remove step numbers from sync handler comments
jerico c4d456f
Rename sync/ directory to sync-local/ to match the command name
jerico d699d98
Drop unimplemented --json option from sync-local commands
jerico b6d3f23
Drop new from progressStream call — it's a factory, not a class
jerico 05f7f57
Use wp cli has-command to probe for altis post-sync registration
jerico 69360ea
Comment why downloadArchive uses plain fetch instead of Vantage client
jerico 3d8dc9c
Rename partial archive to .partial on failure to distinguish from com…
jerico 4aea52a
Replace process.exit(0) in confirm() with a typed UserCancelled error
jerico 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
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,21 @@ | ||
| import { getApp, fetchJSON, printJSON, printTable } from './util.js'; | ||
| import Vantage from '../../vantage.js'; | ||
|
|
||
| const handler = async argv => { | ||
| const app = await getApp( argv ); | ||
| const v = new Vantage( argv.config ); | ||
| const data = await fetchJSON( v, `stack/applications/${ app }/database-tables` ); | ||
| const sites = data.sites || []; | ||
| if ( argv.json ) { | ||
| printJSON( sites ); | ||
| return; | ||
| } | ||
| printTable( sites.map( ( { id, domain, path } ) => ( { id, domain, path } ) ) ); | ||
| }; | ||
|
|
||
| export default { | ||
| command: 'sites [stack]', | ||
| description: 'List multisite sites and their IDs for an application.', | ||
| builder: yargs => yargs.option( 'json', { type: 'boolean', description: 'Print JSON output.' } ), | ||
| handler, | ||
| }; |
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,296 @@ | ||
| import chalk from 'chalk'; | ||
| import fs from 'fs'; | ||
| import ora from 'ora'; | ||
| import path from 'path'; | ||
| import { tmpdir } from 'os'; | ||
|
|
||
| import Vantage from '../../../vantage.js'; | ||
| import { | ||
| addCommonOptions, | ||
| addDatabaseOptions, | ||
| addUploadsOptions, | ||
| confirm, | ||
| copyUploads, | ||
| downloadArchive, | ||
| extractArchive, | ||
| findCompletedBackup, | ||
| formatAge, | ||
| getLatestBackup, | ||
| importUploads, | ||
| promptBackupChoice, | ||
| resolveMappings, | ||
| resolveTablesForSiteIds, | ||
| runComposerServer, | ||
| runPostSync, | ||
| searchReplaceAndImport, | ||
| startBackup, | ||
| UserCancelled, | ||
| validateLocalProject, | ||
| waitForBackup, | ||
| } from './util.js'; | ||
|
|
||
| const handler = async function ( argv ) { | ||
| const { | ||
| app, | ||
| config, | ||
| path: pathOpt, | ||
| uploadsPath, | ||
| outputDir, | ||
| keepArchive, | ||
| yes, | ||
| latest, | ||
| resume, | ||
| debug, | ||
| table, | ||
| siteId, | ||
| searchReplaceKey, | ||
| replace: replacePairs, | ||
| skipSearchReplace, | ||
| dryRunSearchReplace, | ||
| skipPostSync, | ||
| } = argv; | ||
|
|
||
| const localPath = pathOpt || process.cwd(); | ||
| const v = new Vantage( config ); | ||
|
|
||
| // --dry-run-search-replace: just print mappings, no validation or network needed | ||
| if ( dryRunSearchReplace ) { | ||
| try { | ||
| const mappings = resolveMappings( localPath, searchReplaceKey, replacePairs, skipSearchReplace ); | ||
| if ( Object.keys( mappings ).length === 0 ) { | ||
| console.log( chalk.yellow( 'No search-replace mappings found.' ) ); | ||
| console.log( `Configure them under extra.altis.cloud.search-replace.${ searchReplaceKey } in composer.json, or use --replace.` ); | ||
| } else { | ||
| console.log( chalk.bold( 'Search-replace mappings that would be applied:' ) ); | ||
| for ( const [ from, to ] of Object.entries( mappings ) ) { | ||
| console.log( ` ${ chalk.red( from ) } → ${ chalk.green( to ) }` ); | ||
| } | ||
| } | ||
| } catch ( err ) { | ||
| console.error( chalk.red( err.message ) ); | ||
| process.exit( 1 ); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| // Validate local project | ||
| const spinner = ora( 'Validating local project…' ).start(); | ||
| try { | ||
| validateLocalProject( localPath ); | ||
| spinner.succeed( `Local project: ${ chalk.underline( localPath ) }` ); | ||
| } catch ( err ) { | ||
| spinner.fail( err.message ); | ||
| process.exit( 1 ); | ||
| } | ||
|
|
||
| // Resolve search-replace mappings (fail fast before any remote calls) | ||
| let mappings; | ||
| try { | ||
| mappings = resolveMappings( localPath, searchReplaceKey, replacePairs, skipSearchReplace ); | ||
| } catch ( err ) { | ||
| console.error( chalk.red( err.message ) ); | ||
| process.exit( 1 ); | ||
| } | ||
|
|
||
| if ( Object.keys( mappings ).length === 0 ) { | ||
| console.log( chalk.yellow( | ||
| `No search-replace mappings found for key "${ searchReplaceKey }". Skipping search-replace. ` + | ||
| `Configure them under extra.altis.cloud.search-replace.${ searchReplaceKey } in composer.json, or use --replace.` | ||
| ) ); | ||
| } else { | ||
| console.log( chalk.dim( `Search-replace: ${ Object.keys( mappings ).length } mapping(s) from composer.json[${ searchReplaceKey }]` ) ); | ||
| } | ||
|
|
||
| const siteIds = siteId?.flatMap( s => String( s ).split( ',' ) ) ?? []; | ||
|
|
||
| if ( siteIds.length > 1 ) { | ||
| console.error( chalk.red( 'Error: --site-id only accepts a single value for full sync (uploads can only target one path).' ) ); | ||
| process.exit( 1 ); | ||
| } | ||
|
|
||
| // Choose backup (before confirm, so the user knows what they're agreeing to) | ||
| const startTime = new Date(); | ||
| let logId = resume; | ||
| let backup = null; | ||
|
|
||
| const isPartialSync = siteIds.length || table?.length; | ||
|
|
||
| if ( latest ) { | ||
| if ( isPartialSync ) { | ||
| console.log( chalk.yellow( 'Warning: --latest cannot be used with --site-id or --table (existing backups are full backups). Creating a new partial backup instead.' ) ); | ||
| } else { | ||
| backup = await getLatestBackup( v, app ); | ||
| if ( ! backup ) { | ||
| console.error( chalk.red( `No existing backup found for ${ chalk.bold( app ) }.` ) ); | ||
| process.exit( 1 ); | ||
| } | ||
| const age = formatAge( new Date( backup.date ) ); | ||
| console.log( chalk.dim( `Using latest backup: ${ backup.id } (${ age } old)` ) ); | ||
| } | ||
| } else if ( ! logId ) { | ||
| if ( isPartialSync ) { | ||
| console.log( chalk.dim( 'Partial sync requested — creating a new backup (existing backups are full backups).' ) ); | ||
| } else { | ||
| backup = await promptBackupChoice( v, app ); | ||
| } | ||
| } | ||
|
|
||
| if ( logId && isPartialSync ) { | ||
| console.log( chalk.yellow( 'Warning: --site-id/--table is ignored when using --resume (backup already created).' ) ); | ||
| } | ||
|
|
||
| let confirmMsg; | ||
| if ( backup ) { | ||
| const age = formatAge( new Date( backup.date ) ); | ||
| confirmMsg = `Import backup ${ chalk.bold( backup.id ) } (${ chalk.dim( age + ' old' ) }) — replace local DB and merge uploads?`; | ||
| } else if ( logId ) { | ||
| confirmMsg = `Resume backup ${ chalk.bold( logId ) } and import DB + uploads into local project?`; | ||
| } else { | ||
| confirmMsg = `Create a new backup of ${ chalk.bold( app ) } and replace local DB and uploads?`; | ||
| } | ||
|
|
||
| const workDir = outputDir || fs.mkdtempSync( path.join( tmpdir(), 'altis-sync-' ) ); | ||
| const archivePath = path.join( workDir, `${ app }.tar` ); | ||
| const extractDir = path.join( workDir, `${ app }-extracted` ); | ||
|
|
||
| try { | ||
| if ( ! yes && ! latest ) { | ||
| await confirm( confirmMsg ); | ||
| } | ||
| // Create backup if needed | ||
| if ( ! backup && ! logId ) { | ||
| const backupSpinner = ora( `Creating remote backup for ${ chalk.bold( app ) }…` ).start(); | ||
| let tableList = table ? table.flatMap( t => String( t ).split( ',' ) ) : []; | ||
| if ( siteIds.length ) { | ||
| if ( siteIds.includes( '1' ) ) { | ||
| console.log( chalk.yellow( 'Note: site ID 1 is the main site — its tables have no numeric prefix, so the full DB will be synced.' ) ); | ||
| } | ||
| const siteTables = await resolveTablesForSiteIds( v, app, siteIds ); | ||
| tableList = [ ...new Set( [ ...tableList, ...siteTables ] ) ]; | ||
| } | ||
| // Site ID 1 is the main site — its uploads are at the root, not sites/1/ | ||
| let resolvedUploadsPath = uploadsPath; | ||
| if ( siteIds.length ) { | ||
| if ( Number( siteIds[0] ) === 1 ) { | ||
| console.log( chalk.yellow( 'Note: site ID 1 is the main site — syncing all uploads.' ) ); | ||
| resolvedUploadsPath = null; | ||
| } else { | ||
| resolvedUploadsPath = `sites/${ siteIds[0] }`; | ||
| } | ||
| } | ||
| const opts = { | ||
| database: 1, | ||
| uploads: 1, | ||
| ...( tableList.length ? { tables: tableList } : {} ), | ||
| ...( resolvedUploadsPath ? { uploads_path: resolvedUploadsPath } : {} ), | ||
| }; | ||
| try { | ||
| logId = await startBackup( v, app, opts ); | ||
| backupSpinner.succeed( `Backup started (log: ${ chalk.dim( logId ) })` ); | ||
| console.log( chalk.dim( `Resume later with: altis-cli app sync-local all ${ app } --resume ${ logId }` ) ); | ||
| } catch ( err ) { | ||
| backupSpinner.fail( `Failed to start backup: ${ err.message }` ); | ||
| process.exit( 1 ); | ||
| } | ||
| } | ||
|
|
||
| // Wait for backup to complete (if creating new or resuming) | ||
| if ( ! backup ) { | ||
| console.log( chalk.bold( 'Streaming backup progress…' ) ); | ||
| await waitForBackup( v, app, logId, startTime, debug ); | ||
|
|
||
| // Find completed backup | ||
| const findSpinner = ora( 'Finding completed backup…' ).start(); | ||
| backup = await findCompletedBackup( v, app, startTime ); | ||
| if ( ! backup ) { | ||
| findSpinner.fail( | ||
| `Backup completed but no download URL found. Run:\n altis-cli app backups ${ app }` | ||
| ); | ||
| process.exit( 1 ); | ||
| } | ||
| findSpinner.succeed( `Backup: ${ chalk.dim( backup.id ) }` ); | ||
| } | ||
|
|
||
| // Download archive | ||
| fs.mkdirSync( workDir, { recursive: true } ); | ||
| await downloadArchive( backup.url, archivePath ); | ||
|
|
||
| // Extract | ||
| const extractSpinner = ora( 'Extracting archive…' ).start(); | ||
| await extractArchive( archivePath, extractDir ); | ||
| const sqlGzPath = path.join( extractDir, 'database.sql.gz' ); | ||
| const uploadsDir = path.join( extractDir, 'uploads' ); | ||
| if ( ! fs.existsSync( sqlGzPath ) ) { | ||
| extractSpinner.fail( 'database.sql.gz not found in archive.' ); | ||
| process.exit( 1 ); | ||
| } | ||
| if ( ! fs.existsSync( uploadsDir ) ) { | ||
| extractSpinner.fail( 'uploads/ not found in archive.' ); | ||
| process.exit( 1 ); | ||
| } | ||
| extractSpinner.succeed( 'Extracted.' ); | ||
|
|
||
| // Search-replace + import database | ||
| console.log( chalk.bold( 'Importing database…' ) ); | ||
| await searchReplaceAndImport( sqlGzPath, mappings, localPath ); | ||
|
|
||
| // Cache flush | ||
| console.log( chalk.dim( 'Flushing object cache…' ) ); | ||
| await runComposerServer( localPath, [ 'cli', '--', 'cache', 'flush' ] ); | ||
|
|
||
| // Post-sync hook | ||
| if ( ! skipPostSync ) { | ||
| console.log( chalk.dim( 'Running wp altis post-sync…' ) ); | ||
| await runPostSync( localPath ); | ||
| } | ||
|
|
||
| // Copy uploads + import into S3 | ||
| console.log( chalk.bold( 'Syncing uploads…' ) ); | ||
| const copySpinner = ora( 'Copying uploads to content/uploads…' ).start(); | ||
| copyUploads( extractDir, localPath ); | ||
| copySpinner.succeed( 'Uploads copied.' ); | ||
|
|
||
| console.log( chalk.dim( 'Syncing uploads to local S3…' ) ); | ||
| await importUploads( localPath ); | ||
|
|
||
| console.log( chalk.bold.green( `\n✓ Database and uploads synced from ${ app }` ) ); | ||
|
|
||
| } catch ( err ) { | ||
| if ( err instanceof UserCancelled ) { | ||
| console.log( chalk.yellow( 'Cancelled.' ) ); | ||
| process.exit( 0 ); | ||
| } | ||
| console.error( chalk.red( `\nSync failed: ${ err.message }` ) ); | ||
| if ( fs.existsSync( archivePath ) ) { | ||
| const partial = archivePath + '.partial'; | ||
| fs.renameSync( archivePath, partial ); | ||
| console.log( chalk.dim( 'Kept files for debugging:' ) ); | ||
| console.log( ` ${ partial }` ); | ||
| } | ||
| if ( fs.existsSync( extractDir ) ) { | ||
| console.log( ` ${ extractDir }` ); | ||
| } | ||
| process.exit( 1 ); | ||
| } | ||
|
|
||
| // Cleanup | ||
| if ( ! keepArchive ) { | ||
| try { | ||
| fs.rmSync( archivePath, { force: true } ); | ||
| fs.rmSync( extractDir, { recursive: true, force: true } ); | ||
| } catch { | ||
| // Non-fatal | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| export default { | ||
| command: 'all <app>', | ||
| description: 'Sync database and uploads from a remote Altis Dashboard app into local-server.', | ||
| builder: cmd => { | ||
| addCommonOptions( cmd ); | ||
| addDatabaseOptions( cmd ); | ||
| addUploadsOptions( cmd ); | ||
| }, | ||
| handler, | ||
| }; | ||
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.
Uh oh!
There was an error while loading. Please reload this page.