Search in sources :

Example 1 with FilesystemInfo

use of org.platformlayer.ops.filesystem.FilesystemInfo in project platformlayer by platformlayer.

the class OpsTargetBase method getFilesystemInfoDir.

@Override
public List<FilesystemInfo> getFilesystemInfoDir(File path) throws OpsException {
    List<FilesystemInfo> fsInfoList = doFind(path, 1);
    if (fsInfoList == null) {
        return Collections.emptyList();
    }
    List<FilesystemInfo> ret = Lists.newArrayList();
    for (FilesystemInfo fsInfo : fsInfoList) {
        if (fsInfo.depth == 0) {
            continue;
        }
        ret.add(fsInfo);
    }
    return ret;
}
Also used : FilesystemInfo(org.platformlayer.ops.filesystem.FilesystemInfo)

Example 2 with FilesystemInfo

use of org.platformlayer.ops.filesystem.FilesystemInfo in project platformlayer by platformlayer.

the class OpsTargetBase method doFind.

protected List<FilesystemInfo> doFind(File path, Integer maxDepth) throws OpsException {
    Command command = maybeSudo("find");
    String[] fields = new String[] { "T@", "s", "m", "u", "g", "n", "p", "l", "y", "d" };
    StringBuilder format = new StringBuilder();
    for (int i = 0; i < fields.length; i++) {
        if (i != 0) {
            format.append("\\t");
        }
        format.append("%");
        format.append(fields[i]);
    }
    format.append("\\n");
    command.addFile(path);
    if (maxDepth != null) {
        command.addLiteral("-maxdepth");
        command.addQuoted(maxDepth.toString());
    }
    command.addLiteral("-printf");
    command.addQuoted(format.toString());
    ProcessExecution execution;
    try {
        execution = executeCommand(command);
    } catch (ProcessExecutionException e) {
        execution = e.getExecution();
        if (execution != null && execution.getExitCode() == 1 && execution.getStdErr().contains("No such file or directory")) {
            return null;
        }
        throw new OpsException("Error executing find command", e);
    }
    List<FilesystemInfo> filesystemInfos = Lists.newArrayList();
    String stdout = execution.getStdOut();
    for (String line : stdout.split("\n")) {
        String[] fieldValues = line.split("\t");
        if (fieldValues.length != fields.length) {
            throw new OpsException("Cannot parse line: " + line);
        }
        FilesystemInfo filesystemInfo = new FilesystemInfo();
        for (int i = 0; i < fieldValues.length; i++) {
            String field = fields[i];
            String fieldValue = fieldValues[i];
            if (field.equals("u")) {
                filesystemInfo.owner = fieldValue;
            } else if (field.equals("g")) {
                filesystemInfo.group = fieldValue;
            } else if (field.equals("n")) {
                filesystemInfo.links = fieldValue;
            } else if (field.equals("m")) {
                filesystemInfo.mode = fieldValue;
            } else if (field.equals("p")) {
                filesystemInfo.name = fieldValue;
            } else if (field.equals("s")) {
                filesystemInfo.size = Long.parseLong(fieldValue);
            } else if (field.equals("y")) {
                filesystemInfo.type = fieldValue;
            } else if (field.equals("l")) {
                if (!Strings.isNullOrEmpty(fieldValue)) {
                    filesystemInfo.symlinkTarget = fieldValue;
                }
            } else if (field.equals("T@")) {
                filesystemInfo.date = fieldValue;
            } else if (field.equals("d")) {
                filesystemInfo.depth = Integer.parseInt(fieldValue);
            } else {
                throw new IllegalStateException();
            }
        }
        filesystemInfos.add(filesystemInfo);
    }
    return filesystemInfos;
}
Also used : FilesystemInfo(org.platformlayer.ops.filesystem.FilesystemInfo) ProcessExecution(org.platformlayer.ops.process.ProcessExecution) ProcessExecutionException(org.platformlayer.ops.process.ProcessExecutionException)

Example 3 with FilesystemInfo

use of org.platformlayer.ops.filesystem.FilesystemInfo in project platformlayer by platformlayer.

the class FilesystemBackedPool method release.

@Override
public void release(PlatformLayerKey owner, T item) throws OpsException {
    File symlink = new File(assignedDir, toKey(item));
    FilesystemInfo info = target.getFilesystemInfoFile(symlink);
    if (info == null) {
        throw new OpsException("Symlink not found");
    }
    if (!Objects.equal(info.symlinkTarget, toFile(owner).getAbsolutePath())) {
        throw new OpsException("Resource not assigned to owner");
    }
    target.rm(symlink);
}
Also used : FilesystemInfo(org.platformlayer.ops.filesystem.FilesystemInfo) OpsException(org.platformlayer.ops.OpsException) File(java.io.File)

Example 4 with FilesystemInfo

use of org.platformlayer.ops.filesystem.FilesystemInfo in project platformlayer by platformlayer.

the class MountCgroups method handler.

@Handler
public void handler() throws OpsException, IOException {
    // TODO: Only if not installed
    OpsTarget target = OpsContext.get().getInstance(OpsTarget.class);
    File cgroupsFile = new File("/cgroup");
    FilesystemInfo info = target.getFilesystemInfoFile(cgroupsFile);
    if (info != null) {
        // TODO: Better idempotency
        return;
    }
    String fstabLine = "\ncgroup\t/cgroup\tcgroup\tdefaults\t0\t0";
    File fstabFile = new File("/etc/fstab");
    String fstab = target.readTextFile(fstabFile);
    fstab += fstabLine;
    FileUpload.upload(target, fstabFile, fstab);
    target.mkdir(cgroupsFile);
    Command mountCommand = Command.build("mount cgroup");
    target.executeCommand(mountCommand);
// mkdir /cgroup
// echo -e "cgroup\t/cgroup\tcgroup\tdefaults\t0\t0" >> /etc/fstab
// mount cgroup
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) FilesystemInfo(org.platformlayer.ops.filesystem.FilesystemInfo) Command(org.platformlayer.ops.Command) File(java.io.File) Handler(org.platformlayer.ops.Handler)

Example 5 with FilesystemInfo

use of org.platformlayer.ops.filesystem.FilesystemInfo in project platformlayer by platformlayer.

the class FilesystemCasStore method checkDirectory.

private File checkDirectory(File base, Md5Hash hash, int splits) throws OpsException {
    String relativePath = toRelativePath(hash, splits);
    File seedFile = new File(base, relativePath);
    FilesystemInfo seedFileInfo = host.getFilesystemInfoFile(seedFile);
    if (seedFileInfo != null) {
        Md5Hash seedFileHash = host.getFileHash(seedFile);
        if (!seedFileHash.equals(hash)) {
            log.warn("Hash mismatch on file: " + seedFile);
            return null;
        }
        // For LRU
        host.touchFile(seedFile);
        return seedFile;
    }
    return null;
}
Also used : FilesystemInfo(org.platformlayer.ops.filesystem.FilesystemInfo) File(java.io.File) Md5Hash(com.fathomdb.hash.Md5Hash)

Aggregations

FilesystemInfo (org.platformlayer.ops.filesystem.FilesystemInfo)11 File (java.io.File)5 Command (org.platformlayer.ops.Command)3 OpsTarget (org.platformlayer.ops.OpsTarget)3 Handler (org.platformlayer.ops.Handler)2 OpsException (org.platformlayer.ops.OpsException)2 ProcessExecution (org.platformlayer.ops.process.ProcessExecution)2 ProcessExecutionException (org.platformlayer.ops.process.ProcessExecutionException)2 Md5Hash (com.fathomdb.hash.Md5Hash)1 List (java.util.List)1 HostPolicy (org.platformlayer.core.model.HostPolicy)1 TagChanges (org.platformlayer.core.model.TagChanges)1 Tags (org.platformlayer.core.model.Tags)1 DiskImageRecipe (org.platformlayer.images.model.DiskImageRecipe)1 ChrootOpsTarget (org.platformlayer.ops.ChrootOpsTarget)1 CommandEnvironment (org.platformlayer.ops.CommandEnvironment)1 Machine (org.platformlayer.ops.Machine)1 MachineCreationRequest (org.platformlayer.ops.MachineCreationRequest)1 SshKey (org.platformlayer.ops.helpers.SshKey)1 ImageFormat (org.platformlayer.ops.images.ImageFormat)1