Skip to content

Commit 867aba1

Browse files
committed
Add first-run setup screens
1 parent c317a1c commit 867aba1

23 files changed

Lines changed: 1045 additions & 266 deletions

.cfignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

bluemix-settings.js

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2014, 2015 IBM Corp.
2+
* Copyright 2014, 2017 IBM Corp.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,19 +16,20 @@
1616

1717
var path = require("path");
1818
var when = require("when");
19+
var util = require("util");
1920

2021
var cfenv = require("cfenv");
2122
var appEnv = cfenv.getAppEnv();
2223

23-
var VCAP_APPLICATION = JSON.parse(process.env.VCAP_APPLICATION);
24-
var VCAP_SERVICES = JSON.parse(process.env.VCAP_SERVICES);
25-
2624
var settings = module.exports = {
2725
uiPort: process.env.PORT || 1880,
2826
mqttReconnectTime: 15000,
29-
serialReconnectTime: 15000,
3027
debugMaxLength: 1000,
3128

29+
userDir: path.join(__dirname,".node-red"),
30+
31+
flowFile: "flows.json",
32+
3233
// Add the bluemix-specific nodes in
3334
nodesDir: path.join(__dirname,"nodes"),
3435

@@ -47,46 +48,41 @@ var settings = module.exports = {
4748

4849
functionGlobalContext: { },
4950

50-
storageModule: require("./couchstorage")
51-
}
52-
// You can set adminAuth yourself in this file, but it will look for the
53-
// the following environment variables and automatically enable adminAuth
54-
// if they have been set. That means you don't have to hardcode any
55-
// credentials in this file.
56-
if (process.env.NODE_RED_USERNAME && process.env.NODE_RED_PASSWORD) {
57-
settings.adminAuth = {
58-
type: "credentials",
59-
users: function(username) {
60-
if (process.env.NODE_RED_USERNAME == username) {
61-
return when.resolve({username:username,permissions:"*"});
62-
} else {
63-
return when.resolve(null);
64-
}
65-
},
66-
authenticate: function(username, password) {
67-
if (process.env.NODE_RED_USERNAME == username &&
68-
process.env.NODE_RED_PASSWORD == password) {
69-
return when.resolve({username:username,permissions:"*"});
70-
} else {
71-
return when.resolve(null);
72-
}
51+
// Configure the logging output
52+
logging: {
53+
// Only console logging is currently supported
54+
console: {
55+
// Level of logging to be recorded. Options are:
56+
// fatal - only those errors which make the application unusable should be recorded
57+
// error - record errors which are deemed fatal for a particular request + fatal errors
58+
// warn - record problems which are non fatal + errors + fatal errors
59+
// info - record information about the general running of the application + warn + error + fatal errors
60+
// debug - record information which is more verbose than info + info + warn + error + fatal errors
61+
// trace - record very detailed logging + debug + info + warn + error + fatal errors
62+
// off - turn off all logging (doesn't affect metrics or audit)
63+
level: "info",
64+
// Whether or not to include metric events in the log output
65+
metrics: false,
66+
// Whether or not to include audit events in the log output
67+
audit: true
7368
}
7469
}
75-
}
76-
77-
settings.couchAppname = VCAP_APPLICATION['application_name'];
70+
};
7871

72+
// Look for the attached Cloudant instance to use for storage
73+
settings.couchAppname = appEnv.name;
7974
// NODE_RED_STORAGE_NAME is automatically set by this applications manifest.
8075
var storageServiceName = process.env.NODE_RED_STORAGE_NAME || new RegExp("^"+settings.couchAppname+".cloudantNoSQLDB");
8176
var couchService = appEnv.getService(storageServiceName);
8277

8378
if (!couchService) {
84-
console.log("Failed to find Cloudant service");
79+
util.log("Failed to find Cloudant service: "+storageServiceName);
8580
if (process.env.NODE_RED_STORAGE_NAME) {
86-
console.log(" - using NODE_RED_STORAGE_NAME environment variable: "+process.env.NODE_RED_STORAGE_NAME);
81+
util.log(" - using NODE_RED_STORAGE_NAME environment variable: "+process.env.NODE_RED_STORAGE_NAME);
8782
}
88-
throw new Error("No cloudant service found");
89-
}
90-
settings.couchUrl = couchService.credentials.url;
91-
92-
83+
//fall back to localfilesystem storage
84+
} else {
85+
util.log("Using Cloudant service: "+storageServiceName);
86+
settings.storageModule = require("./couchstorage");
87+
settings.couchUrl = couchService.credentials.url;
88+
}

couchstorage.js

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2014 IBM Corp.
2+
* Copyright 2014, 2017 IBM Corp.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,32 +37,33 @@ function prepopulateFlows(resolve) {
3737
try {
3838
var flow = fs.readFileSync(__dirname+"/defaults/flow.json","utf8");
3939
var flows = JSON.parse(flow);
40-
console.log(">> Adding default flow");
40+
util.log("[couchstorage] Installing default flow");
4141
promises.push(couchstorage.saveFlows(flows));
42-
} catch(err) {
43-
console.log(">> Failed to save default flow");
44-
console.log(err);
42+
} catch(err2) {
43+
util.log("[couchstorage] Failed to save default flow");
44+
util.log(err2);
4545
}
4646
} else {
47-
console.log(">> No default flow found");
47+
util.log("[couchstorage] No default flow found");
4848
}
4949
if (fs.existsSync(__dirname+"/defaults/flow_cred.json")) {
5050
try {
5151
var cred = fs.readFileSync(__dirname+"/defaults/flow_cred.json","utf8");
5252
var creds = JSON.parse(cred);
53-
console.log(">> Adding default credentials");
53+
util.log("[couchstorage] Installing default credentials");
5454
promises.push(couchstorage.saveCredentials(creds));
55-
} catch(err) {
56-
console.log(">> Failed to save default credentials");
57-
console.log(err);
55+
} catch(err2) {
56+
util.log("[couchstorage] Failed to save default credentials");
57+
util.log(err2);
5858
}
5959
} else {
60-
console.log(">> No default credentials found");
60+
util.log("[couchstorage] No default credentials found");
6161
}
6262
when.settle(promises).then(function() {
63-
resolve();
63+
resolve();
6464
});
6565
} else {
66+
// Flows already exist - leave them alone
6667
resolve();
6768
}
6869
});
@@ -75,7 +76,7 @@ var couchstorage = {
7576
var couchDb = nano(settings.couchUrl);
7677
appname = settings.couchAppname || require('os').hostname();
7778
var dbname = settings.couchDb||"nodered";
78-
79+
7980
return when.promise(function(resolve,reject) {
8081
couchDb.db.get(dbname,function(err,body) {
8182
if (err) {
@@ -127,19 +128,17 @@ var couchstorage = {
127128
} else {
128129
flowDb = couchDb.use(dbname);
129130
prepopulateFlows(resolve);
130-
resolve();
131131
}
132132
});
133133
});
134134
},
135-
136-
135+
137136
getFlows: function() {
138137
var key = appname+"/"+"flow";
139138
return when.promise(function(resolve,reject) {
140139
flowDb.get(key,function(err,doc) {
141140
if (err) {
142-
if (err.status_code != 404) {
141+
if (err.statusCode != 404) {
143142
reject(err.toString());
144143
} else {
145144
resolve([]);
@@ -151,7 +150,7 @@ var couchstorage = {
151150
});
152151
});
153152
},
154-
153+
155154
saveFlows: function(flows) {
156155
var key = appname+"/"+"flow";
157156
return when.promise(function(resolve,reject) {
@@ -169,13 +168,13 @@ var couchstorage = {
169168
});
170169
});
171170
},
172-
171+
173172
getCredentials: function() {
174173
var key = appname+"/"+"credential";
175174
return when.promise(function(resolve,reject) {
176175
flowDb.get(key,function(err,doc) {
177176
if (err) {
178-
if (err.status_code != 404) {
177+
if (err.statusCode != 404) {
179178
reject(err.toString());
180179
} else {
181180
resolve({});
@@ -187,7 +186,7 @@ var couchstorage = {
187186
});
188187
});
189188
},
190-
189+
191190
saveCredentials: function(credentials) {
192191
var key = appname+"/"+"credential";
193192
return when.promise(function(resolve,reject) {
@@ -205,13 +204,13 @@ var couchstorage = {
205204
});
206205
});
207206
},
208-
207+
209208
getSettings: function() {
210209
var key = appname+"/"+"settings";
211210
return when.promise(function(resolve,reject) {
212211
flowDb.get(key,function(err,doc) {
213212
if (err) {
214-
if (err.status_code != 404) {
213+
if (err.statusCode != 404) {
215214
reject(err.toString());
216215
} else {
217216
resolve({});
@@ -223,7 +222,7 @@ var couchstorage = {
223222
});
224223
});
225224
},
226-
225+
227226
saveSettings: function(settings) {
228227
var key = appname+"/"+"settings";
229228
return when.promise(function(resolve,reject) {
@@ -241,7 +240,7 @@ var couchstorage = {
241240
});
242241
});
243242
},
244-
243+
245244
getAllFlows: function() {
246245
var key = [appname,"flow"];
247246
return when.promise(function(resolve,reject) {
@@ -268,7 +267,7 @@ var couchstorage = {
268267
});
269268
});
270269
},
271-
270+
272271
getFlow: function(fn) {
273272
if (fn.substr(0) != "/") {
274273
fn = "/"+fn;
@@ -284,7 +283,7 @@ var couchstorage = {
284283
});
285284
});
286285
},
287-
286+
288287
saveFlow: function(fn,data) {
289288
if (fn.substr(0) != "/") {
290289
fn = "/"+fn;
@@ -304,10 +303,10 @@ var couchstorage = {
304303
}
305304
});
306305
});
307-
306+
308307
});
309308
},
310-
309+
311310
getLibraryEntry: function(type,path) {
312311
var key = appname+"/lib/"+type+(path.substr(0)!="/"?"/":"")+path;
313312
if (libraryCache[key]) {
@@ -374,7 +373,7 @@ var couchstorage = {
374373
}
375374
});
376375
});
377-
376+
378377
});
379378
}
380379
};

0 commit comments

Comments
 (0)