This repository has been archived by the owner on Jun 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
executable file
·288 lines (262 loc) · 10.9 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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const argv = require('yargs');
const axios = require('axios');
const chalk = require('chalk');
const dotenv = require('dotenv');
const git = require('simple-git/promise');
const inquirer = require('inquirer');
const {spawn} = require('child_process');
const compose = require('docker-compose');
const deps = require('./deps');
const docker = require('./docker');
const contracts = require('./contracts');
const constants = require('./constants');
const questions = require('./questions');
function spawnProcess(cmd, args, options){
const p = spawn(cmd, args, options);
p.stdout.on('data', (data) => {
process.stdout.write(data.toString('utf-8'));
});
p.stderr.on('data', (data) => {
process.stderr.write(data.toString('utf-8'));
});
const promise = new Promise((resolve, reject) => {
p.on('error', reject);
p.on('exit', code => {
if (code === 0) {
resolve()
} else {
const err = new Error(`child exited with code ${code}`)
err.code = code
reject(err)
}
})
})
promise.child = p;
return promise;
}
async function downloadFiles() {
console.log('Downloading files...')
await axios.all([
axios.get(constants.URL.DOCKER_COMPOSE_HW),
axios.get(constants.URL.DOCKER_COMPOSE_SW),
axios.get(constants.URL.CARGO_TOML),
axios.get(constants.URL.SAMPLE_SECRET_CONTRACT),
axios.get(constants.URL.SAMPLE_SMART_CONTRACT),
axios.get(constants.URL.MIGRATIONS_CONTRACT),
axios.get(constants.URL.INITIAL_MIGRATION),
axios.get(constants.URL.DEPLOY_CONTRACTS),
axios.get(constants.URL.TRUFFLE_JS),
axios.get(constants.URL.TEST_CONTRACT),
axios.get(constants.URL.PACKAGE_JSON),
]).then(axios.spread(async (r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11) => {
fs.writeFileSync(constants.FILE.DOCKER_COMPOSE_HW, r1.data);
fs.writeFileSync(constants.FILE.DOCKER_COMPOSE_SW, r2.data);
fs.writeFileSync(`${constants.FOLDER.SECRET_CONTRACTS}/${constants.FILE.CARGO_TOML}.template`, r3.data);
let sampleContractFolder = path.join(constants.FOLDER.SECRET_CONTRACTS, constants.FOLDER.SAMPLE_CONTRACT);
if (!fs.existsSync(path.join(sampleContractFolder,'src'))) {
fs.mkdirSync(path.join(sampleContractFolder,'src'));
}
fs.writeFileSync(path.join(sampleContractFolder,'src/lib.rs'), r4.data);
fs.writeFileSync(path.join(sampleContractFolder, constants.FILE.CARGO_TOML), r3.data);
fs.writeFileSync(path.join(constants.FOLDER.SMART_CONTRACTS, constants.FILE.SAMPLE_SMART_CONTRACT), r5.data);
fs.writeFileSync(path.join(constants.FOLDER.SMART_CONTRACTS, constants.FILE.MIGRATIONS_CONTRACT), r6.data);
fs.writeFileSync(path.join(constants.FOLDER.MIGRATIONS, constants.FILE.INITIAL_MIGRATION), r7.data);
fs.writeFileSync(path.join(constants.FOLDER.MIGRATIONS, constants.FILE.DEPLOY_CONTRACTS), r8.data);
fs.writeFileSync(constants.FILE.TRUFFLE_JS, r9.data);
fs.writeFileSync(path.join(constants.FOLDER.TEST, constants.FILE.TEST_CONTRACT), r10.data)
fs.writeFileSync(constants.FILE.PACKAGE_JSON, JSON.stringify(r11.data));
console.log('Installing package dependendecies...')
await git().clone(constants.URL.GIT_CLIENT, constants.FOLDER.CLIENT)
.catch((err) => {console.log(err); process.exit(1);});
await spawnProcess('npm', ['install'], {cwd: constants.FOLDER.CLIENT});
await spawnProcess('npm', ['install']);
}))
.catch(error => {
console.log(error);
});
}
function createFolders() {
if (!fs.existsSync(constants.FOLDER.SMART_CONTRACTS)){
fs.mkdirSync(constants.FOLDER.SMART_CONTRACTS);
}
if (!fs.existsSync(constants.FOLDER.SECRET_CONTRACTS)){
fs.mkdirSync(constants.FOLDER.SECRET_CONTRACTS);
}
if (!fs.existsSync(path.join(constants.FOLDER.SECRET_CONTRACTS, constants.FOLDER.SAMPLE_CONTRACT))){
fs.mkdirSync(path.join(constants.FOLDER.SECRET_CONTRACTS, constants.FOLDER.SAMPLE_CONTRACT));
}
if (!fs.existsSync(constants.FOLDER.BUILD)){
fs.mkdirSync(path.join(constants.FOLDER.BUILD));
}
if (!fs.existsSync(path.join(constants.FOLDER.BUILD, constants.FOLDER.SMART_CONTRACTS))){
fs.mkdirSync(path.join(constants.FOLDER.BUILD, constants.FOLDER.SMART_CONTRACTS));
}
if (!fs.existsSync(path.join(constants.FOLDER.BUILD, constants.FOLDER.ENIGMA_CONTRACTS))){
fs.mkdirSync(path.join(constants.FOLDER.BUILD, constants.FOLDER.ENIGMA_CONTRACTS));
}
if (!fs.existsSync(constants.FOLDER.MIGRATIONS)){
fs.mkdirSync(constants.FOLDER.MIGRATIONS);
}
if (!fs.existsSync(constants.FOLDER.TEST)){
fs.mkdirSync(constants.FOLDER.TEST);
}
if (!fs.existsSync(constants.FOLDER.CLIENT)){
fs.mkdirSync(constants.FOLDER.CLIENT);
}
}
function contentsEnvFile(mode) {
const folder = path.join(process.cwd(), constants.FOLDER.BUILD, constants.FOLDER.ENIGMA_CONTRACTS);
return `COMPOSE_PROJECT_NAME=enigma\nSGX_MODE=${mode}\nBUILD_CONTRACTS_PATH=${folder}\nDOCKER_TAG=latest`;
}
function swhwMode() {
inquirer.prompt(questions.mode).then(answer => {
if(answer.mode === 'sw' | answer.mode === 'SW') {
fs.writeFileSync(constants.FILE.ENV, contentsEnvFile('SW'), constants.ENCODING);
if(fs.existsSync('docker-compose.yml')) {
fs.unlinkSync('docker-compose.yml');
}
fs.symlinkSync(constants.FILE.DOCKER_COMPOSE_SW, 'docker-compose.yml')
pullImages(false);
} else {
fs.writeFileSync(constants.FILE.ENV, contentsEnvFile('HW'), constants.ENCODING);
if(fs.existsSync('docker-compose.yml')) {
fs.unlinkSync('docker-compose.yml');
}
fs.symlinkSync(constants.FILE.DOCKER_COMPOSE_HW, 'docker-compose.yml')
pullImages(true);
}
});
}
function getHWMode(){
dotenv.config({path: path.resolve(deps.findBasePath(), '.env')});
if(typeof process.env.SGX_MODE === 'undefined' || (process.env.SGX_MODE != 'SW' && process.env.SGX_MODE != 'HW' )) {
console.log(`Error reading ".env" file. Run this command from the top-most project folder. Aborting....`);
process.exit();
} else if (process.env.SGX_MODE == 'HW') {
return true;
} else {
return false;
}
}
function pullImages(hwMode) {
// Should Get/Compute the actual sizes of those images
inquirer.prompt(questions.size).then(async function(answer) {
if(answer.size === 'n' | answer.size === 'N'){
process.exit()
}
dotenv.config({path: path.resolve(deps.findBasePath(), '.env')});
let dockerTag;
if (typeof process.env.DOCKER_TAG !== 'undefined' && process.env.DOCKER_TAG == 'develop') {
dockerTag = 'develop';
} else {
dockerTag = 'latest';
}
console.log('Pulling Enigma Docker images...')
await docker.pullImage(constants.DOCKER.P2P, dockerTag);
await docker.pullImage(constants.DOCKER.CONTRACT, dockerTag);
if(hwMode){
await docker.pullImage(constants.DOCKER.KM_HW, dockerTag);
await docker.pullImage(constants.DOCKER.CORE_HW, dockerTag);
} else {
await docker.pullImage(constants.DOCKER.KM_SW, dockerTag);
await docker.pullImage(constants.DOCKER.CORE_SW, dockerTag);
}
});
}
function init() {
const e = ' ______ _ \n / ____/___ (_)___ _____ ___ ____ _ \n / __/ / __ \\/ / __ `/ __ `__ \\/ __ `/ \n / /___/ / / / / /_/ / / / / / / /_/ / \n/_____/_/ /_/_/\\__, /_/ /_/ /_/\\__,_/ \n /____/ \n ____ __ __ \n / __ \\_________ / /_____ _________ / / \n / /_/ / ___/ __ \\/ __/ __ \\/ ___/ __ \\/ / \n / ____/ / / /_/ / /_/ /_/ / /__/ /_/ / / \n/_/ /_/ \\____/\\__/\\____/\\___/\\____/_/ \n \n ____ _ \n / __ \\(_)_____________ _ _____ _______ __\n / / / / / ___/ ___/ __ \\ | / / _ \\/ ___/ / / /\n / /_/ / (__ ) /__/ /_/ / |/ / __/ / / /_/ / \n/_____/_/____/\\___/\\____/|___/\\___/_/ \\__, / \n /____/ ';
const ae = e.split('\n');
const l = ` .:.
.::::-:/:.
.:::- .:/:.
.:::- .- .//:
.:::- : .//:
/o: -::/- -/s
-/ ://. -:::. -//. -:::.-/
+ :/+/:. :o/::. +
/- /-:/:. -:::/. /-
.+ .+ .:/:-:::. ./ .+
+. +. +. /. +.
-: -/ -/ -: -/
+ - :/: + .:::-. +
/-. .- .//- /-.:::- -.. /-
o.- -/o:- ::/
://. +. -:::-
://. -/ -:::.
.:/:. + -:::.
.:/://:::.
.:. `;
const al = l.split(`\n`);
const offset=1;
output = '';
for(let i=0; i<al.length; i++){
if(i>= offset && i < ae.length+offset){
output += ae[i-offset];
} else {
output += ' ';
}
output += ' ';
output += al[i]+'\n';
}
console.log(
chalk.cyan(
output
)
);
inquirer.prompt(questions.start).then(async function(answer) {
if(answer.start === 'n' | answer.start === 'N'){
process.exit()
}
await deps.checkDependencies();
createFolders();
await downloadFiles();
swhwMode();
});
}
function start() {
const baseFolder = deps.findBasePath();
compose.ps({cwd: baseFolder})
.then( async (output) => {
let myReg = new RegExp(Object.keys(constants.SERVICE).map((key) => {return `${constants.SERVICE[key]}_1`}).join('|'))
if(output.out.match(myReg)){
console.log('Enigma Network is already running, stopping it first.')
await stop();
}
console.log('Starting Enigma Network...');
spawnProcess('docker-compose', ['up'], {cwd: baseFolder});
});
}
async function stop() {
const baseFolder = deps.findBasePath();
return compose.down({ cwd: baseFolder, log: true })
.then(
err => { if(err.message){ console.log('something went wrong:', err.message)}}
);
}
argv
.command('init', 'Initialize Enigma Discovery development environment', () => {}, () => {
init();
})
.command('compile', 'Compile Secret Contracts and Smart Contracts', () => {}, () => {
deps.compile();
})
.command('migrate', 'Migrate Secret Contracts and Smart Contracts', () => {}, () => {
contracts.migrate();
})
.command('pull', 'Pull the latest images for the containers in the network', () => {}, () => {
pullImages(getHWMode());
})
.command('start', 'Launch the Discovery Docker network', () => {}, () => {
start();
})
.command('stop', 'Stop the network by stopping and removing all containers', () => {}, () => {
stop();
})
.command('test', 'Test Secret Contracts and Smart Contracts', () => {}, () => {
contracts.test();
})
.demandCommand(1)
.argv