Example #1
0
    def backup(self, database_path="/tmp/evm_db.backup"):
        """Backup VMDB database
        Changed from Rake task due to a bug in 5.9

        """
        from cfme.utils.appliance import ApplianceException
        self.logger.info('Backing up database')
        result = self.appliance.ssh_client.run_command(
            'pg_dump --format custom --file {} vmdb_production'.format(
                database_path))
        if result.failed:
            msg = 'Failed to backup database'
            self.logger.error(msg)
            raise ApplianceException(msg)
Example #2
0
    def restore(self, database_path="/tmp/evm_db.backup"):
        """Restore VMDB database

        """
        from cfme.utils.appliance import ApplianceException
        self.logger.info('Restoring database')
        result = self.appliance.ssh_client.run_rake_command(
            f'evm:db:restore:local --trace -- --local-file "{database_path}"')
        if result.failed:
            msg = 'Failed to restore database on appl {}, output is {}'.format(
                self.address, result.output)
            self.logger.error(msg)
            raise ApplianceException(msg)
        result = self.ssh_client.run_command(
            "fix_auth --databaseyml -i {}".format(
                conf.credentials["database"].password),
            timeout=45,
        )
        if result.failed:
            self.logger.error(
                f"Failed to change invalid db password: {result.output}")
Example #3
0
    def enable_external(self,
                        db_address,
                        region=0,
                        db_name=None,
                        db_username=None,
                        db_password=None,
                        key_address=None):
        """Enables external database

        Args:
            db_address: Address of the external database
            region: Number of region to join
            db_name: Name of the external DB
            db_username: Username to access the external DB
            db_password: Password to access the external DB
            key_address: Address of the host from which to get the key

        Returns a tuple of (exitstatus, script_output) for reporting, if desired
        """
        self.logger.info(
            'Enabling external DB (db_address {}, region {}) on {}.'.format(
                db_address, region, self.appliance.hostname))

        # default
        db_name = db_name or 'vmdb_production'
        db_username = db_username or conf.credentials['database']['username']
        db_password = db_password or conf.credentials['database']['password']

        appliance_client = self.appliance.ssh_client

        if self.appliance.has_cli:

            if not appliance_client.is_pod:
                # copy v2 key
                rand_filename = f"/tmp/v2_key_{fauxfactory.gen_alphanumeric()}"
                master_client = appliance_client(hostname=key_address)
                master_client.get_file("/var/www/miq/vmdb/certs/v2_key",
                                       rand_filename)
                appliance_client.put_file(rand_filename,
                                          "/var/www/miq/vmdb/certs/v2_key")

            # enable external DB with cli
            cmd = (
                f'appliance_console_cli --hostname {db_address}'
                f' --dbname {db_name} --username {db_username} --password {db_password}'
            )
            result = appliance_client.run_command(cmd)
        else:
            # no cli, use the enable external db script
            # TODO: add key_address
            rbt_repl = {
                'miq_lib': '/var/www/miq/lib',
                'host': db_address,
                'region': region,
                'database': db_name,
                'username': db_username,
                'password': db_password
            }

            # Find and load our rb template with replacements
            rbt = datafile.data_path_for_filename('enable-internal-db.rbt',
                                                  scripts_path.strpath)
            rb = datafile.load_data_file(rbt, rbt_repl)

            # Init SSH client and sent rb file over to /tmp
            remote_file = f'/tmp/{fauxfactory.gen_alphanumeric()}'
            appliance_client.put_file(rb.name, remote_file)

            # Run the rb script, clean it up when done
            result = appliance_client.run_command(f'ruby {remote_file}')
            appliance_client.run_command(f'rm {remote_file}')

        if result.failed:
            self.logger.error('error enabling external db')
            self.logger.error(result.output)
            msg = ('Appliance {} failed to enable external DB running on {}'.
                   format(self.appliance.hostname, db_address))
            self.logger.error(msg)
            from cfme.utils.appliance import ApplianceException
            raise ApplianceException(msg)

        return result.rc, result.output
Example #4
0
    def enable_external(self,
                        db_address,
                        region=0,
                        db_name=None,
                        db_username=None,
                        db_password=None):
        """Enables external database

        Args:
            db_address: Address of the external database
            region: Number of region to join
            db_name: Name of the external DB
            db_username: Username to access the external DB
            db_password: Password to access the external DB

        Returns a tuple of (exitstatus, script_output) for reporting, if desired
        """
        self.logger.info(
            'Enabling external DB (db_address {}, region {}) on {}.'.format(
                db_address, region, self.address))
        # reset the db address and clear the cached db object if we have one
        self.address = db_address
        clear_property_cache(self, 'client')

        # default
        db_name = db_name or 'vmdb_production'
        db_username = db_username or conf.credentials['database']['username']
        db_password = db_password or conf.credentials['database']['password']

        client = self.ssh_client

        if self.appliance.has_cli:

            if not client.is_pod:
                # copy v2 key
                master_client = client(hostname=self.address)
                rand_filename = "/tmp/v2_key_{}".format(
                    fauxfactory.gen_alphanumeric())
                master_client.get_file("/var/www/miq/vmdb/certs/v2_key",
                                       rand_filename)
                client.put_file(rand_filename,
                                "/var/www/miq/vmdb/certs/v2_key")

            # enable external DB with cli
            result = client.run_command(
                'appliance_console_cli '
                '--hostname {0} --region {1} --dbname {2} --username {3} --password {4}'
                .format(self.address, region, db_name, db_username,
                        db_password))
        else:
            # no cli, use the enable external db script
            rbt_repl = {
                'miq_lib': '/var/www/miq/lib',
                'host': self.address,
                'region': region,
                'database': db_name,
                'username': db_username,
                'password': db_password
            }

            # Find and load our rb template with replacements
            rbt = datafile.data_path_for_filename('enable-internal-db.rbt',
                                                  scripts_path.strpath)
            rb = datafile.load_data_file(rbt, rbt_repl)

            # Init SSH client and sent rb file over to /tmp
            remote_file = '/tmp/{}'.format(fauxfactory.gen_alphanumeric())
            client.put_file(rb.name, remote_file)

            # Run the rb script, clean it up when done
            result = client.run_command('ruby {}'.format(remote_file))
            client.run_command('rm {}'.format(remote_file))

        if result.failed:
            self.logger.error('error enabling external db')
            self.logger.error(result.output)
            msg = ('Appliance {} failed to enable external DB running on {}'.
                   format(self.appliance.hostname, db_address))
            self.logger.error(msg)
            from cfme.utils.appliance import ApplianceException
            raise ApplianceException(msg)

        return result.rc, result.output