Skip to content

Commit

Permalink
Merge pull request #203 from matrixik/fix_lint4
Browse files Browse the repository at this point in the history
Fix some linter warnings, part 3
  • Loading branch information
kornicameister authored Sep 18, 2017
2 parents 5b0089b + 1399976 commit 687431c
Show file tree
Hide file tree
Showing 17 changed files with 81 additions and 51 deletions.
65 changes: 40 additions & 25 deletions monasca-alarms/monasca_alarm_definition.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/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 All @@ -14,10 +15,13 @@
# License for the specific language governing permissions and limitations
# under the License.
#

from __future__ import print_function
'''
Loads Notifications and Alarm Definitions into Monasca. These are configured via a yaml file which contains two
sections: notifications and alarm_definitions. Within the sections are the notifications and alarm definitions to be
Loads Notifications and Alarm Definitions into Monasca. These are configured
via a yaml file which contains two sections:
notifications and alarm_definitions.
Within the sections are the notifications and alarm definitions to be
created, updated or deleted.
For the possible arguments, see the argument parser definition.
Expand Down Expand Up @@ -152,8 +156,9 @@
else:
monascaclient_found = True


class MonascaLoadDefinitions(object):
""" Loads Notifications and Alarm Definitions into Monasca
"""Loads Notifications and Alarm Definitions into Monasca
"""
def __init__(self, args):
self._args = args
Expand All @@ -162,13 +167,13 @@ def __init__(self, args):
self._verbose = args['verbose']

def _keystone_auth(self):
""" Authenticate to Keystone and set self._token and self._api_url
"""Authenticate to Keystone and set self._token and self._api_url
"""
if not self._args['keystone_token']:
try:
ks = ksclient.KSClient(**self._args)
except Exception, e:
raise Exception('Keystone KSClient Exception: %s' % e)
except Exception as err:
raise Exception('Keystone KSClient Exception: {}'.format(err))

self._token = ks.token
if not self._args['monasca_api_url']:
Expand All @@ -177,7 +182,8 @@ def _keystone_auth(self):
self._api_url = self._args['monasca_api_url']
else:
if self._args['monasca_api_url'] is None:
raise Exception('Error: When specifying keystone_token, monasca_api_url is required')
raise Exception('Error: When specifying keystone_token, '
'monasca_api_url is required')
self._token = self._args['keystone_token']
self._api_url = self._args['monasca_api_url']

Expand All @@ -195,25 +201,30 @@ def run(self, data_file):
with open(data_file) as f:
yaml_text = f.read()
except IOError:
raise Exception('Unable to open yaml alarm definitions: %s' % data_file)
raise Exception('Unable to open yaml alarm definitions: {}'
.format(data_file))

yaml_data = yaml.safe_load(yaml_text)

self._keystone_auth()
self._monasca = client.Client(self._args['api_version'], self._api_url, token=self._token)
self._print_message('Using Monasca at {}'.format(self._api_url))
if 'notifications' not in yaml_data:
raise Exception('No notifications section in %s' % data_file)
raise Exception('No notifications section in {}'.format(data_file))

(processed, changed, notification_ids) = self._do_notifications(yaml_data['notifications'])
self._print_message('%d Notifications Processed %d Notifications Changed' % (processed, changed))
self._print_message(
'{:d} Notifications Processed {:d} Notifications Changed'
.format(processed, changed))

if 'alarm_definitions' not in yaml_data:
raise Exception('No alarm_definitions section in %s' % data_file)
raise Exception('No alarm_definitions section in {}'
.format(data_file))

(processed, changed) = self.do_alarm_definitions(yaml_data['alarm_definitions'], notification_ids)
self._print_message('%d Alarm Definitions Processed %d Alarm Definitions Changed' % (processed, changed))

self._print_message(
'{:d} Alarm Definitions Processed {:d} Alarm Definitions Changed'
.format(processed, changed))

def _do_notifications(self, notifications):
processed = 0
Expand Down Expand Up @@ -321,13 +332,13 @@ def _process_alarm_definition(self, definition, notification_ids):
else:
raise Exception(str(resp.status_code) + resp.text)
else: # Only other option is state=present

alarm_actions = self._map_notifications(definition.get('alarm_actions', []), notification_ids)
ok_actions = self._map_notifications(definition.get('ok_actions', []), notification_ids)
undetermined_actions = self._map_notifications(definition.get('undetermined_actions', []), notification_ids)
def_kwargs = {'name': name, 'description': definition.get('description', ''), 'expression': expression,
'match_by': definition.get('match_by', []), 'severity': definition.get('severity', 'LOW').upper(),
'alarm_actions': alarm_actions,'ok_actions': ok_actions,
'alarm_actions': alarm_actions, 'ok_actions': ok_actions,
'undetermined_actions': undetermined_actions}

if name in definitions.keys():
Expand Down Expand Up @@ -359,10 +370,11 @@ def _process_alarm_definition(self, definition, notification_ids):
else:
raise Exception(body)


def _get_parser():
parser = argparse.ArgumentParser(
prog='monasca_alarm_definition',
#description=__doc__.strip(),
# description=__doc__.strip(),
add_help=False,
# formatter_class=HelpFormatter,
formatter_class=lambda prog: argparse.HelpFormatter(
Expand Down Expand Up @@ -514,18 +526,20 @@ def _get_parser():

return parser


def _env(*vars, **kwargs):
"""Search for the first defined of possibly many env vars
Returns the first environment variable defined in vars, or
returns the default defined in kwargs.
"""
for v in vars:
value = os.environ.get(v)
for var in vars:
value = os.environ.get(var)
if value:
return value
return kwargs.get('default', '')


def main(args=None):
if args is None:
args = sys.argv[1:]
Expand All @@ -539,15 +553,15 @@ def main(args=None):

if not args.os_username and not args.os_auth_token:
raise Exception("You must provide a username via"
" either --os-username or env[OS_USERNAME]"
" or a token via --os-auth-token or"
" env[OS_AUTH_TOKEN]")
" either --os-username or env[OS_USERNAME]"
" or a token via --os-auth-token or"
" env[OS_AUTH_TOKEN]")

if not args.os_password and not args.os_auth_token:
raise Exception("You must provide a password via"
" either --os-password or env[OS_PASSWORD]"
" or a token via --os-auth-token or"
" env[OS_AUTH_TOKEN]")
" either --os-password or env[OS_PASSWORD]"
" or a token via --os-auth-token or"
" env[OS_AUTH_TOKEN]")

kwargs = {
'username': args.os_username,
Expand Down Expand Up @@ -578,5 +592,6 @@ def main(args=None):

definition.run(args.definitions_file)


if __name__ == "__main__":
main()
4 changes: 2 additions & 2 deletions monasca-alarms/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if [ -n "$MONASCA_WAIT_FOR_API" ]; then
echo "Waiting for Monasca API to become available..."
success="false"

for i in $(seq $MONASCA_API_WAIT_RETRIES); do
for i in $(seq "$MONASCA_API_WAIT_RETRIES"); do
monasca alarm-definition-list --limit 1
if [ $? -eq 0 ]; then
success="true"
Expand All @@ -29,4 +29,4 @@ fi
echo "Loading Definitions...."

python /template.py /config/definitions.yml.j2 /config/definitions.yml
python monasca_alarm_definition.py --verbose --definitions-file /config/definitions.yml
python monasca_alarm_definition.py --verbose --definitions-file /config/definitions.yml
13 changes: 8 additions & 5 deletions monasca-alarms/template.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
# coding=utf-8

# (C) Copyright 2017 Hewlett Packard Enterprise Development LP
#
Expand All @@ -13,6 +14,7 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from __future__ import print_function

import os
Expand All @@ -30,11 +32,12 @@ def main():
out_path = sys.argv[2]

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


if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion monasca-api-python/health-check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ KEYSTONE_TOKEN=$(curl --include --silent --show-error --output - --header "Conte
}'
) && \
curl --include --silent --show-error --output - --header "X-Auth-Token:${KEYSTONE_TOKEN}" \
http://localhost:${MONASCA_CONTAINER_API_PORT} 2>&1 | \
http://localhost:"${MONASCA_CONTAINER_API_PORT}" 2>&1 | \
awk '
BEGIN {status_code="0"; body=""; output=""}
$1 ~ /^HTTP\// {status_line=$0; status_code=$2}
Expand Down
1 change: 1 addition & 0 deletions monasca-api-python/kafka_wait_for_topics.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
# coding=utf-8

# (C) Copyright 2017 Hewlett Packard Enterprise Development LP
#
Expand Down
6 changes: 3 additions & 3 deletions monasca-api-python/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ KAFKA_WAIT_DELAY=${KAFKA_WAIT_DELAY:-"5"}
if [ "$MYSQL_WAIT_RETRIES" != "0" ]; then
echo "Waiting for MySQL to become available..."
success="false"
for i in $(seq $MYSQL_WAIT_RETRIES); do
for i in $(seq "$MYSQL_WAIT_RETRIES"); do
mysqladmin status \
--host="$MYSQL_HOST" \
--user="$MYSQL_USER" \
Expand All @@ -41,7 +41,7 @@ if [ -n "$KAFKA_WAIT_FOR_TOPICS" ]; then
echo "Waiting for Kafka topics to become available..."
success="false"

for i in $(seq $KAFKA_WAIT_RETRIES); do
for i in $(seq "$KAFKA_WAIT_RETRIES"); do
python /kafka_wait_for_topics.py
if [ $? -eq 0 ]; then
success="true"
Expand Down Expand Up @@ -89,7 +89,7 @@ gunicorn --capture-output \
-n monasca-api \
--worker-class="$GUNICORN_WORKER_CLASS" \
--worker-connections="$GUNICORN_WORKER_CONNECTIONS" \
--backlog=$GUNICORN_BACKLOG \
--backlog="$GUNICORN_BACKLOG" \
$access_arg \
--access-logformat "$ACCESS_LOG_FIELDS" \
--paste /etc/monasca/api-config.ini \
Expand Down
6 changes: 4 additions & 2 deletions monasca-api-python/template.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
# coding=utf-8

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

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


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

# (C) Copyright 2017 Hewlett Packard Enterprise Development LP
#
Expand Down
2 changes: 1 addition & 1 deletion monasca-forwarder/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if [ -n "$KAFKA_WAIT_FOR_TOPICS" ]; then
echo "Waiting for Kafka topics to become available..."
success="false"

for i in $(seq $KAFKA_WAIT_RETRIES); do
for i in $(seq "$KAFKA_WAIT_RETRIES"); do
python /kafka_wait_for_topics.py
if [ $? -eq 0 ]; then
success="true"
Expand Down
12 changes: 7 additions & 5 deletions monasca-forwarder/template.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
# coding=utf-8

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

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


if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion monasca-log-api/health-check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
MONASCA_CONTAINER_LOG_API_PORT=${1:-5607}

curl --include --silent --show-error --output - \
http://localhost:${MONASCA_CONTAINER_LOG_API_PORT}/healthcheck 2>&1 | \
http://localhost:"${MONASCA_CONTAINER_LOG_API_PORT}"/healthcheck 2>&1 | \
awk '
BEGIN {status_code="0"; body=""; output=""}
$1 ~ /^HTTP\// {status_line=$0; status_code=$2}
Expand Down
1 change: 1 addition & 0 deletions monasca-log-api/kafka_wait_for_topics.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
# coding=utf-8

# (C) Copyright 2017 Hewlett Packard Enterprise Development LP
#
Expand Down
2 changes: 1 addition & 1 deletion monasca-log-api/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ if [ -n "$KAFKA_WAIT_FOR_TOPICS" ]; then
echo "Waiting for Kafka topics to become available..."
success="false"

for i in $(seq $KAFKA_WAIT_RETRIES); do
for i in $(seq "$KAFKA_WAIT_RETRIES"); do
python /kafka_wait_for_topics.py
if [ $? -eq 0 ]; then
success="true"
Expand Down
6 changes: 4 additions & 2 deletions monasca-log-api/template.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
# coding=utf-8

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

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


if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions monasca-log-metrics/kafka_wait_for_topics.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
# coding=utf-8

# (C) Copyright 2017 Hewlett Packard Enterprise Development LP
#
Expand Down
2 changes: 1 addition & 1 deletion monasca-log-metrics/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if [ -n "$KAFKA_WAIT_FOR_TOPICS" ]; then
echo "Waiting for Kafka topics to become available..."
success="false"

for i in $(seq $KAFKA_WAIT_RETRIES); do
for i in $(seq "$KAFKA_WAIT_RETRIES"); do
python /kafka_wait_for_topics.py
if [ $? -eq 0 ]; then
success="true"
Expand Down
Loading

0 comments on commit 687431c

Please sign in to comment.