Skip to content

Commit

Permalink
Merge 3724c8a into 9b0a4a2
Browse files Browse the repository at this point in the history
  • Loading branch information
mayn committed May 1, 2018
2 parents 9b0a4a2 + 3724c8a commit 64ab193
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 33 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# top-most EditorConfig file
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
trim_trailing_whitespace = true

[*.py]
max_line_length = 119
indent_size = 4
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@
*htmlcov
.DS_Store
.localized
*.idea
.tox
.pytest_cache
9 changes: 5 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
language: python
python:
- "2.7"
- "2.6"
- "2.7"
- "3.6"

install:
- python setup.py install
- pip install coveralls
- pip install tox-travis

before_script:
- export PYTHONPATH=$PYTHONPATH:$PWD

script:
- coverage run --source License2Deploy setup.py test
- tox

after_success:
coveralls
- if [ "$TRAVIS_PYTHON_VERSION" == "2.7" ]; then coveralls; fi
18 changes: 9 additions & 9 deletions License2Deploy/rolling_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import argparse
from sys import exit, argv
from time import sleep, time
from AWSConn import AWSConn
from set_logging import SetLogging
from .AWSConn import AWSConn
from .set_logging import SetLogging
from retry.api import retry_call

class RollingDeploy(object):
Expand Down Expand Up @@ -94,7 +94,7 @@ def get_autoscale_group_name(self):
''' Search for project in autoscale groups and return autoscale group name '''
if self.stack_name:
return self.get_autoscaling_group_name_from_cloudformation()
return next((instance.name for instance in filter(lambda n: n.name, self.get_group_info()) if self.project in instance.name and self.env in instance.name), None)
return next((instance.name for instance in [n for n in self.get_group_info() if n.name] if self.project in instance.name and self.env in instance.name), None)

def get_autoscaling_group_name_from_cloudformation(self):
if not self.autoscaling_group:
Expand Down Expand Up @@ -133,7 +133,7 @@ def calculate_autoscale_desired_instance_count(self, group_name, desired_state):
except Exception as e:
logging.error("Please make sure the desired_state is set to either increase or decrease: {0}".format(e))
exit(self.exit_error_code)

def double_autoscale_instance_count(self, count):
''' Multiply current count by 2 '''
return count * 2
Expand Down Expand Up @@ -223,16 +223,16 @@ def wait_for_new_instances(self, instance_ids, retry=10, wait_time=30):
def lb_healthcheck(self, new_ids):
''' Confirm that the healthchecks report back OK in the LB. '''
instance_ids = self.conn_elb.describe_instance_health(self.load_balancer, new_ids)
status = filter(lambda instance: instance.state != "InService", instance_ids)
status = [instance for instance in instance_ids if instance.state != "InService"]
if status:
raise Exception('Must check load balancer again. Following instance(s) are not "InService": {0}'.format(status))
else:
logging.info('ELB healthcheck OK')
return True

def calculate_max_minutes(self, tries, delay):
return tries * delay / 60

def only_new_instances_check(self):
instance_ids = self.conn_elb.describe_instance_health(self.load_balancer)
for instance in instance_ids:
Expand All @@ -254,7 +254,7 @@ def tag_ami(self, ami_id, env):
''' Tagging AMI with DEPLOYED tag '''
try:
current_tag = self.conn_ec2.get_all_images(image_ids=ami_id)[0].tags.get('deployed')
if not current_tag:
if not current_tag:
logging.info("No DEPLOY tags exist, tagging with {0}".format(env))
self.conn_ec2.create_tags([self.ami_id], {"deployed": env})
elif env not in current_tag:
Expand Down Expand Up @@ -407,6 +407,6 @@ def main(): # pragma: no cover
SetLogging.setup_logging()
deployObj = RollingDeploy(args.env, args.project, args.build_number, args.ami_id, args.profile, args.config, args.stack_name, args.force_redeploy, None, args.creation_wait, args.ready_wait, args.health_wait, args.only_new_wait)
deployObj.deploy()

if __name__ == "__main__": # pragma: no cover
main()
13 changes: 13 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[coverage:run]
branch = true
source = License2Deploy

[coverage:report]
omit =
tests
.tox

[tool:pytest]
addopts = --verbose
testpaths =
tests
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
"PyYaml",
'placebo',
'boto3',
'retry'
'retry',
'pytest',
'coverage',
]

def read(fname):
Expand Down
1 change: 0 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
#
10 changes: 5 additions & 5 deletions tests/cloudformation_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ def setUp(self):
self.rolling_deploy = RollingDeploy('stg', 'server-gms-extender', '0', 'ami-abcd1234', None, './regions.yml', stack_name='test-stack-name', session=session)

def test_get_autoscaling_group_name_via_cloudformation(self):
self.assertEquals(self.rolling_deploy.autoscaling_group, False)
self.assertEqual(self.rolling_deploy.autoscaling_group, False)
asg_name = self.rolling_deploy.get_autoscale_group_name()
self.assertTrue(self.rolling_deploy.autoscaling_group)
self.assertEquals(asg_name, 'dnbi-backend-qa-dnbigmsextenderASGqa-1NP5ZBSVZRD0N')
self.assertEqual(asg_name, 'dnbi-backend-qa-dnbigmsextenderASGqa-1NP5ZBSVZRD0N')

def test_retrieve_project_cloudwatch_alarms(self):
self.assertEquals(self.rolling_deploy.stack_resources, False)
self.assertEquals(self.rolling_deploy.cloudwatch_alarms, False)
self.assertEqual(self.rolling_deploy.stack_resources, False)
self.assertEqual(self.rolling_deploy.cloudwatch_alarms, False)
cloudwatch_alarms = self.rolling_deploy.retrieve_project_cloudwatch_alarms()
self.assertTrue(self.rolling_deploy.stack_resources)
self.assertEquals(cloudwatch_alarms, ['dnbi-servergmsextender-SCALEDOWNALARMqa-123123', 'dnbi-servergmsextender-SCALEUPALARMqa-4asdhjks'])
self.assertEqual(cloudwatch_alarms, ['dnbi-servergmsextender-SCALEDOWNALARMqa-123123', 'dnbi-servergmsextender-SCALEUPALARMqa-4asdhjks'])

20 changes: 7 additions & 13 deletions tests/rolling_deploy_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python

import sys
import pytest
import unittest
import boto
from boto.ec2.autoscale.launchconfig import LaunchConfiguration
Expand All @@ -10,7 +11,6 @@
from moto import mock_ec2_deprecated
from moto import mock_elb_deprecated
from moto.cloudwatch import mock_cloudwatch_deprecated
from nose.tools import raises

from License2Deploy.rolling_deploy import RollingDeploy
from License2Deploy.AWSConn import AWSConn
Expand Down Expand Up @@ -137,7 +137,7 @@ def test_retrieve_project_cloudwatch_alarms(self):
instance_ids = self.setUpEC2()
self.setUpCloudWatch(instance_ids)
cloud_watch_alarms = self.rolling_deploy.retrieve_project_cloudwatch_alarms()
print cloud_watch_alarms
print(cloud_watch_alarms)
self.assertEqual(1, len(cloud_watch_alarms))

@mock_cloudwatch_deprecated
Expand Down Expand Up @@ -214,7 +214,7 @@ def test_confirm_lb_has_only_new_instances(self):
@mock_elb_deprecated
def test_get_lb(self):
self.setUpELB()
self.assertEqual(u'servergmsextenderELBstg', self.rolling_deploy.get_lb()) #Return All LB's with the proper build number
self.assertEqual('servergmsextenderELBstg', self.rolling_deploy.get_lb()) #Return All LB's with the proper build number

# assertRaises is a context manager since Python 2.7. Only testing in Python 2.7
# https://docs.python.org/2.7/library/unittest.html
Expand Down Expand Up @@ -295,15 +295,15 @@ def test_is_redeploy(self):
self.setUpEC2()
self.assertTrue(self.rolling_deploy.is_redeploy())

@raises(SystemExit)
@mock_ec2_deprecated
def test_is_redeploy_fails(self):
self.setUpEC2(tag=False)
self.assertRaises(self.rolling_deploy.is_redeploy(), Exception)
with pytest.raises(SystemExit):
self.rolling_deploy.is_redeploy()

@raises(SystemExit)
def test_stop_deploy(self):
self.assertRaises(self.rolling_deploy.stop_deploy('error!'), Exception)
with pytest.raises(SystemExit):
self.rolling_deploy.stop_deploy('error!')

@mock_ec2_deprecated
@mock_autoscaling_deprecated
Expand Down Expand Up @@ -389,9 +389,3 @@ def test_double_autoscale_instance_count(self):

def test_decrease_autoscale_instance_count(self):
self.assertEqual(self.rolling_deploy.decrease_autoscale_instance_count(4), 2)

def main():
unittest.main()

if __name__ == "__main__":
main()
25 changes: 25 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[tox]
minversion=2.6.0
envlist = py26,py27,py36
skip_missing_interpreters = True

[testenv]
usedevelop = true
extras =
test
whitelist_externals =
find
commands=
find . -type f -name "*.pyc" -delete
find . -type d -name "__pycache__" -delete
{envbindir}/coverage erase
{envbindir}/coverage run \
{envbindir}/pytest --basetemp={envtmpdir} {posargs}
{envbindir}/coverage html -d {toxinidir}/htmlcov/{envname}

[testenv:appveyor]
basepython = {env:PYTHON:}\python.exe
commands=
{envbindir}/coverage run \
-m pytest --basetemp={envtmpdir} {posargs}
{envbindir}/coverage html -d {toxinidir}/htmlcov/{envname}

0 comments on commit 64ab193

Please sign in to comment.