summaryrefslogtreecommitdiff
path: root/bin/cfn-hup
blob: 690eca4b5a5c1db64cb759987bbf286c20e7b95d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/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)