@@ -149,6 +149,23 @@ const instancesTypes = {
149
149
}
150
150
}
151
151
152
+ // 1. create ssh key in ec2 tab -> save the name
153
+ // 2. IAM role: access to ssh key ("iam:GetSSHPublicKey",)
154
+ // 3. IAM role: ec2 access
155
+ // 4. ec2 give role for s3 access
156
+ // 5. have an api (express) running on the vm
157
+ // 6. have a script that runs on the vm that does the verification
158
+ // 7. JWT Authorization: Bearer <token>
159
+ // each circuit document needs to have the instance id of the vm
160
+ /*
161
+ {
162
+ bucket: "x",
163
+ action: "verify/checkStatus",
164
+ "zKeyIndex": 0,
165
+ "zKeyStoragePath": /circuit/..,
166
+ }
167
+ */
168
+
152
169
/**
153
170
* Creates a new EC2 instance
154
171
* @param ec2 <EC2Client> the EC2 client to talk to AWS
@@ -164,7 +181,9 @@ export const createEC2Instance = async (ec2: EC2Client): Promise<P0tionEC2Instan
164
181
"sudo apt install awscli -y" ,
165
182
"touch /tmp/test.txt" ,
166
183
"echo 'hello world' > /tmp/test.txt" ,
167
- "aws s3 cp /tmp/test.txt s3://p0tion-test-bucket/test.txt"
184
+ "aws s3 cp /tmp/test.txt s3://p0tion-test-bucket/test.txt" ,
185
+ "npm install -g p0tion-api" ,
186
+ "p0tion-api " ,
168
187
]
169
188
170
189
// create the params
@@ -190,7 +209,7 @@ export const createEC2Instance = async (ec2: EC2Client): Promise<P0tionEC2Instan
190
209
throw new Error ( "Could not create a new EC2 instance" )
191
210
}
192
211
193
- const instance : P0tionEC2Instance = {
212
+ const instance : P0tionEC2Instance = {
194
213
InstanceId : response . Instances ! [ 0 ] . InstanceId ! ,
195
214
ImageId : response . Instances ! [ 0 ] . ImageId ! ,
196
215
InstanceType : response . Instances ! [ 0 ] . InstanceType ! ,
@@ -207,7 +226,7 @@ export const createEC2Instance = async (ec2: EC2Client): Promise<P0tionEC2Instan
207
226
* @param instanceId <string> the id of the instance to check
208
227
* @returns <Promise<bool>> the status of the instance
209
228
*/
210
- export const checkEC2Status = async ( ec2Client : EC2Client , instanceId : string ) => {
229
+ export const checkEC2Status = async ( ec2Client : EC2Client , instanceId : string ) : Promise < boolean > => {
211
230
const command = new DescribeInstanceStatusCommand ( {
212
231
InstanceIds : [ instanceId ]
213
232
} )
@@ -216,8 +235,28 @@ export const checkEC2Status = async (ec2Client: EC2Client, instanceId: string) =
216
235
if ( response . $metadata . httpStatusCode !== 200 ) {
217
236
throw new Error ( "Could not get the status of the EC2 instance" )
218
237
}
238
+
219
239
return response . InstanceStatuses ! [ 0 ] . InstanceState ! . Name === "running"
240
+ }
220
241
242
+ /**
243
+ * Get the IP of an EC2 instance
244
+ * @notice the IP will change at every restart
245
+ * @param ec2Client <EC2Client> the EC2 client to talk to AWS
246
+ * @param instanceId <string> the id of the instance to get the IP of
247
+ * @returns <Promise<string>> the IP of the instance
248
+ */
249
+ export const getEC2Ip = async ( ec2Client : EC2Client , instanceId : string ) => {
250
+ const command = new DescribeInstancesCommand ( {
251
+ InstanceIds : [ instanceId ]
252
+ } )
253
+
254
+ const response = await ec2Client . send ( command )
255
+ if ( response . $metadata . httpStatusCode !== 200 ) {
256
+ throw new Error ( "Could not get the IP of the EC2 instance" )
257
+ }
258
+
259
+ return response . Reservations ! [ 0 ] . Instances ! [ 0 ] . PublicIpAddress
221
260
}
222
261
223
262
/**
@@ -274,24 +313,3 @@ export const terminateEC2Instance = async (ec2: EC2Client, instanceId: string) =
274
313
throw new Error ( "Could not terminate the EC2 instance" )
275
314
}
276
315
}
277
-
278
- /**
279
- * Get the EC2 public ip
280
- * @notice At each restart, the EC2 instance gets a new IP
281
- * @param ec2 <EC2Client> the EC2 client to talk to AWS
282
- * @param instanceId <string> the id of the instance to get the IP of
283
- * @returns <Promise<string>> the IP of the instance
284
- */
285
- export const getEC2Ip = async ( ec2 : EC2Client , instanceId : string ) : Promise < string > => {
286
- const command = new DescribeInstancesCommand ( {
287
- InstanceIds : [ instanceId ]
288
- } )
289
-
290
- const response = await ec2 . send ( command )
291
-
292
- if ( response . $metadata . httpStatusCode !== 200 ) {
293
- throw new Error ( "Could not get the EC2 instance" )
294
- }
295
-
296
- return response . Reservations ! [ 0 ] . Instances ! [ 0 ] . PublicIpAddress
297
- }
0 commit comments