-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfigure.js
183 lines (168 loc) · 7.09 KB
/
configure.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const { execSync } = require("child_process");
const { writeFileSync } = require("fs");
const prompts = require("prompts");
const axios = require("axios");
const fs = require("fs")
const sleep = (ms) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
const log = async (s) => {
return new Promise(async (resolve, reject) => {
for (const c of s) {
process.stdout.write(c);
await sleep(20);
}
await sleep(500);
process.stdout.write("\n");
resolve();
});
};
(async function configure() {
await log("Node Setup Initiated")
await log("Installing Dependency: caddy...")
execSync("apt-get update -y");
execSync("apt-get install -y debian-keyring debian-archive-keyring apt-transport-https wget");
execSync("curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | gpg --batch --yes --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg");
execSync("curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | tee /etc/apt/sources.list.d/caddy-stable.list");
execSync("apt-get update -y");
execSync("apt-get install caddy -y")
await log("✅ Dependency caddy successfully installed");
let useDomain;
let address;
let port;
useDomain = await prompts({
type: "confirm",
name: "value",
message: "Is this Ararat install going to use a domain? If so, please point your domain to this install before proceeding."
});
address = await prompts({
type: "text",
name: "value",
message: `Enter the ${useDomain.value ? "domain" : "ip address"} of this Ararat install ${useDomain.value ? "(ex: example.hyeararat.com)" : "(ex: 1.1.1.1)"}`
});
port = await prompts({
type: "number",
name: "value",
message: `Enter the port you would like the Ararat Web UI to listen on. (443 is reccomended)`
})
await log("Setting up web server...");
let caddyConfig = {
"apps": {
"http": {
"servers": {
"ararat": {
"listen": [`:${port.value}`],
"routes": [
{
"match": [
{
"host": [
address.value
]
}
],
"handle": [
{
"handler": "reverse_proxy",
"upstreams": [
{
"dial": "127.0.0.1:3000"
}
]
}
]
}
]
}
}
},
}
}
writeFileSync("./caddyConfig.json", JSON.stringify(caddyConfig))
let newConf = await axios.post("http://127.0.0.1:2019/load", caddyConfig)
await log("✅ Web server successfully configured");
let alreadyHasDB = await prompts({
type: "confirm",
name: "value",
message: "Does this Ararat install already have a database?"
});
let mongoUsername;
let mongoPassword;
let mongoHost = {value: "localhost"};
let mongoPort = {value: 27017};
let mongoDBName = {value: "ararat"};
if (!alreadyHasDB.value) {
await log("Installing MongoDB...");
execSync("apt-get install gnupg");
execSync("curl -fsSL https://pgp.mongodb.com/server-7.0.asc | \
gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \
--dearmor")
execSync(`echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list`)
execSync("apt-get update -y");
execSync("apt-get install -y mongodb-org")
execSync("systemctl start mongod")
execSync("systemctl enable mongod")
await log("✅ MongoDB successfully installed");
await log("Creating Ararat database...");
await log("Configuring MongoDB...");
mongoUsername = await prompts({
type: "text",
name: "value",
message: "Enter a username for the MongoDB database"
});
mongoPassword = await prompts({
type: "password",
name: "value",
message: "Enter a password for the MongoDB database"
});
execSync(`mongosh --eval "db.getSiblingDB('ararat').createUser({user: '${mongoUsername.value}', pwd: '${mongoPassword.value}', roles: [{role: 'readWrite', db: 'ararat'}]})"`)
execSync('echo "security:\n authorization: enabled" >> /etc/mongod.conf');
execSync("systemctl restart mongod")
await log("✅ MongoDB successfully configured");
}
if (alreadyHasDB.value) {
mongoHost = await prompts({
type: "text",
name: "value",
message: "Enter the hostname of the MongoDB server"
})
mongoPort = await prompts({
type: "number",
name: "value",
message: "Enter the port of the MongoDB server"
});
mongoDBName = await prompts({
type: "text",
name: "value",
message: "Enter the database name you want Ararat to use on the MongoDB server"
});
mongoUsername = await prompts({
type: "text",
name: "value",
message: "Enter the username to the MongoDB datbase"
});
mongoPassword = await prompts({
type: "text",
name: "value",
message: "Enter the password to the MongoDB database"
});
}
await log("Generating Environment Variables...")
let mongoURI = `mongodb://${mongoUsername.value}:${mongoPassword.value}@${mongoHost.value}:${mongoPort.value}/${mongoDBName.value}`;
let env = `DATABASE_URL=${mongoURI}\n`
let key = [...Array(32)].map(i=>(~~(Math.random()*36)).toString(36)).join('')
env+=`ENC_KEY=${key}\n`
env+=`URL=${address.value}:${port.value}\n`
fs.writeFileSync("./.env.local", env);
await log("✅ Environment variables successfully generated");
await log("Generating encryption keys...");
const generateKeyPair = require("jose").generateKeyPair;
const exportJWK = require("jose").exportJWK;
if (!fs.existsSync("./key")) {
const keypair = await generateKeyPair("RS256");
const jwk = await exportJWK(keypair.privateKey);
fs.writeFileSync("./key", JSON.stringify(jwk));
await log("✅ Encryption keys generated");
};
await log(`Ararat is all setup and ready to go! You can now create your first user account by running the command: node cli/createUser.js. To start Ararat, build it using the command: npm run build, then start it using the command: npm run start!`);
}())