|
| 1 | +const fs = require('fs') |
| 2 | + |
| 3 | +const hyperfuse = require('hyperdrive-fuse') |
| 4 | +const chalk = require('chalk') |
| 5 | +const { exec } = require('child_process') |
| 6 | + |
| 7 | +exports.command = 'check' |
| 8 | +exports.desc = 'Check configuration of FUSE and hyperdrive mount point.' |
| 9 | +exports.builder = { |
| 10 | + user: { |
| 11 | + description: 'User that should own the /hyperdrive directory', |
| 12 | + type: 'string', |
| 13 | + default: process.geteuid(), |
| 14 | + alias: 'U' |
| 15 | + }, |
| 16 | + group: { |
| 17 | + description: 'User that should own the /hyperdrive directory', |
| 18 | + type: 'string', |
| 19 | + default: process.getgid(), |
| 20 | + alias: 'G' |
| 21 | + } |
| 22 | +} |
| 23 | +exports.handler = async function (argv) { |
| 24 | + console.log(chalk.blue('Configuring FUSE...')) |
| 25 | + |
| 26 | + configureFuse((err, fuseMsg) => { |
| 27 | + if (err) return onerror(err) |
| 28 | + return makeRootDrive((err, driveMsg) => { |
| 29 | + if (err) return onerror(err) |
| 30 | + return onsuccess([fuseMsg, driveMsg]) |
| 31 | + }) |
| 32 | + }) |
| 33 | + |
| 34 | + function configureFuse (cb) { |
| 35 | + hyperfuse.isConfigured((err, fuseConfigured) => { |
| 36 | + if (err) return onerror(err) |
| 37 | + return cb(null, 'FUSE is configured correctly') |
| 38 | + }) |
| 39 | + } |
| 40 | + |
| 41 | + function makeRootDrive (cb) { |
| 42 | + fs.stat('/hyperdrive', (err, stat) => { |
| 43 | + if (err && err.errno !== -2) return cb(new Error('Could not get the status of /hyperdrive.')) |
| 44 | + if (!err && !stat) return cb(null, false) |
| 45 | + |
| 46 | + exec(`id -G ${argv.user}`, (err, stdout) => { |
| 47 | + if (err) return cb(new Error(`Could not resolve groups: ${err}`)) |
| 48 | + const userGroups = stdout.trim().split(' ') |
| 49 | + if (userGroups.includes(stat.gid) === false) return cb(new Error('/hyperdrive is not part of any groups the user is part of')) |
| 50 | + |
| 51 | + exec(`id -u ${argv.user}`, (err, stdout) => { |
| 52 | + if (err) return cb(new Error(`Could not resolve user: ${err}`)) |
| 53 | + if (stdout.trim() === stat.uid) return cb(new Error(`/hyperdrive is not owned by user: ${argv.user}`)) |
| 54 | + |
| 55 | + return cb(null, '/hyperdrive is configured correctly') |
| 56 | + }) |
| 57 | + }) |
| 58 | + }) |
| 59 | + } |
| 60 | + |
| 61 | + function onsuccess (msgs) { |
| 62 | + console.log(chalk.green('Successfully checked FUSE:')) |
| 63 | + console.log() |
| 64 | + for (const msg of msgs) { |
| 65 | + console.log(chalk.green(` * ${msg}`)) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + function onerror (err) { |
| 70 | + console.error(chalk.red(`Could not check FUSE.`)) |
| 71 | + if (err) console.error(chalk.red(err)) |
| 72 | + } |
| 73 | +} |
0 commit comments