Skip to content

Commit

Permalink
Add creation and linking of git folder
Browse files Browse the repository at this point in the history
  • Loading branch information
chee committed Aug 15, 2019
1 parent f0ea14a commit c940eb9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 59 deletions.
8 changes: 4 additions & 4 deletions commands/create-snoot.js
Expand Up @@ -127,11 +127,11 @@ module.exports = async function createSnoot () {
})
}

log("creating a bare git repo for them to live at /repo")
await snoots.createBareRepo(snoot)
log("generating their base files! 📠 🎰")
await snoots.createBase(snoot, {authorizedKeys})

log("generating their base application files! 📠 🎰")
await snoots.createBaseApplication(snoot, {authorizedKeys})
log("creating a git folder for them")
await snoots.createRepository(snoot)

log("fixing ssh permssions")
await snoots.fixSshPermissions(snoot)
Expand Down
36 changes: 16 additions & 20 deletions library/skeletons.js
@@ -1,5 +1,4 @@
let fs = require("fs-extra")
let os = require("os")
let inquirer = require("inquirer")

exports.files = {
Expand All @@ -13,14 +12,6 @@ exports.files = {
name = ${snoot}
email ${snoot}@snoot.club`
},
repo: {
hooks: {
"post-receive" () {
return `#!/bin/sh
npx @snootclub/post-receive`
}
}
},
application: {
boops: {
".gitkeep": () => "a polite request to git to keep this empty directory"
Expand Down Expand Up @@ -112,15 +103,24 @@ module.exports = (request, response) =>
Welcome to ${snoot}'s homepage
</h1>
<h2>getting started:</h2>
<p>
if you are <strong>${snoot}</strong>, you can now ssh or ftp into your account!
</p>
<p>
if you want to host a webpage, put files in the directory
<code>application/website</code>
</p>
<p>
the page you are reading right now is the file located at
<code>./application/website/index.html</code>
</p>
<h2>advanced users:</h2>
<p>
the start script in your <code>package.json</code> will be run automatically.
you can replace it with anything, and as long as it creates and listens on a
Expand All @@ -130,10 +130,6 @@ module.exports = (request, response) =>
<code>./application/boops</code> using <a
href="https://github.com/snootclub/boop">boops</a>.
</p>
<p>
i promise that's cool and fun
</p>
`
}
}
Expand All @@ -156,8 +152,7 @@ exports.write = async function write (options) {
render,
files = exports.files,
uid,
gid,
getPermissions = () => ({uid, gid, mode})
gid
} = options

for (let [key, value] of Object.entries(files)) {
Expand All @@ -167,7 +162,6 @@ exports.write = async function write (options) {
: fileTypes.directory

let filePath = fileResolver.path
let permissions = getPermissions({filePath, fileType}) || {}

out:
if (fileType == fileTypes.file) { // if this is a file node
Expand All @@ -185,12 +179,14 @@ exports.write = async function write (options) {
}
}
await fs.outputFile(filePath, render(fileCreator))
await fs.chmod(filePath, permissions.mode || 0o644)
let rw_r__r__ = 0o644
await fs.chmod(filePath, rw_r__r__)
} else {
// this is a directory node
let files = value
let rwxrwxr_x = 0o775
await fs.mkdirp(filePath)
await fs.chmod(filePath, permissions.mode || 0o775)
await fs.chmod(filePath, rwxrwxr_x)
await write(merge(options, {
resolver: fileResolver,
files
Expand All @@ -199,8 +195,8 @@ exports.write = async function write (options) {

await fs.chown(
filePath,
permissions.uid || uid,
permissions.gid || gid
uid,
gid
)
}
}
61 changes: 26 additions & 35 deletions library/snoots.js
Expand Up @@ -5,39 +5,38 @@ let shell = require("./shell.js")
let skeletons = require("./skeletons.js")
let {shout} = require("./loggo.js")

let gitRootResolver = createResolver("/mnt/extra-goose/git")
let rootResolver = createResolver("/www/snoot.club")
let resolver = rootResolver("snoots")

let validNameRegex = /^[a-z][a-z0-9]{0,30}$/

let validateName = snoot =>
validNameRegex.test(snoot)
let validateName = snoot => validNameRegex.test(snoot)

let applicationResolver = (snoot, ...paths) =>
resolver(snoot, "application", ...paths)

let repoResolver = (snoot, ...paths) =>
resolver(snoot, "repo", ...paths)
let repoResolver = snoot => resolver(snoot, "git")

let websiteResolver = (snoot, ...paths) =>
resolver(snoot, "application", "website", ...paths)

async function getAuthorizedKeys (snoot) {
async function getAuthorizedKeys(snoot) {
let snootResolver = resolver(snoot)
let sshDirectoryResolver = snootResolver(".ssh")
let authorizedKeysPath = sshDirectoryResolver("authorized_keys").path
return fs.readFile(authorizedKeysPath, "utf-8")
}

async function fixSshPermissions (snoot) {
async function fixSshPermissions(snoot) {
let snootResolver = resolver(snoot)
let sshDirectoryResolver = snootResolver(".ssh")
let authorizedKeysPath = sshDirectoryResolver("authorized_keys").path

let snootOwnedPaths = [
sshDirectoryResolver.path,
authorizedKeysPath,
snootResolver.path
snootResolver.path,
]

let snootId = await unix.getUserId(snoot)
Expand All @@ -54,58 +53,50 @@ async function fixSshPermissions (snoot) {
}
}

async function createUnixAccount (snoot) {
async function createUnixAccount(snoot) {
return unix.createUser({
user: snoot,
groups: [unix.commonGroupName, unix.lowerGroupName],
homeDirectory: createResolver("/")("snoots", snoot).path
homeDirectory: createResolver("/")("snoots", snoot).path,
})
}

async function createBareRepo (snoot) {
let snootRepoResolver = repoResolver(snoot)
await shell.run(`git init --bare ${snootRepoResolver.path}`)
async function createRepository(snoot) {
let snootGitPath = resolver(snoot)("git").path
let gitSnootPath = gitRootResolver(snoot).path
await fs.mkdir(gitSnootPath)
await unix.chown({
user: await unix.getUserId(snoot),
group: await unix.getCommonGid(),
path: snootRepoResolver.path,
recurse: true
path: gitSnootPath,
recurse: true,
})
await unix.ln({
to: gitSnootPath,
from: snootGitPath,
})
}

async function createBaseApplication (snoot, data) {
async function createBase(snoot, data) {
await skeletons.write({
resolver: resolver(snoot),
uid: await unix.getUserId(snoot),
gid: await unix.getCommonGid(),
render: compile => compile(snoot, data),
getPermissions ({filePath, fileType}) {
if (fileType == skeletons.fileTypes.file) {
let rwxr_xr_x = 0o755
let postReceive = repoResolver(snoot, "hooks", "post-receive").path
if (filePath == postReceive) {
return {
mode: rwxr_xr_x
}
}
}
}
})
}

async function getNames () {
async function getNames() {
let files = await fs.readdir(resolver.path)
return files.filter(name =>
validateName(name)
)
return files.filter(name => validateName(name))
}

async function checkExists (snoot) {
async function checkExists(snoot) {
let names = await getNames()
return names.includes(snoot)
}

async function demandExistence (snoot) {
async function demandExistence(snoot) {
let names = await getNames()

if (!names.includes(snoot)) {
Expand All @@ -120,12 +111,12 @@ module.exports = {
applicationResolver,
websiteResolver,
createUnixAccount,
createBaseApplication,
createBase,
fixSshPermissions,
checkExists,
validateName,
getNames,
demandExistence,
createBareRepo,
getAuthorizedKeys
createRepository,
getAuthorizedKeys,
}

0 comments on commit c940eb9

Please sign in to comment.