-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.js
122 lines (118 loc) · 4.35 KB
/
install.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
'use strict';
var colors = require('colors');
var AWS = require('aws-sdk');
var settings = require('./server/config/environment');
var s3 = new AWS.S3({
apiVersion: '2006-03-01',
accessKeyId: settings.amazon.aws.s3.accessKeyId,
secretAccessKey: settings.amazon.aws.s3.secretAccessKey,
region: 'us-west-1',
sslEnabled: true,
paramValidation: true
});
var db = require('./server/components/database');
var adminDB = require('./server/components/database/admins');
var userDB = require('./server/components/database/users');
var appDB = require('./server/components/database/applications');
var caseDB = require('./server/components/database/cases');
var adminView = require('./server/components/views/admin.json');
var userView = require('./server/components/views/user.json');
var appView = require('./server/components/views/application.json');
var caseView = require('./server/components/views/case.json');
/**
* Kick off the Amazon S3 Workflow
* @param {Function} callback The callback function.
* @return {Function} The callback function.
*/
function _s3Workflow (callback) {
console.log('Getting Amazon buckets.'.yellow);
s3.listBuckets(function (error, bucketData) {
if(error) {
return callback(error);
}
// Get buckets.
var buckets = bucketData.Buckets;
var bucketNames = buckets.map(function (bucket) {
return bucket.Name;
});
// Get index for the bucket we want to use for our site.
var bucketIndex = bucketNames.indexOf(settings.amazon.aws.s3.bucket);
var bucketLocation;
if(bucketIndex === -1) {
// Create bucket if we don't already have it.
var params = {
Bucket: settings.amazon.aws.s3.bucket,
ACL: 'public-read',
CreateBucketConfiguration: {
LocationConstraint: 'us-west-1'
}
};
console.log('Creating Amazon bucket.'.yellow);
s3.createBucket(params, function (error, data) {
if(error) {
return callback(error);
}
console.log('Amazon bucket created.'.green);
return callback(null);
});
} else {
console.log('Amazon Bucket already exists.'.green);
return callback(null);
}
});
}
_s3Workflow(function () {
db.create(settings.couchdb.dbs.admins, function (err) {
if(err && err.statusCode !== 412) {
console.log('Error creating admins database.'.red);
return console.log(err);
}
db.create(settings.couchdb.dbs.users, function (err) {
if(err && err.statusCode !== 412) {
console.log('Error creating users database.'.red);
return console.log(err);
}
db.create(settings.couchdb.dbs.applications, function (err) {
if(err && err.statusCode !== 412) {
console.log('Error creating application database.'.red);
return console.log(err);
}
db.create(settings.couchdb.dbs.cases, function (err) {
if(err && err.statusCode !== 412) {
console.log('Error creating cases database.'.red);
return console.log(err);
}
adminDB.insert(adminView, '_design/admins', function (err) {
// 409 is Document update conflict.
if(err && err.statusCode !== 409) {
console.log('Error inserting admin view.'.red);
return console.log(err);
}
userDB.insert(userView, '_design/users', function (err) {
// 409 is Document update conflict.
if(err && err.statusCode !== 409) {
console.log('Error inserting user view.'.red);
return console.log(err);
}
appDB.insert(appView, '_design/applications', function (err) {
// 409 is Document update conflict.
if(err && err.statusCode !== 409) {
console.log('Error inserting application view.'.red);
return console.log(err);
}
caseDB.insert(caseView, '_design/cases', function (err) {
// 409 is Document update conflict.
if(err && err.statusCode !== 409) {
console.log('Error inserting cases view.'.red);
return console.log(err);
}
console.log('DB Installation successful.'.green);
});
});
});
});
});
});
});
});
});