-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdeployment-script.js
More file actions
226 lines (203 loc) · 7.45 KB
/
deployment-script.js
File metadata and controls
226 lines (203 loc) · 7.45 KB
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
//load files from the .env file
require('dotenv').load();
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Load credentials and set region from JSON file
// AWS.config.loadFromPath('./config.json');
// Currently loading from env file
// Load the other scripts
var websiteScript = require('./website-script');
var lambdaScript = require('./lambda-script');
// Create S3 service objects
s3 = new AWS.S3({apiVersion: '2006-03-01'});
route53 = new AWS.Route53({apiVersion: '2013-04-01'});
var iam = new AWS.IAM({apiVersion: '2010-05-08'});
var lambda = new AWS.Lambda({apiVersion: '2015-03-31'});
var apigateway = new AWS.APIGateway({apiVersion: '2015-07-09'});
// interact with fs
const path = require("path");
const fs = require('fs');
// Package to open browser window
const open = require('open');
// package to use stdin/out
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
//converts console input to y and n
function convertInput(input) {
var mininput = input.toLowerCase()
if (mininput == 'y' || mininput == 'yes') {
output = 'y';
}
else if(mininput == 'n' || mininput == 'no') {
output = 'n';
}
else {
output = '';
}
return output;
}
// ADD THE VALUES TO THE VARIABLES.JSON FILE
function addVars(jsonVar, jsonVal, jsonDir){
let rawdata = fs.readFileSync(jsonDir);
obj = JSON.parse(rawdata);
obj[jsonVar] = jsonVal;
jsonObj = JSON.stringify(obj);
fs.writeFileSync(jsonDir, jsonObj);
console.log('saved your public site name in the variables.json file')
}
function stmt1(){
readline.question(`Want to deploy a new static website? [Y/n]`, (res) => {
switch(convertInput(res)) {
case 'y':
stmt3();
break;
case 'n':
stmt2();
break;
default:
console.log('please enter a valid yes/no response');
stmt1();
}
});
}
function stmt2(){
readline.question(`I guess you want to install a backend for an existing static website [Y/n]`, (res2) => {
switch(convertInput(res2)) {
case 'y':
stmt6();
break;
case 'n':
console.log('Sorry there isnt much we can do for you right now.')
readline.close();
break;
default:
console.log('please enter a valid yes/no response');
stmt2();
}
});
}
function stmt3(){
readline.question(`Will you want to install a backend for your site? [Y/n]`, (res3) => {
switch(convertInput(res3)) {
case 'y':
stmt4();
break;
case 'n':
console.log(`please create a new IAM user and enter the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY env variables`)
console.log(`for more info check out https://github.com/gkpty/torus_cms`);
(async () => {
await open('https://console.aws.amazon.com/iam/home?region=us-east-1#/users$new?step=details');
})();
stmt5();
break;
default:
console.log('please enter a valid yes/no response');
stmt3();
}
});
}
function stmt4(){
readline.question(`Have you laready configured the backend? [Y/n]`, (res4) => {
switch(convertInput(res4)) {
case 'y':
// EXECUTE THE WEBSITE FUNCTION
siteFunc(false);
setTimeout(storBkt, 30000);
break;
case 'n':
console.log('Please configure Amplify');
console.log('run: amplify configure');
console.log('run: amplify init');
console.log('Add AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY env variables with the credentials of the IAM user created for Amplify');
console.log('Add a AWS_REGION env variable with your selected region i.e. us-east-1');
console.log('re-run the deployment script.')
readline.close();
break;
default:
console.log('please enter a valid yes/no response');
stmt4();
}
});
}
function stmt5(){
readline.question(`Have you finished configuring the env variables? [Y/n]`, (res5) => {
switch(convertInput(res5)) {
case 'y':
siteFunc(true);
break;
default:
console.log('please enter a valid yes/no response');
stmt5();
}
});
}
function stmt6(){
readline.question(`Please enter the domain name of your site ex. yourdomain.com `, (domainName) => {
if (/^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$/.test(domainName)) {
console.log("Valid Domain Name");
addVars('public_site', domainName, 'variables.json');
//addVars('bucketName', `admin.${domainName}`, 'amplify/backend/hosting/S3AndCloudFront/parameters.json');
sitebackend();
}
else {
stmt6();
}
});
}
function sitebackend(){
readline.question(`Have you already configured Amplify? [Y/n]`, (res6) => {
switch(convertInput(res6)) {
case 'y':
storBkt();
break;
case 'n':
console.log('Please configure Amplify');
console.log('run: amplify configure');
console.log('run: amplify init');
console.log('Add AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY env variables with the credentials of the IAM user created for Amplify');
console.log('Add a AWS_REGION env variable with your selected region i.e. us-east-1');
console.log('re-run the deployment script.')
readline.close();
break;
default:
console.log('please enter a valid yes/no response');
sitebackend();
}
});
}
function siteFunc(cond) {
readline.question(`Please enter the domain name of your site ex. yourdomain.com `, (domainName) => {
if (/^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$/.test(domainName)) {
console.log("Valid Domain Name");
addVars('public_site', domainName, 'variables.json');
websiteScript.script(s3, route53, path, fs, domainName);
if (cond) {
readline.close();
}
} else {
console.log("Enter Valid Domain Name");
siteFunc();
}
});
}
function storBkt() {
readline.question(`Enter the name of your storage bucket created by Amplify `, (storBktName) => {
if (storBktName.length < 1) {
console.log("Please enter a valid bucket Name");
storBkt();
} else {
addVars('storage_bucket', storBktName, 'variables.json');
//addVars('storage_bucket', storBktName, 'variables.json');
let rawdata = fs.readFileSync('variables.json');
obj = JSON.parse(rawdata);
const savedDomain = obj.public_site;
console.log('Deploying your backend...');
lambdaScript.script(iam, fs, lambda, apigateway, savedDomain, storBktName);
readline.close();
}
});
}
console.log('Hi, welcome to the almost installer!');
stmt1();