pull/699/head
mike dupont 4 months ago
parent 19651e9532
commit e6391981ea

@ -1,17 +0,0 @@
import json
# Read JSON data from the file
with open('.cache/aws_ssm_list_commands', 'r') as file:
data = json.load(file)
# Loop through each command
for command in data.get("Commands", []):
command_id = command.get("CommandId")
status = command.get("Status")
# Check if the status is 'Success'
if status == "Success":
# Fetch logs of the command (you may need to implement this function)
print(f"Fetching logs for Command ID: {command_id}")
# Example: fetch_logs(command_id)
# use aws ssm to fetch the logs

@ -0,0 +1,82 @@
# # 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
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 instance_id, state in instances:
if state == 'running':
print(f"Starting command for instance: {instance_id}")
# Send command to the instance
send_command_output = ssm_client.send_command(
DocumentName="AWS-RunShellScript",
Targets=[{"Key": "instanceids", "Values": [instance_id]}],
Parameters={'commands': ['sudo su - -c "tail /var/log/cloud-init-output.log"']}
)
# Get the command ID
command_id = parse_command_id(send_command_output)
# Check the command status every second for 4 seconds
for _ in range(4):
time.sleep(1)
command_status = ssm_client.list_command_invocations(CommandId=command_id, Details=True)
print(command_status)
if __name__ == "__main__":
main()

@ -1,43 +0,0 @@
rewrite in python boto3
mkdir -p .cache
cache(){
name=".cache/${1}"
value="${2}"
if [ ! -f $name ];
then
echo $value > $name
fi
}
aws_ssm_list_commands=$(aws ssm list-commands)
cache "aws_ssm_list_commands" "$aws_ssm_list_commands"
#command_status=$(echo "${aws_ssm_list_commands}" | jq '.Commands[]|[.CommandId,.Status]')
commands=$(echo "${aws_ssm_list_commands}" | jq '.Commands[]')
#run_ids=$(echo $command_status | jq .[0] -r)
run_ids=$(echo $command_status | jq -r)
echo RUNIDS:$run_ids;
while read -r instance_id state; do
if [[ $state == "Success" ]]; then
echo "Check $instance_id"
# "DocumentName": "AWS-RunShellScript",
# "Parameters": {
# "commands": [
# "sudo su - -c \"tail /var/log/cloud-init-output.log\""
# "Status": "Success",
# "CompletedCount": 1,
# "ErrorCount": 0,
# "DeliveryTimedOutCount": 0,
fi
done <<< "$run_ids"
It seems you might be looking for a complete Python script that replicates the functionality of your provided shell script using Boto3. Heres an example:
```python
```
This Python script uses Boto3 to list SSM commands, caches them in a file, and checks the status of each command. Adjust the logic as needed to fit your specific processing requirements.

@ -1,22 +0,0 @@
# #read this file one command at a time and loop over it in python
# # read out the attributes of each object into variables in the loop
# # tail .cache/aws_ssm_list_commands
# # {"CommandId":"820dcf47-e8d7-4c23-8e8a-bc64de2883ff","DocumentName":"AWS-RunShellScript","DocumentVersion":"$DEFAULT","Comment":"","ExpiresAfter":1734111684.683,"Parameters":{"commands":["sudo su - -c \"tail /var/log/cloud-init-output.log\""]},"InstanceIds":[],"Targets":[{"Key":"instanceids","Values":["i-073378237c5a9dda1"]}],"RequestedDateTime":1734104484.683,"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":[]}
# # read this result
# commands=$(jq -e ".Commands[]" -c .cache/aws_ssm_list_commands )
# for each command {
# id=getid
# status=get status
# if status == successs:
# fetch logs of commnd id
# }
rewrite in python
Here's a Python script that reads the JSON data from the specified file, extracts the attributes of each command, and processes them in a loop:
```python
```
Make sure to implement the `fetch_logs(command_id)` function according to your requirements.

@ -1,63 +0,0 @@
Starting session for instance: i-073378237c5a9dda1
+ 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\""]'
{
"Command": {
"CommandId": "820dcf47-e8d7-4c23-8e8a-bc64de2883ff",
"DocumentName": "AWS-RunShellScript",
"DocumentVersion": "$DEFAULT",
"Comment": "",
"ExpiresAfter": 1734111684.683,
"Parameters": {
"commands": [
"sudo su - -c \"tail /var/log/cloud-init-output.log\""
]
},
"InstanceIds": [],
"Targets": [
{
"Key": "instanceids",
"Values": [
"i-073378237c5a9dda1"
]
}
],
"RequestedDateTime": 1734104484.683,
"Status": "Pending",
"StatusDetails": "Pending",
"OutputS3Region": "us-east-1",
"OutputS3BucketName": "",
"OutputS3KeyPrefix": "",
"MaxConcurrency": "50",
"MaxErrors": "0",
"TargetCount": 0,
"CompletedCount": 0,
"ErrorCount": 0,
"DeliveryTimedOutCount": 0,
"ServiceRole": "",
"NotificationConfig": {
"NotificationArn": "",
"NotificationEvents": [],
"NotificationType": ""
},
"CloudWatchOutputConfig": {
"CloudWatchLogGroupName": "",
"CloudWatchOutputEnabled": false
},
"TimeoutSeconds": 3600,
"AlarmConfiguration": {
"IgnorePollAlarmFailure": false,
"Alarms": []
},
"TriggeredAlarms": []
}
}
read this and check on the results now pls
To check the results of the command you executed using AWS SSM, you can use the following command:
```bash
aws ssm list-command-invocations --command-id 820dcf47-e8d7-4c23-8e8a-bc64de2883ff --details
```
This command will provide the status and output of the command executed on your instance. Look for the `Status` and `StandardOutputContent` fields in the response to determine the result of your command.

@ -1,57 +0,0 @@
send_command_output='{
"Command": {
"CommandId": "c019b2a0-0884-45df-af55-da8bb3d18dc5",
"DocumentName": "AWS-RunShellScript",
"DocumentVersion": "$DEFAULT",
"Comment": "",
"ExpiresAfter": 1734112021.438,
"Parameters": {
"commands": [
"sudo su - -c \"tail /var/log/cloud-init-output.log\""
]
},
"InstanceIds": [],
"Targets": [
{
"Key": "instanceids",
"Values": [
"i-073378237c5a9dda1"
]
}
],
"RequestedDateTime": 1734104821.438,
"Status": "Pending",
"StatusDetails": "Pending",
"OutputS3Region": "us-east-1",
"OutputS3BucketName": "",
"OutputS3KeyPrefix": "",
"MaxConcurrency": "50",
"MaxErrors": "0",
"TargetCount": 0,
"CompletedCount": 0,
"ErrorCount": 0,
"DeliveryTimedOutCount": 0,
"ServiceRole": "",
"NotificationConfig": {
"NotificationArn": "",
"NotificationEvents": [],
"NotificationType": ""
},
"CloudWatchOutputConfig": {
"CloudWatchLogGroupName": "",
"CloudWatchOutputEnabled": false
},
"TimeoutSeconds": 3600,
"AlarmConfiguration": {
"IgnorePollAlarmFailure": false,
"Alarms": []
},
"TriggeredAlarms": []
}
}'
#++ parse_command_id send_command_output
#++ local send_command_output=send_command_output
echo $send_command_output | jq -r '.Command.CommandId'

@ -0,0 +1,7 @@
#!/bin/bash
instance_ids=$(aws ec2 describe-instances --query "Reservations[*].Instances[*].InstanceId" --output text)
for instance_id in $instance_ids; do
echo "Starting session for instance: $instance_id"
aws ec2 terminate-instances --instance-ids "$instance_id"
done
Loading…
Cancel
Save