You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cvm/src/api/types/block-device.js

96 lines
2.7 KiB
JavaScript

"use strict";
const Promise = require("bluebird");
const fs = Promise.promisifyAll(require("fs"));
const matchValue = require("match-value");
const { createDataObject, LocalProperties, ID, Dynamic } = require("../../packages/graphql-interface/data-object");
const All = require("../../packages/graphql-interface/symbols/all");
const treecutter = require("../../packages/treecutter");
module.exports = function (types) {
return function BlockDevice({ name, path, _treecutterDepth, _treecutterSequenceNumber }) {
// if (name != null) {
// path = `/dev/${name}`;
// } else if (path != null) {
// name = deviceNameFromPath(path);
// }
// return Promise.try(() => {
// return fs.realpathAsync(path);
// }).then((realPath) => {
/* FIXME: parent */
return createDataObject({
[LocalProperties]: {
_treecutterDepth,
_treecutterSequenceNumber
},
[Dynamic]: {
mounts: function ({ type }, { resolveProperty, resolvePropertyPath, resolveDataSource }) {
return Promise.try(() => {
return resolveDataSource("findmnt", All);
}).then((allMounts) => {
return treecutter.flatten(allMounts);
}).map((mount) => {
return types.Mount({ mountpoint: mount.mountpoint });
}).filter((mount) => {
return Promise.try(() => {
return resolvePropertyPath([ "sourceDevice", "path" ], mount);
}).then((sourceDevicePath) => {
// FIXME: Get own path dynamically
return (sourceDevicePath === path);
});
}).then((mounts) => {
if (type != null) {
return Promise.filter(mounts, (mount) => {
return Promise.try(() => {
return resolveProperty("type", mount);
}).then((mountType) => {
return (mountType === type);
});
});
} else {
return mounts;
}
});
}
},
// findmnt: {
// [ID]: All,
// mounts: function (allMounts, { type }, context) {
// let { resolveProperty } = context;
// console.log("CONTEXT", context);
// // FIXME: Why is this called so often?
// }
// },
lsblk: {
[ID]: { name, path },
name: "name",
path: (device) => {
return fs.realpathAsync(device.path);
},
type: (device) => {
return matchValue(device.type, {
partition: "PARTITION",
disk: "DISK",
loopDevice: "LOOP_DEVICE"
});
},
size: "size",
mountpoint: "mountpoint", // FIXME: Isn't this obsoleted by `mounts`?
deviceNumber: "deviceNumber",
removable: "removable",
readOnly: "readOnly",
children: (device) => {
return device.children.map((child) => {
return BlockDevice({ name: child.name });
});
}
}
});
// });
};
};