Add Flake8 checks for bin/cfn-* to Gating

Fix the Pep8/hacking/pyflakes warnings in bin/cfn-*.

Change-Id: Ie21b3909c80d33adc712c2f3c9494d0fdbd90608
This commit is contained in:
Dirk Mueller 2013-06-13 09:34:11 +02:00
parent 971c165242
commit 87e08b0b4b
7 changed files with 137 additions and 138 deletions

View File

@ -26,26 +26,30 @@ def create_symlink(source_file, target_file, override=False):
if (override): if (override):
os.remove(target_file) os.remove(target_file)
else: else:
print '%s already exists, will not replace with symlink' % target_file print('%s already exists, will not replace with symlink'
% target_file)
return return
print '%s -> %s' % (source_file, target_file) print '%s -> %s' % (source_file, target_file)
os.symlink(source_file, target_file) os.symlink(source_file, target_file)
def check_dirs(source_dir, target_dir): def check_dirs(source_dir, target_dir):
print '%s -> %s' % (source_dir, target_dir) print '%s -> %s' % (source_dir, target_dir)
if source_dir == target_dir: if source_dir == target_dir:
print 'Source and target are the same %s' % target_dir print 'Source and target are the same %s' % target_dir
return False return False
if not os.path.exists(target_dir): if not os.path.exists(target_dir):
try: try:
os.makedirs(target_dir) os.makedirs(target_dir)
except OSError as exc: except OSError as exc:
print 'Could not create target directory %s: %s' % (target_dir, exc) print('Could not create target directory %s: %s'
% (target_dir, exc))
return False return False
return True return True
def create_symlinks(source_dir, target_dir, glob_pattern, override): def create_symlinks(source_dir, target_dir, glob_pattern, override):
source_files = glob.glob(os.path.join(source_dir, glob_pattern)) source_files = glob.glob(os.path.join(source_dir, glob_pattern))
for source_file in source_files: for source_file in source_files:
@ -53,7 +57,7 @@ def create_symlinks(source_dir, target_dir, glob_pattern, override):
create_symlink(source_file, target_file, override=override) create_symlink(source_file, target_file, override=override)
if __name__ == '__main__': if __name__ == '__main__':
description = 'Creates symlinks for the cfn-* scripts in this directory to /opt/aws/bin' description = 'Creates symlinks for the cfn-* scripts to /opt/aws/bin'
parser = argparse.ArgumentParser(description=description) parser = argparse.ArgumentParser(description=description)
parser.add_argument( parser.add_argument(
'-t', '--target', '-t', '--target',
@ -64,14 +68,16 @@ if __name__ == '__main__':
parser.add_argument( parser.add_argument(
'-s', '--source', '-s', '--source',
dest="source_dir", dest="source_dir",
help="Source directory to create symlinks from. Defaults to the directory where this script is", help="Source directory to create symlinks from. "
"Defaults to the directory where this script is",
default='/usr/bin', default='/usr/bin',
required=False) required=False)
parser.add_argument( parser.add_argument(
'-f', '--force', '-f', '--force',
dest="force", dest="force",
action='store_true', action='store_true',
help="If specified, will create symlinks even if there is already a target file", help="If specified, will create symlinks even if "
"there is already a target file",
required=False) required=False)
args = parser.parse_args() args = parser.parse_args()

View File

@ -16,49 +16,45 @@
Implements cfn-get-metadata CloudFormation functionality Implements cfn-get-metadata CloudFormation functionality
""" """
import argparse import argparse
import io
import logging import logging
import os
import os.path
import sys
from heat_cfntools.cfntools.cfn_helper import * from heat_cfntools.cfntools import cfn_helper
description = " " description = " "
parser = argparse.ArgumentParser(description=description) parser = argparse.ArgumentParser(description=description)
parser.add_argument('-s', '--stack', parser.add_argument('-s', '--stack',
dest="stack_name", dest="stack_name",
help="A Heat stack name", help="A Heat stack name",
required=True) required=True)
parser.add_argument('-r', '--resource', parser.add_argument('-r', '--resource',
dest="logical_resource_id", dest="logical_resource_id",
help="A Heat logical resource ID", help="A Heat logical resource ID",
required=True) required=True)
parser.add_argument('--access-key', parser.add_argument('--access-key',
dest="access_key", dest="access_key",
help="A Keystone access key", help="A Keystone access key",
required=False) required=False)
parser.add_argument('--secret-key', parser.add_argument('--secret-key',
dest="secret_key", dest="secret_key",
help="A Keystone secret key", help="A Keystone secret key",
required=False) required=False)
parser.add_argument('--region', parser.add_argument('--region',
dest="region", dest="region",
help="Openstack region", help="Openstack region",
required=False) required=False)
parser.add_argument('--credential-file', parser.add_argument('--credential-file',
dest="credential_file", dest="credential_file",
help="credential-file", help="credential-file",
required=False) required=False)
parser.add_argument('-u', '--url', parser.add_argument('-u', '--url',
dest="url", dest="url",
help="service url", help="service url",
required=False) required=False)
parser.add_argument('-k', '--key', parser.add_argument('-k', '--key',
dest="key", dest="key",
help="key", help="key",
required=False) required=False)
args = parser.parse_args() args = parser.parse_args()
if not args.stack_name: if not args.stack_name:
@ -78,12 +74,12 @@ file_handler = logging.FileHandler(log_file_name)
file_handler.setFormatter(logging.Formatter(log_format)) file_handler.setFormatter(logging.Formatter(log_format))
LOG.addHandler(file_handler) LOG.addHandler(file_handler)
metadata = Metadata(args.stack_name, metadata = cfn_helper.Metadata(args.stack_name,
args.logical_resource_id, args.logical_resource_id,
access_key=args.access_key, access_key=args.access_key,
secret_key=args.secret_key, secret_key=args.secret_key,
region=args.region, region=args.region,
credentials_file=args.credential_file) credentials_file=args.credential_file)
metadata.retrieve() metadata.retrieve()
LOG.debug(str(metadata)) LOG.debug(str(metadata))
metadata.display() metadata.display()

View File

@ -16,32 +16,30 @@
Implements cfn-hup CloudFormation functionality Implements cfn-hup CloudFormation functionality
""" """
import argparse import argparse
import io
import logging import logging
import os import os
import os.path import os.path
import sys
from heat_cfntools.cfntools.cfn_helper import * from heat_cfntools.cfntools import cfn_helper
description = " " description = " "
parser = argparse.ArgumentParser(description=description) parser = argparse.ArgumentParser(description=description)
parser.add_argument('-c', '--config', parser.add_argument('-c', '--config',
dest="config_dir", dest="config_dir",
help="Hook Config Directory", help="Hook Config Directory",
required=False, required=False,
default='/etc/cfn/hooks.d') default='/etc/cfn/hooks.d')
parser.add_argument('-f', '--no-daemon', parser.add_argument('-f', '--no-daemon',
dest="no_deamon", dest="no_deamon",
action="store_true", action="store_true",
help="Do not run as a deamon", help="Do not run as a deamon",
required=False) required=False)
parser.add_argument('-v', '--verbose', parser.add_argument('-v', '--verbose',
action="store_true", action="store_true",
dest="verbose", dest="verbose",
help="Verbose logging", help="Verbose logging",
required=False) required=False)
args = parser.parse_args() args = parser.parse_args()
# Setup logging # Setup logging
@ -90,7 +88,7 @@ if not config_files:
exit(1) exit(1)
try: try:
mainconfig = HupConfig([main_config_file] + config_files) mainconfig = cfn_helper.HupConfig([main_config_file] + config_files)
except Exception as ex: except Exception as ex:
LOG.error('Cannot load configuration: %s' % str(ex)) LOG.error('Cannot load configuration: %s' % str(ex))
exit(1) exit(1)
@ -103,10 +101,10 @@ if not mainconfig.unique_resources_get():
for r in mainconfig.unique_resources_get(): for r in mainconfig.unique_resources_get():
LOG.debug('Checking resource %s' % r) LOG.debug('Checking resource %s' % r)
metadata = Metadata(mainconfig.stack, metadata = cfn_helper.Metadata(mainconfig.stack,
r, r,
credentials_file=mainconfig.credential_file, credentials_file=mainconfig.credential_file,
region=mainconfig.region) region=mainconfig.region)
metadata.retrieve() metadata.retrieve()
try: try:
metadata.cfn_hup(mainconfig.hooks) metadata.cfn_hup(mainconfig.hooks)

View File

@ -17,38 +17,36 @@ Implements cfn-init CloudFormation functionality
""" """
import argparse import argparse
import logging import logging
import os
import sys
from heat_cfntools.cfntools.cfn_helper import * from heat_cfntools.cfntools import cfn_helper
description = " " description = " "
parser = argparse.ArgumentParser(description=description) parser = argparse.ArgumentParser(description=description)
parser.add_argument('-s', '--stack', parser.add_argument('-s', '--stack',
dest="stack_name", dest="stack_name",
help="A Heat stack name", help="A Heat stack name",
required=False) required=False)
parser.add_argument('-r', '--resource', parser.add_argument('-r', '--resource',
dest="logical_resource_id", dest="logical_resource_id",
help="A Heat logical resource ID", help="A Heat logical resource ID",
required=False) required=False)
parser.add_argument('--access-key', parser.add_argument('--access-key',
dest="access_key", dest="access_key",
help="A Keystone access key", help="A Keystone access key",
required=False) required=False)
parser.add_argument('--secret-key', parser.add_argument('--secret-key',
dest="secret_key", dest="secret_key",
help="A Keystone secret key", help="A Keystone secret key",
required=False) required=False)
parser.add_argument('--region', parser.add_argument('--region',
dest="region", dest="region",
help="Openstack region", help="Openstack region",
required=False) required=False)
parser.add_argument('-c', '--configsets', parser.add_argument('-c', '--configsets',
dest="configsets", dest="configsets",
help="An optional list of configSets (default: default)", help="An optional list of configSets (default: default)",
required=False) required=False)
args = parser.parse_args() args = parser.parse_args()
log_format = '%(levelname)s [%(asctime)s] %(message)s' log_format = '%(levelname)s [%(asctime)s] %(message)s'
@ -60,12 +58,12 @@ file_handler = logging.FileHandler(log_file_name)
file_handler.setFormatter(logging.Formatter(log_format)) file_handler.setFormatter(logging.Formatter(log_format))
LOG.addHandler(file_handler) LOG.addHandler(file_handler)
metadata = Metadata(args.stack_name, metadata = cfn_helper.Metadata(args.stack_name,
args.logical_resource_id, args.logical_resource_id,
access_key=args.access_key, access_key=args.access_key,
secret_key=args.secret_key, secret_key=args.secret_key,
region=args.region, region=args.region,
configsets=args.configsets) configsets=args.configsets)
metadata.retrieve() metadata.retrieve()
try: try:
metadata.cfn_init() metadata.cfn_init()

View File

@ -19,13 +19,11 @@ import argparse
import logging import logging
import os import os
import subprocess import subprocess
import random
import sys
# Override BOTO_CONFIG, which makes boto look only at the specified # Override BOTO_CONFIG, which makes boto look only at the specified
# config file, instead of the default locations # config file, instead of the default locations
os.environ['BOTO_CONFIG'] = '/var/lib/heat-cfntools/cfn-boto-cfg' os.environ['BOTO_CONFIG'] = '/var/lib/heat-cfntools/cfn-boto-cfg'
from boto.ec2.cloudwatch import CloudWatchConnection from boto.ec2 import cloudwatch
log_format = '%(levelname)s [%(asctime)s] %(message)s' log_format = '%(levelname)s [%(asctime)s] %(message)s'
@ -39,17 +37,17 @@ try:
import psutil import psutil
except ImportError: except ImportError:
LOG.warn("psutil not available. If you want process and memory " LOG.warn("psutil not available. If you want process and memory "
"statistics, you need to install it.") "statistics, you need to install it.")
from heat_cfntools.cfntools.cfn_helper import * from heat_cfntools.cfntools import cfn_helper
KILO = 1024 KILO = 1024
MEGA = 1048576 MEGA = 1048576
GIGA = 1073741824 GIGA = 1073741824
unit_map = {'bytes': 1, unit_map = {'bytes': 1,
'kilobytes': KILO, 'kilobytes': KILO,
'megabytes': MEGA, 'megabytes': MEGA,
'gigabytes': GIGA} 'gigabytes': GIGA}
description = " " description = " "
parser = argparse.ArgumentParser(description=description) parser = argparse.ArgumentParser(description=description)
@ -63,9 +61,11 @@ parser.add_argument('--service-failure', required=False, action="store_true",
parser.add_argument('--mem-util', required=False, action="store_true", parser.add_argument('--mem-util', required=False, action="store_true",
help='Reports memory utilization in percentages.') help='Reports memory utilization in percentages.')
parser.add_argument('--mem-used', required=False, action="store_true", parser.add_argument('--mem-used', required=False, action="store_true",
help='Reports memory used (excluding cache and buffers) in megabytes.') help='Reports memory used (excluding cache/buffers) '
'in megabytes.')
parser.add_argument('--mem-avail', required=False, action="store_true", parser.add_argument('--mem-avail', required=False, action="store_true",
help='Reports available memory (including cache and buffers) in megabytes.') help='Reports available memory (including cache/buffers) '
'in megabytes.')
parser.add_argument('--swap-util', required=False, action="store_true", parser.add_argument('--swap-util', required=False, action="store_true",
help='Reports swap utilization in percentages.') help='Reports swap utilization in percentages.')
parser.add_argument('--swap-used', required=False, action="store_true", parser.add_argument('--swap-used', required=False, action="store_true",
@ -74,7 +74,7 @@ parser.add_argument('--disk-space-util', required=False, action="store_true",
help='Reports disk space utilization in percentages.') help='Reports disk space utilization in percentages.')
parser.add_argument('--disk-space-used', required=False, action="store_true", parser.add_argument('--disk-space-used', required=False, action="store_true",
help='Reports allocated disk space in gigabytes.') help='Reports allocated disk space in gigabytes.')
parser.add_argument('--disk-space-avail',required=False, action="store_true", parser.add_argument('--disk-space-avail', required=False, action="store_true",
help='Reports available disk space in gigabytes.') help='Reports available disk space in gigabytes.')
parser.add_argument('--memory-units', required=False, default='megabytes', parser.add_argument('--memory-units', required=False, default='megabytes',
help='Specifies units for memory metrics.') help='Specifies units for memory metrics.')
@ -103,7 +103,7 @@ args = parser.parse_args()
LOG.debug('cfn-push-stats called %s ' % (str(args))) LOG.debug('cfn-push-stats called %s ' % (str(args)))
credentials = parse_creds_file(args.credential_file) credentials = cfn_helper.parse_creds_file(args.credential_file)
namespace = 'system/linux' namespace = 'system/linux'
data = {} data = {}
@ -185,6 +185,7 @@ if args.cpu_util:
'Value': cpu_percent, 'Value': cpu_percent,
'Units': 'Percent'} 'Units': 'Percent'}
# HAProxy # HAProxy
# ======= # =======
def parse_haproxy_unix_socket(res, latency_only=False): def parse_haproxy_unix_socket(res, latency_only=False):
@ -198,7 +199,7 @@ def parse_haproxy_unix_socket(res, latency_only=False):
def add_stat(key, value, unit='Counter'): def add_stat(key, value, unit='Counter'):
res[key] = {'Value': value, res[key] = {'Value': value,
'Units': unit} 'Units': unit}
echo = subprocess.Popen(['echo', 'show stat'], echo = subprocess.Popen(['echo', 'show stat'],
stdout=subprocess.PIPE) stdout=subprocess.PIPE)
@ -206,8 +207,8 @@ def parse_haproxy_unix_socket(res, latency_only=False):
stdin=echo.stdout, stdin=echo.stdout,
stdout=subprocess.PIPE) stdout=subprocess.PIPE)
end_pipe = socat.stdout end_pipe = socat.stdout
raw = [l.strip('\n').split(',') for l in end_pipe raw = [l.strip('\n').split(',')
if l[0] != '#' and len(l) > 2] for l in end_pipe if l[0] != '#' and len(l) > 2]
latency = 0 latency = 0
up_count = 0 up_count = 0
down_count = 0 down_count = 0
@ -242,10 +243,10 @@ def send_stats(info):
# Create boto connection, need the hard-coded port/path as boto # Create boto connection, need the hard-coded port/path as boto
# can't read these from config values in BOTO_CONFIG # can't read these from config values in BOTO_CONFIG
# FIXME : currently only http due to is_secure=False # FIXME : currently only http due to is_secure=False
client = CloudWatchConnection( client = cloudwatch.CloudWatchConnection(
aws_access_key_id=credentials['AWSAccessKeyId'], aws_access_key_id=credentials['AWSAccessKeyId'],
aws_secret_access_key=credentials['AWSSecretKey'], aws_secret_access_key=credentials['AWSSecretKey'],
is_secure=False, port=8003, path="/v1", debug=0) is_secure=False, port=8003, path="/v1", debug=0)
# Then we send the metric datapoints passed in "info", note this could # Then we send the metric datapoints passed in "info", note this could
# contain multiple keys as the options parsed above are noe exclusive # contain multiple keys as the options parsed above are noe exclusive
@ -254,7 +255,7 @@ def send_stats(info):
metric_dims = [{'AlarmName': args.watch}] metric_dims = [{'AlarmName': args.watch}]
for key in info: for key in info:
LOG.info("Sending watch %s metric %s, Units %s, Value %s" % LOG.info("Sending watch %s metric %s, Units %s, Value %s" %
(args.watch, key, info[key]['Units'], info[key]['Value'])) (args.watch, key, info[key]['Units'], info[key]['Value']))
client.put_metric_data(namespace=namespace, client.put_metric_data(namespace=namespace,
name=key, name=key,
value=info[key]['Value'], value=info[key]['Value'],

View File

@ -17,42 +17,41 @@ Implements cfn-signal CloudFormation functionality
""" """
import argparse import argparse
import logging import logging
import os
import sys import sys
from heat_cfntools.cfntools.cfn_helper import * from heat_cfntools.cfntools import cfn_helper
description = " " description = " "
parser = argparse.ArgumentParser(description=description) parser = argparse.ArgumentParser(description=description)
parser.add_argument('-s', '--success', parser.add_argument('-s', '--success',
dest="success", dest="success",
help="signal status to report", help="signal status to report",
default='true', default='true',
required=False) required=False)
parser.add_argument('-r', '--reason', parser.add_argument('-r', '--reason',
dest="reason", dest="reason",
help="The reason for the failure", help="The reason for the failure",
default="Configuration Complete", default="Configuration Complete",
required=False) required=False)
parser.add_argument('-d', '--data', parser.add_argument('-d', '--data',
dest="data", dest="data",
default="Application has completed configuration.", default="Application has completed configuration.",
help="The data to send", help="The data to send",
required=False) required=False)
parser.add_argument('-i', '--id', parser.add_argument('-i', '--id',
dest="unique_id", dest="unique_id",
help="the unique id to send back to the WaitCondition", help="the unique id to send back to the WaitCondition",
default='00000', default='00000',
required=False) required=False)
parser.add_argument('-e', '--exit', parser.add_argument('-e', '--exit',
dest="exit_code", dest="exit_code",
help="The exit code from a procecc to interpret", help="The exit code from a procecc to interpret",
default=None, default=None,
required=False) required=False)
parser.add_argument('url', parser.add_argument('url',
help='the url to post to') help='the url to post to')
args = parser.parse_args() args = parser.parse_args()
log_format = '%(levelname)s [%(asctime)s] %(message)s' log_format = '%(levelname)s [%(asctime)s] %(message)s'
@ -76,13 +75,13 @@ else:
status = 'SUCCESS' status = 'SUCCESS'
body = { body = {
"Status" : status, "Status": status,
"Reason" : args.reason, "Reason": args.reason,
"UniqueId" : args.unique_id, "UniqueId": args.unique_id,
"Data" : args.data "Data": args.data
} }
cmd_str = "curl -X PUT -H \'Content-Type:\' --data-binary \'%s\' \"%s\"" % \ cmd_str = "curl -X PUT -H \'Content-Type:\' --data-binary \'%s\' \"%s\"" % \
(json.dumps(body), args.url) (cfn_helper.json.dumps(body), args.url)
command = CommandRunner(cmd_str).run() command = cfn_helper.CommandRunner(cmd_str).run()
sys.exit(command.status) sys.exit(command.status)

View File

@ -15,6 +15,7 @@ downloadcache = ~/cache/pip
[testenv:pep8] [testenv:pep8]
commands = flake8 commands = flake8
flake8 --filename=cfn-* bin
[testenv:pylint] [testenv:pylint]
setenv = VIRTUAL_ENV={envdir} setenv = VIRTUAL_ENV={envdir}