pull/699/head
mike dupont 4 months ago
parent d1b7171f07
commit 4cd59cc18c

@ -0,0 +1,52 @@
import os
import json
import boto3
# Create .cache directory if it doesn't exist
os.makedirs('.cache', exist_ok=True)
def cache(name, value):
cache_file = f'.cache/{name}'
if not os.path.isfile(cache_file):
with open(cache_file, 'w') as f:
f.write(value)
# Initialize Boto3 SSM client
ssm = boto3.client('ssm')
# List commands from AWS SSM
response = ssm.list_commands()
cache("aws_ssm_list_commands", response)
# Retrieve commands
print(response)
commands = response["Commands"]
run_ids = [cmd['CommandId'] for cmd in commands]
print(f"RUNIDS: {run_ids}")
# Check the status of each command
for command in commands:
#print(command)
command_id = command['CommandId']
status = command['Status']
#eG: command= {'CommandId': '820dcf47-e8d7-4c23-8e8a-bc64de2883ff', 'DocumentName': 'AWS-RunShellScript', 'DocumentVersion': '$DEFAULT', 'Comment': '', 'ExpiresAfter': datetime.datetime(2024, 12, 13, 12, 41, 24, 683000, tzinfo=tzlocal()), 'Parameters': {'commands': ['sudo su - -c "tail /var/log/cloud-init-output.log"']}, 'InstanceIds': [], 'Targets': [{'Key': 'instanceids', 'Values': ['i-073378237c5a9dda1']}], 'RequestedDateTime': datetime.datetime(2024, 12, 13, 10, 41, 24, 683000, tzinfo=tzlocal()), 'Status': 'Success', 'StatusDetails': 'Success', 'OutputS3Region': 'us-east-1', 'OutputS3BucketName': '', 'OutputS3KeyPrefix': '', 'MaxConcurrency': '50', 'MaxErrors': '0', 'TargetCount': 1, 'CompletedCount': 1, 'ErrorCount': 0, 'DeliveryTimedOutCount': 0, 'ServiceRole': '', 'NotificationConfig': {'NotificationArn': '', 'NotificationEvents': [], 'NotificationType': ''}, 'CloudWatchOutputConfig': {'CloudWatchLogGroupName': '', 'CloudWatchOutputEnabled': False}, 'TimeoutSeconds': 3600, 'AlarmConfiguration': {'IgnorePollAlarmFailure': False, 'Alarms': []}, 'TriggeredAlarms': []}], 'ResponseMetadata': {'RequestId': '535839c4-9b87-4526-9c01-ed57f07d21ef', 'HTTPStatusCode': 200, 'HTTPHeaders': {'server': 'Server', 'date': 'Fri, 13 Dec 2024 16:58:53 GMT', 'content-type': 'application/x-amz-json-1.1', 'content-length': '2068', 'connection': 'keep-alive', 'x-amzn-requestid': '535839c4-9b87-4526-9c01-ed57f07d21ef'}, 'RetryAttempts': 0}}
if status == "Success":
print(f"Check logs of {command_id}")
# use ssm to fetch logs using CommandId
# Assuming you have the command_id from the previous command output
command_id = command['CommandId']
instance_id = command['Targets'][0]['Values'][0] # Get the instance ID
# Fetching logs using CommandId
log_response = ssm.get_command_invocation(
CommandId=command_id,
InstanceId=instance_id
)
print(log_response['StandardOutputContent']) # Output logs
print(log_response['StandardErrorContent']) # Error logs (if any)
print(f"aws ssm start-session --target {instance_id}")

@ -1,47 +1,8 @@
import time
# # Get the list of instance IDs and their states
# instances=$(aws ec2 describe-instances --query "Reservations[*].Instances[*].[InstanceId,State.Name]" --output text)
# # aws ssm send-command --document-name AWS-RunShellScript --targets Key=instanceids,Values=i-073378237c5a9dda1 --parameters 'commands=["sudo su - -c \"tail /var/log/cloud-init-output.log\""]'
# parse_command_id(){
# # send_command_output
# local send_command_output=$1
# echo "$send_command_output" | jq -r '.Command.CommandId'
# }
# # Loop through each instance ID and state
# while read -r instance_id state; do
# if [[ $state == "running" ]]; then
# echo "Starting session for instance: $instance_id"
# # Start a session and execute commands (replace with your commands)
# #aws ssm start-session --target "$instance_id" --document-name "AWS-StartInteractiveCommand" --parameters 'commands=["sudo su -","tail /var/log/cloud-init-output.log"]'
# #--target "$instance_id"
# send_command_output=$(aws ssm send-command --document-name "AWS-RunShellScript" --targets "Key=instanceids,Values=$instance_id" --parameters 'commands=["sudo su - -c \"tail /var/log/cloud-init-output.log\""]')
# # now get the command id
# command_id=$(parse_command_id send_command_output)
# # now for 4 times, sleep 1 sec,
# for i in {1..4}; do
# sleep 1
# command_status=$(aws ssm list-command-invocations --command-id "$command_id" --details)
# echo "$command_status"
# done
# fi
# done <<< "$instances"
# # rewrite in python
# Here is the equivalent Python script using `boto3` to interact with AWS SSM:
# ```python
import boto3
import time
#from dateutil import tz
def parse_command_id(send_command_output):
return send_command_output['Command']['CommandId']
@ -74,9 +35,20 @@ def main():
# Check the command status every second for 4 seconds
for _ in range(4):
time.sleep(1)
time.sleep(20)
command_status = ssm_client.list_command_invocations(CommandId=command_id, Details=True)
print(command_status)
for invocation in command_status['CommandInvocations']:
if invocation['Status'] == 'Success':
for plugin in invocation['CommandPlugins']:
if plugin['Status'] == 'Success':
print(f"Output from instance {instance_id}:\n{plugin['Output']}")
else:
print(f"Error in plugin execution for instance {instance_id}: {plugin['StatusDetails']}")
else:
print(f"Command for instance {instance_id} is still in progress... Status: {invocation['Status']}")
if __name__ == "__main__":
main()

@ -15,7 +15,7 @@ fi
if [ ! -f "${ROOT}/opt/swarms/install/apt.txt" ]; then
apt update
apt install --allow-change-held-packages -y git python3-virtualenv nginx
snap install expect
apt install --allow-change-held-packages -y expect
snap install aws-cli --classic
echo 1 >"${ROOT}/opt/swarms/install/apt.txt"
fi

@ -0,0 +1,32 @@
import time
import boto3
#from dateutil import tz
def parse_command_id(send_command_output):
return send_command_output['Command']['CommandId']
def main():
ec2_client = boto3.client('ec2')
ssm_client = boto3.client('ssm')
# Get the list of instance IDs and their states
instances_response = ec2_client.describe_instances()
instances = [
(instance['InstanceId'], instance['State']['Name'])
for reservation in instances_response['Reservations']
for instance in reservation['Instances']
]
for reservation in instances_response['Reservations']:
for instance in reservation['Instances']:
print(instance)
instance_id = instance['InstanceId']
state = instance['State']['Name']
if state == 'running':
print(f"Starting command for instance: {instance_id}")
print(f"aws ssm start-session --target {instance_id}")
if __name__ == "__main__":
main()
Loading…
Cancel
Save