<commit_before>from abc import abstractmethod, ABCMeta
import socket
import math
import os
import sys
import time

from jsonschema import ValidationError, Draft4Validator
from fabric.api import run, env
from fabric.context_managers import settings, hide

from cosmo_cli import set_global_verbosity_level, init_logger

lgr, flgr = init_logger()

CLOUDIFY_PACKAGES_PATH = '/cloudify'
CLOUDIFY_COMPONENTS_PACKAGE_PATH = '/cloudify-components'
CLOUDIFY_CORE_PACKAGE_PATH = '/cloudify-core'
CLOUDIFY_UI_PACKAGE_PATH = '/cloudify-ui'
CLOUDIFY_AGENT_PACKAGE_PATH = '/cloudify-agents'


def update_config_at_paths(struct, paths, f):

    """ Transforms properties at given paths using the "f" function.
    Ignores non-existing paths. """

    def kern(struct, path):
        if not path:
            return
        if path[0] not in struct:
            return
        if len(path) == 1:
            struct[path[0]] = f(struct[path[0]])
            return
        kern(struct[path[0]], path[1:])

    for path in paths:
        kern(struct, path)


class BaseProviderClass(object):
    """
    this is the basic provider class supplied with the CLI. it can be imported
     by the provider's code by inheritence into the ProviderManager class.
     each of the below methods can be overriden in favor of a different impl.
    """
    __metaclass__ = ABCMeta

    schema = {}
    CONFIG_NAMES_TO_MODIFY = ()  # No default (empty tuple)
    CONFIG_FILES_PATHS_TO_MODIFY = ()  # No default (empty tuple)

    def __init__(self, provider_config, is_verbose_output):

        set_global_verbosity_level(is_verbose_output)
        self.provider_config = provider_config
        self.is_verbose_output = is_verbose_output

    @abstractmethod
    def provision(self):
        """
        provisions resources for the management server
        """
        return

    @abstractmethod
    def validate(self):
        """
        validations to be performed before provisioning and bootstrapping
        the management server.

        :param dict validation_errors: dict to hold all validation errors.
        :rtype: `dict` of validaiton_errors.
        """
        lgr.debug("no resource validation methods defined!")
        return {}

    @abstractmethod
    def teardown(self, provider_context, ignore_validation=False):
        """
        tears down the management server and its accompanied provisioned
        resources
        """
        return

    def bootstrap(self, mgmt_ip, private_ip, mgmt_ssh_key, mgmt_ssh_user,
                  dev_mode=False):
        """
        bootstraps Cloudify on the management server.

        :param string mgmt_ip: public ip of the provisioned instance.
        :param string private_ip: private ip of the provisioned instance.
         (for configuration purposes).
        :param string mgmt_ssh_key: path to the ssh key to be used for
         connecting to the instance.
        :param string mgmt_ssh_user: the user to use when connecting to the
         instance.
        :param bool dev_mode: states whether dev_mode should be applied.
        :rtype: `bool` True if succeeded, False otherwise. If False is returned
         and 'cfy bootstrap' was executed with the keep-up-on-failure flag, the
         provisioned resources will remain. If the flag is omitted, they will
         be torn down.
        """
        ssh_config = self.provider_config['cloudify']['bootstrap']['ssh']

        env.user = mgmt_ssh_user
        env.key_filename = mgmt_ssh_key
        env.warn_only = True
        env.abort_on_prompts = False
        env.connection_attempts = ssh_config['connection_attempts']
        env.keepalive = 0
        env.linewise = False
        env.pool_size = 0
        env.skip_bad_hosts = False
        env.timeout = ssh_config['connection_timeout']
        env.forward_agent = True
        env.status = False
        env.disable_known_hosts = False

        def _run_with_retries(command, retries=ssh_config['command_retries'],
                              sleeper=ssh_config['retries_interval']):

            for execution in range(retries):
                lgr.debug('running command: {0}'
                          .format(command))
                if not self.is_verbose_output:
                    with hide('running', 'stdout'):
                        r = run(command)
                else:
                    r = run(command)
                if r.succeeded:
                    lgr.debug('successfully ran command: {0}'
                              .format(command))
                    return True
                else:
                    lgr.warning('retrying command: {0}'
                                .format(command))
                    time.sleep(sleeper)
            lgr.error('failed to run: {0}, {1}'
                      .format(command, r.stderr))
            return False

        def _download_package(url, path):
            return _run_with_retries('sudo wget {0} -P {1}'
                                     .format(path, url))
<commit_msg>CFY-847 modified connection_timeout to socket_timeout<commit_after>from abc import abstractmethod, ABCMeta
import socket
import math
import os
import sys
import time

from jsonschema import ValidationError, Draft4Validator
from fabric.api import run, env
from fabric.context_managers import settings, hide

from cosmo_cli import set_global_verbosity_level, init_logger

lgr, flgr = init_logger()

CLOUDIFY_PACKAGES_PATH = '/cloudify'
CLOUDIFY_COMPONENTS_PACKAGE_PATH = '/cloudify-components'
CLOUDIFY_CORE_PACKAGE_PATH = '/cloudify-core'
CLOUDIFY_UI_PACKAGE_PATH = '/cloudify-ui'
CLOUDIFY_AGENT_PACKAGE_PATH = '/cloudify-agents'


def update_config_at_paths(struct, paths, f):

    """ Transforms properties at given paths using the "f" function.
    Ignores non-existing paths. """

    def kern(struct, path):
        if not path:
            return
        if path[0] not in struct:
            return
        if len(path) == 1:
            struct[path[0]] = f(struct[path[0]])
            return
        kern(struct[path[0]], path[1:])

    for path in paths:
        kern(struct, path)


class BaseProviderClass(object):
    """
    this is the basic provider class supplied with the CLI. it can be imported
     by the provider's code by inheritence into the ProviderManager class.
     each of the below methods can be overriden in favor of a different impl.
    """
    __metaclass__ = ABCMeta

    schema = {}
    CONFIG_NAMES_TO_MODIFY = ()  # No default (empty tuple)
    CONFIG_FILES_PATHS_TO_MODIFY = ()  # No default (empty tuple)

    def __init__(self, provider_config, is_verbose_output):

        set_global_verbosity_level(is_verbose_output)
        self.provider_config = provider_config
        self.is_verbose_output = is_verbose_output

    @abstractmethod
    def provision(self):
        """
        provisions resources for the management server
        """
        return

    @abstractmethod
    def validate(self):
        """
        validations to be performed before provisioning and bootstrapping
        the management server.

        :param dict validation_errors: dict to hold all validation errors.
        :rtype: `dict` of validaiton_errors.
        """
        lgr.debug("no resource validation methods defined!")
        return {}

    @abstractmethod
    def teardown(self, provider_context, ignore_validation=False):
        """
        tears down the management server and its accompanied provisioned
        resources
        """
        return

    def bootstrap(self, mgmt_ip, private_ip, mgmt_ssh_key, mgmt_ssh_user,
                  dev_mode=False):
        """
        bootstraps Cloudify on the management server.

        :param string mgmt_ip: public ip of the provisioned instance.
        :param string private_ip: private ip of the provisioned instance.
         (for configuration purposes).
        :param string mgmt_ssh_key: path to the ssh key to be used for
         connecting to the instance.
        :param string mgmt_ssh_user: the user to use when connecting to the
         instance.
        :param bool dev_mode: states whether dev_mode should be applied.
        :rtype: `bool` True if succeeded, False otherwise. If False is returned
         and 'cfy bootstrap' was executed with the keep-up-on-failure flag, the
         provisioned resources will remain. If the flag is omitted, they will
         be torn down.
        """
        ssh_config = self.provider_config['cloudify']['bootstrap']['ssh']

        env.user = mgmt_ssh_user
        env.key_filename = mgmt_ssh_key
        env.warn_only = True
        env.abort_on_prompts = False
        env.connection_attempts = ssh_config['connection_attempts']
        env.keepalive = 0
        env.linewise = False
        env.pool_size = 0
        env.skip_bad_hosts = False
        env.timeout = ssh_config['socket_timeout']
        env.forward_agent = True
        env.status = False
        env.disable_known_hosts = False

        def _run_with_retries(command, retries=ssh_config['command_retries'],
                              sleeper=ssh_config['retries_interval']):

            for execution in range(retries):
                lgr.debug('running command: {0}'
                          .format(command))
                if not self.is_verbose_output:
                    with hide('running', 'stdout'):
                        r = run(command)
                else:
                    r = run(command)
                if r.succeeded:
                    lgr.debug('successfully ran command: {0}'
                              .format(command))
                    return True
                else:
                    lgr.warning('retrying command: {0}'
                                .format(command))
                    time.sleep(sleeper)
            lgr.error('failed to run: {0}, {1}'
                      .format(command, r.stderr))
            return False

        def _download_package(url, path):
            return _run_with_retries('sudo wget {0} -P {1}'
                                     .format(path, url))

{'fraction_non_alphanumeric': 0.07662569389373514, 'fraction_numerical': 0.0034694686756542425, 'mean_word_length': 2.1528125, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5296721', 'n_tokens_mistral': 2842, 'n_tokens_neox': 2699, 'n_words': 812}