MCPcopy Create free account
hub / github.com/agenticsorg/edge-agents / handle_ec2_command

Function handle_ec2_command

scripts/agents/devops/src/cli.py:272–383  ·  view source on GitHub ↗

Handle EC2-related commands.

(args)

Source from the content-addressed store, hash-verified

270
271
272def handle_ec2_command(args):
273 """Handle EC2-related commands."""
274 # Get AWS credentials
275 try:
276 cred_manager = get_credential_manager()
277 aws_creds = cred_manager.get_aws_credentials(region=args.region)
278
279 # Initialize EC2 service
280 ec2 = EC2Service(credentials=aws_creds)
281
282 if args.ec2_command == 'list-instances':
283 filters = []
284 if args.state:
285 filters.append({
286 'Name': 'instance-state-name',
287 'Values': [args.state]
288 })
289
290 instances = ec2.list_instances(filters=filters if filters else None)
291
292 # Simplify instance data for output
293 simplified_instances = []
294 for instance in instances:
295 name = 'unnamed'
296 for tag in instance.get('Tags', []):
297 if tag['Key'] == 'Name':
298 name = tag['Value']
299 break
300
301 simplified_instances.append({
302 'InstanceId': instance['InstanceId'],
303 'Name': name,
304 'State': instance['State']['Name'],
305 'Type': instance['InstanceType'],
306 'LaunchTime': instance['LaunchTime']
307 })
308
309 if not simplified_instances:
310 print(f"{COLORS['yellow']}No instances found.{COLORS['reset']}")
311 else:
312 print(format_output(simplified_instances, args.output))
313
314 elif args.ec2_command == 'get-instance':
315 try:
316 instance = ec2.get_instance(args.instance_id)
317 print(format_output(instance, args.output))
318 except ResourceNotFoundError:
319 print_error(
320 f"Instance {args.instance_id} not found",
321 suggestion="Check the instance ID and region."
322 )
323 return 1
324
325 elif args.ec2_command == 'create-instance':
326 security_group_ids = None
327 if args.security_group_ids:
328 security_group_ids = args.security_group_ids.split(',')
329

Callers 1

mainFunction · 0.70

Calls 15

list_instancesMethod · 0.95
get_instanceMethod · 0.95
create_instanceMethod · 0.95
start_instanceMethod · 0.95
stop_instanceMethod · 0.95
terminate_instanceMethod · 0.95
deploy_from_githubMethod · 0.95
get_credential_managerFunction · 0.85
EC2ServiceClass · 0.85
format_outputFunction · 0.70
print_errorFunction · 0.70
handle_cli_errorFunction · 0.70

Tested by

no test coverage detected