diff options
| author | ckonstanski <carlos.konstanski@olo.com> | 2025-12-29 15:31:22 -0700 |
|---|---|---|
| committer | ckonstanski <carlos.konstanski@olo.com> | 2025-12-29 15:31:22 -0700 |
| commit | 4dfcd1d12b427e5c8e195b6932f9c9b4e647f808 (patch) | |
| tree | e0504562982d504f83bff8616818c99ada8394f7 /lisp/aws/aws.lisp | |
| parent | 85d525bb728953d02a22bfdfd7bb0bb53504b3fa (diff) | |
[INF-18477] Look up all ASG instances at once
Diffstat (limited to 'lisp/aws/aws.lisp')
| -rw-r--r-- | lisp/aws/aws.lisp | 48 |
1 files changed, 34 insertions, 14 deletions
diff --git a/lisp/aws/aws.lisp b/lisp/aws/aws.lisp index 642c7d9..37a02af 100644 --- a/lisp/aws/aws.lisp +++ b/lisp/aws/aws.lisp @@ -52,6 +52,9 @@ (age :initarg :age :initform nil :accessor age) + (instance-ids :initarg :instance-ids + :initform nil + :accessor instance-ids) (instances :initarg :instances :initform nil :accessor instances) @@ -175,7 +178,13 @@ (defmethod get-asgs ((aws aws) filter) (let* ((command "aws autoscaling describe-auto-scaling-groups") - (asgs (loop for asg in (cdar (run-aws-cli aws command)) + (data (cdar (run-aws-cli aws command))) + (instance-ids (org-ckons-core::flatten (loop for asg in data + when (org-ckons-core::match-it filter (cdr (assoc :*auto-scaling-group-name asg))) + collect (loop for instance in (cdr (assoc :*instances asg)) + collect (cdr (assoc :*instance-id instance)))))) + (instances (get-instances aws instance-ids)) + (asgs (loop for asg in data when (org-ckons-core::match-it filter (cdr (assoc :*auto-scaling-group-name asg))) collect (make-instance 'aws-asg :profile (profile aws) @@ -185,26 +194,37 @@ :count-min (cdr (assoc :*min-size asg)) :count-max (cdr (assoc :*max-size asg)) :count-desired (cdr (assoc :*desired-capacity asg)) - :instances (sort (loop for instance in (cdr (assoc :*instances asg)) - collect (get-instance aws (cdr (assoc :*instance-id instance)))) - (lambda (x y) - (string< (launch-time x) (launch-time y)))))))) + :instance-ids (org-ckons-core::flatten (loop for instance in (cdr (assoc :*instances asg)) + collect (cdr (assoc :*instance-id instance)))))))) + (loop for instance in instances + do (loop for asg in asgs + when (intersection `(,(id instance)) (instance-ids asg) :test 'string=) + do (push instance (instances asg)))) (loop for asg in asgs do (handler-case - (setf (age asg) (/ (- (get-universal-time) (local-time:timestamp-to-universal (local-time:parse-timestring (launch-time (first (instances asg)))))) 86400.0)) + (progn + (setf (instances asg) (sort (instances asg) + (lambda (x y) + (string< (launch-time x) (launch-time y))))) + (setf (age asg) (/ (- (get-universal-time) + (local-time:timestamp-to-universal (local-time:parse-timestring (launch-time (first (instances asg)))))) + 86400.0))) (error (e) (declare (ignore e)) (setf (age asg) -1)))) (sort asgs (lambda (x y) (> (age x) (age y)))))) -(defmethod get-instance ((aws aws) instance-id) - (let ((command (format nil "aws ec2 describe-instances --instance-ids ~a" instance-id))) - (car (loop for instance in (cdr (assoc :*instances (cadar (run-aws-cli aws command)))) - collect (make-instance 'aws-instance - :id (cdr (assoc :*instance-id instance)) - :ip (first (loop for interface in (cdr (assoc :*network-interfaces instance)) - collect (cdr (assoc :*private-ip-address interface)))) - :launch-time (cdr (assoc :*launch-time instance))))))) +(defmethod get-instances ((aws aws) instance-ids) + (let ((command (format nil + "aws ec2 describe-instances --instance-ids ~a" + (org-ckons-core::reduce-to-char-separated-string instance-ids " ")))) + (loop for instance in (loop for reservation in (cdr (assoc :*reservations (run-aws-cli aws command))) + collect (car (cdr (assoc :*instances reservation)))) + collect (make-instance 'aws-instance + :id (cdr (assoc :*instance-id instance)) + :ip (first (loop for interface in (cdr (assoc :*network-interfaces instance)) + collect (cdr (assoc :*private-ip-address interface)))) + :launch-time (cdr (assoc :*launch-time instance)))))) (defmethod get-targetgroups ((aws-targetgroup aws-targetgroup)) (let ((command "aws elbv2 describe-target-groups")) |
