def stop_db(self, update_db=False, do_not_start_on_reboot=False): """Stop the database.""" LOG.info(_("Stopping Vertica.")) if do_not_start_on_reboot: self._disable_db_on_boot() try: # Stop vertica-agent service command = (system.VERTICA_AGENT_SERVICE_COMMAND % "stop") system.shell_execute(command) # Using Vertica adminTools to stop db. db_password = self._get_database_password() stop_db_command = (system.STOP_DB % (DB_NAME, db_password)) out, err = system.shell_execute(system.STATUS_ACTIVE_DB, system.VERTICA_ADMIN) if out.strip() == DB_NAME: system.shell_execute(stop_db_command, system.VERTICA_ADMIN) if not self.status._is_restarting: if not self.status.wait_for_real_status_to_change_to( rd_instance.ServiceStatuses.SHUTDOWN, self.state_change_wait_time, update_db): LOG.error(_("Could not stop Vertica.")) self.status.end_restart() raise RuntimeError("Could not stop Vertica!") LOG.debug("Database stopped.") else: LOG.debug("Database is not running.") except exception.ProcessExecutionError: LOG.exception(_("Failed to stop database.")) raise RuntimeError("Could not stop database.")
def authorize_public_keys(self, user, public_keys): """Adds public key to authorized_keys for user.""" LOG.debug("public keys to be added for user: %s." % (user)) user_home_directory = os.path.expanduser('~' + user) authorized_file_name = user_home_directory + '/.ssh/authorized_keys' try: read_key_cmd = ("cat %(file)s" % {'file': authorized_file_name}) out, err = system.shell_execute(read_key_cmd) public_keys.append(out.strip()) except exception.ProcessExecutionError: LOG.debug("Cannot read authorized_keys.") all_keys = '\n'.join(public_keys) + "\n" try: with tempfile.NamedTemporaryFile(delete=False) as tempkeyfile: tempkeyfile.write(all_keys) copy_key_cmd = (("install -o %(user)s -m 600 %(source)s %(target)s" ) % {'user': user, 'source': tempkeyfile.name, 'target': authorized_file_name}) system.shell_execute(copy_key_cmd) os.remove(tempkeyfile.name) except exception.ProcessExecutionError: LOG.exception(_("Cannot install public keys.")) os.remove(tempkeyfile.name) raise
def authorize_public_keys(self, user, public_keys): """Adds public key to authorized_keys for user.""" LOG.debug("public keys to be added for user: %s." % (user)) user_home_directory = os.path.expanduser('~' + user) authorized_file_name = user_home_directory + '/.ssh/authorized_keys' try: read_key_cmd = ("cat %(file)s" % {'file': authorized_file_name}) out, err = system.shell_execute(read_key_cmd) public_keys.append(out.strip()) except exception.ProcessExecutionError: LOG.debug("Cannot read authorized_keys.") all_keys = '\n'.join(public_keys) + "\n" try: with tempfile.NamedTemporaryFile(delete=False) as tempkeyfile: tempkeyfile.write(all_keys) copy_key_cmd = ( ("install -o %(user)s -m 600 %(source)s %(target)s") % { 'user': user, 'source': tempkeyfile.name, 'target': authorized_file_name }) system.shell_execute(copy_key_cmd) os.remove(tempkeyfile.name) except exception.ProcessExecutionError: LOG.exception(_("Cannot install public keys.")) os.remove(tempkeyfile.name) raise
def _disable_db_on_boot(self): try: command = (system.SET_RESTART_POLICY % (DB_NAME, "never")) system.shell_execute(command, system.VERTICA_ADMIN) command = (system.VERTICA_AGENT_SERVICE_COMMAND % "disable") system.shell_execute(command) except exception.ProcessExecutionError: LOG.exception(_("Failed to disable db on boot.")) raise RuntimeError("Could not disable db on boot.")
def _export_conf_to_members(self, members): """This method exports conf files to other members.""" try: for member in members: COPY_CMD = (system.SEND_CONF_TO_SERVER % (system.VERTICA_CONF, member, system.VERTICA_CONF)) system.shell_execute(COPY_CMD) except exception.ProcessExecutionError: LOG.exception(_("Cannot export configuration.")) raise
def update_vertica(self, command, members=netutils.get_my_ipv4()): LOG.info(_("Calling update_vertica with command %s") % command) try: update_vertica_cmd = (system.UPDATE_VERTICA % (command, members, MOUNT_POINT)) system.shell_execute(update_vertica_cmd) except exception.ProcessExecutionError: LOG.exception(_("update_vertica failed.")) raise RuntimeError(_("update_vertica failed.")) # self._generate_database_password() LOG.info(_("update_vertica completed."))
def install_vertica(self, members=netutils.get_my_ipv4()): """Prepare the guest machine with a Vertica db creation.""" LOG.info(_("Installing Vertica Server.")) try: # Create db after install install_vertica_cmd = (system.INSTALL_VERTICA % (members, MOUNT_POINT)) system.shell_execute(install_vertica_cmd) except exception.ProcessExecutionError: LOG.exception(_("install_vertica failed.")) raise RuntimeError(_("install_vertica failed.")) self._generate_database_password() LOG.info(_("install_vertica completed."))
def create_db(self, members=netutils.get_my_ipv4()): """Prepare the guest machine with a Vertica db creation.""" LOG.info(_("Creating database on Vertica host.")) try: # Create db after install db_password = self._get_database_password() create_db_command = (system.CREATE_DB % (members, DB_NAME, MOUNT_POINT, MOUNT_POINT, db_password)) system.shell_execute(create_db_command, system.VERTICA_ADMIN) except Exception: LOG.exception(_("Vertica database create failed.")) raise RuntimeError(_("Vertica database create failed.")) LOG.info(_("Vertica database create completed."))
def create_db(self, members=netutils.get_my_ipv4()): """Prepare the guest machine with a Vertica db creation.""" LOG.info(_("Creating database on Vertica host.")) try: # Create db after install db_password = self._get_database_password() create_db_command = ( system.CREATE_DB % (members, DB_NAME, MOUNT_POINT, MOUNT_POINT, db_password)) system.shell_execute(create_db_command, system.VERTICA_ADMIN) except Exception: LOG.exception(_("Vertica database create failed.")) raise RuntimeError(_("Vertica database create failed.")) LOG.info(_("Vertica database create completed."))
def prepare_for_install_vertica(self): """This method executes preparatory methods before executing install_vertica. """ command = ("VERT_DBA_USR=%s VERT_DBA_HOME=/home/dbadmin " "VERT_DBA_GRP=%s /opt/vertica/oss/python/bin/python" " -m vertica.local_coerce" % (system.VERTICA_ADMIN, system.VERTICA_ADMIN_GRP)) try: self._set_readahead_for_disks() system.shell_execute(command) except exception.ProcessExecutionError: LOG.exception(_("Failed to prepare for install_vertica.")) raise
def add_db_to_node(self, members=netutils.get_my_ipv4()): """Add db to host with admintools""" LOG.info(_("Calling admintools to add DB to host")) try: # Create db after install db_password = self._get_database_password() create_db_command = (system.ADD_DB_TO_NODE % (members, DB_NAME, db_password)) system.shell_execute(create_db_command, "dbadmin") except exception.ProcessExecutionError: # Give vertica some time to get the the node up, won't be available # by the time adminTools -t db_add_node completes LOG.info(_("adminTools failed as expected - wait for node")) self.wait_for_node_status() LOG.info(_("Vertica add db to host completed."))
def write_config(self, config, unlink_function=os.unlink, temp_function=tempfile.NamedTemporaryFile): """Write the configuration contents to vertica.cnf file.""" LOG.debug('Defining config holder at %s.' % system.VERTICA_CONF) tempfile = temp_function(delete=False) try: config.write(tempfile) tempfile.close() command = (("install -o root -g root -m 644 %(source)s %(target)s" ) % {'source': tempfile.name, 'target': system.VERTICA_CONF}) system.shell_execute(command) unlink_function(tempfile.name) except Exception: unlink_function(tempfile.name) raise
def remove_db_from_node(self, members=netutils.get_my_ipv4()): """Remove db from node with admintools""" LOG.info(_("Removing db from node")) try: # Create db after install db_password = self._get_database_password() create_db_command = (system.REMOVE_DB_FROM_NODE % (members, DB_NAME, db_password)) system.shell_execute(create_db_command, "dbadmin") except exception.ProcessExecutionError: # Give vertica some time to get the the node up, won't be available # by the time adminTools -t db_add_node completes LOG.info(_("adminTools failed as expected - wait for node")) # Give vertica some time to take the node down - it won't be available # by the time adminTools -t db_add_node completes self.wait_for_node_status() LOG.info(_("Vertica remove host from db completed."))
def get_public_keys(self, user): """Generates key (if not found), and sends public key for user.""" LOG.debug("Public keys requested for user: %s." % user) user_home_directory = os.path.expanduser('~' + user) public_key_file_name = user_home_directory + '/.ssh/id_rsa.pub' try: key_generate_command = (system.SSH_KEY_GEN % user_home_directory) system.shell_execute(key_generate_command, user) except exception.ProcessExecutionError: LOG.debug("Cannot generate key.") try: read_key_cmd = ("cat %(file)s" % {'file': public_key_file_name}) out, err = system.shell_execute(read_key_cmd) except exception.ProcessExecutionError: LOG.exception(_("Cannot read public key.")) raise return out.strip()
def write_config(self, config, unlink_function=os.unlink, temp_function=tempfile.NamedTemporaryFile): """Write the configuration contents to vertica.cnf file.""" LOG.debug('Defining config holder at %s.' % system.VERTICA_CONF) tempfile = temp_function(delete=False) try: config.write(tempfile) tempfile.close() command = ( ("install -o root -g root -m 644 %(source)s %(target)s") % { 'source': tempfile.name, 'target': system.VERTICA_CONF }) system.shell_execute(command) unlink_function(tempfile.name) except Exception: unlink_function(tempfile.name) raise
def is_root_enabled(self): """Return True if root access is enabled else False.""" LOG.debug("Checking is root enabled.") try: out, err = system.shell_execute( system.USER_EXISTS % (self._get_database_password(), 'root'), system.VERTICA_ADMIN) if err: LOG.error(err) raise RuntimeError(_("Failed to query for root user.")) except exception.ProcessExecutionError: raise RuntimeError(_("Failed to query for root user.")) return out.rstrip() == "1"
def is_root_enabled(self): """Return True if root access is enabled else False.""" LOG.debug("Checking is root enabled.") try: out, err = system.shell_execute(system.USER_EXISTS % (self._get_database_password(), 'root'), system.VERTICA_ADMIN) if err: LOG.error(err) raise RuntimeError(_("Failed to query for root user.")) except exception.ProcessExecutionError: raise RuntimeError(_("Failed to query for root user.")) return out.rstrip() == "1"
def _get_actual_db_status(self): """Get the status of dbaas and report it back.""" try: out, err = system.shell_execute(system.STATUS_ACTIVE_DB, system.VERTICA_ADMIN) if out.strip() == DB_NAME: # UP status is confirmed LOG.info(_("Service Status is RUNNING.")) return rd_instance.ServiceStatuses.RUNNING else: LOG.info(_("Service Status is SHUTDOWN.")) return rd_instance.ServiceStatuses.SHUTDOWN except exception.ProcessExecutionError: LOG.exception(_("Failed to get database status.")) return rd_instance.ServiceStatuses.CRASHED