Skip to content

Commit 1113a77

Browse files
committed
chore: wip
chore: wip chore: wip
1 parent b1b015c commit 1113a77

File tree

6 files changed

+88
-28
lines changed

6 files changed

+88
-28
lines changed

.stacks/core/buddy/src/commands/cloud.ts

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import process from 'node:process'
22
import type { CLI, CloudOptions } from '@stacksjs/types'
3-
import { intro, italic, log, outro, runCommand } from '@stacksjs/cli'
3+
import { intro, italic, log, outro, prompts, runCommand } from '@stacksjs/cli'
44
import { path as p } from '@stacksjs/path'
55
import { ExitCode } from '@stacksjs/types'
6+
import { addJumpBox, deleteJumpBox, getJumpBoxInstanceId } from '@stacksjs/cloud'
67

78
export function cloud(buddy: CLI) {
89
const descriptions = {
910
cloud: 'Interact with the Stacks Cloud',
1011
ssh: 'SSH into the Stacks Cloud',
12+
add: 'Add a resource to the Stacks Cloud.',
1113
remove: 'Remove the Stacks Cloud. In case it fails, try again.',
1214
verbose: 'Enable verbose output',
1315
}
@@ -20,7 +22,8 @@ export function cloud(buddy: CLI) {
2022
.action(async (options: CloudOptions) => {
2123
if (options.ssh || options.connect) {
2224
const startTime = performance.now()
23-
const result = await runCommand('aws ssm start-session --target i-0a4a48be7544a72e3', {
25+
const jumpBoxId = await getJumpBoxInstanceId()
26+
const result = await runCommand(`aws ssm start-session --target ${jumpBoxId}`, {
2427
...options,
2528
cwd: p.projectPath(),
2629
stdin: 'pipe',
@@ -39,18 +42,76 @@ export function cloud(buddy: CLI) {
3942
process.exit(ExitCode.Success)
4043
})
4144

45+
buddy
46+
.command('cloud:add', descriptions.add)
47+
.option('--jump-box', 'Remove the jump-box', { default: true })
48+
.option('--verbose', descriptions.verbose, { default: false })
49+
.action(async (options: CloudOptions) => {
50+
const startTime = await intro('buddy cloud:add')
51+
52+
if (options.jumpBox) {
53+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
54+
const { confirm } = await prompts({
55+
name: 'confirm',
56+
type: 'confirm',
57+
message: 'Would you like to add a jump-box?',
58+
})
59+
60+
if (!confirm) {
61+
await outro('Exited', { startTime, useSeconds: true })
62+
process.exit(ExitCode.Success)
63+
}
64+
65+
log.info('')
66+
log.info('The jump-box is getting added to your cloud resources...')
67+
log.info('This takes a few moments, please be patient')
68+
log.info('')
69+
// sleep for 2 seconds to get the user to read the message
70+
await new Promise(resolve => setTimeout(resolve, 2000))
71+
72+
await addJumpBox()
73+
74+
await outro('Your jump-box was added.', { startTime, useSeconds: true })
75+
process.exit(ExitCode.Success)
76+
}
77+
78+
log.info('')
79+
log.info('This functionality is not yet implemented.')
80+
process.exit(ExitCode.Success)
81+
})
82+
4283
buddy
4384
.command('cloud:remove', descriptions.remove)
4485
.alias('cloud:destroy')
45-
// .option('--jump-box', 'Remove the jump box', { default: true })
86+
.option('--jump-box', 'Remove the jump-box', { default: false })
4687
.option('--verbose', descriptions.verbose, { default: false })
4788
.action(async (options: CloudOptions) => {
4889
const startTime = await intro('buddy cloud:remove')
4990

91+
if (options.jumpBox) {
92+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
93+
const { confirm } = await prompts({
94+
name: 'confirm',
95+
type: 'confirm',
96+
message: 'Would you like to remove your jump-box for now?',
97+
})
98+
99+
if (!confirm) {
100+
await outro('Exited', { startTime, useSeconds: true })
101+
process.exit(ExitCode.Success)
102+
}
103+
104+
await deleteJumpBox()
105+
106+
await outro('Your jump-box was removed.', { startTime, useSeconds: true })
107+
process.exit(ExitCode.Success)
108+
}
109+
50110
log.info('')
51111
log.info('Please note, removing your cloud resources will take a while to complete. Please be patient.')
52112
log.info('')
53-
log.info(italic('If you see an error, please try again. If the error persists, please contact support.'))
113+
log.info(italic('Due to the nature of Lambda@edge functions, this command may fail on first execution.'))
114+
log.info(italic('If it does fail, don\'t worry—please try again, or contact us trough Discord.'))
54115
// sleep for 2 seconds to get the user to read the message
55116
await new Promise(resolve => setTimeout(resolve, 2000))
56117

.stacks/core/buddy/src/commands/deploy.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { runAction } from '@stacksjs/actions'
55
import { intro, italic, outro } from '@stacksjs/cli'
66
import { Action, ExitCode } from '@stacksjs/types'
77
import { Route53 } from '@aws-sdk/client-route-53'
8-
import app from '~/config/app'
8+
import { app, config } from '@stacksjs/config'
9+
import { dd } from '@stacksjs/logging'
910

1011
export function deploy(buddy: CLI) {
1112
const descriptions = {

.stacks/core/buddy/src/commands/domains.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import process from 'node:process'
22
import type { CLI, DomainsOptions } from '@stacksjs/types'
33
import { runAction } from '@stacksjs/actions'
44
import { config } from '@stacksjs/config'
5-
import { bgCyan, bold, dd, intro, italic, outro, prompts } from '@stacksjs/cli'
5+
import { bgCyan, bold, intro, italic, outro, prompts } from '@stacksjs/cli'
66
import { Action, ExitCode } from '@stacksjs/types'
77
import { addDomain } from './deploy'
88

.stacks/core/cloud/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@
6969
"@aws-lambda-powertools/tracer": "^1.13.1",
7070
"@aws-sdk/client-cloudfront": "^3.421.0",
7171
"@aws-sdk/client-route-53-domains": "^3.421.0",
72+
"@aws-sdk/client-cloudformation": "^3.418.0",
73+
"@aws-sdk/client-ec2": "^3.418.0",
74+
"@aws-sdk/client-ssm": "^3.418.0",
7275
"@stacksjs/config": "workspace:*",
7376
"@stacksjs/dns": "workspace:*",
7477
"@stacksjs/logging": "workspace:*",

.stacks/core/cloud/src/cloud.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class StacksCloud extends Stack {
3838
privateSource: string
3939
zone!: route53.IHostedZone
4040
redirectZones: route53.IHostedZone[] = []
41-
ec2Instance!: ec2.Instance
41+
ec2Instance?: ec2.Instance
4242
storage!: {
4343
publicBucket: s3.Bucket
4444
privateBucket: s3.Bucket
@@ -364,21 +364,22 @@ export class StacksCloud extends Stack {
364364

365365
role.addManagedPolicy(iam.ManagedPolicy.fromAwsManagedPolicyName('AmazonSSMManagedInstanceCore'))
366366

367+
// this instance needs to be created once to mount the EFS & clone the Stacks repo
367368
this.ec2Instance = new ec2.Instance(this, 'JumpBoxInstance', {
368369
vpc: this.vpc,
369370
instanceType: ec2.InstanceType.of(ec2.InstanceClass.T2, ec2.InstanceSize.MICRO),
370371
machineImage: new ec2.AmazonLinuxImage(),
371372
role,
372373
userData: ec2.UserData.custom(`
373-
#!/bin/bash
374-
yum update -y
375-
yum install -y amazon-efs-utils
376-
yum install -y git
377-
yum install -y https://s3.us-east-1.amazonaws.com/amazon-ssm-us-east-1/latest/linux_amd64/amazon-ssm-agent.rpm
378-
mkdir /mnt/efs
379-
mount -t efs ${this.storage.fileSystem.fileSystemId}:/ /mnt/efs
380-
git clone https://github.com/stacksjs/stacks.git /mnt/efs
381-
`),
374+
#!/bin/bash
375+
yum update -y
376+
yum install -y amazon-efs-utils
377+
yum install -y git
378+
yum install -y https://s3.us-east-1.amazonaws.com/amazon-ssm-us-east-1/latest/linux_amd64/amazon-ssm-agent.rpm
379+
mkdir /mnt/efs
380+
mount -t efs ${this.storage.fileSystem.fileSystemId}:/ /mnt/efs
381+
git clone https://github.com/stacksjs/stacks.git /mnt/efs
382+
`),
382383
})
383384

384385
this.storage.accessPoint = new efs.AccessPoint(this, 'StacksAccessPoint', {
@@ -556,16 +557,12 @@ export class StacksCloud extends Stack {
556557
})
557558
}
558559

559-
new Output(this, 'JumpBoxInstanceId', {
560-
value: this.ec2Instance.instanceId,
561-
description: 'The ID of the EC2 instance that can be used to SSH into the Stacks Cloud.',
562-
})
563-
564-
// async function getInstanceId(stackName) {
565-
// const data = await cloudformation.describeStacks({ StackName: stackName }).promise()
566-
// const instanceIdOutput = data.Stacks[0].Outputs.find(output => output.OutputKey === 'CloudInstanceId')
567-
// return instanceIdOutput.OutputValue
568-
// }
560+
if (this.ec2Instance?.instanceId) {
561+
new Output(this, 'JumpBoxInstanceId', {
562+
value: this.ec2Instance.instanceId,
563+
description: 'The ID of the EC2 instance that can be used to SSH into the Stacks Cloud.',
564+
})
565+
}
569566

570567
// if docsPrefix is not set, then we know we are in docsMode and the documentation lives at the root of the domain
571568
if (this.shouldDeployDocs() && this.docsPrefix) {

.stacks/core/types/src/cloud.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ export interface CloudOptions {
5959
api: boolean
6060
fileSystem: boolean
6161
}
62-
63-
useJumpBox: boolean
6462
}
6563

6664
export type CloudConfig = Partial<CloudOptions>

0 commit comments

Comments
 (0)