<commit_before>from contextlib import contextmanager
import copy
from datetime import datetime
import functools
import hashlib
import json
import os
import random
import re
import string
import socket
import subprocess
import sys
import time
import uuid
import warnings
import yaml

with warnings.catch_warnings():
    # Fabric maintenance is lagging a bit so let's suppress these warnings.
    warnings.filterwarnings("ignore", category=DeprecationWarning)
    from fabric import Connection
from ipaddress import ip_address, ip_network
from paramiko.ssh_exception import NoValidConnectionsError, SSHException
import requests
import retrying
import textwrap
import winrm

from cloudify_rest_client.exceptions import CloudifyClientError

from cosmo_tester.framework import util
from cosmo_tester.framework.constants import CLOUDIFY_TENANT_HEADER

HEALTHY_STATE = 'OK'
RSYNC_LOCATIONS = ['/etc',
                   '/opt',
                   '/var',
                   '/usr']


def only_manager(func):
    @functools.wraps(func)
    def wrapped(self, *args, **kwargs):
        if not self.is_manager:
            raise RuntimeError('This is not a manager')
        return func(self, *args, **kwargs)
    return wrapped


class VM(object):
    def __init__(self, image_type, test_config, bootstrappable=False):
        self.image_name = None
        self.userdata = ""
        self.username = None
        self.password = <PASSWORD>
        self.api_ca_path = None
        self.enable_ssh_wait = True
        self.should_finalize = True
        self.restservice_expected = False
        self.client = None
        self._test_config = test_config
        self.windows = 'windows' in image_type
        self._tmpdir_base = None
        self.bootstrappable = bootstrappable
        self.image_type = image_type
        self.is_manager = self._is_manager_image_type()
        self.reboot_required = False
        self._set_image_details()
        if self.windows:
            self.prepare_for_windows()

    def assign(
            self,
            public_ip_address,
            private_ip_address,
            networks,
            ssh_key,
            logger,
            tmpdir,
            node_instance_id,
            deployment_id,
            server_id,
            server_index,
    ):
        self.ip_address = public_ip_address
        self.private_ip_address = private_ip_address
        self._ssh_key = ssh_key
        self._logger = logger
        self._tmpdir_base = tmpdir
        self._tmpdir = os.path.join(tmpdir, public_ip_address)
        os.makedirs(self._tmpdir)
        self.node_instance_id = node_instance_id
        self.deployment_id = deployment_id
        self.server_id = server_id
        self.server_index = server_index
        if self.is_manager:
            self.networks = networks
            self.basic_install_config = {
                'manager': {
                    'public_ip': str(public_ip_address),
                    'private_ip': str(private_ip_address),
                    'hostname': str(server_id),
                    'security': {
                        'admin_username': self._test_config[
                            'test_manager']['username'],
                        'admin_password': self._test_config[
                            'test_manager']['password'],
                    },
                },
                'sanity': {'skip_sanity': True},
            }
            self.install_config = copy.deepcopy(self.basic_install_config)
        self._create_conn_script()

    def _create_conn_script(self):
        script_path = self._tmpdir_base / '{prefix}_{index}'.format(
            prefix='rdp' if self.windows else 'ssh',
            index=self.server_index)
        if self.windows:
            script_content = (
                "xfreerdp /u:{user} /p:'{password}' "
                '/w:1366 /h:768 /v:{addr}'
            ).format(
                user=self.username,
                password=self.password,
                addr=self.ip_address,
            )
        else:
            # Don't check this call- it might fail due to missing known_hosts
            # file or similar, and we shouldn't fail the test because of that.
            subprocess.call(['ssh-keygen', '-R', self.ip_address])
            script_content = (
                'ssh -i {key} -o StrictHostKeyChecking=no {connstr} ${{*}}\n'
            ).format(
                key=self._ssh_key.private_key_path,
                connstr='{}@{}'.<EMAIL>(self.username, self.ip_address),
            )
        with open(script_path, 'w') as fh:
            fh.write(script_content)
        subprocess.check_call(['chmod', '+x', script_path])

    def prepare_for_windows(self):
        """Prepare this VM to be created as a windows VM."""
        add_firewall_cmd = "&netsh advfirewall firewall add rule"
        password = ''.join(random.choice(string.ascii_letters + string.digits)
                           for _ in range(16))
        # To meet complexity requirements- the above should be hard enough to<commit_msg>Add log_action on nodes<commit_after>from contextlib import contextmanager
import copy
from datetime import datetime
import functools
import hashlib
import json
import os
import random
import re
import string
import socket
import subprocess
import sys
import time
import uuid
import warnings
import yaml

with warnings.catch_warnings():
    # Fabric maintenance is lagging a bit so let's suppress these warnings.
    warnings.filterwarnings("ignore", category=DeprecationWarning)
    from fabric import Connection
from ipaddress import ip_address, ip_network
from paramiko.ssh_exception import NoValidConnectionsError, SSHException
import requests
import retrying
import textwrap
import winrm

from cloudify_rest_client.exceptions import CloudifyClientError

from cosmo_tester.framework import util
from cosmo_tester.framework.constants import CLOUDIFY_TENANT_HEADER

HEALTHY_STATE = 'OK'
RSYNC_LOCATIONS = ['/etc',
                   '/opt',
                   '/var',
                   '/usr']


def only_manager(func):
    @functools.wraps(func)
    def wrapped(self, *args, **kwargs):
        if not self.is_manager:
            raise RuntimeError('This is not a manager')
        return func(self, *args, **kwargs)
    return wrapped


class VM(object):
    def __init__(self, image_type, test_config, bootstrappable=False):
        self.image_name = None
        self.userdata = ""
        self.username = None
        self.password = <PASSWORD>
        self.api_ca_path = None
        self.enable_ssh_wait = True
        self.should_finalize = True
        self.restservice_expected = False
        self.client = None
        self._test_config = test_config
        self.windows = 'windows' in image_type
        self._tmpdir_base = None
        self.bootstrappable = bootstrappable
        self.image_type = image_type
        self.is_manager = self._is_manager_image_type()
        self.reboot_required = False
        self._set_image_details()
        if self.windows:
            self.prepare_for_windows()

    def assign(
            self,
            public_ip_address,
            private_ip_address,
            networks,
            ssh_key,
            logger,
            tmpdir,
            node_instance_id,
            deployment_id,
            server_id,
            server_index,
    ):
        self.ip_address = public_ip_address
        self.private_ip_address = private_ip_address
        self._ssh_key = ssh_key
        self._logger = logger
        self._tmpdir_base = tmpdir
        self._tmpdir = os.path.join(tmpdir, public_ip_address)
        os.makedirs(self._tmpdir)
        self.node_instance_id = node_instance_id
        self.deployment_id = deployment_id
        self.server_id = server_id
        # This is overridden in some cluster tests
        self.friendly_name = self.server_id
        self.server_index = server_index
        if self.is_manager:
            self.networks = networks
            self.basic_install_config = {
                'manager': {
                    'public_ip': str(public_ip_address),
                    'private_ip': str(private_ip_address),
                    'hostname': str(server_id),
                    'security': {
                        'admin_username': self._test_config[
                            'test_manager']['username'],
                        'admin_password': self._test_config[
                            'test_manager']['password'],
                    },
                },
                'sanity': {'skip_sanity': True},
            }
            self.install_config = copy.deepcopy(self.basic_install_config)
        self._create_conn_script()

    def _create_conn_script(self):
        script_path = self._tmpdir_base / '{prefix}_{index}'.format(
            prefix='rdp' if self.windows else 'ssh',
            index=self.server_index)
        if self.windows:
            script_content = (
                "xfreerdp /u:{user} /p:'{password}' "
                '/w:1366 /h:768 /v:{addr}'
            ).format(
                user=self.username,
                password=<PASSWORD>,
                addr=self.ip_address,
            )
        else:
            # Don't check this call- it might fail due to missing known_hosts
            # file or similar, and we shouldn't fail the test because of that.
            subprocess.call(['ssh-keygen', '-R', self.ip_address])
            script_content = (
                'ssh -i {key} -o StrictHostKeyChecking=no {connstr} ${{*}}\n'
            ).format(
                key=self._ssh_key.private_key_path,
                connstr='{}@{}'.format(self.username, self.ip_address),
            )
        with open(script_path, 'w') as fh:
            fh.write(script_content)
        subprocess.check_call(['chmod', '+x', script_path])

    def log_action(self, action):
        """Log that we're doing something with this node."""
        self._logger.info('%s on %s', action, self.friendly_name)

    def prepare_for_windows(self):
        """Prepare this VM to be created as a windows VM."""
        add_firewall_cmd = "&netsh advfirewall firewall add rule"
        password = ''.join(random.choice(string.ascii_letters + string.digits)
                           for _ in range(16))
        # To meet complexity requirements- the above should be hard enough to
{'fraction_non_alphanumeric': 0.0865041903477507, 'fraction_numerical': 0.0017339370002889896, 'mean_word_length': 2.0241771045732597, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '1349060', 'n_tokens_mistral': 2925, 'n_tokens_neox': 2827, 'n_words': 681}