* Apply a RPC method on the json file * @private * @method actionFromJson * @param {string} action RPC Method * @param {object} options * @param {string|object} file file * @param {string} jsonVia action type (=only 'pipe' ?) * @param {Function}
(action, file, opts, jsonVia, cb)
| 1187 | * @param {Function} |
| 1188 | */ |
| 1189 | actionFromJson (action, file, opts, jsonVia, cb) { |
| 1190 | var appConf = {}; |
| 1191 | var ret_processes = []; |
| 1192 | var that = this; |
| 1193 | |
| 1194 | //accept programmatic calls |
| 1195 | if (typeof file == 'object') { |
| 1196 | cb = typeof jsonVia == 'function' ? jsonVia : cb; |
| 1197 | appConf = file; |
| 1198 | } |
| 1199 | else if (jsonVia == 'file') { |
| 1200 | var data = null; |
| 1201 | |
| 1202 | try { |
| 1203 | data = fs.readFileSync(file); |
| 1204 | } catch(e) { |
| 1205 | Common.printError(conf.PREFIX_MSG_ERR + 'File ' + file +' not found'); |
| 1206 | return cb ? cb(Common.retErr(e)) : that.exitCli(conf.ERROR_EXIT); |
| 1207 | } |
| 1208 | |
| 1209 | try { |
| 1210 | appConf = Common.parseConfig(data, file); |
| 1211 | } catch(e) { |
| 1212 | Common.printError(conf.PREFIX_MSG_ERR + 'File ' + file + ' malformated'); |
| 1213 | console.error(e); |
| 1214 | return cb ? cb(Common.retErr(e)) : that.exitCli(conf.ERROR_EXIT); |
| 1215 | } |
| 1216 | } else if (jsonVia == 'pipe') { |
| 1217 | appConf = Common.parseConfig(file, 'pipe'); |
| 1218 | } else { |
| 1219 | Common.printError('Bad call to actionFromJson, jsonVia should be one of file, pipe'); |
| 1220 | return that.exitCli(conf.ERROR_EXIT); |
| 1221 | } |
| 1222 | |
| 1223 | // Backward compatibility |
| 1224 | if (appConf.apps) |
| 1225 | appConf = appConf.apps; |
| 1226 | |
| 1227 | if (!Array.isArray(appConf)) |
| 1228 | appConf = [appConf]; |
| 1229 | |
| 1230 | if ((appConf = Common.verifyConfs(appConf)) instanceof Error) |
| 1231 | return cb ? cb(appConf) : that.exitCli(conf.ERROR_EXIT); |
| 1232 | |
| 1233 | eachLimit(appConf, conf.CONCURRENT_ACTIONS, function(proc, next1) { |
| 1234 | var name = ''; |
| 1235 | var new_env; |
| 1236 | |
| 1237 | if (!proc.name) |
| 1238 | name = path.basename(proc.script); |
| 1239 | else |
| 1240 | name = proc.name; |
| 1241 | |
| 1242 | if (opts.only && opts.only != name) |
| 1243 | return process.nextTick(next1); |
| 1244 | |
| 1245 | if (opts && opts.env) |
| 1246 | new_env = Common.mergeEnvironmentVariables(proc, opts.env); |