Ejemplo n.º 1
0
def set_yaml_config(config_name, data_dict, hostname=None):
    """Given a yaml name, dictionary and hostname, set the configuration yaml on the server

    The configuration yamls must be inserted into the DB using the ruby console, so this function
    uses SSH, not the database. It makes sense to be included here as a counterpart to
    :py:func:`get_yaml_config`

    Args:
        config_name: Name of the yaml configuration file
        data_dict: Dictionary with data to set/change
        hostname: Hostname/address of the server that we want to set up (default ``None``)

    Note:
        If hostname is set to ``None``, the default server set up for this session will be
        used. See :py:class:``utils.ssh.SSHClient`` for details of the default setup.

    Warning:

        Manually editing the config yamls is potentially dangerous. Furthermore,
        the rails runner doesn't return useful information on the outcome of the
        set request, so errors that arise from the newly loading config file
        will go unreported.

    Usage:

        # Update the appliance name, for example
        vmbd_yaml = get_yaml_config('vmdb')
        vmdb_yaml['server']['name'] = 'EVM IS AWESOME'
        set_yaml_config('vmdb', vmdb_yaml, '1.2.3.4')

    """
    # CFME does a lot of things when loading a configfile, so
    # let their native conf loader handle the job
    # If hostname is defined, connect to the specified server
    if hostname is not None:
        _ssh_client = SSHClient(hostname=hostname)
    # Else, connect to the default one set up for this session
    else:
        _ssh_client = store.current_appliance.ssh_client
    # Build & send new config
    temp_yaml = NamedTemporaryFile()
    dest_yaml = '/tmp/conf.yaml'
    yaml.dump(data_dict, temp_yaml, default_flow_style=False)
    _ssh_client.put_file(temp_yaml.name, dest_yaml)
    # Build and send ruby script
    dest_ruby = '/tmp/load_conf.rb'
    ruby_template = data_path.join('utils', 'cfmedb_load_config.rbt')
    ruby_replacements = {
        'config_name': config_name,
        'config_file': dest_yaml
    }
    temp_ruby = load_data_file(ruby_template.strpath, ruby_replacements)
    _ssh_client.put_file(temp_ruby.name, dest_ruby)

    # Run it
    _ssh_client.run_rails_command(dest_ruby)
    fire('server_details_changed')
    fire('server_config_changed')
Ejemplo n.º 2
0
def set_default_domain():
    if current_version() < "5.3":
        return  # Domains are not in 5.2.x and lower
    ssh_client = SSHClient()
    # The command ignores the case when the Default domain is not present (: true)
    result = ssh_client.run_rails_command(
        "\"d = MiqAeDomain.where :name => 'Default'; puts (d) ? d.first.enabled : true\"")
    if result.output.lower().strip() != "true":
        # Re-enable the domain
        ssh_client.run_rails_command(
            "\"d = MiqAeDomain.where :name => 'Default'; d = d.first; d.enabled = true; d.save!\"")
Ejemplo n.º 3
0
class UiCoveragePlugin(object):
    def __init__(self):
        self.ssh_client = SSHClient()

    # trylast so that terminalreporter's been configured before ui-coverage
    @pytest.mark.trylast
    def pytest_configure(self, config):
        # Eventually, the setup/teardown work for coverage should be handled by
        # utils.appliance.Appliance to make multi-appliance support easy
        self.reporter = config.pluginmanager.getplugin('terminalreporter')
        self.reporter.write_sep('-', 'Setting up UI coverage reporting')
        self.install_simplecov()
        self.install_coverage_hook()
        self.restart_evm()
        self.touch_all_the_things()
        check_appliance_ui(base_url())

    def pytest_unconfigure(self, config):
        self.reporter.write_sep(
            '-', 'Waiting for coverage to finish and collecting reports')
        self.stop_touching_all_the_things()
        self.merge_reports()
        self.collect_reports()
        self.print_report()

    def install_simplecov(self):
        logger.info('Installing coverage gems on appliance')
        self.ssh_client.put_file(gemfile.strpath, rails_root.strpath)
        x, out = self.ssh_client.run_command(
            'cd {}; bundle'.format(rails_root))
        return x == 0

    def install_coverage_hook(self):
        logger.info('Installing coverage hook on appliance')
        # Put the coverage hook in the miq lib path
        self.ssh_client.put_file(
            coverage_hook.strpath,
            rails_root.join('..', 'lib', coverage_hook.basename).strpath)
        replacements = {
            'require': r"require 'coverage_hook'",
            'config': rails_root.join('config').strpath
        }
        # grep/echo to try to add the require line only once
        # This goes in preinitializer after the miq lib path is set up,
        # which makes it so ruby can actually require the hook
        command_template = (
            'cd {config};'
            'grep -q "{require}" preinitializer.rb || echo -e "\\n{require}" >> preinitializer.rb'
        )
        x, out = self.ssh_client.run_command(
            command_template.format(**replacements))
        return x == 0

    def restart_evm(self, rude=True):
        logger.info('Restarting EVM to enable coverage reporting')
        # This is rude by default (issuing a kill -9 on ruby procs), since the most common use-case
        # will be to set up coverage on a freshly provisioned appliance in a jenkins run
        if rude:
            x, out = self.ssh_client.run_command(
                'killall -9 ruby; service evmserverd start')
        else:
            x, out = self.ssh_client.run_comment('service evmserverd restart')
        return x == 0

    def touch_all_the_things(self):
        logger.info(
            'Establishing baseline overage by requiring ALL THE THINGS')
        # send over the thing toucher
        self.ssh_client.put_file(
            thing_toucher.strpath,
            rails_root.join(thing_toucher.basename).strpath)
        # start it in an async process so we can go one testing while this takes place
        self._thing_toucher_proc = Process(target=_thing_toucher_mp_handler,
                                           args=[self.ssh_client])
        self._thing_toucher_proc.start()

    def stop_touching_all_the_things(self):
        logger.info('Waiting for baseline coverage generator to finish')
        # block while the thing toucher is still running
        self._thing_toucher_proc.join()
        return self._thing_toucher_proc.exitcode == 0

    def merge_reports(self):
        logger.info("Merging coverage reports on appliance")
        # install the merger script
        self.ssh_client.put_file(
            coverage_merger.strpath,
            rails_root.join(coverage_merger.basename).strpath)
        # don't async this one since it's happening in unconfigure
        # merge/clean up the coverage reports
        x, out = self.ssh_client.run_rails_command('coverage_merger.rb')
        return x == 0

    def collect_reports(self):
        coverage_dir = log_path.join('coverage')
        # clean out old coverage dir if it exists
        if coverage_dir.check():
            coverage_dir.remove(rec=True, ignore_errors=True)
        # Then ensure the the empty dir exists
        coverage_dir.ensure(dir=True)
        # then copy the remote coverage dir into it
        logger.info("Collecting coverage reports to {}".format(
            coverage_dir.strpath))
        logger.info("Report collection can take several minutes")
        self.ssh_client.get_file(rails_root.join('coverage').strpath,
                                 log_path.strpath,
                                 recursive=True)

    def print_report(self):
        try:
            last_run = json.load(
                log_path.join('coverage', '.last_run.json').open())
            coverage = last_run['result']['covered_percent']
            # TODO: Make the happy vs. sad coverage color configurable, and set it to something
            # good once we know what good is
            style = {'bold': True}
            if coverage > 40:
                style['green'] = True
            else:
                style['red'] = True
            self.reporter.line('UI Coverage Result: {}%'.format(coverage),
                               **style)
        except KeyboardInterrupt:
            # don't block this, so users can cancel out
            raise
        except:
            logger.error(
                'Error printing coverage report to terminal, traceback follows'
            )
            logger.error(traceback.format_exc())
Ejemplo n.º 4
0
class UiCoveragePlugin(object):
    def __init__(self):
        self.ssh_client = SSHClient()

    # trylast so that terminalreporter's been configured before ui-coverage
    @pytest.mark.trylast
    def pytest_configure(self, config):
        # Eventually, the setup/teardown work for coverage should be handled by
        # utils.appliance.Appliance to make multi-appliance support easy
        self.reporter = config.pluginmanager.getplugin('terminalreporter')
        self.reporter.write_sep('-', 'Setting up UI coverage reporting')
        self.install_simplecov()
        self.install_coverage_hook()
        self.restart_evm()
        self.touch_all_the_things()
        check_appliance_ui(base_url())

    def pytest_unconfigure(self, config):
        self.reporter.write_sep('-', 'Waiting for coverage to finish and collecting reports')
        self.stop_touching_all_the_things()
        self.merge_reports()
        self.collect_reports()
        self.print_report()

    def install_simplecov(self):
        logger.info('Installing coverage gems on appliance')
        self.ssh_client.put_file(gemfile.strpath, rails_root.strpath)
        x, out = self.ssh_client.run_command('cd {}; bundle'.format(rails_root))
        return x == 0

    def install_coverage_hook(self):
        logger.info('Installing coverage hook on appliance')
        # Put the coverage hook in the miq lib path
        self.ssh_client.put_file(
            coverage_hook.strpath, rails_root.join('..', 'lib', coverage_hook.basename).strpath
        )
        replacements = {
            'require': r"require 'coverage_hook'",
            'config': rails_root.join('config').strpath
        }
        # grep/echo to try to add the require line only once
        # This goes in preinitializer after the miq lib path is set up,
        # which makes it so ruby can actually require the hook
        command_template = (
            'cd {config};'
            'grep -q "{require}" preinitializer.rb || echo -e "\\n{require}" >> preinitializer.rb'
        )
        x, out = self.ssh_client.run_command(command_template.format(**replacements))
        return x == 0

    def restart_evm(self, rude=True):
        logger.info('Restarting EVM to enable coverage reporting')
        # This is rude by default (issuing a kill -9 on ruby procs), since the most common use-case
        # will be to set up coverage on a freshly provisioned appliance in a jenkins run
        if rude:
            x, out = self.ssh_client.run_command('killall -9 ruby; service evmserverd start')
        else:
            x, out = self.ssh_client.run_comment('service evmserverd restart')
        return x == 0

    def touch_all_the_things(self):
        logger.info('Establishing baseline overage by requiring ALL THE THINGS')
        # send over the thing toucher
        self.ssh_client.put_file(
            thing_toucher.strpath, rails_root.join(thing_toucher.basename).strpath
        )
        # start it in an async process so we can go one testing while this takes place
        self._thing_toucher_proc = Process(target=_thing_toucher_mp_handler, args=[self.ssh_client])
        self._thing_toucher_proc.start()

    def stop_touching_all_the_things(self):
        logger.info('Waiting for baseline coverage generator to finish')
        # block while the thing toucher is still running
        self._thing_toucher_proc.join()
        return self._thing_toucher_proc.exitcode == 0

    def merge_reports(self):
        logger.info("Merging coverage reports on appliance")
        # install the merger script
        self.ssh_client.put_file(
            coverage_merger.strpath, rails_root.join(coverage_merger.basename).strpath
        )
        # don't async this one since it's happening in unconfigure
        # merge/clean up the coverage reports
        x, out = self.ssh_client.run_rails_command('coverage_merger.rb')
        return x == 0

    def collect_reports(self):
        coverage_dir = log_path.join('coverage')
        # clean out old coverage dir if it exists
        if coverage_dir.check():
            coverage_dir.remove(rec=True, ignore_errors=True)
        # Then ensure the the empty dir exists
        coverage_dir.ensure(dir=True)
        # then copy the remote coverage dir into it
        logger.info("Collecting coverage reports to {}".format(coverage_dir.strpath))
        logger.info("Report collection can take several minutes")
        self.ssh_client.get_file(
            rails_root.join('coverage').strpath,
            log_path.strpath,
            recursive=True
        )

    def print_report(self):
        try:
            last_run = json.load(log_path.join('coverage', '.last_run.json').open())
            coverage = last_run['result']['covered_percent']
            # TODO: Make the happy vs. sad coverage color configurable, and set it to something
            # good once we know what good is
            style = {'bold': True}
            if coverage > 40:
                style['green'] = True
            else:
                style['red'] = True
            self.reporter.line('UI Coverage Result: {}%'.format(coverage), **style)
        except KeyboardInterrupt:
            # don't block this, so users can cancel out
            raise
        except:
            logger.error('Error printing coverage report to terminal, traceback follows')
            logger.error(traceback.format_exc())