forked from amazon-archives/aws-ai-tracking-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmasterBuild.js
More file actions
222 lines (197 loc) · 6.39 KB
/
Copy pathmasterBuild.js
File metadata and controls
222 lines (197 loc) · 6.39 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
/*
Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Amazon Software License (the "License"). You may not use this file
except in compliance with the License. A copy of the License is located at
http://aws.amazon.com/asl/
or in the "license" file accompanying this file. This file is distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or implied. See the
License for the specific language governing permissions and limitations under the License.
*/
const cp = require('child_process');
const fs = require('fs');
const AWS = require('aws-sdk');
const argv = require('minimist')(process.argv.slice(2));
const bootstrap_bucket = (argv["bootstrap-bucket"] !== undefined) ? argv["bootstrap-bucket"] : undefined;
const bootstrap_bucket_artifacts = "artifacts";
const bootstrap_bucket_path = bootstrap_bucket + "/" + bootstrap_bucket_artifacts;
const webapp_bucket = (argv["webapp-bucket"] != undefined) ? argv["webapp-bucket"] : process.env.WEBAPP_BUCKET;
const webapp_bucket_dashboard = (argv["webapp-bucket-dashboard"] != undefined) ? argv["webapp-bucket-dashboard"] : process.env.WEBAPP_BUCKET_DASHBOARD;
const botname = (argv.botname != undefined) ? argv.botname : process.env.BOTNAME;
const region = (argv.region != undefined) ? argv.region : process.env.AWS_DEFAULT_REGION;
let model = (argv.model != undefined) ? argv.model : process.env.MODEL;
const cognito_poolid = (argv["cognito-poolid"] !== undefined) ? argv["cognito-poolid"] : undefined;
function usage() {
console.error("Invalid options or environment");
console.error("node masterBuild.js " +
"--bootstrap-bucket bucket " +
"--webapp-bucket bucket " +
"--webapp-bucket-dashboard bucket" +
"--botname botname " +
"--model modelfilepath " +
"--cognito-poolid poolid " +
"--region us-east-1 ");
}
if (webapp_bucket === undefined) {
usage();
return;
} else {
process.env.WEBAPP_BUCKET = webapp_bucket;
}
if (webapp_bucket_dashboard === undefined) {
usage();
return;
} else {
process.env.WEBAPP_BUCKET_DASHBOARD = webapp_bucket_dashboard;
}
if (bootstrap_bucket === undefined) {
usage();
return;
} else {
process.env.BOOTSTRAP_BUCKET_PATH = bootstrap_bucket_path;
}
if (region === undefined) {
usage();
return;
} else {
const awsConfig = {
region: region
};
AWS.config.update(awsConfig);
process.env.AWS_REGION = region;
}
if (botname === undefined) {
usage();
return;
} else {
process.env.BOT_NAME = botname;
}
if (cognito_poolid === undefined) {
usage();
return;
} else {
process.env.POOL_ID = cognito_poolid;
}
if (model === undefined) {
model = "LexAppBuilder/model/TrackingBotModel.json";
}
/**
* Copy TrackingBotModel to dashboard-app for use in the dashboard
*/
copyFile(model,"dashboard-app/dashboard-app/src/assets/TrackingBotModel.json")
.then( function(data) {
console.info("data returned: " + JSON.stringify(data,null,2));
if (data) {
console.info("Lex Model copy complete to dashboard-app");
}
makeLexWebUi(cp).then(function (data) {
if (data) {
console.info("finished making lex web ui");
}
makeDashboardUi(cp).then(function (data) {
if (data) {
console.info("finished making dashboard ui");
}
makeLexApp(cp).then(function (data) {
if (data) {
console.info("finished making lex bot");
}
}, function(error) {
console.error("failed to create lex app and bot: " + JSON.stringify(error,null,2));
process.exitCode = 1;
});
}, function (error) {
console.error("failed to create dashboard web ui: " + JSON.stringify(error,null,2));
process.exitCode = 1;
})
}, function (error) {
console.error("failed to create lex web ui: " + JSON.stringify(error,null,2));
process.exitCode = 1;
});
}, function(error) {
console.info("Lex Model copy to dashboard-app failed: " + JSON.stringify(error,null,2));
process.exitCode = 1;
});
function makeLexWebUi(cp) {
return new Promise(function (resolve, reject) {
process.chdir('aws-lex-web-ui');
let out = cp.spawnSync('make', ['build']);
console.log("stdout");
console.log(out.stdout.toString("utf8"));
console.log("stderr");
console.log(out.stderr.toString("utf8"));
if (out.status !== 0) {
reject(out.error);
}
out = cp.spawnSync('make', ['s3deploy']);
console.log("stdout");
console.log(out.stdout.toString("utf8"));
console.log("stderr");
console.log(out.stderr.toString("utf8"));
if (out.status !== 0) {
reject(out.error);
}
process.chdir('..');
resolve();
});
}
function makeDashboardUi(cp) {
return new Promise(function (resolve, reject) {
process.chdir('dashboard-app');
let out = cp.spawnSync('make', ['build']);
console.log("stdout");
console.log(out.stdout.toString("utf8"));
console.log("stderr");
console.log(out.stderr.toString("utf8"));
if (out.status !== 0) {
reject(out.error);
}
out = cp.spawnSync('make', ['s3deploy']);
console.log("stdout");
console.log(out.stdout.toString("utf8"));
console.log("stderr");
console.log(out.stderr.toString("utf8"));
if (out.status !== 0) {
reject(out.error);
}
process.chdir('..');
resolve();
});
}
function makeLexApp(cp) {
return new Promise(function (resolve, reject) {
const absModelPath = fs.realpathSync(model);
process.chdir('LexAppBuilder');
const out = cp.spawnSync('node', ['install-model.js',
'--botname', botname,
'--model', absModelPath,
'--s3', bootstrap_bucket,
'--region', region]);
console.info("stdout");
console.info(out.stdout.toString("utf8"));
console.info("stderr");
console.info(out.stderr.toString("utf8"));
if (out.status !== 0) {
reject(out.error);
}
process.chdir('..');
resolve();
});
}
function copyFile(source, target) {
return new Promise(function(resolve, reject) {
if (!fs.existsSync(source)) {
reject("Source does not exist");
}
const rd = fs.createReadStream(source);
rd.on('error', rejectCleanup);
const wr = fs.createWriteStream(target);
wr.on('error', rejectCleanup);
function rejectCleanup(err) {
rd.destroy();
wr.end();
reject(err);
}
wr.on('finish', function() {wr.end(); wr.close(); resolve("success")});
rd.pipe(wr);
});
}