Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
configuration
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
configuration
Commits
ad624b5b
Commit
ad624b5b
authored
Aug 21, 2014
by
e0d
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cr comments, improvements
parent
edc5cdeb
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
54 additions
and
28 deletions
+54
-28
util/vpc-tools/asg_lifcycle_watcher.py
+54
-28
No files found.
util/vpc-tools/asg_lifcycle_watcher.py
View file @
ad624b5b
...
@@ -25,27 +25,33 @@ import logging
...
@@ -25,27 +25,33 @@ import logging
class
LifecycleHandler
:
class
LifecycleHandler
:
profile
=
None
INSTANCE_TERMINATION
=
'autoscaling:EC2_INSTANCE_TERMINATING'
queue
=
None
TEST_NOTIFICATION
=
'autoscaling:TEST_NOTIFICATION'
bin
=
None
NUM_MESSAGES
=
10
WAIT_TIME_SECONDS
=
10
def
__init__
(
self
,
profile
,
queue
,
bin
):
def
__init__
(
self
,
profile
,
queue
,
bin
,
dry_run
):
logging
.
basicConfig
(
level
=
logging
.
INFO
)
logging
.
basicConfig
(
level
=
logging
.
INFO
)
self
.
profile
=
profile
self
.
profile
=
profile
self
.
queue
=
queue
self
.
queue
=
queue
self
.
bin
=
bin
self
.
bin
=
bin
self
.
dry_run
=
dry_run
self
.
ec2
=
boto
.
connect_ec2
(
profile_name
=
self
.
profile
)
def
process_lifecycle_messages
(
self
):
def
process_lifecycle_messages
(
self
):
sqs_con
=
boto
.
connect_sqs
()
sqs_con
=
boto
.
connect_sqs
()
q
=
sqs_con
.
get_queue
(
self
.
queue
)
queue
=
sqs_con
.
get_queue
(
self
.
queue
)
q
.
set_message_class
(
RawMessage
)
for
sqs_message
in
q
.
get_messages
(
10
,
wait_time_seconds
=
10
):
# Needed to get unencoded message for ease of processing
queue
.
set_message_class
(
RawMessage
)
for
sqs_message
in
queue
.
get_messages
(
self
.
NUM_MESSAGES
,
wait_time_seconds
=
self
.
WAIT_TIME_SECONDS
):
body
=
json
.
loads
(
sqs_message
.
get_body_encoded
())
body
=
json
.
loads
(
sqs_message
.
get_body_encoded
())
as_message
=
json
.
loads
(
body
[
'Message'
])
as_message
=
json
.
loads
(
body
[
'Message'
])
logging
.
info
(
"Proccessing message {message}."
.
format
(
message
=
as_message
))
logging
.
info
(
"Proccessing message {message}."
.
format
(
message
=
as_message
))
if
'LifecycleTransition'
in
as_message
and
as_message
[
'LifecycleTransition'
]
==
'autoscaling:EC2_INSTANCE_TERMINATING'
:
if
'LifecycleTransition'
in
as_message
and
as_message
[
'LifecycleTransition'
]
==
self
.
INSTANCE_TERMINATION
:
# Convenience vars, set here to avoid messages that don't meet the criteria in
# Convenience vars, set here to avoid messages that don't meet the criteria in
# the if condition above.
# the if condition above.
instance_id
=
as_message
[
'EC2InstanceId'
]
instance_id
=
as_message
[
'EC2InstanceId'
]
...
@@ -58,13 +64,25 @@ class LifecycleHandler:
...
@@ -58,13 +64,25 @@ class LifecycleHandler:
instance
=
instance_id
))
instance
=
instance_id
))
self
.
continue_lifecycle
(
asg
,
token
)
self
.
continue_lifecycle
(
asg
,
token
)
sqs_con
.
delete_message
(
q
,
sqs_message
)
if
not
self
.
dry_run
:
logging
.
info
(
"Deleting message with body {message}"
.
format
(
message
=
as_message
))
sqs_con
.
delete_message
(
queue
,
sqs_message
)
else
:
logging
.
info
(
"Would have deleted message with body {message}"
.
format
(
message
=
as_message
))
else
:
else
:
logging
.
info
(
"Recording lifecycle heartbeat for instance {instance}"
.
format
(
logging
.
info
(
"Recording lifecycle heartbeat for instance {instance}"
.
format
(
instance
=
instance_id
))
instance
=
instance_id
))
self
.
record_lifecycle_action_heartbeat
(
asg
,
token
)
self
.
record_lifecycle_action_heartbeat
(
asg
,
token
)
elif
as_message
[
'Event'
]
==
self
.
TEST_NOTIFICATION
:
if
not
self
.
dry_run
:
logging
.
info
(
"Deleting message with body {message}"
.
format
(
message
=
as_message
))
sqs_con
.
delete_message
(
queue
,
sqs_message
)
else
:
logging
.
info
(
"Would have deleted message with body {message}"
.
format
(
message
=
as_message
))
def
record_lifecycle_action_heartbeat
(
self
,
asg
,
token
):
def
record_lifecycle_action_heartbeat
(
self
,
asg
,
token
):
...
@@ -76,7 +94,7 @@ class LifecycleHandler:
...
@@ -76,7 +94,7 @@ class LifecycleHandler:
"--lifecycle-action-token {token}"
.
format
(
"--lifecycle-action-token {token}"
.
format
(
path
=
self
.
bin
,
asg
=
asg
,
token
=
token
)
path
=
self
.
bin
,
asg
=
asg
,
token
=
token
)
self
.
run_subprocess_command
(
command
)
self
.
run_subprocess_command
(
command
,
self
.
dry_run
)
def
continue_lifecycle
(
self
,
asg
,
token
):
def
continue_lifecycle
(
self
,
asg
,
token
):
command
=
"{path}/python "
\
command
=
"{path}/python "
\
...
@@ -85,29 +103,31 @@ class LifecycleHandler:
...
@@ -85,29 +103,31 @@ class LifecycleHandler:
"CONTINUE"
.
format
(
"CONTINUE"
.
format
(
path
=
self
.
bin
,
asg
=
asg
,
token
=
token
)
path
=
self
.
bin
,
asg
=
asg
,
token
=
token
)
self
.
run_subprocess_command
(
command
)
self
.
run_subprocess_command
(
command
,
self
.
dry_run
)
def
run_subprocess_command
(
self
,
command
,
dry_run
):
def
run_subprocess_command
(
self
,
command
):
logging
.
info
(
"Running command {command}."
.
format
(
command
=
command
))
logging
.
info
(
"Running command {command}."
.
format
(
command
=
command
))
try
:
if
not
dry_run
:
output
=
subprocess
.
check_output
(
command
.
split
(
' '
))
try
:
logging
.
info
(
"Output was {output}"
.
format
(
output
=
output
))
output
=
subprocess
.
check_output
(
command
.
split
(
' '
))
except
Exception
,
e
:
logging
.
info
(
"Output was {output}"
.
format
(
output
=
output
))
logging
.
exception
(
e
)
except
Exception
as
e
:
logging
.
error
(
output
)
logging
.
exception
(
e
)
if
output
:
logging
.
error
(
output
)
raise
e
def
get_ec2_instance_by_id
(
self
,
id
):
def
get_ec2_instance_by_id
(
self
,
id
):
"""
"""
Simple boto call to get the instance based on the instance-id
Simple boto call to get the instance based on the instance-id
"""
"""
ec2
=
boto
.
connect_ec2
(
profile_name
=
self
.
profile
)
instances
=
self
.
ec2
.
get_only_instances
([
id
])
instances
=
ec2
.
get_only_instances
([
id
])
if
len
(
instances
)
==
1
:
if
len
(
instances
)
==
1
:
return
ec2
.
get_only_instances
([
id
])[
0
]
return
self
.
ec2
.
get_only_instances
([
id
])[
0
]
else
:
else
:
return
None
return
None
...
@@ -120,13 +140,16 @@ class LifecycleHandler:
...
@@ -120,13 +140,16 @@ class LifecycleHandler:
instance
=
self
.
get_ec2_instance_by_id
(
id
)
instance
=
self
.
get_ec2_instance_by_id
(
id
)
if
instance
:
if
instance
:
if
'safe_to_retire'
in
instance
.
tags
and
instance
.
tags
[
'safe_to_retire'
]
.
lower
()
==
'true'
:
if
'ok_to_retire'
in
instance
.
tags
and
instance
.
tags
[
'ok_to_retire'
]
.
lower
()
==
'true'
:
logging
.
info
(
"Instance with id {id} is safe to retire."
.
format
(
id
=
id
))
return
True
return
True
else
:
return
False
logging
.
info
(
"Instance with id {id} is not safe to retire."
.
format
(
id
=
id
))
return
False
else
:
else
:
# No instance for id in SQS message.
# No instance for id in SQS message this can happen if something else
# has terminated the instances outside of this workflow
logging
.
warn
(
"Instance with id {id} is referenced in an SQS message, but does not exist."
)
return
True
return
True
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
...
@@ -140,7 +163,10 @@ if __name__=="__main__":
...
@@ -140,7 +163,10 @@ if __name__=="__main__":
parser
.
add_argument
(
'-q'
,
'--queue'
,
required
=
True
,
parser
.
add_argument
(
'-q'
,
'--queue'
,
required
=
True
,
help
=
"The SQS queue containing the lifecyle messages"
)
help
=
"The SQS queue containing the lifecyle messages"
)
parser
.
add_argument
(
'-d'
,
"--dry-run"
,
dest
=
"dry_run"
,
action
=
"store_true"
,
help
=
'Print the commands, but do not do anything'
)
parser
.
set_defaults
(
dry_run
=
False
)
args
=
parser
.
parse_args
()
args
=
parser
.
parse_args
()
lh
=
LifecycleHandler
(
args
.
profile
,
args
.
queue
,
args
.
bin
)
lh
=
LifecycleHandler
(
args
.
profile
,
args
.
queue
,
args
.
bin
,
args
.
dry_run
)
lh
.
process_lifecycle_messages
()
lh
.
process_lifecycle_messages
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment