Exemple #1
0
def utime(path):
    if not os.path.exists(path):
        raise exception.FileNotFound(file_path=path)
    # context wrapper ensures the file exists before trying to modify time
    # which fixes a race condition with NFS image caching (see LP#1809123)
    with open(path, 'a'):
        os.utime(path, None)
Exemple #2
0
def read_file_as_root(file_path):
    """Secure helper to read file as root."""
    try:
        out, _err = execute('cat', file_path, run_as_root=True)
        return out
    except processutils.ProcessExecutionError:
        raise exception.FileNotFound(file_path=file_path)
Exemple #3
0
def generate_key_pair(bits=1024):
    # what is the magic 65537?

    with utils.tempdir() as tmpdir:
        keyfile = os.path.join(tmpdir, 'temp')
        utils.execute('ssh-keygen', '-q', '-b', bits, '-N', '', '-t', 'rsa',
                      '-f', keyfile, '-C', 'Generated by Nova')
        fingerprint = _generate_fingerprint('%s.pub' % (keyfile))
        if not os.path.exists(keyfile):
            raise exception.FileNotFound(keyfile)
        private_key = open(keyfile).read()
        public_key_path = keyfile + '.pub'
        if not os.path.exists(public_key_path):
            raise exception.FileNotFound(public_key_path)
        public_key = open(public_key_path).read()

    return (private_key, public_key, fingerprint)
 def test_get_missing_iscsi_initiator(self):
     self.mox.StubOutWithMock(utils, 'execute')
     file_path = '/etc/iscsi/initiatorname.iscsi'
     utils.execute('cat', file_path, run_as_root=True).AndRaise(
         exception.FileNotFound(file_path=file_path))
     # Start test
     self.mox.ReplayAll()
     result = volumeutils.get_iscsi_initiator()
     self.assertIsNone(result)
Exemple #5
0
def generate_key_pair(bits=None):
    with utils.tempdir() as tmpdir:
        keyfile = os.path.join(tmpdir, 'temp')
        args = ['ssh-keygen', '-q', '-N', '', '-t', 'rsa',
                '-f', keyfile, '-C', 'Generated by Nova']
        if bits is not None:
            args.extend(['-b', bits])
        utils.execute(*args)
        fingerprint = _generate_fingerprint('%s.pub' % (keyfile))
        if not os.path.exists(keyfile):
            raise exception.FileNotFound(keyfile)
        private_key = open(keyfile).read()
        public_key_path = keyfile + '.pub'
        if not os.path.exists(public_key_path):
            raise exception.FileNotFound(public_key_path)
        public_key = open(public_key_path).read()

    return (private_key, public_key, fingerprint)
Exemple #6
0
 def test_check_can_live_migrate_destination_exception(self):
     mock_instance = fake_instance.fake_instance_obj(self.context)
     mock_check = self._pathutils.check_remote_instances_dir_shared
     mock_check.side_effect = exception.FileNotFound(file_path='C:\\baddir')
     self.assertRaises(exception.MigrationPreCheckError,
                       self._livemigrops.check_can_live_migrate_destination,
                       mock.sentinel.context, mock_instance,
                       mock.sentinel.src_comp_info,
                       mock.sentinel.dest_comp_info)
Exemple #7
0
def utime(path):
    if not os.path.exists(path):
        raise exception.FileNotFound(file_path=path)

    # NOTE(mikal): the old version of this used execute(touch, ...), which
    # would apparently fail on shared storage when multiple instances were
    # being launched at the same time. If we see failures here, we might need
    # to wrap this in a try / except.
    os.utime(path, None)
Exemple #8
0
def _remove_file(file_path):
    """Removes a file reference from the db."""
    if _db_content.get("files") is None:
        raise exception.NoFilesFound()
    # Check if the remove is for a single file object or for a folder
    if file_path.find(".vmdk") != -1:
        if file_path not in _db_content.get("files"):
            raise exception.FileNotFound(file_path=file_path)
        _db_content.get("files").remove(file_path)
    else:
        # Removes the files in the folder and the folder too from the db
        for file in _db_content.get("files"):
            if file.find(file_path) != -1:
                lst_files = _db_content.get("files")
                if lst_files and lst_files.count(file):
                    lst_files.remove(file)
Exemple #9
0
    def mounted_on_same_shared_storage(self, context, instance_ref, dest):
        """Check if the src and dest host mount same shared storage.

        At first, dest host creates temp file, and src host can see
        it if they mounts same shared storage. Then src host erase it.

        :param context: security context
        :param instance_ref: nova.db.sqlalchemy.models.Instance object
        :param dest: destination host

        """

        src = instance_ref['host']
        dst_t = db.queue_get_for(context, FLAGS.compute_topic, dest)
        src_t = db.queue_get_for(context, FLAGS.compute_topic, src)

        filename = None

        try:
            # create tmpfile at dest host
            filename = rpc.call(context, dst_t,
                                {"method": 'create_shared_storage_test_file'})

            # make sure existence at src host.
            ret = rpc.call(
                context, src_t, {
                    "method": 'check_shared_storage_test_file',
                    "args": {
                        'filename': filename
                    }
                })
            if not ret:
                raise exception.FileNotFound(file_path=filename)

        except exception.FileNotFound:
            raise

        finally:
            # Should only be None for tests?
            if filename is not None:
                rpc.call(
                    context, dst_t, {
                        "method": 'cleanup_shared_storage_test_file',
                        "args": {
                            'filename': filename
                        }
                    })
    def check_dirs_shared_storage(self, src_dir, dest_dir):
        # Check if shared storage is being used by creating a temporary
        # file at the destination path and checking if it exists at the
        # source path.
        LOG.debug("Checking if %(src_dir)s and %(dest_dir)s point "
                  "to the same location.",
                  dict(src_dir=src_dir, dest_dir=dest_dir))

        try:
            with tempfile.NamedTemporaryFile(dir=dest_dir) as tmp_file:
                src_path = os.path.join(src_dir,
                                        os.path.basename(tmp_file.name))
                shared_storage = os.path.exists(src_path)
        except OSError as e:
            raise exception.FileNotFound(six.text_type(e))

        return shared_storage
Exemple #11
0
def chown(
    path: str,
    uid: int = -1,
    gid: int = -1,
    recursive: bool = False,
) -> None:
    if not os.path.exists(path):
        raise exception.FileNotFound(file_path=path)

    if not recursive or os.path.isfile(path):
        return os.chown(path, uid, gid)

    for root, dirs, files in os.walk(path):
        os.chown(root, uid, gid)
        for item in dirs:
            os.chown(os.path.join(root, item), uid, gid)
        for item in files:
            os.chown(os.path.join(root, item), uid, gid)
Exemple #12
0
def readfile(path):
    if not os.path.exists(path):
        raise exception.FileNotFound(file_path=path)
    with open(path, 'r') as f:
        return f.read()
Exemple #13
0
def rmdir(path):
    if not os.path.exists(path):
        raise exception.FileNotFound(file_path=path)
    os.rmdir(path)
Exemple #14
0
def chmod(path, mode):
    if not os.path.exists(path):
        raise exception.FileNotFound(file_path=path)
    os.chmod(path, mode)
Exemple #15
0
def chown(path, uid=-1, gid=-1):
    if not os.path.exists(path):
        raise exception.FileNotFound(file_path=path)
    return os.chown(path, uid, gid)
Exemple #16
0
def readlink(path):
    if not os.path.exists(path):
        raise exception.FileNotFound(file_path=path)
    return os.readlink(path)
Exemple #17
0
def writefile(path, mode, content):
    if not os.path.exists(os.path.dirname(path)):
        raise exception.FileNotFound(file_path=path)
    with open(path, mode) as f:
        f.write(content)