From 7c9772100e6ef0d92f44d8566dcde226a1c9ee2c Mon Sep 17 00:00:00 2001 From: Jack Naglieri Date: Tue, 12 Dec 2017 10:03:20 -0800 Subject: [PATCH] [cli] migrate versioning into manage_lambda, support version publish by cluster --- stream_alert_cli/manage_lambda/deploy.py | 15 +++++++++------ .../{ => manage_lambda}/version.py | 19 +++++++++++++------ tests/unit/stream_alert_cli/test_version.py | 8 ++------ 3 files changed, 24 insertions(+), 18 deletions(-) rename stream_alert_cli/{ => manage_lambda}/version.py (94%) diff --git a/stream_alert_cli/manage_lambda/deploy.py b/stream_alert_cli/manage_lambda/deploy.py index a8cd39135..c0acca42c 100644 --- a/stream_alert_cli/manage_lambda/deploy.py +++ b/stream_alert_cli/manage_lambda/deploy.py @@ -18,18 +18,20 @@ from stream_alert_cli import helpers from stream_alert_cli.manage_lambda import package as stream_alert_packages +from stream_alert_cli.manage_lambda.version import LambdaVersion from stream_alert_cli.terraform.generate import terraform_generate -from stream_alert_cli.version import LambdaVersion PackageMap = namedtuple('package_attrs', ['package_class', 'targets', 'enabled']) -def _publish_version(packages, config): +def _publish_version(packages, config, clusters): """Publish production Lambda versions Args: - packages (list of LambdaPackage classes) + packages (list[LambdaPackage]) + config (CLIConfig) + clusters (set) Returns: bool: Result of Lambda version publishing @@ -39,10 +41,11 @@ def _publish_version(packages, config): for package in packages: if package.package_name in global_packages: published = LambdaVersion( - config=config, package=package, clustered_deploy=False).publish_function() + config=config, package=package).publish_function(clustered_deploy=False) else: published = LambdaVersion( - config=config, package=package).publish_function() + config=config, package=package).publish_function(clustered_deploy=True, + clusters=clusters) if not published: return False @@ -135,7 +138,7 @@ def deploy(options, config): sys.exit(1) # Publish a new production Lambda version - if not _publish_version(packages, config): + if not _publish_version(packages, config, options.clusters): return # Regenerate the Terraform configuration with the new Lambda versions diff --git a/stream_alert_cli/version.py b/stream_alert_cli/manage_lambda/version.py similarity index 94% rename from stream_alert_cli/version.py rename to stream_alert_cli/manage_lambda/version.py index d1c423374..b851a989b 100644 --- a/stream_alert_cli/version.py +++ b/stream_alert_cli/manage_lambda/version.py @@ -38,11 +38,9 @@ def __init__(self, **kwargs): Keyword Args: config (CLIConfig): Loaded StreamAlert CLI Config package (LambdaPackage): The created Lambda Package - clustered_deploy (bool): Identifies cluster based Lambdas """ self.config = kwargs['config'] self.package = kwargs['package'] - self.clustered_deploy = kwargs.get('clustered_deploy', True) @staticmethod def _version_helper(**kwargs): @@ -140,6 +138,7 @@ def _publish_helper(self, **kwargs): LOGGER_CLI.info('Published version %s for %s', new_version, function_name) self.config['lambda'][self.package.config_key]['current_version'] = new_version + self.config.write() return True @@ -156,10 +155,18 @@ def _publish(self, client, function_name, code_sha_256): return new_version - def publish_function(self): - """Main Publish Function method""" - if self.clustered_deploy: - for cluster in self.config.clusters(): + def publish_function(self, **kwargs): + """Main Publish Function method + + Keyword Args: + clustered_deploy (bool): Identifies cluster based Lambdas + clusters (list): The list of clusters to deploy to + """ + clustered_deploy = kwargs.get('clustered_deploy', True) + clusters = kwargs.get('clusters', []) or self.config.clusters() + + if clustered_deploy: + for cluster in clusters: if not self._publish_helper(cluster=cluster): return False else: diff --git a/tests/unit/stream_alert_cli/test_version.py b/tests/unit/stream_alert_cli/test_version.py index 2ff4e3da3..d791a7dc9 100644 --- a/tests/unit/stream_alert_cli/test_version.py +++ b/tests/unit/stream_alert_cli/test_version.py @@ -18,7 +18,7 @@ from nose.tools import assert_equal, assert_true, assert_false, nottest from stream_alert_cli.manage_lambda.package import AthenaPackage, RuleProcessorPackage -from stream_alert_cli.version import LambdaVersion +from stream_alert_cli.manage_lambda.version import LambdaVersion from tests.unit.helpers.aws_mocks import MockLambdaClient from tests.unit.helpers.base import basic_streamalert_config, MockCLIConfig @@ -37,7 +37,6 @@ def test_publish_helper_clustered(): package = RuleProcessorPackage(config=config) publish = LambdaVersion( config=config, - clustered_deploy=True, package=package ) result = publish._publish_helper(cluster='prod') @@ -56,7 +55,6 @@ def test_publish_helper(): package = AthenaPackage(config=config) publish = LambdaVersion( config=config, - clustered_deploy=False, package=package ) result = publish._publish_helper() @@ -70,7 +68,6 @@ def test_version_helper(): package = AthenaPackage(basic_streamalert_config()) publish = LambdaVersion( config=basic_streamalert_config(), - clustered_deploy=False, package=package ) current_version = 10 @@ -85,13 +82,12 @@ def test_version_helper(): assert_equal(result, current_version + 1) -@patch('stream_alert_cli.version.LOGGER_CLI') +@patch('stream_alert_cli.manage_lambda.version.LOGGER_CLI') def test_version_helper_error(mock_logging): """CLI - Publish Helper Raises Error""" package = AthenaPackage(basic_streamalert_config()) publish = LambdaVersion( config=basic_streamalert_config(), - clustered_deploy=False, package=package ) current_version = 10