/
create-snoot.js
executable file
Β·156 lines (132 loc) Β· 3.99 KB
/
create-snoot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env -S sudo node
let inquirer = require("inquirer")
let {log, warn, shout} = require("../library/loggo.js")
let unix = require("../library/unix.js")
let snoots = require("../library/snoots.js")
let shell = require("../library/shell.js")
let fetch = require("../library/fetch.js")
let fs = require("fs-extra")
let filterDuplicateLines = string => [
...new Set(string.split("\n"))
].join("\n")
process.on("unhandledRejection", error => {
console.error(error)
process.exit(122)
})
process.on("uncaughtException", error => {
console.error(error)
process.exit(222)
})
async function getKeysFromGithub (githubUsername) {
log("gonna get them an authorized_keys file from github")
return fetch(`https://github.com/${githubUsername}.keys`)
.then(response => response.text())
}
module.exports = async function createSnoot () {
let hasPrivilege = unix.checkYourPrivilege()
if (!hasPrivilege) {
shout("oh no: not root")
log("this program needs to be run as root, because it does different things as different snoots")
process.exit(22)
// log("this program needs elevated privileges, so i'll ask you for your password for sudo sometimes")
}
let {
snoot
} = await inquirer.prompt([
{
type: "input",
name: "snoot",
message: "oh, a new snoot? π \nwhat's their name?",
validate: snoots.validateName
},
{
type: "list",
choices: ["yes", "yes"],
name: "isAGoodBoy",
message: "a good boy? ππ",
when: ({snoot}) => snoot == "abe"
}
])
let snootAlreadyExists = await snoots.checkExists(snoot)
let {shouldContinue = true} = await inquirer.prompt({
type: "confirm",
name: "shouldContinue",
message: "πΊ whΓΌmf and wetch? this snoot already exists, should we continue?",
when: () => snootAlreadyExists,
default: false
})
if (!shouldContinue) {
shout("cancelling for there already existed such a snoot")
process.exit(2)
}
let existingKeys = snootAlreadyExists && (
(await snoots.getConfig(snoot)).authorizedKeys
)
let {
githubUsername
} = await inquirer.prompt([
{
type: "confirm",
name: "useExistingKeys",
message: "do you want to use the existing authorized_keys file they had?",
when: () => existingKeys
},
{
type: "confirm",
name: "shouldGetGithubKeys",
message: "do you want to get fresh keys from github?",
when: ({existingKeys}) => existingKeys
},
{
type: "input",
name: "githubUsername",
message: " π π»\nwhat is their github username?",
when: ({existingKeys, shouldGetGithubKeys}) =>
existingKeys
? shouldGetGithubKeys
: true
}
])
let githubKeys = githubUsername
? await getKeysFromGithub(githubUsername)
: ""
let {
authorizedKeys
} = await inquirer.prompt({
type: "editor",
name: "authorizedKeys",
message: "edit their authorized_keys βοΈπ",
default: filterDuplicateLines(githubKeys
.concat(existingKeys || ""))
})
if (await unix.checkUserExists(snoot)) {
warn(`there's already a user called "${snoot}"!! i hope that's ok!οΈοΈ β₯οΈ`)
} else {
log("ok! creating them a unix user π€ account on the computer π₯ β¨οΈ π±")
await snoots
.createUnixAccount(snoot)
.catch(error => {
shout("couldnt create user!")
shout(error)
warn("creating them a directory π π in /snoots as a backup π¦΄")
})
}
log("adding their authorized_keys βπ file so they can log in (:")
await snoots.createHomeSshConfiguration(snoot, {authorizedKeys})
log("creating a bare git repo for them to live at /repo")
await snoots.createBareRepo(snoot)
log("giving them a gitconfig")
await snoots.createHomeGitConfiguration(snoot)
log("generating their base application files! π π°")
await snoots.createBaseApplication(snoot)
await fs.move(
snoots.applicationResolver("nginx.conf").path,
snoots.rootResolver("snoots-nginx")(`${snoot}.conf`).path
)
log("restarting nginx π")
await shell.run("nginx -s reload")
}
let beingRunDirectly = process.argv[1].match(/create-snoot($|\.js$)/)
if (beingRunDirectly) {
module.exports()
}