Add cfn-get-metadata make all cfn tools more consistent

Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
This commit is contained in:
Angus Salkeld 2012-04-27 13:02:58 +10:00
parent 1f141a6d04
commit f7c6773542
5 changed files with 104 additions and 31 deletions

83
heat/cfntools/cfn-get-metadata Executable file
View File

@ -0,0 +1,83 @@
#!/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-get-metadata CloudFormation functionality
"""
import argparse
import io
import logging
import os
import os.path
import sys
if os.path.exists('/opt/aws/bin'):
sys.path.insert(0, '/opt/aws/bin')
from cfn_helper import *
else:
from heat.cfntools.cfn_helper import *
description = " "
parser = argparse.ArgumentParser(description=description)
parser.add_argument('-s', '--stack',
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)
parser.add_argument('--access-key',
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)
parser.add_argument('--region',
dest="region",
help="Openstack region",
required=False)
parser.add_argument('--credential-file',
dest="credential_file",
help="credential-file",
required=False)
parser.add_argument('-u', '--url',
dest="url",
help="service url",
required=False)
parser.add_argument('-k', '--key',
dest="key",
help="key",
required=False)
args = parser.parse_args()
log_format = '%(levelname)s [%(asctime)s] %(message)s'
logging.basicConfig(format=log_format, level=logging.DEBUG)
logger = logging.getLogger('cfn-get-metadata')
log_file_name = "/var/log/cfn-get-metadata.log"
file_handler = logging.FileHandler(log_file_name)
file_handler.setFormatter(logging.Formatter(log_format))
logger.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)
metadata.retrieve()
print str(metadata)

View File

@ -47,20 +47,19 @@ parser.add_argument('-v', '--verbose',
help="Verbose logging",
required=False)
args = parser.parse_args()
# FIXME: implement real arg
logger = logging.getLogger('cfn-hup')
log_file_name = "/var/log/cfn-hup.log"
log_format = '%(levelname)s [%(asctime)s] %(message)s'
file_handler = logging.FileHandler(log_file_name)
file_handler.setFormatter(logging.Formatter(log_format))
logging.getLogger().addHandler(file_handler)
if args.verbose:
logging.basicConfig(format=log_format, level=logging.DEBUG)
else:
logging.basicConfig(format=log_format, level=logging.INFO)
logger = logging.getLogger('cfn-hup')
log_file_name = "/var/log/cfn-hup.log"
file_handler = logging.FileHandler(log_file_name)
file_handler.setFormatter(logging.Formatter(log_format))
logger.addHandler(file_handler)
main_conf_path = '/etc/cfn/cfn-hup.conf'
try:
main_config_file = open(main_conf_path)

View File

@ -14,20 +14,6 @@
"""
Implements cfn-init CloudFormation functionality
Resource metadata currently implemented:
* config/packages
* config/services
Not implemented yet:
* config sets
* config/sources
* config/commands
* config/files
* config/users
* config/groups
* command line args
- placeholders are ignored
"""
import argparse
@ -42,14 +28,6 @@ if os.path.exists('/opt/aws/bin'):
else:
from heat.cfntools.cfn_helper import *
logger = logging.getLogger('cfn-init')
log_file_name = "/var/log/cfn-init.log"
log_format = '%(levelname)s [%(asctime)s] %(message)s'
file_handler = logging.FileHandler(log_file_name)
file_handler.setFormatter(logging.Formatter(log_format))
logging.getLogger().addHandler(file_handler)
logging.basicConfig(format=log_format, level=logging.DEBUG)
description = " "
parser = argparse.ArgumentParser(description=description)
parser.add_argument('-s', '--stack',
@ -73,7 +51,15 @@ parser.add_argument('--region',
help="Openstack region",
required=False)
args = parser.parse_args()
# FIXME: implement real arg
log_format = '%(levelname)s [%(asctime)s] %(message)s'
logging.basicConfig(format=log_format, level=logging.DEBUG)
logger = logging.getLogger('cfn-init')
log_file_name = "/var/log/cfn-init.log"
file_handler = logging.FileHandler(log_file_name)
file_handler.setFormatter(logging.Formatter(log_format))
logger.addHandler(file_handler)
metadata = Metadata(args.stack_name,
args.logical_resource_id,

View File

@ -681,6 +681,9 @@ class Metadata(object):
else:
self._metadata = self._data
def __str__(self):
return self._metadata
def _is_valid_metadata(self):
"""
Should find the AWS::CloudFormation::Init json key

View File

@ -137,7 +137,9 @@ def jeos_create(options, arguments):
# and injecting them into the TDL at the appropriate place
if instance_type == 'cfntools':
tdl_xml = libxml2.parseFile(tdl_path)
for cfnname in ['cfn-init', 'cfn-hup', 'cfn-signal', 'cfn_helper.py']:
cfn_tools = ['cfn-init', 'cfn-hup', 'cfn-signal', \
'cfn-get-metadata', 'cfn_helper.py']
for cfnname in cfn_tools:
f = open('%s/%s' % (cfntools_path, cfnname), 'r')
cfscript_e64 = base64.b64encode(f.read())
f.close()