Skip to content

Commit 8b8b84f

Browse files
committed
fix: imports and package version
1 parent 08486b2 commit 8b8b84f

File tree

4 files changed

+132
-864
lines changed

4 files changed

+132
-864
lines changed

packages/actions/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
},
4242
"dependencies": {
4343
"@adobe/node-fetch-retry": "^2.2.0",
44+
"@aws-sdk/client-ec2": "^3.357.0",
45+
"@aws-sdk/client-ssm": "^3.357.0",
4446
"@octokit/auth-oauth-device": "^4.0.4",
4547
"@types/winston": "^2.4.4",
4648
"blakejs": "^1.2.1",
@@ -62,8 +64,6 @@
6264
"solc": "^0.8.19"
6365
},
6466
"devDependencies": {
65-
"@aws-sdk/client-ec2": "^3.316.0",
66-
"@aws-sdk/client-ssm": "^3.357.0",
6767
"@ethersproject/providers": "^5.7.2",
6868
"@nomicfoundation/hardhat-chai-matchers": "^1.0.6",
6969
"@nomiclabs/hardhat-ethers": "^2.2.3",

packages/actions/src/helpers/ec2.ts

+115-118
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
import {
2-
EC2Client,
32
DescribeInstanceStatusCommand,
4-
RunInstancesCommand,
5-
StartInstancesCommand,
6-
StopInstancesCommand,
3+
RunInstancesCommand,
4+
StartInstancesCommand,
5+
StopInstancesCommand,
76
TerminateInstancesCommand,
8-
DescribeInstancesCommand
7+
DescribeInstancesCommand,
8+
EC2Client
99
} from "@aws-sdk/client-ec2"
10-
import { GetCommandInvocationCommand, SSMClient, SendCommandCommand, SendCommandCommandInput } from "@aws-sdk/client-ssm"
11-
import { P0tionEC2Instance } from "../types"
10+
import {
11+
GetCommandInvocationCommand,
12+
SSMClient,
13+
SendCommandCommand,
14+
SendCommandCommandInput
15+
} from "@aws-sdk/client-ssm"
1216
import dotenv from "dotenv"
17+
import { P0tionEC2Instance } from "../types"
18+
1319
dotenv.config()
1420

1521
/**
1622
* Extract AWS related environment variables
1723
*/
1824
export const getAWSVariables = () => {
1925
if (
20-
!process.env.AWS_ACCESS_KEY_ID ||
21-
!process.env.AWS_SECRET_ACCESS_KEY ||
26+
!process.env.AWS_ACCESS_KEY_ID ||
27+
!process.env.AWS_SECRET_ACCESS_KEY ||
2228
!process.env.AWS_ROLE_ARN ||
2329
!process.env.AWS_AMI_ID ||
24-
!process.env.AWS_KEY_NAME
25-
)
30+
!process.env.AWS_KEY_NAME
31+
)
2632
throw new Error("AWS related environment variables are not set. Please check your env file and try again.")
2733

2834
return {
@@ -44,13 +50,13 @@ export const createEC2Client = async (): Promise<EC2Client> => {
4450

4551
const ec2: EC2Client = new EC2Client({
4652
credentials: {
47-
accessKeyId: accessKeyId,
48-
secretAccessKey: secretAccessKey
53+
accessKeyId,
54+
secretAccessKey
4955
},
50-
region: region
56+
region
5157
})
5258

53-
return ec2
59+
return ec2
5460
}
5561

5662
/**
@@ -62,10 +68,10 @@ export const createSSMClient = async (): Promise<SSMClient> => {
6268

6369
const ssm: SSMClient = new SSMClient({
6470
credentials: {
65-
accessKeyId: accessKeyId,
66-
secretAccessKey: secretAccessKey
71+
accessKeyId,
72+
secretAccessKey
6773
},
68-
region: region
74+
region
6975
})
7076

7177
return ssm
@@ -77,14 +83,11 @@ export const createSSMClient = async (): Promise<SSMClient> => {
7783
* @param ptauPath <string> path to ptau file
7884
* @returns <string[]> array of commands to be run by the EC2 instance
7985
*/
80-
export const generateVMCommand = (
81-
zKeyPath: string,
82-
ptauPath: string,
83-
): string[] => {
86+
export const generateVMCommand = (zKeyPath: string, ptauPath: string): string[] => {
8487
const command = [
8588
"#!/usr/bin/env bash",
8689
"sudo apt update",
87-
"sudo apt install awscli -y", // install aws cli
90+
"sudo apt install awscli -y", // install aws cli
8891
"curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash", // install nvm
8992
"source ~/.bashrc",
9093
"nvm install 16",
@@ -93,84 +96,84 @@ export const generateVMCommand = (
9396
`aws s3 cp s3://${zKeyPath} ./genesisZkey.zkey`,
9497
`aws s3 cp s3://${ptauPath} ./pot.ptau`
9598
]
96-
99+
97100
return command
98101
}
99102

100103
/**
101104
* Determine the VM specs based on the circuit constraints (TODO)
102105
* @param circuitConstraints <string> the constraints of the circuit
103106
*/
104-
export const determineVMSpecs = async (circuitConstraints: string) => {}
105-
106-
// RAM -> instanceId
107-
const instancesTypes = {
108-
"t3.nano": {
109-
RAM: "0.5 GiB",
110-
VCPU: "2"
111-
},
112-
"t3.micro": {
113-
RAM: "1 GiB",
114-
VCPU: "2"
115-
},
116-
"t3.small": {
117-
RAM: "2 GiB",
118-
VCPU: "2"
119-
},
120-
"t3.medium": {
121-
RAM: "4 GiB",
122-
VCPU: "2"
123-
},
124-
"t3.large": {
125-
RAM: "8 GiB",
126-
VCPU: "2"
127-
},
128-
"t3.xlarge": {
129-
RAM: "16 GiB",
130-
VCPU: "4"
131-
},
132-
"t3.2xlarge": {
133-
RAM: "32 GiB",
134-
VCPU: "8"
135-
},
136-
"c5.9xlarge": {
137-
RAM: "36 GiB",
138-
VCPU: "36"
139-
},
140-
"c5.18xlarge": {
141-
RAM: "72 GiB",
142-
VCPU: "72"
143-
},
144-
"c5a.8xlarge": {
145-
RAM: "64 GiB",
146-
VCPU: "32"
147-
},
148-
"c5.12xlarge": {
149-
RAM: "96 GiB",
150-
VCPU: "48"
151-
},
152-
"c5a.16xlarge": {
153-
RAM: "128 GiB",
154-
VCPU: "64"
155-
},
156-
"c6i.32xlarge": {
157-
RAM: "256 GiB",
158-
VCPU: "128"
159-
},
160-
"m6a.32xlarge": {
161-
RAM: "512 GiB",
162-
VCPU: "128"
163-
}
164-
}
165-
166-
// 1. create ssh key in ec2 tab -> save the name
107+
// export const determineVMSpecs = async (circuitConstraints: string) => {}
108+
109+
// // RAM -> instanceId
110+
// const instancesTypes = {
111+
// "t3.nano": {
112+
// RAM: "0.5 GiB",
113+
// VCPU: "2"
114+
// },
115+
// "t3.micro": {
116+
// RAM: "1 GiB",
117+
// VCPU: "2"
118+
// },
119+
// "t3.small": {
120+
// RAM: "2 GiB",
121+
// VCPU: "2"
122+
// },
123+
// "t3.medium": {
124+
// RAM: "4 GiB",
125+
// VCPU: "2"
126+
// },
127+
// "t3.large": {
128+
// RAM: "8 GiB",
129+
// VCPU: "2"
130+
// },
131+
// "t3.xlarge": {
132+
// RAM: "16 GiB",
133+
// VCPU: "4"
134+
// },
135+
// "t3.2xlarge": {
136+
// RAM: "32 GiB",
137+
// VCPU: "8"
138+
// },
139+
// "c5.9xlarge": {
140+
// RAM: "36 GiB",
141+
// VCPU: "36"
142+
// },
143+
// "c5.18xlarge": {
144+
// RAM: "72 GiB",
145+
// VCPU: "72"
146+
// },
147+
// "c5a.8xlarge": {
148+
// RAM: "64 GiB",
149+
// VCPU: "32"
150+
// },
151+
// "c5.12xlarge": {
152+
// RAM: "96 GiB",
153+
// VCPU: "48"
154+
// },
155+
// "c5a.16xlarge": {
156+
// RAM: "128 GiB",
157+
// VCPU: "64"
158+
// },
159+
// "c6i.32xlarge": {
160+
// RAM: "256 GiB",
161+
// VCPU: "128"
162+
// },
163+
// "m6a.32xlarge": {
164+
// RAM: "512 GiB",
165+
// VCPU: "128"
166+
// }
167+
// }
168+
169+
// 1. create ssh key in ec2 tab -> save the name
167170
// 2. IAM role: access to ssh key ("iam:GetSSHPublicKey",)
168171
// 3. IAM role: ec2 access
169172
// 4. ec2 give role for s3 access
170-
// 5. have an api (express) running on the vm
173+
// 5. have an api (express) running on the vm
171174
// 6. have a script that runs on the vm that does the verification
172175
// 7. JWT Authorization: Bearer <token>
173-
// each circuit document needs to have the instance id of the vm
176+
// each circuit document needs to have the instance id of the vm
174177
/*
175178
{
176179
bucket: "x",
@@ -181,7 +184,7 @@ const instancesTypes = {
181184
*/
182185

183186
/**
184-
* Creates a new EC2 instance
187+
* Creates a new EC2 instance
185188
* @param ec2 <EC2Client> the EC2 client to talk to AWS
186189
* @param commands <string[]> the commands to be run on the EC2 instance
187190
* @param instanceType <string> the type of instance to be created
@@ -191,46 +194,45 @@ const instancesTypes = {
191194
* @returns <Promise<P0tionEC2Instance>> the instance that was created
192195
*/
193196
export const createEC2Instance = async (
194-
ec2: EC2Client,
195-
commands: string[],
197+
ec2: EC2Client,
198+
commands: string[],
196199
instanceType: string,
197-
amiId: string,
200+
amiId: string,
198201
keyName: string,
199202
roleArn: string
200-
): Promise<P0tionEC2Instance> => {
201-
202-
// create the params
203+
): Promise<P0tionEC2Instance> => {
204+
// create the params
203205
const params = {
204206
ImageId: amiId,
205207
InstanceType: instanceType, // to be determined programmatically
206208
MaxCount: 1,
207209
MinCount: 1,
208210
KeyName: keyName,
209211
// remember how to find this (iam -> roles -> role_name )
210-
IamInstanceProfile: {
211-
Arn: roleArn,
212+
IamInstanceProfile: {
213+
Arn: roleArn
212214
},
213215
// how to run commands on startup
214-
UserData: Buffer.from(commands.join("\n")).toString('base64')
216+
UserData: Buffer.from(commands.join("\n")).toString("base64")
215217
}
216218

217-
// create command
219+
// create command
218220
try {
219221
const command = new RunInstancesCommand(params)
220222
const response = await ec2.send(command)
221-
223+
222224
if (response.$metadata.httpStatusCode !== 200) {
223225
throw new Error("Could not create a new EC2 instance")
224226
}
225-
226-
const instance: P0tionEC2Instance = {
227+
228+
const instance: P0tionEC2Instance = {
227229
InstanceId: response.Instances![0].InstanceId!,
228230
ImageId: response.Instances![0].ImageId!,
229231
InstanceType: response.Instances![0].InstanceType!,
230232
KeyName: response.Instances![0].KeyName!,
231233
LaunchTime: response.Instances![0].LaunchTime!.toISOString()
232234
}
233-
235+
234236
return instance
235237
} catch (error: any) {
236238
console.log("[*] Debug", error)
@@ -248,11 +250,10 @@ export const checkEC2Status = async (ec2Client: EC2Client, instanceId: string):
248250
const command = new DescribeInstanceStatusCommand({
249251
InstanceIds: [instanceId]
250252
})
251-
253+
252254
const response = await ec2Client.send(command)
253-
if (response.$metadata.httpStatusCode !== 200)
254-
throw new Error("Could not get the status of the EC2 instance")
255-
255+
if (response.$metadata.httpStatusCode !== 200) throw new Error("Could not get the status of the EC2 instance")
256+
256257
return response.InstanceStatuses![0].InstanceState!.Name === "running"
257258
}
258259

@@ -291,7 +292,7 @@ export const startEC2Instance = async (ec2: EC2Client, instanceId: string) => {
291292

292293
if (response.$metadata.httpStatusCode !== 200) {
293294
throw new Error("Could not start the EC2 instance")
294-
}
295+
}
295296
}
296297

297298
/**
@@ -337,17 +338,13 @@ export const terminateEC2Instance = async (ec2: EC2Client, instanceId: string) =
337338
* @param commands <string[]> the commands to run
338339
* @return <Promise<any>> the command id
339340
*/
340-
export const runCommandOnEC2 = async (
341-
ssmClient: SSMClient,
342-
instanceId: string,
343-
commands: string[]
344-
): Promise<any> => {
341+
export const runCommandOnEC2 = async (ssmClient: SSMClient, instanceId: string, commands: string[]): Promise<any> => {
345342
// the params for the command
346343
const params: SendCommandCommandInput = {
347344
DocumentName: "AWS-RunShellScript",
348345
InstanceIds: [instanceId],
349346
Parameters: {
350-
"commands": commands
347+
commands
351348
},
352349
TimeoutSeconds: 1200
353350
}
@@ -373,10 +370,10 @@ export const runCommandOnEC2 = async (
373370
export const retrieveCommandOutput = async (
374371
ssmClient: SSMClient,
375372
commandId: string,
376-
instanceId: string
373+
instanceId: string
377374
): Promise<any> => {
378375
const command = new GetCommandInvocationCommand({
379-
CommandId: commandId,
376+
CommandId: commandId,
380377
InstanceId: instanceId
381378
})
382379

@@ -386,4 +383,4 @@ export const retrieveCommandOutput = async (
386383
} catch (error: any) {
387384
throw new Error("Could not retrieve the output of the command")
388385
}
389-
}
386+
}

0 commit comments

Comments
 (0)