Skip to content

Commit

Permalink
Revert "Fix some flake8 warns in kafka, keystone and kibana"
Browse files Browse the repository at this point in the history
This reverts commit e76063c.
  • Loading branch information
matrixik committed Sep 18, 2017
1 parent e76063c commit ed7d5d0
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 71 deletions.
22 changes: 10 additions & 12 deletions kafka-init/create_topics.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# coding=utf-8

# (C) Copyright 2017 Hewlett Packard Enterprise Development LP
# Copyright 2017 Fujitsu LIMITED
Expand All @@ -19,6 +18,8 @@
import logging
import os
import subprocess
import sys
import time

from itertools import chain

Expand Down Expand Up @@ -48,19 +49,18 @@ def __init__(self, retcode, stdout, stderr):
self.stderr = stderr

def __str__(self):
return 'CaptureException(retcode={!s}, stdout={!r}, stderr={!r})' \
.format(
self.retcode,
self.stdout,
self.stderr
)
return 'CaptureException(retcode=%s, stdout=%r, stderr=%r)' % (
self.retcode,
self.stdout,
self.stderr
)


def kafka_topics(verb, args=None):
args = [
SCRIPT_PATH,
'--zookeeper', ZOOKEEPER_CONNECTION_STRING,
'--{}'.format(verb)
'--%s' % verb
] + (args if args is not None else [])

logger.debug('running: %s: %r', SCRIPT_PATH, args)
Expand Down Expand Up @@ -96,7 +96,7 @@ def list_topics():

def create_topic(name, partitions, replicas, configs=None):
if configs:
arg_pairs = map(lambda item: ['--config', '{0}={0}'.format(item)],
arg_pairs = map(lambda item: ['--config', '%s=%s' % item],
configs.items())
config_args = list(chain(*arg_pairs))
else:
Expand All @@ -123,8 +123,7 @@ def create_topics(default_config, existing_topics):
replicas = None

if topic_name in existing_topics:
logger.info('Topic already exists, will not create: %s',
topic_name)
logger.info('Topic already exists, will not create: %s', topic_name)
continue

index = 0
Expand Down Expand Up @@ -181,6 +180,5 @@ def main():
logger.info('Topic creation finished successfully. Created: %r',
created_topics)


if __name__ == '__main__':
main()
7 changes: 3 additions & 4 deletions kafka/heap.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# coding=utf-8

# (C) Copyright 2017 Hewlett Packard Enterprise Development LP
#
Expand Down Expand Up @@ -51,7 +50,7 @@ def get_effective_memory_limit_mb():

def main():
if HEAP_OVERRIDE_MB:
print('{}m'.format(HEAP_OVERRIDE_MB))
print('%sm' % HEAP_OVERRIDE_MB)
return

system_max = get_system_memory_mb()
Expand All @@ -69,11 +68,11 @@ def main():
else:
arg_max = effective_max

print('{:d}m'.format(min([
print('%dm' % min([
effective_max,
env_max,
arg_max
])))
]))


if __name__ == '__main__':
Expand Down
2 changes: 0 additions & 2 deletions kafka/kafka_mirror.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# coding=utf-8

# (C) Copyright 2017 Hewlett Packard Enterprise Development LP
#
Expand Down Expand Up @@ -46,6 +45,5 @@ def main():

print(PATH.format(mirror=mirror, kafka=kafka_version, scala=scala_version))


if __name__ == '__main__':
main()
2 changes: 0 additions & 2 deletions kafka/template.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# coding=utf-8

# (C) Copyright 2017 Hewlett Packard Enterprise Development LP
#
Expand Down Expand Up @@ -37,6 +36,5 @@ def main():
trim_blocks=True)
out_file.write(t.render(os.environ))


if __name__ == '__main__':
main()
30 changes: 13 additions & 17 deletions keystone-init/keystone_init.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# coding=utf-8

# (C) Copyright 2017 Hewlett Packard Enterprise Development LP
#
Expand Down Expand Up @@ -27,16 +26,14 @@

import yaml

from keystoneauth1.exceptions import NotFound
from keystoneauth1.exceptions import RetriableConnectionFailure
from keystoneauth1.exceptions import RetriableConnectionFailure, NotFound
from keystoneauth1.identity import Password
from keystoneauth1.session import Session
from keystoneclient.discover import Discover
from requests import HTTPError
from requests import RequestException

from kubernetes import KubernetesAPIClient
from kubernetes import KubernetesAPIResponse
from kubernetes import KubernetesAPIClient, KubernetesAPIResponse

PASSWORD_CHARACTERS = string.ascii_letters + string.digits
KEYSTONE_PASSWORD_ARGS = [
Expand Down Expand Up @@ -270,8 +267,7 @@ def get_or_create_role(client, domain, name):

global_role = first(lambda r: r.name == name, _global_role_cache)
if global_role:
logger.info('found existing global role: name=%s id=%s',
global_role.name, global_role.id)
logger.info('found existing global role: name=%s id=%s', global_role.name, global_role.id)
return global_role

cache = _role_cache[domain.id]
Expand Down Expand Up @@ -448,8 +444,8 @@ def get_kubernetes_secret(name, namespace=None):

try:
return client.get('/api/v1/namespaces/{}/secrets/{}', namespace, name)
except HTTPError as err:
if err.response.status_code != 404:
except HTTPError as e:
if e.response.status_code != 404:
raise

return None
Expand Down Expand Up @@ -494,10 +490,10 @@ def create_kubernetes_secret(fields, name, namespace=None, replace=False):
name, namespace)
return client.request('PUT', '/api/v1/namespaces/{}/secrets/{}',
namespace, name, json=secret)

logger.info('creating secret "%s" in namespace "%s"', name, namespace)
return client.post('/api/v1/namespaces/{}/secrets',
namespace, json=secret)
else:
logger.info('creating secret "%s" in namespace "%s"', name, namespace)
return client.post('/api/v1/namespaces/{}/secrets',
namespace, json=secret)


def diff_kubernetes_secret(secret, desired_fields):
Expand Down Expand Up @@ -531,8 +527,8 @@ def parse_secret(secret):
if '/' in secret:
namespace, name = secret.split('/', 1)
return namespace, name

return None, secret
else:
return None, secret

return secret['namespace'], secret['name']

Expand All @@ -546,8 +542,8 @@ def get_password(secret):
if 'OS_PASSWORD' not in secret.data:
# probably not recoverable, short of deleting the existing
# secret and re-creating (which would be awful in its own way)
raise KeystoneInitException('existing secret {} is '
'invalid'.format(secret.metadata.name))
raise KeystoneInitException('existing secret %s is '
'invalid' % secret.metadata.name)

pass_bytes = base64.b64decode(secret.data['OS_PASSWORD'])
return pass_bytes.decode('utf-8')
Expand Down
11 changes: 4 additions & 7 deletions keystone-init/kubernetes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#!/usr/bin/env python
# coding=utf-8

# (C) Copyright 2017 Hewlett Packard Enterprise Development LP
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
Expand Down Expand Up @@ -37,8 +34,8 @@


def load_current_kube_credentials():
with open(os.path.expanduser(KUBE_CONFIG_PATH), 'r') as kcf:
config = DotMap(yaml.safe_load(kcf))
with open(os.path.expanduser(KUBE_CONFIG_PATH), 'r') as f:
config = DotMap(yaml.safe_load(f))

ctx_name = config['current-context']
ctx = next(c for c in config.contexts if c.name == ctx_name)
Expand All @@ -52,8 +49,8 @@ def load_current_kube_credentials():
if ctx.context.user:
user = next(u for u in config.users if u.name == ctx.context.user).user
return cluster.server, ca_cert, (user['client-certificate'], user['client-key'])

return cluster.server, ca_cert, None
else:
return cluster.server, ca_cert, None


class KubernetesAPIError(Exception):
Expand Down
6 changes: 2 additions & 4 deletions keystone/exit-event-listener.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# coding=utf-8

# see also: http://stackoverflow.com/a/37527488
from __future__ import print_function
Expand All @@ -22,14 +21,13 @@ def main():

phead, _ = childutils.eventdata(payload + '\n')
if phead['processname'] == PROCESS_NAME and phead['expected'] == '0':
print('Process {} failed, killing supervisord...'
.format(PROCESS_NAME), file=sys.stderr)
print('Process %s failed, killing supervisord...' % PROCESS_NAME,
file=sys.stderr)

# touch /kill-supervisor to tell wrapper script to exit uncleanly
open('/kill-supervisor', 'w').close()

os.kill(os.getppid(), signal.SIGTERM)


if __name__ == "__main__":
main()
5 changes: 2 additions & 3 deletions keystone/k8s_get_service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# coding=utf-8

# (C) Copyright 2017 Hewlett Packard Enterprise Development LP
#
Expand Down Expand Up @@ -81,8 +80,8 @@ def get_node_port(service_name, port=None):
return ip, port_def['nodePort']

raise KubernetesAPIException(
'Could not get NodePort from k8s API: service={}, '
'port={}'.format(service_name, port))
'Could not get NodePort from k8s API: service=%s, '
'port=%s' % (service_name, port))


def get_node_ip():
Expand Down
28 changes: 12 additions & 16 deletions keystone/preload.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#!/usr/bin/env python
# coding=utf-8

# (C) Copyright 2015-2017 Hewlett-Packard Enterprise Development LP
# Copyright 2015 FUJITSU LIMITED

Expand Down Expand Up @@ -43,8 +40,8 @@ def _retry(func, retries=5, exc=None):
time.sleep(1.0)
continue
else:
print("Max retries reached after {:d} attempts: {}"
.format(retries, e))
print("Max retries reached after %d attempts: %s" % (retries,
e))
raise


Expand All @@ -66,10 +63,10 @@ def get_project(ks_client, project_name):
return None


def add_projects(ks_client, project_names):
def add_projects(ks_client, project_name):
"""Add the given project_names if they don't already exist"""
default_domain = get_default_domain(ks_client)
for project_name in project_names:
for project_name in project_name:
if not get_project(ks_client, project_name):
_retry(lambda: ks_client.projects.create(name=project_name,
domain=default_domain,
Expand Down Expand Up @@ -144,8 +141,8 @@ def add_user_roles(ks_client, users):
return True


def add_service_endpoint(ks_client, name, description, endpoint_type,
url, region, interface):
def add_service_endpoint(ks_client, name, description, type, url, region,
interface):
"""Add the Monasca service to the catalog with the specified endpoint,
if it doesn't yet exist."""
services = _retry(lambda: ks_client.services.list())
Expand All @@ -155,9 +152,9 @@ def add_service_endpoint(ks_client, name, description, endpoint_type,
else:
service = _retry(lambda: ks_client.services.create(
name=name,
type=endpoint_type,
type=type,
description=description))
print("Created service '{}' of type '{}'".format(name, endpoint_type))
print("Created service '{}' of type '{}'".format(name, type))

for endpoint in _retry(lambda: ks_client.endpoints.list()):
if endpoint.service_id == service.id:
Expand Down Expand Up @@ -200,17 +197,16 @@ def resolve_k8s_service_by_url(url):


def main(argv):
"""Get credentials to create a keystoneauth Session to instantiate a
""" Get credentials to create a keystoneauth Session to instantiate a
Keystone Client and then call methods to add users, projects and roles"""

path = os.environ.get('PRELOAD_YAML_PATH', '/preload.yml')
try:
with open(path, 'r') as pre:
data = yaml.load(pre)
with open(path, 'r') as f:
data = yaml.load(f)
except IOError:
data = {'users': [], 'endpoints': []}
print('No preload.yml at {}, using default values: {!r}'
.format(path, data))
print('No preload.yml at %s, using default values: %r' % (path, data))

users = data['users']
url = 'http://localhost:35357/v3'
Expand Down
6 changes: 2 additions & 4 deletions kibana/template.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# coding=utf-8

# (C) Copyright 2017 Hewlett Packard Enterprise Development LP
#
Expand Down Expand Up @@ -32,9 +31,8 @@ def main():
out_path = sys.argv[2]

with open(in_path, 'r') as in_file, open(out_path, 'w') as out_file:
tmle = Template(in_file.read())
out_file.write(tmle.render(os.environ))

t = Template(in_file.read())
out_file.write(t.render(os.environ))

if __name__ == '__main__':
main()

0 comments on commit ed7d5d0

Please sign in to comment.