Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion packages/cli/src/commands/scale.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, it, expect, afterEach } from 'vitest';
import { mkdtempSync, writeFileSync, rmSync, existsSync, readFileSync } from 'node:fs';
import { mkdtempSync, writeFileSync, rmSync, existsSync, readFileSync, mkdirSync } from 'node:fs';
import { spawnSync } from 'node:child_process';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import {
Expand Down Expand Up @@ -414,6 +415,55 @@ describe('auto-scale rules', () => {
});
});

describe('DNS JSON mode', () => {
it('persists DNS config when --json is used without --dry-run', () => {
const dir = makeTempDir();
const stateDir = join(dir, '.sh1pt');
mkdirSync(stateDir, { recursive: true });
const statePath = join(stateDir, 'credentials.json');
writeFileSync(statePath, JSON.stringify({
apiKey: 'preserve-me',
instances: [
{
id: 'inst-0001',
provider: 'aws',
status: 'running',
publicIp: '203.0.113.10',
createdAt: '',
hourlyRate: 0.1,
},
],
lastUpdated: '',
}));

const scaleModule = new URL('./scale.ts', import.meta.url).href;
const runDns = [
`import { scaleCmd } from ${JSON.stringify(scaleModule)};`,
`await scaleCmd.parseAsync(['node', 'sh1pt', 'dns', '--provider', 'dns-cloudflare', '--domain', 'api.example.com', '--json']);`,
].join('\n');
const result = spawnSync(
process.execPath,
['--import', 'tsx', '--input-type=module', '--eval', runDns],
{
env: { ...process.env, HOME: dir, USERPROFILE: dir },
encoding: 'utf8',
},
);

expect(result.status, result.stderr).toBe(0);
const output = JSON.parse(result.stdout);
expect(output.recordCount).toBe(1);

const saved = JSON.parse(readFileSync(statePath, 'utf-8'));
expect(saved.apiKey).toBe('preserve-me');
expect(saved.dns).toMatchObject({
domain: 'api.example.com',
provider: 'dns-cloudflare',
ips: ['203.0.113.10'],
});
});
});

// ---------------------------------------------------------------------------
// rollbackPlanIps — rollback plan must list the OLD fleet, not the new one
// ---------------------------------------------------------------------------
Expand Down
44 changes: 25 additions & 19 deletions packages/cli/src/commands/scale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -643,30 +643,31 @@ scaleCmd
records,
};

if (opts.json) {
console.log(JSON.stringify(summary, null, 2));
return;
}
if (!opts.json) {
console.log(kleur.bold('\n🌐 DNS Round-Robin Plan'));
console.log(kleur.dim('─'.repeat(56)));
console.log(`${kleur.cyan('Domain:'.padEnd(20))} ${opts.domain}`);
console.log(`${kleur.cyan('DNS Provider:'.padEnd(20))} ${opts.provider}`);
console.log(`${kleur.cyan('TTL:'.padEnd(20))} ${opts.ttl}s`);
if (opts.proxied) {
console.log(`${kleur.cyan('Proxied:'.padEnd(20))} ${kleur.yellow('yes (Cloudflare edge)')}`);
}
console.log(`${kleur.cyan('Records:'.padEnd(20))} ${records.length} A record(s)`);
console.log(kleur.dim('─'.repeat(56)));

console.log(kleur.bold('\n🌐 DNS Round-Robin Plan'));
console.log(kleur.dim('─'.repeat(56)));
console.log(`${kleur.cyan('Domain:'.padEnd(20))} ${opts.domain}`);
console.log(`${kleur.cyan('DNS Provider:'.padEnd(20))} ${opts.provider}`);
console.log(`${kleur.cyan('TTL:'.padEnd(20))} ${opts.ttl}s`);
if (opts.proxied) {
console.log(`${kleur.cyan('Proxied:'.padEnd(20))} ${kleur.yellow('yes (Cloudflare edge)')}`);
}
console.log(`${kleur.cyan('Records:'.padEnd(20))} ${records.length} A record(s)`);
console.log(kleur.dim('─'.repeat(56)));
for (const rec of records) {
console.log(` ${kleur.green('A')} ${rec.name.padEnd(30)} → ${rec.value} ${kleur.dim(`(inst: ${rec.instanceId}, ${rec.provider})`)}`);
}

for (const rec of records) {
console.log(` ${kleur.green('A')} ${rec.name.padEnd(30)} → ${rec.value} ${kleur.dim(`(inst: ${rec.instanceId}, ${rec.provider})`)}`);
console.log(kleur.dim('─'.repeat(56)));
}

console.log(kleur.dim('─'.repeat(56)));

if (opts.dryRun) {
console.log(kleur.dim('Dry-run — no DNS changes made.'));
if (opts.json) {
console.log(JSON.stringify(summary, null, 2));
} else {
console.log(kleur.dim('Dry-run — no DNS changes made.'));
}
return;
}

Expand All @@ -692,6 +693,11 @@ scaleCmd
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
writeFileSync(credsPath, JSON.stringify(creds, null, 2));

if (opts.json) {
console.log(JSON.stringify(summary, null, 2));
return;
}

console.log(kleur.green(`✅ DNS round-robin configured for ${opts.domain} with ${records.length} A record(s).`));
console.log(kleur.dim(`DNS provider: ${opts.provider}`));
console.log(kleur.dim(`Next step: verify DNS propagation with \`dig ${opts.domain}\``));
Expand Down
Loading