|
| 1 | + |
| 2 | +import { SpawnStatus } from 'codify-schemas'; |
| 3 | + |
| 4 | +import { PluginInitOrchestrator } from '../common/initialize-plugins.js'; |
| 5 | +import { ProcessName, ctx } from '../events/context.js'; |
| 6 | +import { Reporter } from '../ui/reporters/reporter.js'; |
| 7 | +import { sleep } from '../utils/index.js'; |
| 8 | +import { spawn, spawnSafe } from '../utils/spawn.js'; |
| 9 | +import { PlanOrchestrator } from './plan.js'; |
| 10 | +import { ValidateOrchestrator } from './validate.js'; |
| 11 | + |
| 12 | +export interface TestArgs { |
| 13 | + path?: string; |
| 14 | + secure?: boolean; |
| 15 | + verbosityLevel?: number; |
| 16 | +} |
| 17 | + |
| 18 | +export const TestOrchestrator = { |
| 19 | + async run(args: TestArgs, reporter: Reporter): Promise<void> { |
| 20 | + |
| 21 | + // Perform validation initially to ensure the project is valid |
| 22 | + const initializationResult = await PluginInitOrchestrator.run(args, reporter); |
| 23 | + await ValidateOrchestrator.run({ existing: initializationResult }, reporter); |
| 24 | + |
| 25 | + const planResult = await PlanOrchestrator.run({ |
| 26 | + codifyConfigs: [{ |
| 27 | + type: 'project', |
| 28 | + plugins: { default: '/Users/kevinwang/Projects/codify-homebrew-plugin/src/index.ts' } |
| 29 | + }, { |
| 30 | + type: 'homebrew', |
| 31 | + formulae: ['sshpass'] |
| 32 | + }, { |
| 33 | + type: 'tart', |
| 34 | + clone: [{ sourceName: 'ghcr.io/cirruslabs/macos-tahoe-base:latest', name: 'codify-test-vm' }], |
| 35 | + }], |
| 36 | + }, reporter); |
| 37 | + |
| 38 | + // Short circuit and exit if every change is NOOP |
| 39 | + if (!planResult.plan.isEmpty()) { |
| 40 | + const confirm = await reporter.promptConfirmation('The following resources will need to be installed (Tart VM - 25gb). Do you want to continue?') |
| 41 | + if (!confirm) { |
| 42 | + return process.exit(0); |
| 43 | + } |
| 44 | + |
| 45 | + const { plan, pluginManager, project } = planResult; |
| 46 | + const filteredPlan = plan.filterNoopResources() |
| 47 | + |
| 48 | + ctx.processStarted(ProcessName.APPLY); |
| 49 | + await pluginManager.apply(project, filteredPlan); |
| 50 | + ctx.processFinished(ProcessName.APPLY); |
| 51 | + } |
| 52 | + |
| 53 | + const vmName = this.generateVmName(); |
| 54 | + await spawnSafe(`tart clone codify-test-vm ${vmName}`, { interactive: true }); |
| 55 | + |
| 56 | + // Run this in the background. The user will have to manually exit the GUI to stop the test. |
| 57 | + spawnSafe(`tart run ${vmName}`, { interactive: true }) |
| 58 | + .finally(() => { |
| 59 | + console.log('VM has been killed... exiting.') |
| 60 | + process.exit(1); |
| 61 | + }) |
| 62 | + await sleep(10_000); |
| 63 | + await this.waitUntilVmIsReady(vmName); |
| 64 | + |
| 65 | + // Install codify on the VM |
| 66 | + // await spawn(`tart exec ${vmName} /bin/bash -c "$(curl -fsSL https://releases.codifycli.com/install.sh)"`, { interactive: true }); |
| 67 | + const { data: ip } = await spawnSafe(`tart ip ${vmName}`, { interactive: true }); |
| 68 | + await spawn(`sshpass -p "admin" scp -o PubkeyAuthentication=no -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ${initializationResult.project.codifyFiles[0]} admin@${ip}:~/codify.jsonc`, { interactive: true }); |
| 69 | + // await spawn(`tart exec ${vmName} codify apply`, undefined, { interactive: true }); |
| 70 | + |
| 71 | + await spawn(`tart exec ${vmName} osascript -e "tell application \\"Terminal\\" to do script \\"cd ~/ && codify apply\\""`) |
| 72 | + |
| 73 | + await sleep(1_000_000_000); |
| 74 | + }, |
| 75 | + |
| 76 | + generateVmName(): string { |
| 77 | + return `codify-test-vm-${Date.now()}`; |
| 78 | + }, |
| 79 | + |
| 80 | + async waitUntilVmIsReady(vmName: string): Promise<void> { |
| 81 | + while (true) { |
| 82 | + const result = await spawnSafe(`tart exec ${vmName} pwd`, { interactive: true, timeout: 5000 }) |
| 83 | + if (result.status === SpawnStatus.SUCCESS) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + await sleep(1000); |
| 88 | + } |
| 89 | + } |
| 90 | +}; |
0 commit comments