This repository has been archived by the owner on Apr 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
admin.js
executable file
·210 lines (198 loc) · 6.63 KB
/
admin.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
#!/usr/bin/env node
// Licensed under the Apache 2.0 License. See footer for details.
var express = require('express'),
http = require('http'),
path = require('path'),
cloudant = require('cloudant'),
program = require('commander'),
dotenv = require('dotenv'),
crypto = require('crypto'),
pkg = require(path.join(__dirname, 'package.json'));
http.post = require('http-post');
dotenv.load();
var app = express();
(function(app) {
if (process.env.VCAP_SERVICES) {
var vcapServices = JSON.parse(process.env.VCAP_SERVICES);
app.set('vcapServices', vcapServices);
if (vcapServices.cloudantNoSQLDB && vcapServices.cloudantNoSQLDB.length > 0) {
var service = vcapServices.cloudantNoSQLDB[0];
if (service.credentials) {
app.set('cloudant-fieldwork-db', cloudant({
username: service.credentials.username,
password: service.credentials.password,
account: service.credentials.username
}));
}
}
}
})(app);
program.version(pkg.version);
program
.command('db <method>')
.description('Create (put) or delete the database')
.action(function(method, options) {
var fieldworkDb = app.get('cloudant-fieldwork-db');
if (!fieldworkDb) {
console.error('No database configured');
return;
}
// set up CORS for web app access
fieldworkDb.set_cors({
enable_cors: true,
allow_credentials: true,
// origins: [ "http://*.mybluemix.net","https://*.mybluemix.net"]
origins: [ "*"]
},
function(err, data) {
console.log(err, data);
});
switch (method) {
case 'put':
fieldworkDb.db.create('fieldedits', function(err, body) {
if (!err) {
console.log('Fieldedits database created');
} else {
if (412 == err.statusCode) {
console.log('Fieldedits database already exists');
} else {
console.error('Error creating fieldedits database');
}
}
});
break;
case 'delete':
fieldworkDb.db.destroy('fieldedits', function(err, body) {
if (!err) {
console.log('Fieldedits database deleted');
} else {
if (404 == err.statusCode) {
console.log('Fieldedits database does not exist');
} else {
console.error('Error deleting Fieldedits database');
}
}
});
break;
}
}).on('--help', function() {
console.log(' Examples:');
console.log();
console.log(' $ db put');
console.log(' $ db delete');
console.log();
});
program
.command('ddoc <method>')
.description('Create (put) or delete design documents')
.action(function(method, options) {
var fieldworkDb = app.get('cloudant-fieldwork-db');
if (!fieldworkDb) {
console.error('No database configured');
return;
}
var fieldeditsDb = fieldworkDb.use('fieldedits');
switch (method) {
case 'put':
// TODO: Allow this to handle migrations
var ddoc = {
_id: '_design/SpatialView',
"st_indexes": {
"spatial": {
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"index": "function(doc){\n if (doc.geometry) { st_index(doc.geometry); }\n}"
}
}
};
fieldeditsDb.insert(ddoc, function(err, body) {
if (!err) {
console.log('Design document created');
} else {
if (409 == err.statusCode) {
fieldeditsDb.get(ddoc._id, function(err, body) {
var rev = body._rev;
delete body._rev;
ddocHash = crypto.createHash('md5').update(JSON.stringify(ddoc)).digest('hex');
bodyHash = crypto.createHash('md5').update(JSON.stringify(body)).digest('hex');
if (ddocHash != bodyHash) {
ddoc._rev = rev;
fieldeditsDb.insert(ddoc, function(err, body) {
if (!err) {
console.log('Design document updated');
} else {
console.error('Error updating design document database');
}
});
} else {
console.log('Design document already exists and does not need updating');
}
});
} else {
console.error('Error creating design document database');
}
}
});
break;
case 'delete':
eventsDb.get('_design/SpatialView', function(err, doc) {
if (!err) {
eventsDb.destroy('_design/SpatialView', doc._rev, function(err, body) {
if (!err) {
console.log('Design document deleted');
} else {
if (404 == err.statusCode) {
console.log('Design document does not exist');
} else {
console.error('Error deleting design document');
}
}
});
} else {
if (404 == err.statusCode) {
console.log('Design document does not exist');
} else {
console.error('Error getting design document');
}
}
});
break;
}
}).on('--help', function() {
console.log(' Examples:');
console.log();
console.log(' $ ddoc put');
console.log(' $ ddoc delete');
console.log();
});
program
.command('track')
.description('Track application deployments')
.action(function(options) {
require('cf-deployment-tracker-client').track();
}).on('--help', function() {
console.log(' Examples:');
console.log();
console.log(' $ track');
console.log();
});
program.parse(process.argv);
//-------------------------------------------------------------------------------
// Copyright IBM Corp. 2015
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//-------------------------------------------------------------------------------