예제 #1
0
 def read_module_results(cls, is_admin=False, include_contents=False):
     """Read all the module results on the guest and return a list
     of them.
     """
     results = []
     pattern = cls.MODULE_RESULT_FILENAME
     result_files = operating_system.list_files_in_directory(
         cls.MODULE_BASE_DIR, recursive=True, pattern=pattern)
     for result_file in result_files:
         result = cls.read_module_result(result_file)
         if (not result.get('removed')
                 and (is_admin or result.get('visible'))):
             if include_contents:
                 codec = stream_codecs.Base64Codec()
                 if not is_admin and result.get('admin_only'):
                     contents = (
                         "Must be admin to retrieve contents for module %s"
                         % result.get('name', 'Unknown'))
                     result['contents'] = codec.serialize(contents)
                 else:
                     contents_dir = os.path.dirname(result_file)
                     contents_file = cls.build_contents_filename(
                         contents_dir)
                     result['contents'] = operating_system.read_file(
                         contents_file, codec=codec, decode=False)
             results.append(result)
     return results
예제 #2
0
 def write_module_contents(cls, module_dir, contents, md5):
     contents_file = cls.build_contents_filename(module_dir)
     operating_system.write_file(contents_file,
                                 contents,
                                 codec=stream_codecs.Base64Codec(),
                                 encode=False)
     return contents_file
예제 #3
0
    def get_master_ref(self, service, snapshot_info):
        """Capture information from a master node"""
        pfile = '/tmp/init%s_stby.ora' % self._get_config().db_name
        pwfile = ('%(ora_home)s/dbs/orapw%(db_name)s' % {
            'ora_home': CONF.get(MANAGER).oracle_home,
            'db_name': self._get_config().db_name
        })
        ctlfile = '/tmp/%s_stby.ctl' % self._get_config().db_name
        oratabfile = '/etc/oratab'
        oracnffile = CONF.get(MANAGER).conf_file
        datafile = '/tmp/oradata.tar.gz'

        def _cleanup_tmp_files():
            operating_system.remove(ctlfile, force=True, as_root=True)
            operating_system.remove(pfile, force=True, as_root=True)
            operating_system.remove(datafile, force=True, as_root=True)

        _cleanup_tmp_files()

        with ora_service.LocalOracleClient(self._get_config().db_name,
                                           service=True) as client:
            client.execute("ALTER DATABASE CREATE STANDBY CONTROLFILE AS "
                           "'%s'" % ctlfile)
            ora_service.OracleAdmin().create_parameter_file(target=pfile,
                                                            client=client)
            q = sql_query.Query()
            q.columns = ["value"]
            q.tables = ["v$parameter"]
            q.where = ["name = 'fal_server'"]
            client.execute(str(q))
            row = client.fetchone()
            db_list = []
            if row is not None and row[0] is not None:
                db_list = str(row[0]).split(",")
            db_list.insert(0, self._get_config().db_name)

        # Create a tar file containing files needed for slave creation
        utils.execute_with_timeout('tar',
                                   '-Pczvf',
                                   datafile,
                                   ctlfile,
                                   pwfile,
                                   pfile,
                                   oratabfile,
                                   oracnffile,
                                   run_as_root=True,
                                   root_helper='sudo')
        oradata_encoded = operating_system.read_file(
            datafile,
            codec=stream_codecs.Base64Codec(),
            as_root=True,
            decode=False)
        _cleanup_tmp_files()
        master_ref = {
            'host': netutils.get_my_ipv4(),
            'db_name': self._get_config().db_name,
            'db_list': db_list,
            'oradata': oradata_encoded,
        }
        return master_ref
예제 #4
0
    def test_serialize_deserialize_base64codec(self):
        random_data = bytearray(Random.new().read(12))
        data = ['abc', 'numbers01234', random_data]

        codec = stream_codecs.Base64Codec()
        for datum in data:
            serialized_data = codec.serialize(datum)
            deserialized_data = codec.deserialize(serialized_data)
            self.assertEqual(datum, deserialized_data,
                             "Serialize/Deserialize failed")
예제 #5
0
    def test_serialize_deserialize_base64codec(self):
        random_data = bytearray(os.urandom(12))
        data = [b'abc',
                b'numbers01234',
                b'non-ascii:\xe9\xff',
                random_data]

        codec = stream_codecs.Base64Codec()
        for datum in data:
            serialized_data = codec.serialize(datum)
            deserialized_data = codec.deserialize(serialized_data)
            self. assertEqual(datum, deserialized_data,
                              "Serialize/Deserialize failed")
예제 #6
0
    def get_master_ref(self, service, snapshot_info):
        """Capture information from a master node"""
        ctlfile = path.join(TMP_DIR,
                            '%s_stby.ctl' % service.admin.database_name)
        datafile = path.join(TMP_DIR, 'oradata.tar.gz')

        def _cleanup_tmp_files():
            operating_system.remove(ctlfile, force=True, as_root=True)
            operating_system.remove(datafile, force=True, as_root=True)

        _cleanup_tmp_files()

        with service.cursor(service.admin.database_name) as cursor:
            cursor.execute(
                str(
                    sql_query.AlterDatabase(
                        "CREATE STANDBY CONTROLFILE AS '%s'" % ctlfile)))
            cursor.execute(
                str(
                    sql_query.Query(columns=['VALUE'],
                                    tables=['V$PARAMETER'],
                                    where=["NAME = 'fal_server'"])))
            row = cursor.fetchone()
            db_list = []
            if row is not None and row[0] is not None:
                db_list = str(row[0]).split(",")
            db_list.insert(0, service.admin.database_name)

        # Create a tar file containing files needed for slave creation
        utils.execute_with_timeout('tar',
                                   '-Pczvf',
                                   datafile,
                                   ctlfile,
                                   service.paths.orapw_file,
                                   service.paths.oratab_file,
                                   CONF.get(MANAGER).conf_file,
                                   run_as_root=True,
                                   root_helper='sudo')
        oradata_encoded = operating_system.read_file(
            datafile,
            codec=stream_codecs.Base64Codec(),
            as_root=True,
            decode=False)
        _cleanup_tmp_files()
        master_ref = {
            'host': netutils.get_my_ipv4(),
            'db_name': service.admin.database_name,
            'db_list': db_list,
            'oradata': oradata_encoded,
        }
        return master_ref
예제 #7
0
    def prepare_slave(self, snapshot):
        """Prepare the environment needed for starting the slave Oracle
        processes.
        """
        master_info = snapshot['master']
        db_name = master_info['db_name']

        tmp_dir = '/tmp'
        tmp_data_path = path.join(tmp_dir, 'oradata.tar.gz')
        orabase_path = CONF.get(MANAGER).oracle_base
        orahome_path = CONF.get(MANAGER).oracle_home
        db_data_path = path.join(orabase_path, 'oradata', db_name)
        fast_recovery_path = path.join(orabase_path, 'fast_recovery_area')
        db_fast_recovery_path = path.join(fast_recovery_path, db_name)
        audit_path = path.join(orabase_path, 'admin', db_name, 'adump')
        admin_path = path.join(orabase_path, 'admin')

        # Create necessary directories and set permissions
        directories = [db_data_path, db_fast_recovery_path, audit_path]
        for directory in directories:
            operating_system.create_directory(directory,
                                              system.ORACLE_INSTANCE_OWNER,
                                              system.ORACLE_GROUP_OWNER,
                                              as_root=True)
        operating_system.chown(fast_recovery_path,
                               system.ORACLE_INSTANCE_OWNER,
                               system.ORACLE_GROUP_OWNER,
                               as_root=True)
        operating_system.chown(admin_path,
                               system.ORACLE_INSTANCE_OWNER,
                               system.ORACLE_GROUP_OWNER,
                               as_root=True)

        # Install on the slave files extracted from the master
        # (e.g. the control, pfile, password, oracle.cnf file ... etc)
        oradata = master_info['oradata']
        operating_system.write_file(tmp_data_path,
                                    oradata,
                                    codec=stream_codecs.Base64Codec())
        utils.execute_with_timeout('tar',
                                   '-Pxzvf',
                                   tmp_data_path,
                                   run_as_root=True,
                                   root_helper='sudo')

        # Put the control file in place
        tmp_ctlfile_path = path.join(tmp_dir, '%s_stby.ctl' % db_name)
        ctlfile1_path = path.join(db_data_path, 'control01.ctl')
        ctlfile2_path = path.join(db_fast_recovery_path, 'control02.ctl')
        operating_system.move(tmp_ctlfile_path, ctlfile1_path, as_root=True)
        operating_system.copy(ctlfile1_path,
                              ctlfile2_path,
                              preserve=True,
                              as_root=True)

        db_unique_name = ('%(db_name)s_%(replica_label)s' % {
            'db_name': db_name,
            'replica_label': utils.generate_random_string(6)
        })

        # Customize the pfile for slave and put it in the right place.
        # The pfile that came from master is owned by the 'oracle' user,
        # so we need to change ownership first before editing it.
        tmp_pfile_path = path.join(tmp_dir, 'init%s_stby.ora' % db_name)
        pfile_path = path.join(orahome_path, 'dbs', 'init%s.ora' % db_name)
        operating_system.chown(tmp_pfile_path,
                               getpass.getuser(),
                               None,
                               as_root=True)
        with open(tmp_pfile_path, 'a') as pfile:
            pfile.write("*.db_unique_name='%s'\n" % db_unique_name)

        # Finished editing pfile, put it in the proper directory and chown
        # back to oracle user and group
        operating_system.move(tmp_pfile_path,
                              pfile_path,
                              force=True,
                              as_root=True)
        operating_system.chown(pfile_path,
                               system.ORACLE_INSTANCE_OWNER,
                               system.ORACLE_GROUP_OWNER,
                               as_root=True)

        self.ORA_CONF.db_name = db_name
        self.ORA_CONF.db_unique_name = db_unique_name

        # Set proper permissions on the oratab file
        operating_system.chown('/etc/oratab',
                               system.ORACLE_INSTANCE_OWNER,
                               system.ORACLE_GROUP_OWNER,
                               as_root=True)

        # Create the listener.ora file
        self._create_lsnr_file()

        # Restart the listener
        utils.execute_with_timeout("sudo",
                                   "su",
                                   "-",
                                   "oracle",
                                   "-c",
                                   "lsnrctl reload",
                                   timeout=CONF.usage_timeout)
예제 #8
0
def decode_data(data):
    return stream_codecs.Base64Codec().deserialize(data)
예제 #9
0
def encode_data(data):
    if isinstance(data, six.text_type):
        data = data.encode('utf-8')
    return stream_codecs.Base64Codec().serialize(data)
예제 #10
0
def encode_data(data):
    # NOTE(zhaochao) No need to encoding string object any more,
    # as Base64Codec is now using oslo_serialization.base64 which
    # could take care of this.
    return stream_codecs.Base64Codec().serialize(data)
예제 #11
0
    def prepare_slave(self, service, snapshot):
        """Prepare the environment needed for starting the slave Oracle
        processes.
        """
        master_info = snapshot['master']
        db_name = master_info['db_name']
        db_unique_name = ('%(db_name)s_%(replica_label)s' % {
            'db_name': db_name,
            'replica_label': utils.generate_random_string(6)
        })
        service.paths.update_db_name(db_name)

        # Create necessary directories and set necessary permissions
        new_dirs = [
            service.paths.db_data_dir, service.paths.db_fast_recovery_logs_dir,
            service.paths.db_fast_recovery_dir, service.paths.audit_dir
        ]
        for directory in new_dirs:
            operating_system.create_directory(directory,
                                              service.instance_owner,
                                              service.instance_owner_group,
                                              as_root=True)

        chown_dirs = [
            service.paths.fast_recovery_area, service.paths.admin_dir
        ]
        for directory in chown_dirs:
            operating_system.chown(directory,
                                   service.instance_owner,
                                   service.instance_owner_group,
                                   as_root=True)

        # Install on the slave files extracted from the master
        # (e.g. the control, pfile, password, oracle.cnf file ... etc)
        oradata_encoded = master_info['oradata']
        tmp_data_path = path.join(TMP_DIR, 'oradata.tar.gz')
        operating_system.write_file(tmp_data_path,
                                    oradata_encoded,
                                    codec=stream_codecs.Base64Codec(),
                                    encode=False)
        utils.execute_with_timeout('tar',
                                   '-Pxzvf',
                                   tmp_data_path,
                                   run_as_root=True,
                                   root_helper='sudo')

        # Put the control file in place
        tmp_ctlfile_path = path.join(TMP_DIR, '%s_stby.ctl' % db_name)
        operating_system.move(tmp_ctlfile_path,
                              service.paths.ctlfile1_file,
                              as_root=True)
        operating_system.copy(service.paths.ctlfile1_file,
                              service.paths.ctlfile2_file,
                              preserve=True,
                              as_root=True)

        # Set the db_name and db_unique_name via the PFILE which will be
        # removed later
        operating_system.write_file(service.paths.pfile,
                                    "*.db_unique_name='%s'\n"
                                    "*.db_name='%s'\n" %
                                    (db_unique_name, db_name),
                                    as_root=True)
        operating_system.chown(service.paths.pfile,
                               service.instance_owner,
                               service.instance_owner_group,
                               as_root=True,
                               force=True)

        service.admin.delete_conf_cache()
        service.admin.ora_config.db_name = db_name
        service.admin.ora_config.db_unique_name = db_unique_name

        # Set proper permissions on the oratab file
        operating_system.chown(service.paths.oratab_file,
                               service.instance_owner,
                               service.instance_owner_group,
                               as_root=True,
                               force=True)

        # Create the listener.ora file and restart
        service.configure_listener()