diff --git a/packages/ctl/ctl.js b/packages/ctl/ctl.js index cc1ce40f8a..64f618bf30 100644 --- a/packages/ctl/ctl.js +++ b/packages/ctl/ctl.js @@ -36,8 +36,8 @@ Ctl.Commands.push({ if (appConfig.admin) { bindPathPrefix = "/" + Ctl.myAppName(); proxyConfig = { - securePort: null, - insecurePort: 9414, + securePort: appConfig.securePort || null, + insecurePort: appConfig.insecurePort || 9414, bindHost: "localhost", bindPathPrefix: bindPathPrefix }; @@ -57,7 +57,8 @@ Ctl.Commands.push({ "mongo-livedata": { url: appConfig.MONGO_URL } - } + }, + deployInfo: appConfig.deployInfo }; // XXX args? env? diff --git a/tools/deploy-galaxy.js b/tools/deploy-galaxy.js index 36609ddd97..8ae60b0168 100644 --- a/tools/deploy-galaxy.js +++ b/tools/deploy-galaxy.js @@ -4,6 +4,7 @@ var path = require('path'); var fs = require('fs'); var unipackage = require('./unipackage.js'); var Fiber = require('fibers'); +var request = require('request'); // a bit of a hack var _meteor; @@ -24,7 +25,7 @@ var getGalaxy = function (context) { if (! _galaxy) { var Meteor = getMeteor(context); if (!context.galaxyUrl) { - process.stderr.write("GALAXY environment variable must be set.\n"); + process.stderr.write("Must have a deploy endpoint.\n"); process.exit(1); } @@ -71,8 +72,31 @@ var prettySub = function (galaxy, name, args, messages) { return ret; }; +exports.discoverGalaxy = function (app) { + app = app + ":" + (process.env.DISCOVERY_PORT || 443); + var url = "https://" + app + "/discovery/_GALAXY_"; + var fut = new Future(); -exports.deleteApp = function (app) { + var noDiscoveryResult = {}; + if (process.env.GALAXY) + noDiscoveryResult.deployEndpoint = process.env.GALAXY; + + request(url, function (err, resp, body) { + if (err || resp.statusCode !== 200) { + fut.return(noDiscoveryResult); + } else { + try { + var result = JSON.parse(body); + fut.return(result); + } catch (e) { + fut.return(noDiscoveryResult); + } + } + }); + return fut.wait(); +}; + +exports.deleteApp = function (context) { throw new Error("Not implemented"); }; @@ -143,7 +167,6 @@ exports.deploy = function (options) { // XXX copied from galaxy/tool/galaxy.js var fileSize = fs.statSync(starball).size; var fileStream = fs.createReadStream(starball); - var request = require('request'); var future = new Future; var req = request.put({ url: info.put, diff --git a/tools/meteor.js b/tools/meteor.js index 2835f2b930..87e9d62306 100644 --- a/tools/meteor.js +++ b/tools/meteor.js @@ -19,6 +19,7 @@ Fiber(function () { var project = require('./project.js'); var warehouse = require('./warehouse.js'); var logging = require('./logging.js'); + var deployGalaxy = require('./deploy-galaxy.js'); var Future = require('fibers/future'); // This code is duplicated in app/server/server.js. @@ -29,6 +30,8 @@ Fiber(function () { process.exit(1); } + var tunnel; + var sshTunnel = function (to, localPort, remoteEnd, keyfile) { var args = []; if (to.split(':')[1]){ @@ -103,18 +106,6 @@ Fiber(function () { // Figures out if we're in an app dir, what release we're using, etc. May // download the release if necessary. var calculateContext = function (argv) { - // 9414 because 9414xy (gAlAxy) in 1337 - context.galaxyPort = process.env.PORT || 9414; - if (process.env.GALAXY && process.env.GALAXY.indexOf("ssh://") === 0) { - context.galaxyUrl = "localhost:" + context.galaxyPort + "/ultraworld"; - context.adminBaseUrl = "localhost:" + context.galaxyPort + "/"; - context.galaxyHost = process.env.GALAXY.substr("ssh://".length); - context.sshIdentity = argv["ssh-identity"]; - } else { - context.galaxyUrl = process.env.GALAXY + "/ultraworld"; - context.adminBaseUrl = process.env.GALAXY + "/"; - } - var appDir = files.findAppDir(); context.appDir = appDir && path.resolve(appDir); context.globalReleaseVersion = calculateReleaseVersion(argv); @@ -131,6 +122,36 @@ Fiber(function () { toolsDebugMessage("Running Meteor Release " + context.releaseVersion); }; + var calculateGalaxyContextAndTunnel = function (deployEndpoint, + context, argv) { + // 9414 because 9414xy (gAlAxy) in 1337 + context.galaxyPort = process.env.PORT || 9414; + if (deployEndpoint && deployEndpoint.indexOf("ssh://") === 0) { + context.galaxyUrl = "localhost:" + context.galaxyPort + "/ultraworld"; + context.adminBaseUrl = "localhost:" + context.galaxyPort + "/"; + context.galaxyHost = deployEndpoint.substr("ssh://".length); + context.sshIdentity = argv["ssh-identity"]; + tunnel = sshTunnel(context.galaxyHost, context.galaxyPort, + "localhost:9414", context.sshIdentity); + tunnel.waitConnected(); + } else { + context.galaxyUrl = deployEndpoint; + context.adminBaseUrl = process.env.GALAXY + "/"; + } + }; + + var removeRootFromSiteName = function (site, rootSiteName) { + // If appName ends in .foo.com (where foo.com is the rootSiteName), then + // remove it. + if (! rootSiteName) + return site; + var suffixStart = site.length - rootSiteName.length; + if (suffixStart > 0 && + site.substring(suffixStart) === rootSiteName) + return site.substring(0, suffixStart - 1); // -1 to remove the dot + return site; + }; + var setReleaseVersion = function (version) { context.releaseVersion = version; @@ -793,15 +814,19 @@ Fiber(function () { mongoUrl = fut.wait(); } else if (new_argv._.length === 2) { + var discoverResults = deployGalaxy.discoverGalaxy(new_argv._[1]); + var deployEndpoint = discoverResults.deployEndpoint; + var site = removeRootFromSiteName(new_argv._[1], + discoverResults.rootSiteName); + calculateGalaxyContextAndTunnel(discoverResults, context, new_argv); // remote mode if (context.galaxyUrl) { - var deployGalaxy = require('./deploy-galaxy.js'); mongoUrl = deployGalaxy.temporaryMongoUrl({ - app: new_argv._[1], + app: site, context: context }); } else { - mongoUrl = deploy.temporaryMongoUrl(new_argv._[1]); + mongoUrl = deploy.temporaryMongoUrl(site); } } else { // usage @@ -871,14 +896,15 @@ Fiber(function () { process.exit(1); } var site = new_argv._[1]; - var useGalaxy = !!context.galaxyUrl; - - if (useGalaxy) - var deployGalaxy = require('./deploy-galaxy.js'); + var discoverResults = deployGalaxy.discoverGalaxy(site); + var deployEndpoint = discoverResults.deployEndpoint; + var rootSiteName = discoverResults.rootSiteName; + site = removeRootFromSiteName(site, rootSiteName); + calculateGalaxyContextAndTunnel(deployEndpoint, context, new_argv); if (new_argv.delete) { - if (useGalaxy) - deployGalaxy.deleteApp(site); + if (deployEndpoint) + deployGalaxy.deleteApp(context); else deploy.delete_app(site); } else { @@ -890,7 +916,7 @@ Fiber(function () { if (new_argv.settings) settings = runner.getSettings(new_argv.settings); - if (useGalaxy) { + if (deployEndpoint) { if (new_argv.password) { process.stderr.write("Galaxy does not support --password.\n"); process.exit(1); @@ -1343,12 +1369,7 @@ Fiber(function () { if (PROFILE_REQUIRE) require('./profile-require.js').printReport(); - var tunnel; try { - if (context.galaxyHost) { - tunnel = sshTunnel(context.galaxyHost, context.galaxyPort, "localhost:9414", context.sshIdentity); - tunnel.waitConnected(); - } findCommand(cmd).func(argv); } finally { if (tunnel) {