<commit_before># Copyright 2015 Cloudbase Solutions Srl
# All Rights Reserved.
#
#    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.

import abc
import time

from heatclient import exc
import six

from argus.backends import base
from argus.backends.heat import client
from argus.backends import windows
from argus.backends.tempest import manager as api_manager
from argus import exceptions
from argus import util


OS_NOVA_RESOURCE = 'OS::Nova::Server'
OS_NEUTRON_FLOATING_IP = "OS::Neutron::FloatingIP"
RESOURCE_COMPLETED_STATUS = "CREATE_COMPLETE"
HEAT_RESOURCE_LIMIT = 10


# pylint: disable=abstract-method; FP: https://bitbucket.org/logilab/pylint/issues/565
@six.add_metaclass(abc.ABCMeta)
class BaseHeatBackend(base.CloudBackend):
    """A backend which uses Heat as the driving core."""

    def __init__(self, conf, name=None, userdata=None, metadata=None,
                 availability_zone=None):
        super(BaseHeatBackend, self).__init__(
            conf, name=name, userdata=userdata, metadata=metadata,
            availability_zone=availability_zone)

        self._manager = api_manager.APIManager()
        self._heat_client = client.heat_client(
            self._manager.primary_credentials())
        self._keypair = None

    @staticmethod
    def _build_template(instance_name, key,
                        image_name, flavor_name, user_data,
                        floating_network_id, private_net_id):
        return {
            u'heat_template_version': u'2013-05-23',
            u'description': u'argus',
            u'resources': {
                u'server_floating_ip': {
                    u'type': u'OS::Neutron::FloatingIP',
                    u'properties': {
                        u'floating_network_id': floating_network_id,
                        u'port_id': {u'get_resource': u'server_port'}
                    }
                },
                instance_name: {
                    u'type': u'OS::Nova::Server',
                    u'properties': {
                        u'key_name': key,
                        u'image': image_name,
                        u'flavor': flavor_name,
                        u'user_data_format': 'RAW',
                        u'user_data': user_data,
                        u'networks': [
                            {u'port': {u'get_resource': u'server_port'}}
                        ]
                    }
                },
                u'server_port': {
                    u'type': u'OS::Neutron::Port',
                    u'properties': {
                        u'network_id': private_net_id,
                        u'security_groups': [{u'get_resource': u'server_security_group'}]}
                },
                u'server_security_group': {
                    u'type': u'OS::Neutron::SecurityGroup',
                    u'properties': {
                        u'rules': [
                            {u'remote_ip_prefix': u'0.0.0.0/0',
                             u'port_range_max': 5986,
                             u'port_range_min': 5986,
                             u'protocol': u'tcp'},
                            {u'remote_ip_prefix': u'0.0.0.0/0',
                             u'port_range_max': 5985,
                             u'port_range_min': 5985,
                             u'protocol': u'tcp'},
                            {u'remote_ip_prefix': u'0.0.0.0/0',
                             u'port_range_max': 3389,
                             u'port_range_min': 3389,
                             u'protocol': u'tcp'},
                            {u'remote_ip_prefix': u'0.0.0.0/0',
                             u'port_range_max': 22,
                             u'port_range_min': 22,
                             u'protocol': u'tcp'}
                        ],
                        u'description': u'Add security group rules for server',
                        u'name': u'security-group'}
                }
            }
        }

    def setup_instance(self):
        super(BaseHeatBackend, self).setup_instance()

        # Get the image and the flavor name
        image_name = self._manager.image_client.get_image_meta(
            self._conf.openstack.image_ref)['name']
        flavor_name = self._manager.flavors_client.show_flavor(
            self._conf.openstack.flavor_ref)['flavor']['name']
        self._keypair = self._manager.create_keypair(
            name=self.__class__.__name__)

        # Get network info.
        credentials = self._manager.primary_credentials()
        floating_network_id = credentials.router['external_gateway_info']['network_id']
        private_net_id = credentials.network['id']

        template = self._build_template(
            self._name, self._keypair.name,
            image_name, flavor_name, self.userdata,
            floating_network_id, private_net_id)
        fields = {
            'stack_name': self._name,
            'disable_rollback': True,
            'parameters': {},
            'template': template,
            'files': {},
            'environment': {},
        }

        self._heat_client.stacks.create(**fields)

    def cleanup(self):
        if self._keypair:
            self._keypair.destroy()

        try:
            self._heat_client.stacks.delete(stack_id=self._name)
        finally:
            self._manager.floating_ips_client.delete_floating_ip(
                self._floating_ip_resource['id'])
            self._manager.cleanup_credentials()

    def _find_resource(self, resource_name, limit=HEAT_RESOURCE_LIMIT):
        fields = {
            'stack_id': self._name,
            'nested_depth': 1,
        }
        while limit > 0:
            try:
                resources = self._heat_client.resources.list(**fields)
            except exc.HTTPNotFound:
                raise exceptions.ArgusError('Stack not found: %s' % self._name)
            else:
                for resource in resources:
                    if resource.resource_type == resource_name:
                        # Found the resource we were needing
                        if resource.resource_status == RESOURCE_COMPLETED_STATUS:
                            return resource.physical_resource_id
                        else:
                            limit -= 1
                            time.sleep(0.05)
                            break
                else:
                    break
<commit_msg>Increate the readiness timeout of Heat resources.<commit_after># Copyright 2015 Cloudbase Solutions Srl
# All Rights Reserved.
#
#    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.

import abc
import time

from heatclient import exc
import six

from argus.backends import base
from argus.backends.heat import client
from argus.backends import windows
from argus.backends.tempest import manager as api_manager
from argus import exceptions
from argus import util


OS_NOVA_RESOURCE = 'OS::Nova::Server'
OS_NEUTRON_FLOATING_IP = "OS::Neutron::FloatingIP"
RESOURCE_COMPLETED_STATUS = "CREATE_COMPLETE"
HEAT_RESOURCE_LIMIT = 10
HEAT_RESOURCE_TIMEOUT = 0.5


# pylint: disable=abstract-method; FP: https://bitbucket.org/logilab/pylint/issues/565
@six.add_metaclass(abc.ABCMeta)
class BaseHeatBackend(base.CloudBackend):
    """A backend which uses Heat as the driving core."""

    def __init__(self, conf, name=None, userdata=None, metadata=None,
                 availability_zone=None):
        super(BaseHeatBackend, self).__init__(
            conf, name=name, userdata=userdata, metadata=metadata,
            availability_zone=availability_zone)

        self._manager = api_manager.APIManager()
        self._heat_client = client.heat_client(
            self._manager.primary_credentials())
        self._keypair = None

    @staticmethod
    def _build_template(instance_name, key,
                        image_name, flavor_name, user_data,
                        floating_network_id, private_net_id):
        return {
            u'heat_template_version': u'2013-05-23',
            u'description': u'argus',
            u'resources': {
                u'server_floating_ip': {
                    u'type': u'OS::Neutron::FloatingIP',
                    u'properties': {
                        u'floating_network_id': floating_network_id,
                        u'port_id': {u'get_resource': u'server_port'}
                    }
                },
                instance_name: {
                    u'type': u'OS::Nova::Server',
                    u'properties': {
                        u'key_name': key,
                        u'image': image_name,
                        u'flavor': flavor_name,
                        u'user_data_format': 'RAW',
                        u'user_data': user_data,
                        u'networks': [
                            {u'port': {u'get_resource': u'server_port'}}
                        ]
                    }
                },
                u'server_port': {
                    u'type': u'OS::Neutron::Port',
                    u'properties': {
                        u'network_id': private_net_id,
                        u'security_groups': [{u'get_resource': u'server_security_group'}]}
                },
                u'server_security_group': {
                    u'type': u'OS::Neutron::SecurityGroup',
                    u'properties': {
                        u'rules': [
                            {u'remote_ip_prefix': u'0.0.0.0/0',
                             u'port_range_max': 5986,
                             u'port_range_min': 5986,
                             u'protocol': u'tcp'},
                            {u'remote_ip_prefix': u'0.0.0.0/0',
                             u'port_range_max': 5985,
                             u'port_range_min': 5985,
                             u'protocol': u'tcp'},
                            {u'remote_ip_prefix': u'0.0.0.0/0',
                             u'port_range_max': 3389,
                             u'port_range_min': 3389,
                             u'protocol': u'tcp'},
                            {u'remote_ip_prefix': u'0.0.0.0/0',
                             u'port_range_max': 22,
                             u'port_range_min': 22,
                             u'protocol': u'tcp'}
                        ],
                        u'description': u'Add security group rules for server',
                        u'name': u'security-group'}
                }
            }
        }

    def setup_instance(self):
        super(BaseHeatBackend, self).setup_instance()

        # Get the image and the flavor name
        image_name = self._manager.image_client.get_image_meta(
            self._conf.openstack.image_ref)['name']
        flavor_name = self._manager.flavors_client.show_flavor(
            self._conf.openstack.flavor_ref)['flavor']['name']
        self._keypair = self._manager.create_keypair(
            name=self.__class__.__name__)

        # Get network info.
        credentials = self._manager.primary_credentials()
        floating_network_id = credentials.router['external_gateway_info']['network_id']
        private_net_id = credentials.network['id']

        template = self._build_template(
            self._name, self._keypair.name,
            image_name, flavor_name, self.userdata,
            floating_network_id, private_net_id)
        fields = {
            'stack_name': self._name,
            'disable_rollback': True,
            'parameters': {},
            'template': template,
            'files': {},
            'environment': {},
        }

        self._heat_client.stacks.create(**fields)

    def cleanup(self):
        if self._keypair:
            self._keypair.destroy()

        try:
            self._heat_client.stacks.delete(stack_id=self._name)
        finally:
            self._manager.floating_ips_client.delete_floating_ip(
                self._floating_ip_resource['id'])
            self._manager.cleanup_credentials()

    def _find_resource(self, resource_name, limit=HEAT_RESOURCE_LIMIT):
        fields = {
            'stack_id': self._name,
            'nested_depth': 1,
        }
        while limit > 0:
            try:
                resources = self._heat_client.resources.list(**fields)
            except exc.HTTPNotFound:
                raise exceptions.ArgusError('Stack not found: %s' % self._name)
            else:
                for resource in resources:
                    if resource.resource_type == resource_name:
                        # Found the resource we were needing
                        if resource.resource_status == RESOURCE_COMPLETED_STATUS:
                            return resource.physical_resource_id
                        else:
                            limit -= 1
                            time.sleep(HEAT_RESOURCE_TIMEOUT)
                            break
                else:
                    break

{'fraction_non_alphanumeric': 0.09461687334241273, 'fraction_numerical': 0.010680237975772345, 'mean_word_length': 1.5623507805325987, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 8, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4607435', 'n_tokens_mistral': 3891, 'n_tokens_neox': 3705, 'n_words': 756}