Skip to content

Check setup #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions bin/check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const fs = require('fs')

const hyperfuse = require('hyperdrive-fuse')
const chalk = require('chalk')
const { exec } = require('child_process')

exports.command = 'check'
exports.desc = 'Check configuration of FUSE and hyperdrive mount point.'
exports.builder = {
user: {
description: 'User that should own the /hyperdrive directory',
type: 'string',
default: process.geteuid(),
alias: 'U'
},
group: {
description: 'User that should own the /hyperdrive directory',
type: 'string',
default: process.getgid(),
alias: 'G'
}
}
exports.handler = async function (argv) {
console.log(chalk.blue('Configuring FUSE...'))

configureFuse((err, fuseMsg) => {
if (err) return onerror(err)
return makeRootDrive((err, driveMsg) => {
if (err) return onerror(err)
return onsuccess([fuseMsg, driveMsg])
})
})

function configureFuse (cb) {
hyperfuse.isConfigured((err, fuseConfigured) => {
if (err) return onerror(err)
return cb(null, 'FUSE is configured correctly')
})
}

function makeRootDrive (cb) {
fs.stat('/hyperdrive', (err, stat) => {
if (err && err.errno !== -2) return cb(new Error('Could not get the status of /hyperdrive.'))
if (!err && !stat) return cb(null, false)

exec(`id -G ${argv.user}`, (err, stdout) => {
if (err) return cb(new Error(`Could not resolve groups: ${err}`))
const userGroups = stdout.trim().split(' ')
if (userGroups.includes(stat.gid) === false) return cb(new Error('/hyperdrive is not part of any groups the user is part of'))

exec(`id -u ${argv.user}`, (err, stdout) => {
if (err) return cb(new Error(`Could not resolve user: ${err}`))
if (stdout.trim() === stat.uid) return cb(new Error(`/hyperdrive is not owned by user: ${argv.user}`))

return cb(null, '/hyperdrive is configured correctly')
})
})
})
}

function onsuccess (msgs) {
console.log(chalk.green('Successfully checked FUSE:'))
console.log()
for (const msg of msgs) {
console.log(chalk.green(` * ${msg}`))
}
}

function onerror (err) {
console.error(chalk.red(`Could not check FUSE.`))
if (err) console.error(chalk.red(err))
}
}