Merge "Handle artifacts defining a Ansible Galaxy role."

This commit is contained in:
Jenkins 2016-08-10 13:57:44 +00:00 committed by Gerrit Code Review
commit 1e86410da4
5 changed files with 95 additions and 20 deletions

View File

@ -46,7 +46,7 @@ class HotResource(object):
# special case for HOT softwareconfig
if type == 'OS::Heat::SoftwareConfig':
config = self.properties.get('config')
if config:
if isinstance(config, dict):
implementation_artifact = config.get('get_file')
if implementation_artifact:
filename, file_extension = os.path.splitext(
@ -174,10 +174,13 @@ class HotResource(object):
# in operations_deploy_sequence
# TODO(anyone): find some better way to encode this implicit sequence
group = {}
op_index_min = None
op_index_max = -1
for op, hot in deploy_lookup.items():
# position to determine potential preceding nodes
op_index = operations_deploy_sequence.index(op.name)
if op_index_min is None or op_index < op_index_min:
op_index_min = op_index
if op_index > op_index_max:
op_index_max = op_index
for preceding_op_name in \
@ -201,8 +204,54 @@ class HotResource(object):
for hot in hot_resources:
hot.group_dependencies.update(group)
roles_deploy_resource = self._handle_ansiblegalaxy_roles(
hot_resources, node_name, hosting_on_server)
# add a dependency to this ansible roles deploy to
# the first "classic" deploy generated for this node
if roles_deploy_resource and op_index_min:
first_deploy = deploy_lookup.get(operations.get(
operations_deploy_sequence[op_index_min]))
first_deploy.depends_on.append(roles_deploy_resource)
first_deploy.depends_on_nodes.append(roles_deploy_resource)
return hot_resources, deploy_lookup, last_deploy
def _handle_ansiblegalaxy_roles(self, hot_resources, initial_node_name,
hosting_on_server):
artifacts = self.get_all_artifacts(self.nodetemplate)
install_roles_script = ''
for artifact_name, artifact in six.iteritems(artifacts):
artifact_type = artifact.get('type', '').lower()
if artifact_type == 'tosca.artifacts.ansiblegalaxy.role':
role = artifact.get('file', None)
if role:
install_roles_script += 'ansible-galaxy install ' + role \
+ '\n'
if install_roles_script:
# remove trailing \n
install_roles_script = install_roles_script[:-1]
# add shebang and | to use literal scalar type (for multiline)
install_roles_script = '|\n#!/bin/bash\n' + install_roles_script
config_name = initial_node_name + '_install_roles_config'
deploy_name = initial_node_name + '_install_roles_deploy'
hot_resources.append(
HotResource(self.nodetemplate, config_name,
'OS::Heat::SoftwareConfig',
{'config': install_roles_script}))
sd_config = {'config': {'get_resource': config_name},
'server': {'get_resource':
hosting_on_server}}
deploy_resource = \
HotResource(self.nodetemplate, deploy_name,
'OS::Heat::SoftwareDeployment',
sd_config)
hot_resources.append(deploy_resource)
return deploy_resource
def handle_connectsto(self, tosca_source, tosca_target, hot_source,
hot_target, config_location, operation):
# The ConnectsTo relationship causes a configuration operation in
@ -347,16 +396,35 @@ class HotResource(object):
tosca_props[prop.name] = prop.value
return tosca_props
@staticmethod
def get_all_artifacts(nodetemplate):
# workaround bug in the parser
base_type = HotResource.get_base_type_str(nodetemplate.type_definition)
if base_type == "tosca.policies.Placement":
artifacts = {}
else:
artifacts = nodetemplate.type_definition.get_value('artifacts',
parent=True)
if not artifacts:
artifacts = {}
tpl_artifacts = nodetemplate.entity_tpl.get('artifacts')
if tpl_artifacts:
artifacts.update(tpl_artifacts)
return artifacts
@staticmethod
def get_all_operations(node):
operations = {}
for operation in node.interfaces:
operations[operation.name] = operation
# workaround bug in the parser
base_type = HotResource.get_base_type_str(node.type_definition)
if base_type == "tosca.policies.Placement":
return operations
node_type = node.type_definition
if isinstance(node_type, str) or \
node_type.type == "tosca.policies.Placement":
return operations
while True:
type_operations = HotResource._get_interface_operations_from_type(

View File

@ -80,5 +80,7 @@ class HotTemplate(object):
yaml.add_representer(dict, self.represent_ordereddict)
yaml_string = yaml.dump(dict_output, default_flow_style=False)
# get rid of the '' from yaml.dump around numbers
yaml_string = yaml_string.replace('\'', '')
# also replace double return lines with a single one
# seems to be a bug in the serialization of multiline literal scalars
yaml_string = yaml_string.replace('\'', '') .replace('\n\n', '\n')
return version_string + desc_str + yaml_string

View File

@ -381,7 +381,7 @@ class TranslateNodeTemplates(object):
tosca_template)
if tosca_target:
artifacts = self.get_all_artifacts(tosca_target)
artifacts = HotResource.get_all_artifacts(tosca_target)
if artifact_name in artifacts:
artifact = artifacts[artifact_name]
if artifact.get('type', None) == 'tosca.artifacts.File':
@ -514,18 +514,6 @@ class TranslateNodeTemplates(object):
return tosca_target, prop_name, prop_arg
@staticmethod
def get_all_artifacts(nodetemplate):
artifacts = nodetemplate.type_definition.get_value('artifacts',
parent=True)
if not artifacts:
artifacts = {}
tpl_artifacts = nodetemplate.entity_tpl.get('artifacts')
if tpl_artifacts:
artifacts.update(tpl_artifacts)
return artifacts
def _get_attachment_node(self, node, suffix, volume_name):
attach = False
ntpl = self.nodetemplates

View File

@ -1,10 +1,24 @@
heat_template_version: 2013-05-23
description: >
TOSCA template to test artifact usage
TOSCA template to test file and Ansible Galaxy role artifacts
parameters: {}
resources:
customwebserver_install_roles_deploy:
type: OS::Heat::SoftwareDeployment
properties:
config:
get_resource: customwebserver_install_roles_config
server:
get_resource: server
customwebserver_install_roles_config:
type: OS::Heat::SoftwareConfig
properties:
config: |
#!/bin/bash
ansible-galaxy install user.role
group: script
customwebserver_create_deploy:
type: OS::Heat::SoftwareDeployment
properties:

View File

@ -1,11 +1,14 @@
tosca_definitions_version: tosca_simple_yaml_1_0
description: TOSCA template to test artifact usage
description: TOSCA template to test file and Ansible Galaxy role artifacts
node_types:
tosca.nodes.CustomWebServer:
derived_from: tosca.nodes.WebServer
artifacts:
my_galaxyansible_role:
file: user.role
type: tosca.artifacts.AnsibleGalaxy.role
web_content:
file: http://www.mycompany.org/content.tgz
type: tosca.artifacts.File