-
Notifications
You must be signed in to change notification settings - Fork 241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added a script to drain and kill a marathon app as gracefully as possible #143
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
94e8b0f
added graceful_app_drain script
mjksmith 7b288fa
cleaned graceful_app_drain up a little
mjksmith 292d3c1
edited graceful_app_drain to be PEP8 compliant
mjksmith 22fa606
updated graceful_app_drain to work with the /appid format
mjksmith 7d0c440
added a description to graceful_app_drain that states its intended use
mjksmith 11b7957
removed zookeeper lock from graceful_app_drain
mjksmith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#!/usr/bin/env python | ||
|
||
import argparse | ||
import sys | ||
import time | ||
|
||
import service_configuration_lib | ||
from paasta_tools import marathon_tools | ||
from paasta_tools import drain_lib | ||
from paasta_tools import bounce_lib | ||
from paasta_tools.utils import decompose_job_id | ||
from paasta_tools.setup_marathon_job import do_bounce, get_old_live_draining_tasks | ||
from paasta_tools.utils import load_system_paasta_config | ||
|
||
|
||
def parse_args(): | ||
parser = argparse.ArgumentParser( | ||
description="""This script attempts to gracefully drain and kill a marathon app. | ||
It is intended for use in emergencies when the regular bounce script can't proceed, | ||
and needs to kill a specific app to get going.""", | ||
) | ||
parser.add_argument( | ||
'appname', | ||
help="the app that will be drained", | ||
) | ||
parser.add_argument( | ||
'-d', '--soa-dir', | ||
dest="soa_dir", | ||
metavar="SOA_DIR", | ||
default=service_configuration_lib.DEFAULT_SOA_DIR, | ||
help="define a different soa config directory", | ||
) | ||
return parser.parse_args() | ||
|
||
|
||
def main(): | ||
args = parse_args() | ||
full_appid = args.appname.lstrip('/') | ||
soa_dir = args.soa_dir | ||
marathon_config = marathon_tools.load_marathon_config() | ||
client = marathon_tools.get_marathon_client( | ||
url=marathon_config.get_url(), | ||
user=marathon_config.get_username(), | ||
passwd=marathon_config.get_password(), | ||
) | ||
|
||
if not marathon_tools.is_app_id_running(app_id=full_appid, client=client): | ||
print("Couldn't find an app named {0}".format(full_appid)) | ||
sys.exit(1) | ||
|
||
service, instance, _, __ = (s.replace('--', '_') for s in decompose_job_id(full_appid)) | ||
complete_config = marathon_tools.create_complete_config(service, instance, marathon_config) | ||
cluster = load_system_paasta_config().get_cluster() | ||
service_instance_config = marathon_tools.load_marathon_service_config( | ||
service=service, | ||
instance=instance, | ||
cluster=cluster, | ||
soa_dir=soa_dir, | ||
) | ||
nerve_ns = service_instance_config.get_nerve_namespace() | ||
service_namespace_config = marathon_tools.load_service_namespace_config(service=service, namespace=nerve_ns) | ||
drain_method = drain_lib.get_drain_method( | ||
service_instance_config.get_drain_method(service_namespace_config), | ||
service=service, | ||
instance=instance, | ||
nerve_ns=nerve_ns, | ||
drain_method_params=service_instance_config.get_drain_method_params(service_namespace_config), | ||
) | ||
|
||
bounce_func = bounce_lib.get_bounce_method_func('down') | ||
|
||
while marathon_tools.is_app_id_running(app_id=full_appid, client=client): | ||
app_to_kill = client.get_app(full_appid) | ||
old_app_live_tasks, old_app_draining_tasks = get_old_live_draining_tasks([app_to_kill], drain_method) | ||
do_bounce( | ||
bounce_func=bounce_func, | ||
drain_method=drain_method, | ||
config=complete_config, | ||
new_app_running='', | ||
happy_new_tasks=[], | ||
old_app_live_tasks=old_app_live_tasks, | ||
old_app_draining_tasks=old_app_draining_tasks, | ||
serviceinstance="{0}.{1}".format(service, instance), | ||
bounce_method='down', | ||
service=service, | ||
cluster=cluster, | ||
instance=instance, | ||
marathon_jobid=full_appid, | ||
client=client, | ||
soa_dir=soa_dir, | ||
) | ||
|
||
print "Sleeping for 10 seconds to give the tasks time to drain" | ||
time.sleep(10) | ||
|
||
print("Sucessfully killed {0}".format(full_appid)) | ||
|
||
if __name__ == '__main__': | ||
main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like it would iterate really fast, can you make it a bit more relaxed and iterate every 10s like the normal bounce? (but print that it is sleeping to let the user know why it "isn't doing anything")