Skip to content

Commit

Permalink
Solve some issues pointed out by pylint in senza/templates/*
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcs committed Jan 6, 2017
1 parent 026a6ed commit e015b90
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
21 changes: 15 additions & 6 deletions senza/templates/__init__.py
@@ -1,13 +1,22 @@
"""
Modules and functions for senza component templates
"""

from types import ModuleType, FunctionType
import pkg_resources


def get_template_description(name, module: ModuleType):
def get_template_description(name, module: ModuleType) -> str:
"""
Gets the human-readable template description based on the name of the
component and the component's module docstring
"""
return '{}: {}'.format(name, (module.__doc__ or "").strip())


def has_functions(module, names):
return all(isinstance(getattr(module, function_name, None), FunctionType) for function_name in names)
return all(isinstance(getattr(module, function_name, None), FunctionType)
for function_name in names)


def get_templates() -> dict:
Expand All @@ -16,15 +25,15 @@ def get_templates() -> dict:
"""
entry_points = pkg_resources.iter_entry_points('senza.templates')
template_modules = {}
for e in entry_points: # type: pkg_resources.EntryPoint
for entry_point in entry_points: # type: pkg_resources.EntryPoint
try:
module = e.resolve()
module = entry_point.resolve()
except ImportError:
# ignore bad entry points
continue
else:
# make sure the entry point resolves to a module with the essential interface functions
if (isinstance(module, ModuleType) and
has_functions(module, ('gather_user_variables', 'generate_definition'))):
template_modules[e.name] = module
has_functions(module, ('gather_user_variables', 'generate_definition'))):
template_modules[entry_point.name] = module
return template_modules
11 changes: 7 additions & 4 deletions senza/templates/postgresapp.py
Expand Up @@ -398,7 +398,7 @@ def gather_user_variables(variables, region, account_info):
prompt(variables, 'instance_type', 'EC2 instance type', default='t2.medium')

variables['hosted_zone'] = account_info.Domain or defaults['hosted_zone']
if (variables['hosted_zone'][-1:] != '.'):
if variables['hosted_zone'][-1:] != '.':
variables['hosted_zone'] += '.'
prompt(variables, 'discovery_domain', 'ETCD Discovery Domain',
default='postgres.' + variables['hosted_zone'][:-1])
Expand Down Expand Up @@ -503,11 +503,14 @@ def get_latest_image(registry_domain='registry.opensource.zalan.do', team='acid'
Gets the full name of latest image for an artifact
"""
try:
r = requests.get('https://{0}/teams/{1}/artifacts/{2}/tags'.format(registry_domain, team, artifact))
if r.ok:
url = 'https://{0}/teams/{1}/artifacts/{2}/tags'.format(registry_domain,
team,
artifact)
response = requests.get(url)
if response.ok:
# sort the tags by creation date
latest = None
for entry in sorted(r.json(), key=lambda t: t['created'], reverse=True):
for entry in sorted(response.json(), key=lambda t: t['created'], reverse=True):
tag = entry['name']
# try to avoid snapshots if possible
if 'SNAPSHOT' not in tag:
Expand Down
16 changes: 11 additions & 5 deletions senza/templates/redisnode.py
@@ -1,6 +1,6 @@
'''
"""
Elasticache node running redis, without replication / HA (for caching)
'''
"""

from clickclick import warning
from senza.utils import pystache_render
Expand Down Expand Up @@ -28,7 +28,10 @@
'''


def gather_user_variables(variables, region, account_info):
def gather_user_variables(variables, region, account_info): # pylint: disable=locally-disabled, unused-argument
"""
Gather all the variables needed to create the redis node
"""
# maximal 32 characters because of the loadbalancer-name
prompt(variables, 'application_id', 'Application ID', default='hello-world',
value_proc=check_value(18, '^[a-zA-Z][-a-zA-Z0-9]*$'))
Expand All @@ -38,12 +41,15 @@ def gather_user_variables(variables, region, account_info):

rules_missing = check_security_group(sg_name, [('tcp', 6379)], region, allow_from_self=True)
if ('tcp', 6379) in rules_missing:
warning('Security group {} does not allow tcp/6379 access, you will not be able to access your redis'.format(
sg_name))
warning('Security group {} does not allow tcp/6379 access, '
'you will not be able to access your redis'.format(sg_name))

return variables


def generate_definition(variables):
"""
Generates the redis node definition yaml from template
"""
definition_yaml = pystache_render(TEMPLATE, variables)
return definition_yaml

0 comments on commit e015b90

Please sign in to comment.