# print password and addresses for all seats and instances
jq '.seats[] | {password, instances}' < instances.json

# output key of last seat in a file to "key"
jq -r '.seats[-1]|.key' < instances.json > key

# output list of all subnet Name tags
# there's 1 subnet per course, so this is a good proxy to get a list of existing course IDs
aws ec2 describe-subnets --filters Name=tag:CourseId,Values='my-class-*' | jq -r '.Subnets[] | .Tags[] | select(.Key=="CourseId") | .Value'

# all running ec2 instances
aws ec2 describe-instances --filters Name=instance-state-name,Values=running

# upgrade TiKV volumes for better performance
aws ec2 describe-instances --filter Name=private-ip-address,Values="$(ssh ubuntu@"$(tail +28 student-0.pem | jq -r '.instances[0].public')" -- .tiup/bin/tiup cluster display --format=json -R tikv upstream | jq -r '.instances| map(.host) | join(",")')" | jq -r '.Reservations[] | .Instances[] | .BlockDeviceMappings[0] | .Ebs.VolumeId' | while read -r v; do aws ec2 modify-volume --volume-id="$v" --volume-type=gp3 --size=256 --iops=16000 --throughput=1000; done

# apply IAM policy/profile to all instances
tail +28 student-0.pem | jq -r '.instances[] | .id' | while read -r inst; do aws ec2 associate-iam-instance-profile --instance-id="$inst" --iam-instance-profile Arn=arn:aws:iam::577523860935:instance-profile/write-s3-kolbe-test-1627426353-ec2; done

# print "host" yaml stubs for each instance
tail +28 student-0.pem | jq -r '.instances[] | " - host: \(.private)"'

# connect to first instance
ssh ubuntu@"$(tail +28 student-0.pem | jq -r '.instances[0].public')"
