diff --git a/bin/cfn-create-aws-symlinks b/bin/cfn-create-aws-symlinks index 3d075db..3fbe252 100755 --- a/bin/cfn-create-aws-symlinks +++ b/bin/cfn-create-aws-symlinks @@ -26,26 +26,30 @@ def create_symlink(source_file, target_file, override=False): if (override): os.remove(target_file) else: - print '%s already exists, will not replace with symlink' % target_file + print('%s already exists, will not replace with symlink' + % target_file) return print '%s -> %s' % (source_file, target_file) os.symlink(source_file, target_file) + def check_dirs(source_dir, target_dir): print '%s -> %s' % (source_dir, target_dir) - + if source_dir == target_dir: print 'Source and target are the same %s' % target_dir return False - + if not os.path.exists(target_dir): try: os.makedirs(target_dir) 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 True + def create_symlinks(source_dir, target_dir, glob_pattern, override): source_files = glob.glob(os.path.join(source_dir, glob_pattern)) 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) 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.add_argument( '-t', '--target', @@ -64,14 +68,16 @@ if __name__ == '__main__': parser.add_argument( '-s', '--source', 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', required=False) parser.add_argument( '-f', '--force', dest="force", 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) args = parser.parse_args() diff --git a/bin/cfn-get-metadata b/bin/cfn-get-metadata index 6aa9251..84f28b9 100755 --- a/bin/cfn-get-metadata +++ b/bin/cfn-get-metadata @@ -16,49 +16,45 @@ Implements cfn-get-metadata CloudFormation functionality """ import argparse -import io import logging -import os -import os.path -import sys -from heat_cfntools.cfntools.cfn_helper import * +from heat_cfntools.cfntools import cfn_helper description = " " parser = argparse.ArgumentParser(description=description) parser.add_argument('-s', '--stack', - dest="stack_name", - help="A Heat stack name", - required=True) + dest="stack_name", + help="A Heat stack name", + required=True) parser.add_argument('-r', '--resource', - dest="logical_resource_id", - help="A Heat logical resource ID", - required=True) + dest="logical_resource_id", + help="A Heat logical resource ID", + required=True) parser.add_argument('--access-key', - dest="access_key", - help="A Keystone access key", - required=False) + dest="access_key", + help="A Keystone access key", + required=False) parser.add_argument('--secret-key', - dest="secret_key", - help="A Keystone secret key", - required=False) + dest="secret_key", + help="A Keystone secret key", + required=False) parser.add_argument('--region', - dest="region", - help="Openstack region", - required=False) + dest="region", + help="Openstack region", + required=False) parser.add_argument('--credential-file', - dest="credential_file", - help="credential-file", - required=False) + dest="credential_file", + help="credential-file", + required=False) parser.add_argument('-u', '--url', - dest="url", - help="service url", - required=False) + dest="url", + help="service url", + required=False) parser.add_argument('-k', '--key', - dest="key", - help="key", - required=False) + dest="key", + help="key", + required=False) args = parser.parse_args() if not args.stack_name: @@ -78,12 +74,12 @@ file_handler = logging.FileHandler(log_file_name) file_handler.setFormatter(logging.Formatter(log_format)) LOG.addHandler(file_handler) -metadata = Metadata(args.stack_name, - args.logical_resource_id, - access_key=args.access_key, - secret_key=args.secret_key, - region=args.region, - credentials_file=args.credential_file) +metadata = cfn_helper.Metadata(args.stack_name, + args.logical_resource_id, + access_key=args.access_key, + secret_key=args.secret_key, + region=args.region, + credentials_file=args.credential_file) metadata.retrieve() LOG.debug(str(metadata)) metadata.display() diff --git a/bin/cfn-hup b/bin/cfn-hup index 18ff5bb..faeccaa 100755 --- a/bin/cfn-hup +++ b/bin/cfn-hup @@ -16,32 +16,30 @@ Implements cfn-hup CloudFormation functionality """ import argparse -import io import logging import os import os.path -import sys -from heat_cfntools.cfntools.cfn_helper import * +from heat_cfntools.cfntools import cfn_helper description = " " parser = argparse.ArgumentParser(description=description) parser.add_argument('-c', '--config', - dest="config_dir", - help="Hook Config Directory", - required=False, - default='/etc/cfn/hooks.d') + dest="config_dir", + help="Hook Config Directory", + required=False, + default='/etc/cfn/hooks.d') parser.add_argument('-f', '--no-daemon', - dest="no_deamon", - action="store_true", - help="Do not run as a deamon", - required=False) + dest="no_deamon", + action="store_true", + help="Do not run as a deamon", + required=False) parser.add_argument('-v', '--verbose', - action="store_true", - dest="verbose", - help="Verbose logging", - required=False) + action="store_true", + dest="verbose", + help="Verbose logging", + required=False) args = parser.parse_args() # Setup logging @@ -90,7 +88,7 @@ if not config_files: exit(1) try: - mainconfig = HupConfig([main_config_file] + config_files) + mainconfig = cfn_helper.HupConfig([main_config_file] + config_files) except Exception as ex: LOG.error('Cannot load configuration: %s' % str(ex)) exit(1) @@ -103,10 +101,10 @@ if not mainconfig.unique_resources_get(): for r in mainconfig.unique_resources_get(): LOG.debug('Checking resource %s' % r) - metadata = Metadata(mainconfig.stack, - r, - credentials_file=mainconfig.credential_file, - region=mainconfig.region) + metadata = cfn_helper.Metadata(mainconfig.stack, + r, + credentials_file=mainconfig.credential_file, + region=mainconfig.region) metadata.retrieve() try: metadata.cfn_hup(mainconfig.hooks) diff --git a/bin/cfn-init b/bin/cfn-init index 5338cae..fb658e4 100755 --- a/bin/cfn-init +++ b/bin/cfn-init @@ -17,38 +17,36 @@ Implements cfn-init CloudFormation functionality """ import argparse import logging -import os -import sys -from heat_cfntools.cfntools.cfn_helper import * +from heat_cfntools.cfntools import cfn_helper description = " " parser = argparse.ArgumentParser(description=description) parser.add_argument('-s', '--stack', - dest="stack_name", - help="A Heat stack name", - required=False) + dest="stack_name", + help="A Heat stack name", + required=False) parser.add_argument('-r', '--resource', - dest="logical_resource_id", - help="A Heat logical resource ID", - required=False) + dest="logical_resource_id", + help="A Heat logical resource ID", + required=False) parser.add_argument('--access-key', - dest="access_key", - help="A Keystone access key", - required=False) + dest="access_key", + help="A Keystone access key", + required=False) parser.add_argument('--secret-key', - dest="secret_key", - help="A Keystone secret key", - required=False) + dest="secret_key", + help="A Keystone secret key", + required=False) parser.add_argument('--region', - dest="region", - help="Openstack region", - required=False) + dest="region", + help="Openstack region", + required=False) parser.add_argument('-c', '--configsets', - dest="configsets", - help="An optional list of configSets (default: default)", - required=False) + dest="configsets", + help="An optional list of configSets (default: default)", + required=False) args = parser.parse_args() 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)) LOG.addHandler(file_handler) -metadata = Metadata(args.stack_name, - args.logical_resource_id, - access_key=args.access_key, - secret_key=args.secret_key, - region=args.region, - configsets=args.configsets) +metadata = cfn_helper.Metadata(args.stack_name, + args.logical_resource_id, + access_key=args.access_key, + secret_key=args.secret_key, + region=args.region, + configsets=args.configsets) metadata.retrieve() try: metadata.cfn_init() diff --git a/bin/cfn-push-stats b/bin/cfn-push-stats index 2da08df..d8aa3d9 100755 --- a/bin/cfn-push-stats +++ b/bin/cfn-push-stats @@ -19,13 +19,11 @@ import argparse import logging import os import subprocess -import random -import sys # Override BOTO_CONFIG, which makes boto look only at the specified # config file, instead of the default locations 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' @@ -39,17 +37,17 @@ try: import psutil except ImportError: 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 MEGA = 1048576 GIGA = 1073741824 unit_map = {'bytes': 1, - 'kilobytes': KILO, - 'megabytes': MEGA, - 'gigabytes': GIGA} + 'kilobytes': KILO, + 'megabytes': MEGA, + 'gigabytes': GIGA} 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", help='Reports memory utilization in percentages.') 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", - 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", help='Reports swap utilization in percentages.') 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.') parser.add_argument('--disk-space-used', required=False, action="store_true", 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.') parser.add_argument('--memory-units', required=False, default='megabytes', help='Specifies units for memory metrics.') @@ -103,7 +103,7 @@ args = parser.parse_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' data = {} @@ -185,6 +185,7 @@ if args.cpu_util: 'Value': cpu_percent, 'Units': 'Percent'} + # HAProxy # ======= 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'): res[key] = {'Value': value, - 'Units': unit} + 'Units': unit} echo = subprocess.Popen(['echo', 'show stat'], stdout=subprocess.PIPE) @@ -206,8 +207,8 @@ def parse_haproxy_unix_socket(res, latency_only=False): stdin=echo.stdout, stdout=subprocess.PIPE) end_pipe = socat.stdout - raw = [l.strip('\n').split(',') for l in end_pipe - if l[0] != '#' and len(l) > 2] + raw = [l.strip('\n').split(',') + for l in end_pipe if l[0] != '#' and len(l) > 2] latency = 0 up_count = 0 down_count = 0 @@ -242,10 +243,10 @@ def send_stats(info): # Create boto connection, need the hard-coded port/path as boto # can't read these from config values in BOTO_CONFIG # FIXME : currently only http due to is_secure=False - client = CloudWatchConnection( - aws_access_key_id=credentials['AWSAccessKeyId'], - aws_secret_access_key=credentials['AWSSecretKey'], - is_secure=False, port=8003, path="/v1", debug=0) + client = cloudwatch.CloudWatchConnection( + aws_access_key_id=credentials['AWSAccessKeyId'], + aws_secret_access_key=credentials['AWSSecretKey'], + is_secure=False, port=8003, path="/v1", debug=0) # Then we send the metric datapoints passed in "info", note this could # contain multiple keys as the options parsed above are noe exclusive @@ -254,7 +255,7 @@ def send_stats(info): metric_dims = [{'AlarmName': args.watch}] for key in info: 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, name=key, value=info[key]['Value'], diff --git a/bin/cfn-signal b/bin/cfn-signal index e8118c8..29d487b 100755 --- a/bin/cfn-signal +++ b/bin/cfn-signal @@ -17,42 +17,41 @@ Implements cfn-signal CloudFormation functionality """ import argparse import logging -import os import sys -from heat_cfntools.cfntools.cfn_helper import * +from heat_cfntools.cfntools import cfn_helper description = " " parser = argparse.ArgumentParser(description=description) parser.add_argument('-s', '--success', - dest="success", - help="signal status to report", - default='true', - required=False) + dest="success", + help="signal status to report", + default='true', + required=False) parser.add_argument('-r', '--reason', - dest="reason", - help="The reason for the failure", - default="Configuration Complete", - required=False) + dest="reason", + help="The reason for the failure", + default="Configuration Complete", + required=False) parser.add_argument('-d', '--data', - dest="data", - default="Application has completed configuration.", - help="The data to send", - required=False) + dest="data", + default="Application has completed configuration.", + help="The data to send", + required=False) parser.add_argument('-i', '--id', - dest="unique_id", - help="the unique id to send back to the WaitCondition", - default='00000', - required=False) + dest="unique_id", + help="the unique id to send back to the WaitCondition", + default='00000', + required=False) parser.add_argument('-e', '--exit', - dest="exit_code", - help="The exit code from a procecc to interpret", - default=None, - required=False) + dest="exit_code", + help="The exit code from a procecc to interpret", + default=None, + required=False) parser.add_argument('url', - help='the url to post to') + help='the url to post to') args = parser.parse_args() log_format = '%(levelname)s [%(asctime)s] %(message)s' @@ -76,13 +75,13 @@ else: status = 'SUCCESS' body = { - "Status" : status, - "Reason" : args.reason, - "UniqueId" : args.unique_id, - "Data" : args.data + "Status": status, + "Reason": args.reason, + "UniqueId": args.unique_id, + "Data": args.data } cmd_str = "curl -X PUT -H \'Content-Type:\' --data-binary \'%s\' \"%s\"" % \ - (json.dumps(body), args.url) -command = CommandRunner(cmd_str).run() + (cfn_helper.json.dumps(body), args.url) +command = cfn_helper.CommandRunner(cmd_str).run() sys.exit(command.status) diff --git a/tox.ini b/tox.ini index 3616e8d..04c4c87 100644 --- a/tox.ini +++ b/tox.ini @@ -15,6 +15,7 @@ downloadcache = ~/cache/pip [testenv:pep8] commands = flake8 + flake8 --filename=cfn-* bin [testenv:pylint] setenv = VIRTUAL_ENV={envdir}