2015-02-10 19:41:24 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
require("amd-loader");
|
|
|
|
try {
|
|
|
|
require("heapdump");
|
|
|
|
} catch(e) {}
|
|
|
|
|
|
|
|
var path = require("path");
|
|
|
|
var architect = require("architect");
|
|
|
|
var optimist = require("optimist");
|
2015-06-07 19:59:50 +00:00
|
|
|
var async = require("async");
|
2015-06-17 14:08:22 +00:00
|
|
|
var os = require("os");
|
2015-06-29 08:31:41 +00:00
|
|
|
var urls = require("c9/urls");
|
2015-12-15 11:04:31 +00:00
|
|
|
var child_process = require("child_process");
|
2015-06-28 13:09:57 +00:00
|
|
|
require("c9/setup_paths.js");
|
2015-02-10 19:41:24 +00:00
|
|
|
|
|
|
|
if (process.version.match(/^v0/) && parseFloat(process.version.substr(3)) < 10) {
|
|
|
|
console.warn("You're using Node.js version " + process.version
|
|
|
|
+ ". Version 0.10 or higher is recommended. Some features will not work.");
|
|
|
|
}
|
|
|
|
|
|
|
|
var DEFAULT_CONFIG = "s";
|
|
|
|
var DEFAULT_SETTINGS = getDefaultSettings();
|
|
|
|
|
|
|
|
var shortcuts = {
|
2015-10-20 10:47:14 +00:00
|
|
|
"dev": ["ide", "preview", "user-content", "vfs", "api", "sapi", "proxy", "redis", "profile", "oldclient", "homepage", "apps-proxy", "-s", "devel"],
|
2015-12-14 10:08:40 +00:00
|
|
|
"onlinedev": ["ide", "preview", "user-content", "vfs", "api", "proxy", "oldclient", "homepage", "apps-proxy", "profile", "-s", "onlinedev"],
|
2015-10-20 10:47:14 +00:00
|
|
|
"beta": ["ide", "preview", "user-content", "vfs", "proxy", "-s", "beta"],
|
2015-12-15 11:04:31 +00:00
|
|
|
"s": ["standalone", "-s", "standalone"],
|
2015-02-10 19:41:24 +00:00
|
|
|
};
|
2015-12-19 13:30:11 +00:00
|
|
|
shortcuts.localdev = shortcuts.onlinedev.concat([
|
2015-12-15 11:04:31 +00:00
|
|
|
"-s", "beta",
|
|
|
|
"--ide.packed", "false",
|
2015-12-19 14:23:28 +00:00
|
|
|
"--ide.cdn", "false",
|
|
|
|
"--ide.forceDev", "true",
|
|
|
|
"--homepage.cdn", "false",
|
|
|
|
"--helpWithSudo",
|
2015-12-15 14:22:18 +00:00
|
|
|
"--api.port", "8281",
|
2015-12-16 22:03:09 +00:00
|
|
|
"--infra_port", "8282",
|
2015-12-19 14:24:52 +00:00
|
|
|
"--api_url", "http://api.c9.local:8281",
|
2015-12-19 13:30:11 +00:00
|
|
|
"--domains", "c9.local",
|
|
|
|
"--cdn.abbreviateVersion", "true",
|
|
|
|
]);
|
2015-10-20 10:49:21 +00:00
|
|
|
shortcuts.odev = shortcuts.onlinedev; // For backwards compatibility, if you see this in 2016 remove this line
|
2015-11-17 11:58:26 +00:00
|
|
|
var delayLoadConfigs = [
|
|
|
|
// Services that are usually not immediately needed
|
|
|
|
"preview", "user-content", "api", "apps-proxy", "worker",
|
|
|
|
// Services that are very slow to load, blocking others
|
|
|
|
"profile",
|
|
|
|
];
|
2015-02-10 19:41:24 +00:00
|
|
|
|
|
|
|
module.exports = main;
|
|
|
|
|
|
|
|
if (!module.parent)
|
|
|
|
main(process.argv.slice(2));
|
|
|
|
|
|
|
|
function getDefaultSettings() {
|
2015-06-17 14:08:22 +00:00
|
|
|
var hostname = os.hostname();
|
2015-02-10 19:41:24 +00:00
|
|
|
|
|
|
|
var suffix = hostname.trim().split("-").pop() || "";
|
|
|
|
var modes = {
|
2015-11-03 16:23:30 +00:00
|
|
|
"workflowstaging": "workflow-staging",
|
2015-02-10 19:41:24 +00:00
|
|
|
"prod": "deploy",
|
|
|
|
"beta": "beta",
|
|
|
|
"dev": "devel",
|
2015-10-28 16:34:18 +00:00
|
|
|
"onlinedev": "onlinedev",
|
|
|
|
"test": "test"
|
2015-02-10 19:41:24 +00:00
|
|
|
};
|
|
|
|
return modes[suffix] || "devel";
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.getDefaultSettings = getDefaultSettings;
|
|
|
|
|
2015-06-07 19:59:50 +00:00
|
|
|
function main(argv, config, onLoaded) {
|
2015-06-17 14:08:22 +00:00
|
|
|
var inContainer = os.hostname().match(/-\d+$/);
|
2015-06-17 14:22:35 +00:00
|
|
|
|
2015-02-10 19:41:24 +00:00
|
|
|
var options = optimist(argv)
|
|
|
|
.usage("Usage: $0 [CONFIG_NAME] [--help]")
|
|
|
|
.alias("s", "settings")
|
|
|
|
.default("settings", DEFAULT_SETTINGS)
|
|
|
|
.describe("settings", "Settings file to use")
|
|
|
|
.describe("dump", "dump config file as JSON")
|
2015-06-29 19:47:13 +00:00
|
|
|
.describe("domains", "Primary and any secondary top-level domains to use (e.g, c9.io,c9.dev)")
|
2015-06-07 20:00:43 +00:00
|
|
|
.describe("exclude", "Exclude specified service")
|
2015-12-18 10:19:03 +00:00
|
|
|
.describe("include", "Include only specified service")
|
2015-07-01 09:43:59 +00:00
|
|
|
.default("domains", inContainer && process.env.C9_HOSTNAME || process.env.C9_DOMAINS)
|
2015-02-10 19:41:24 +00:00
|
|
|
.boolean("help")
|
|
|
|
.describe("help", "Show command line options.");
|
|
|
|
|
|
|
|
var configs = options.argv._;
|
|
|
|
if (!configs.length)
|
|
|
|
configs = [config || DEFAULT_CONFIG];
|
2015-06-10 19:39:52 +00:00
|
|
|
if (options.argv.exclude && !Array.isArray(options.argv.exclude.length))
|
|
|
|
options.argv.exclude = [options.argv.exclude];
|
2015-12-15 11:04:31 +00:00
|
|
|
|
2015-12-15 15:38:05 +00:00
|
|
|
var expanded = expandShortCuts(configs);
|
2015-12-15 11:04:31 +00:00
|
|
|
|
2015-06-07 19:59:50 +00:00
|
|
|
if (expanded.length > configs.length)
|
|
|
|
return main(expanded.concat(argv.filter(function(arg) {
|
|
|
|
return !shortcuts[arg];
|
|
|
|
})), config, onLoaded);
|
|
|
|
|
2015-12-18 10:19:03 +00:00
|
|
|
if (options.argv.include)
|
|
|
|
expanded = [].concat(options.argv.include);
|
|
|
|
|
2015-06-07 19:59:50 +00:00
|
|
|
var delayed = expanded.filter(function(c) { return delayLoadConfigs.indexOf(c) !== -1 });
|
|
|
|
var notDelayed = expanded.filter(function(c) { return delayLoadConfigs.indexOf(c) === -1 });
|
|
|
|
|
2015-12-19 14:23:28 +00:00
|
|
|
if (options.helpWithSudo)
|
2015-12-15 11:04:56 +00:00
|
|
|
return child_process.execFile("sudo", ["echo -n"], main.bind(null, argv.filter(function(a) {
|
2015-12-19 14:23:28 +00:00
|
|
|
return a !== "--helpWithSudo";
|
2015-12-15 11:04:56 +00:00
|
|
|
}), config, onLoaded));
|
|
|
|
|
2015-06-07 19:59:50 +00:00
|
|
|
startConfigs(notDelayed, function() {
|
2015-11-24 00:10:39 +00:00
|
|
|
startConfigs(delayed, function() {
|
|
|
|
console.log("Cloud9 is up and running");
|
|
|
|
});
|
2015-02-10 19:41:24 +00:00
|
|
|
});
|
2015-06-07 19:59:50 +00:00
|
|
|
|
|
|
|
function startConfigs(configs, done) {
|
|
|
|
async.each(configs, function(config, next) {
|
2015-06-10 19:39:52 +00:00
|
|
|
if (options.argv.exclude && options.argv.exclude.indexOf(config) > -1)
|
2015-06-07 20:00:43 +00:00
|
|
|
return next();
|
2015-06-16 09:54:18 +00:00
|
|
|
start(config, options, function(err, result, path) {
|
|
|
|
onLoaded && onLoaded(err, result, path);
|
2015-06-07 19:59:50 +00:00
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
}, done);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function expandShortCuts(configs) {
|
|
|
|
var results = configs.slice();
|
|
|
|
for (var i = 0; i < results.length; i++) {
|
|
|
|
var expanded = shortcuts[results[i]];
|
|
|
|
if (expanded) {
|
|
|
|
results.splice.apply(results, [i, 1].concat(expanded));
|
|
|
|
i += expanded.length - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results;
|
2015-02-10 19:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function start(configName, options, callback) {
|
2015-06-07 19:59:50 +00:00
|
|
|
console.log("Starting", configName);
|
|
|
|
|
2015-02-10 19:41:24 +00:00
|
|
|
var argv = options.argv;
|
|
|
|
var settingsName = argv.settings;
|
|
|
|
|
|
|
|
if (typeof settingsName != "string")
|
|
|
|
settingsName = settingsName.pop();
|
|
|
|
|
|
|
|
var configPath = configName;
|
|
|
|
if (configPath[0] !== "/")
|
|
|
|
configPath = path.join(__dirname, "/configs/", configName);
|
|
|
|
|
|
|
|
var settings = require(path.join(__dirname, "./settings", settingsName))();
|
|
|
|
|
2015-06-29 19:47:13 +00:00
|
|
|
argv.domains = argv.domains || settings.domains;
|
|
|
|
if (settings.c9 && argv.domains)
|
2015-06-27 09:19:42 +00:00
|
|
|
urls.replaceDomains(settings, argv.domains);
|
2015-04-22 13:30:39 +00:00
|
|
|
|
2015-02-10 19:41:24 +00:00
|
|
|
var plugins = require(configPath)(settings, options);
|
|
|
|
|
|
|
|
if (argv.help) {
|
|
|
|
options.usage("Usage: $0 " + configName);
|
|
|
|
options.showHelp();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!plugins)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (module.exports.onResolvePlugins)
|
|
|
|
module.exports.onResolvePlugins(plugins, __dirname + "/plugins");
|
|
|
|
|
|
|
|
architect.resolveConfig(plugins, __dirname + "/plugins", function(err, config) {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argv.dump) {
|
|
|
|
console.log(JSON.stringify(config, null, 2));
|
|
|
|
return callback && callback(null, config);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argv._getConfig)
|
2015-04-29 20:08:55 +00:00
|
|
|
return callback && callback(null, config, configPath);
|
2015-02-10 19:41:24 +00:00
|
|
|
|
|
|
|
var app = architect.createApp(config, function (err, app) {
|
|
|
|
if (err) {
|
|
|
|
console.trace("Error while starting '%s':", configPath);
|
|
|
|
console.log(err, err.stack);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
console.log("Started '%s' with config '%s'!", configPath, settingsName);
|
|
|
|
|
|
|
|
callback && callback(null, app);
|
|
|
|
});
|
|
|
|
|
|
|
|
app.on("service", function(name, plugin) {
|
|
|
|
if (typeof plugin !== "function")
|
|
|
|
plugin.name = name;
|
|
|
|
});
|
|
|
|
});
|
2015-06-17 10:14:53 +00:00
|
|
|
}
|