heat-cfntools/bin/cfn-hup
tyagi bd194f3f53 Fix typos in cfn scripts
Change-Id: Ie411a1d5229a89b545d949b79552c3f6d240783d
2015-06-04 01:12:13 -07:00

109 lines
3.4 KiB
Python
Executable File

#!/usr/bin/env python
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
Implements cfn-hup CloudFormation functionality
"""
import argparse
import logging
import os
import os.path
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')
parser.add_argument('-f', '--no-daemon',
dest="no_daemon",
action="store_true",
help="Do not run as a daemon",
required=False)
parser.add_argument('-v', '--verbose',
action="store_true",
dest="verbose",
help="Verbose logging",
required=False)
args = parser.parse_args()
# Setup logging
log_format = '%(levelname)s [%(asctime)s] %(message)s'
log_file_name = "/var/log/cfn-hup.log"
log_level = logging.INFO
if args.verbose:
log_level = logging.DEBUG
logging.basicConfig(filename=log_file_name,
format=log_format,
level=log_level)
LOG = logging.getLogger('cfntools')
main_conf_path = '/etc/cfn/cfn-hup.conf'
try:
main_config_file = open(main_conf_path)
except IOError as exc:
LOG.error('Could not open main configuration at %s' % main_conf_path)
exit(1)
config_files = []
hooks_conf_path = '/etc/cfn/hooks.conf'
if os.path.exists(hooks_conf_path):
try:
config_files.append(open(hooks_conf_path))
except IOError as exc:
LOG.exception(exc)
if args.config_dir and os.path.exists(args.config_dir):
try:
for f in os.listdir(args.config_dir):
config_files.append(open(os.path.join(args.config_dir, f)))
except OSError as exc:
LOG.exception(exc)
if not config_files:
LOG.error('No hook files found at %s or %s' % (hooks_conf_path,
args.config_dir))
exit(1)
try:
mainconfig = cfn_helper.HupConfig([main_config_file] + config_files)
except Exception as ex:
LOG.error('Cannot load configuration: %s' % str(ex))
exit(1)
if not mainconfig.unique_resources_get():
LOG.error('No hooks were found. Add some to %s or %s' % (hooks_conf_path,
args.config_dir))
exit(1)
for r in mainconfig.unique_resources_get():
LOG.debug('Checking resource %s' % r)
metadata = cfn_helper.Metadata(mainconfig.stack,
r,
credentials_file=mainconfig.credential_file,
region=mainconfig.region)
metadata.retrieve()
try:
metadata.cfn_hup(mainconfig.hooks)
except Exception as e:
LOG.exception("Error processing metadata")
exit(1)