-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·296 lines (262 loc) · 7.77 KB
/
index.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#! /usr/bin/env node
const yargs = require("yargs");
const env = require("./env");
const prompts = require("prompts");
const password = require("./password");
const shell = require("shelljs");
const fs = require("fs");
const secure = require("./secure");
const git = require("./git");
const cfg = require("./config");
async function create_field() {
console.log(
"Setting up a new field. It'll become the new active field for your project."
);
const response = await prompts([
{
type: "text",
name: "field",
message: "What's the name of the field? (e.g. staging)",
},
{
type: "text",
name: "app_name",
message: "What's the name of this app?",
},
{
type: "password",
name: "password",
message:
"What password should we use for encrypting files? (Keep this in a password manager for later!)",
},
]);
password.store(response.field, cfg.workDir, response.password);
const field = response.field;
const app_name = response.app_name;
// CREATE THE FILES!
if (shell.test("-d", `${field}`)) {
console.log(
"A folder already exists with that field name. Skipping creation!"
);
process.exit(0);
}
shell.mkdir("-p", `${field}/vars`);
shell.mkdir("-p", `${field}/files`);
fs.writeFileSync(
`${field}/vars/common.yml`,
`app_name: ${app_name}\nenv: ${field}\n`
);
fs.writeFileSync(
`${field}/infra.yml`,
`# Write a playbook to set up actual cloud resources here!`
);
fs.writeFileSync(
`${field}/provision.yml`,
`# Write a playbook to provision your nifty new servers.`
);
fs.writeFileSync(
`${field}/deploy.yml`,
`# Write a playbook that deploys your code to freshly provisioned boxes!`
);
fs.writeFileSync(
`${field}/app_env`,
`# You'll want to copy this file to your servers and source it before running your apps.
# Fill it with environment variables like this:
#
# export NODE_ENV=${field}
#
# It's automatically encrypted when it's stored in your code, so you can put
# secrets in here.`
);
secure.protect(`${field}/app_env`);
fs.writeFileSync(`${field}/requirements.yml`, `# Add your requirements here`);
}
async function init() {
var response = await prompts({
type: "select",
name: "intent",
message: "Hi! Want to create a new field? or activate an existing one? 🏈",
choices: [
{ title: "create", value: "create" },
{ title: "activate", value: "activate" },
],
});
if (response.intent == "create") {
await create_field();
} else {
var response = await prompts({
type: "text",
name: "name",
message: "What's the name of the field you want to activate?",
});
await switch_field(response.name);
}
}
async function ensure_initted() {
// Try to get the password.
// If it's not there, ask for it.
let pw = password.get(cfg.workDir, cfg.field);
if (!pw) {
console.log(
`Looks like you don't have a password stored in the vault yet for this app and field.`
);
const response = await prompts([
{
type: "password",
name: "password",
message:
"What password should we use for encrypting files? (Keep this in a password manager for later!)",
},
]);
password.store(cfg.field, cfg.workDir, response.password);
}
}
var argv = yargs
.usage("Usage: $0 <command> [options]")
.command("init", "Give QB the context it needs to get running")
.command(
"field <operation>",
"Create or switch active fields 🙌",
(yargs) => {
yargs.positional("operation", {
choices: ["new"],
});
}
)
.command(
"env <operation>",
"Easily work with the app environment for the active field.",
(yargs) => {
yargs
.positional("operation", {
choices: ["show", "edit", "set"],
})
.example(
"$0 env show",
"Print out an unencrypted version of the app env."
)
.example(
"$0 env edit",
"Edit the app env, saving it in a safe, ecrypted form."
)
.example(
"$0 env set NODE_ENV production",
"Add or modify an environment variable in the encrypted app env."
);
}
)
.command("run <playbook>", "Run a playbook!", (yargs) => {
yargs.positional("playbook", {
choices: ["infra", "provision", "deploy"],
});
})
.command(
"ci",
"Deploys to a field without any safety checks. Set your ENVs correctly!"
)
.command("edit <file>", "Edit an encrypted file.")
.command("show <file>", "Print the contents of an encrypted file to stdout")
.command("protect <file>", "Encrypt a file in-place")
.command("protect_string <string>", "Encrypt a string and print the result")
.command("expose <file>", "Decrypt a file in-place")
.command(
"install",
"Installs roles from Ansible Galaxy listed in the field's requirements.yml file"
)
.demandCommand(1)
.help("h")
.alias("h", "help").argv;
async function main() {
if (argv._[0] == "init") {
init();
}
if (argv._[0] == "field" && argv.operation == "new") {
create_field().catch(console.error.bind(this));
}
if (argv._[0] == "run") {
await ensure_initted();
let inventory = argv.playbook != "infra" ? `-i ${cfg.field}/inventory` : "";
if (!argv.force) {
if (argv.playbook == "deploy") {
let isGitClean = git.isClean();
shell.exec("git status --porcelain", { silent: true }).stdout.trim() ==
"";
if (!isGitClean) {
console.log(
"Looks like you have uncommited changes in your git repository. Please commit or stash all changes and run again."
);
process.exit(1);
}
let unpushedCommits = git.unpushed();
if (unpushedCommits != "") {
console.log(
`It looks like you have local commits that you haven't yet pushed to the remote branch. Please do so before deploying.`
);
process.exit(1);
}
}
}
shell.exec(
`ANSIBLE_FORCE_COLOR=true ANSIBLE_HOST_KEY_CHECKING=false ansible-playbook ${cfg.field}/${argv.playbook}.yml --vault-password-file ${secure.pass_getter} ${inventory}`,
{ fatal: true }
);
}
if (argv._[0] == "ci") {
await ensure_initted();
let inventory = `-i ${cfg.field}/inventory`;
let r = shell.exec(
`ANSIBLE_FORCE_COLOR=true ANSIBLE_HOST_KEY_CHECKING=false ansible-playbook ${cfg.field}/deploy.yml --vault-password-file ${secure.pass_getter} ${inventory}`,
{ fatal: true }
);
if (r.code != 0) {
process.exit(-1);
}
}
if (argv._[0] == "env" && argv.operation == "show") {
await ensure_initted();
env.show();
}
if (argv._[0] == "env" && argv.operation == "edit") {
await ensure_initted();
env.edit();
}
if (argv._[0] == "env" && argv.operation == "set") {
await ensure_initted();
let varName = argv._[1];
let varVal = argv._[2];
if (!varName || !varVal) {
console.log(
"You tried to set an env var without passing in the name or value of it! See qb env -h for an example."
);
process.exit(-1);
}
env.set(varName, varVal);
}
if (argv._[0] == "edit") {
await ensure_initted();
secure.edit(argv.file);
}
if (argv._[0] == "show") {
await ensure_initted();
secure.show(argv.file);
}
if (argv._[0] == "protect") {
await ensure_initted();
secure.protect(argv.file);
}
if (argv._[0] == "protect_string") {
await ensure_initted();
secure.protect_string(argv.string);
}
if (argv._[0] == "expose") {
await ensure_initted();
secure.expose(argv.file);
}
if (argv._[0] == "install") {
await ensure_initted();
shell.exec(`cd ${cfg.field} && ansible-galaxy install -r requirements.yml`);
}
}
main().catch((err) => {
throw err;
});