Skip to content

Commit

Permalink
Sort Out Unordered Threaded Output (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
kolanos authored May 15, 2020
1 parent e4815b3 commit 4790412
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 102 deletions.
47 changes: 18 additions & 29 deletions newrelic_lambda_cli/cli/layers.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# -*- coding: utf-8 -*-

from concurrent.futures import as_completed, ThreadPoolExecutor
import json

import boto3
import click

from newrelic_lambda_cli import layers, permissions
from newrelic_lambda_cli.cliutils import done, failure, success
from newrelic_lambda_cli.cliutils import done, failure
from newrelic_lambda_cli.cli.decorators import add_options, AWS_OPTIONS
from newrelic_lambda_cli.functions import get_aliased_functions

Expand Down Expand Up @@ -84,23 +83,20 @@ def install(

functions = get_aliased_functions(session, functions, excludes)

install_success = True
futures = []

with ThreadPoolExecutor() as executor:
for function in functions:
futures.append(
executor.submit(
layers.install, session, function, layer_arn, nr_account_id, upgrade
)
futures = [
executor.submit(
layers.install,
session,
function,
layer_arn,
nr_account_id,
upgrade,
ctx.obj["VERBOSE"],
)
for future in as_completed(futures):
res = future.result()
install_success = res and install_success
if res:
success("Successfully installed layer on %s" % function)
if ctx.obj["VERBOSE"]:
click.echo(json.dumps(res, indent=2))
for function in functions
]
install_success = all(future.result() for future in as_completed(futures))

if install_success:
done("Install Complete")
Expand Down Expand Up @@ -137,19 +133,12 @@ def uninstall(ctx, aws_profile, aws_region, aws_permissions_check, functions, ex

functions = get_aliased_functions(session, functions, excludes)

uninstall_success = True
futures = []

with ThreadPoolExecutor() as executor:
for function in functions:
futures.append(executor.submit(layers.uninstall, session, function))
for future in as_completed(futures):
res = future.result()
uninstall_success = res and uninstall_success
if res:
success("Successfully uninstalled layer on %s" % function)
if ctx.obj["VERBOSE"]:
click.echo(json.dumps(res, indent=2))
futures = [
executor.submit(layers.uninstall, session, function, ctx.obj["VERBOSE"])
for function in functions
]
uninstall_success = all(future.result() for future in as_completed(futures))

if uninstall_success:
done("Uninstall Complete")
Expand Down
43 changes: 12 additions & 31 deletions newrelic_lambda_cli/cli/subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import click

from newrelic_lambda_cli import permissions, subscriptions
from newrelic_lambda_cli.cliutils import done, failure, success
from newrelic_lambda_cli.cliutils import done, failure
from newrelic_lambda_cli.cli.decorators import add_options, AWS_OPTIONS
from newrelic_lambda_cli.functions import get_aliased_functions

Expand Down Expand Up @@ -60,24 +60,14 @@ def install(

functions = get_aliased_functions(session, functions, excludes)

install_success = True
futures = []

with ThreadPoolExecutor() as executor:
for function in functions:
futures.append(
executor.submit(
subscriptions.create_log_subscription,
session,
function,
filter_pattern,
)
futures = [
executor.submit(
subscriptions.create_log_subscription, session, function, filter_pattern
)
for future in as_completed(futures):
result = future.result()
install_success = result and install_success
if result:
success("Successfully installed log subscription on %s" % function)
for function in functions
]
install_success = all(future.result() for future in as_completed(futures))

if install_success:
done("Install Complete")
Expand Down Expand Up @@ -113,21 +103,12 @@ def uninstall(aws_profile, aws_region, aws_permissions_check, functions, exclude

functions = get_aliased_functions(session, functions, excludes)

uninstall_success = True
futures = []

with ThreadPoolExecutor() as executor:
for function in functions:
futures.append(
executor.submit(
subscriptions.remove_log_subscription, session, function
)
)
for future in as_completed(futures):
result = future.result()
uninstall_success = result and uninstall_success
if result:
success("Successfully uninstalled log subscription on %s" % function)
futures = [
executor.submit(subscriptions.remove_log_subscription, session, function)
for function in functions
]
uninstall_success = all(future.result() for future in as_completed(futures))

if uninstall_success:
done("Uninstall Complete")
Expand Down
20 changes: 12 additions & 8 deletions newrelic_lambda_cli/cliutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,31 @@

def done(message):
"""Prints a done message to the terminal"""
click.echo(emoji.emojize(":sparkles: ", use_aliases=True), color="yellow", nl=False)
click.echo(message, nl=False)
click.echo(emoji.emojize(" :sparkles:", use_aliases=True), color="yellow")
click.echo(emoji.emojize(":sparkles: %s :sparkles:" % message, use_aliases=True))


def failure(message, exit=False):
"""Prints a failure message to the terminal"""
click.echo(
emoji.emojize(":heavy_multiplication_x: ", use_aliases=True),
emoji.emojize(":heavy_multiplication_x: %s" % message, use_aliases=True),
color="red",
err=True,
nl=False,
)
click.echo(message, err=True)
if exit:
raise Exit(1)


def success(message):
"""Prints a success message to the terminal"""
click.echo(
emoji.emojize(":heavy_check_mark: ", use_aliases=True), color="green", nl=False
emoji.emojize(":heavy_check_mark: %s" % message, use_aliases=True),
color="green",
)


def warning(message):
"""Prints a warningmessage to the terminal"""
click.echo(
emoji.emojize(":heavy_exclamation_mark: %s" % message, use_aliases=True),
color="blue",
)
click.echo(message)
43 changes: 28 additions & 15 deletions newrelic_lambda_cli/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import botocore
import click
import json
import requests

from newrelic_lambda_cli import utils
Expand All @@ -25,7 +26,7 @@ def _add_new_relic(config, region, layer_arn, account_id, allow_upgrade):
"Unsupported Lambda runtime for '%s': %s"
% (config["Configuration"]["FunctionArn"], runtime)
)
return
return False

handler = config["Configuration"]["Handler"]
runtime_handler = utils.RUNTIME_CONFIG.get(runtime, {}).get("Handler")
Expand All @@ -35,7 +36,7 @@ def _add_new_relic(config, region, layer_arn, account_id, allow_upgrade):
"upgrade or reinstall to latest layer version."
% config["Configuration"]["FunctionArn"]
)
return
return False

existing_layers = [
layer["Arn"]
Expand All @@ -55,7 +56,7 @@ def _add_new_relic(config, region, layer_arn, account_id, allow_upgrade):
"No Lambda layers published for '%s' runtime: %s"
% (config["Configuration"]["FunctionArn"], runtime)
)
return
return False

# TODO: MAke this a layer selection screen
if len(available_layers) > 1:
Expand Down Expand Up @@ -98,26 +99,32 @@ def _add_new_relic(config, region, layer_arn, account_id, allow_upgrade):
return update_kwargs


def install(session, function_arn, layer_arn, account_id, allow_upgrade):
def install(session, function_arn, layer_arn, account_id, allow_upgrade, verbose):
client = session.client("lambda")
config = get_function(session, function_arn)
if not config:
failure("Could not find function: %s" % function_arn)
return
return False

region = session.region_name

update_kwargs = _add_new_relic(config, region, layer_arn, account_id, allow_upgrade)
if not update_kwargs:
return
return False

try:
return client.update_function_configuration(**update_kwargs)
res = client.update_function_configuration(**update_kwargs)
except botocore.exceptions.ClientError as e:
failure(
"Failed to update configuration for '%s': %s"
% (config["Configuration"]["FunctionArn"], e)
)
return
return False
else:
if verbose:
click.echo(json.dumps(res, indent=2))
success("Successfully installed layer on %s" % function_arn)
return True


def _remove_new_relic(config, region):
Expand All @@ -127,7 +134,7 @@ def _remove_new_relic(config, region):
"Unsupported Lambda runtime for '%s': %s"
% (config["Configuration"]["FunctionArn"], runtime)
)
return
return False

handler = config["Configuration"]["Handler"]

Expand All @@ -138,7 +145,7 @@ def _remove_new_relic(config, region):
"function '%s'. Unrecognized handler in deployed function."
% config["Configuration"]["FunctionArn"]
)
return
return False

env_handler = (
config["Configuration"]
Expand All @@ -153,7 +160,7 @@ def _remove_new_relic(config, region):
"function '%s'. Environment variable NEW_RELIC_LAMBDA_HANDLER not found."
% config["Configuration"]["FunctionArn"]
)
return
return False

# Delete New Relic env vars
config["Configuration"]["Environment"]["Variables"] = {
Expand All @@ -180,24 +187,30 @@ def _remove_new_relic(config, region):
}


def uninstall(session, function_arn):
def uninstall(session, function_arn, verbose):
client = session.client("lambda")

config = get_function(session, function_arn)
if not config:
failure("Could not find function: %s" % function_arn)
return
return False

region = session.region_name

update_kwargs = _remove_new_relic(config, region)
if not update_kwargs:
return
return False

try:
return client.update_function_configuration(**update_kwargs)
res = client.update_function_configuration(**update_kwargs)
except botocore.exceptions.ClientError as e:
failure(
"Failed to update configuration for '%s': %s"
% (config["Configuration"]["FunctionArn"], e)
)
return False
else:
if verbose:
click.echo(json.dumps(res, indent=2))
success("Successfully uninstalled layer on %s" % function_arn)
return True
Loading

0 comments on commit 4790412

Please sign in to comment.