UNPKG

volxitcloud

Version:

CLI tool for VolxitCloud. See Volxit.com for more details.

263 lines 13.7 kB
#!/usr/bin/env node "use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const Constants_1 = require("../utils/Constants"); const StdOutUtil_1 = require("../utils/StdOutUtil"); const Utils_1 = require("../utils/Utils"); const CliHelper_1 = require("../utils/CliHelper"); const StorageHelper_1 = require("../utils/StorageHelper"); const ErrorFactory_1 = require("../utils/ErrorFactory"); const SpinnerHelper_1 = require("../utils/SpinnerHelper"); const ValidationsHandler_1 = require("../utils/ValidationsHandler"); const CliApiManager_1 = require("../api/CliApiManager"); const Command_1 = require("./Command"); const K = Utils_1.default.extendCommonKeys({ ip: 'volxitcloudIP', root: 'volxitcloudRootDomain', newPwd: 'newPassword', newPwdCheck: 'newPasswordCheck', email: 'certificateEmail' }); class ServerSetup extends Command_1.default { constructor() { super(...arguments); this.command = 'serversetup'; this.aliases = ['setup']; this.description = 'Performs necessary actions to prepare VolxitCloud on your server.'; this.machine = { authToken: '', baseUrl: '', name: '' }; this.password = Constants_1.default.DEFAULT_PASSWORD; this.options = (params) => [ this.getDefaultConfigFileOption(() => this.preQuestions(params)), { name: 'assumeYes', char: 'y', type: 'confirm', message: () => (params ? 'have you' : 'assume you have') + ' already started VolxitCloud container on your server' + (params ? '?' : ''), default: params && true, when: !this.configFileProvided, preProcessParam: (param) => { if (param && !param.value) { StdOutUtil_1.default.printError('\nCannot setup VolxitCloud if container is not started!\n'); StdOutUtil_1.default.printWarning('Start it by running the following line:'); StdOutUtil_1.default.printMessage('docker run -p 80:80 -p 443:443 -p 3000:3000 -v /var/run/docker.sock:/var/run/docker.sock -v /volxit:/volxit volxitcloud/volxitcloud'); StdOutUtil_1.default.printMessage('\nPlease read tutorial on Volxit.com to learn how to install VolxitCloud on a server.\n', true); } } }, { name: K.ip, char: 'i', env: 'VOLXITCLOUD_IP', aliases: [{ name: 'ipAddress', hide: true }], type: 'input', message: 'IP address of your server', default: params && Constants_1.default.SAMPLE_IP, filter: (ip) => ip.trim(), validate: (ip) => (0, ValidationsHandler_1.getErrorForIP)(ip), preProcessParam: (param) => __awaiter(this, void 0, void 0, function* () { this.ip = param.value; if (!this.findParamValue(params, K.pwd)) { // No password provided: try default password this.machine.authToken = yield this.getAuthTokenFromIp(true); } }) }, { name: K.pwd, char: 'p', env: 'VOLXITCLOUD_PASSWORD', aliases: [{ name: 'currentPassword', hide: true }], type: 'password', message: 'current VolxitCloud password', when: () => !this.machine.authToken, validate: (password) => (0, ValidationsHandler_1.getErrorForPassword)(password), preProcessParam: (param) => __awaiter(this, void 0, void 0, function* () { if (param) { // Password provided this.password = param.value; this.machine.authToken = yield this.getAuthTokenFromIp(); } }) }, { name: K.root, char: 'r', env: 'VOLXITCLOUD_ROOT_DOMAIN', aliases: [{ name: 'rootDomain', hide: true }], type: 'input', message: 'VolxitCloud server root domain', when: () => this.checkFreshInstallation(), filter: (domain) => Utils_1.default.cleanDomain(domain), validate: (domain) => domain ? true : // tslint:disable-next-line: max-line-length 'Please enter a valid root domain, for example use "test.yourdomain.com" if you setup your DNS to point "*.test.yourdomain.com" to the ip address of your server.', preProcessParam: (param) => __awaiter(this, void 0, void 0, function* () { return yield this.updateRootDomain(param.value); }) }, { name: K.newPwd, char: 'w', env: 'VOLXITCLOUD_NEW_PASSWORD', type: 'password', message: `new VolxitCloud password (min ${Constants_1.default.MIN_CHARS_FOR_PASSWORD} characters)`, when: () => this.password === Constants_1.default.DEFAULT_PASSWORD, validate: (password) => (0, ValidationsHandler_1.getErrorForPassword)(password, Constants_1.default.MIN_CHARS_FOR_PASSWORD) }, { name: K.newPwdCheck, type: 'password', message: 'enter new VolxitCloud password again', hide: true, when: () => this.paramFrom(params, K.newPwd) === Command_1.ParamType.Question, validate: (password) => (0, ValidationsHandler_1.getErrorForPassword)(password, this.paramValue(params, K.newPwd)) }, { name: K.email, char: 'e', env: 'VOLXITCLOUD_CERTIFICATE_EMAIL', aliases: [{ name: 'emailForHttps', hide: true }], type: 'input', message: '"valid" email address to get certificate and enable HTTPS', filter: (email) => email.trim(), validate: (email) => (0, ValidationsHandler_1.getErrorForEmail)(email), preProcessParam: (param) => this.enableSslAndChangePassword(param.value, this.paramValue(params, K.newPwd)) }, { name: K.name, char: 'n', env: 'VOLXITCLOUD_NAME', aliases: [{ name: 'machineName', hide: true }], type: 'input', message: 'VolxitCloud machine name, with whom the login credentials are stored locally', default: params && CliHelper_1.default.get().findDefaultVolxitName(), filter: (name) => name.trim(), validate: (name) => (0, ValidationsHandler_1.getErrorForMachineName)(name), preProcessParam: (param) => param && (this.machine.name = param.value) } ]; } preAction(cmdLineoptions) { return __awaiter(this, void 0, void 0, function* () { StdOutUtil_1.default.printMessage('Setup VolxitCloud machine on your server...\n'); return Promise.resolve(cmdLineoptions); }); } preQuestions(params) { if (this.findParamValue(params, K.name)) { const err = (0, ValidationsHandler_1.getErrorForMachineName)(this.findParamValue(params, K.name).value); if (err !== true) { StdOutUtil_1.default.printError(`${err || 'Error!'}\n`, true); } } } getAuthTokenFromIp(firstTry) { return __awaiter(this, void 0, void 0, function* () { try { return yield CliApiManager_1.default.get({ authToken: '', baseUrl: `http://${this.ip}:${Constants_1.default.SETUP_PORT}`, name: '' }).getAuthToken(this.password); } catch (e) { if (firstTry && e.volxitStatus === ErrorFactory_1.default.STATUS_WRONG_PASSWORD) { return ''; } if ((e + '').indexOf('Found. Redirecting to https://') >= 0) { StdOutUtil_1.default.printWarning('\nYou may have already setup the server! Use volxitcloud login to log into an existing server.'); } else { StdOutUtil_1.default.printWarning('\nYou may have specified a wrong IP address or not already started VolxitCloud container on your server!'); } StdOutUtil_1.default.errorHandler(e); return ''; } }); } checkFreshInstallation() { return __awaiter(this, void 0, void 0, function* () { try { const rootDomain = (yield CliApiManager_1.default.get({ authToken: this.machine.authToken, baseUrl: `http://${this.ip}:${Constants_1.default.SETUP_PORT}`, name: '' }).getVolxitInfo()).rootDomain; if (rootDomain) { StdOutUtil_1.default.printWarning(`\nYou may have already setup the server with root domain: ${rootDomain}! Use volxitcloud login to log into an existing server.`, true); } } catch (e) { StdOutUtil_1.default.errorHandler(e); } return true; }); } updateRootDomain(rootDomain) { return __awaiter(this, void 0, void 0, function* () { try { yield CliApiManager_1.default.get({ authToken: this.machine.authToken, baseUrl: `http://${this.ip}:${Constants_1.default.SETUP_PORT}`, name: '' }).updateRootDomain(rootDomain); this.machine.baseUrl = `http://${Constants_1.default.ADMIN_DOMAIN}.${rootDomain}`; } catch (e) { if (e.volxitStatus === ErrorFactory_1.default.VERIFICATION_FAILED) { StdOutUtil_1.default.printError(`\nCannot verify that ${StdOutUtil_1.default.getColoredMachineUrl(rootDomain)} points to your server IP.`); StdOutUtil_1.default.printError(`Are you sure that you setup your DNS to point "*.${rootDomain}" to ${this.ip}?`); StdOutUtil_1.default.printError(`Double check your DNS, if everything looks correct note that DNS changes take up to 24 hours to work properly. Check with your Domain Provider.`); } StdOutUtil_1.default.errorHandler(e); } }); } enableSslAndChangePassword(email, newPassword) { return __awaiter(this, void 0, void 0, function* () { let forcedSsl = false; try { SpinnerHelper_1.default.start('Enabling SSL... Takes a few seconds...'); yield CliApiManager_1.default.get(this.machine).enableRootSsl(email); this.machine.baseUrl = Utils_1.default.cleanAdminDomainUrl(this.machine.baseUrl, true); yield CliApiManager_1.default.get(this.machine).forceSsl(true); forcedSsl = true; if (newPassword !== undefined) { yield CliApiManager_1.default.get(this.machine).changePass(this.password, newPassword); this.password = newPassword; yield CliApiManager_1.default.get(this.machine).getAuthToken(this.password); } SpinnerHelper_1.default.stop(); } catch (e) { if (forcedSsl) { StdOutUtil_1.default.printError('\nServer is setup, but password was not changed due to an error. You cannot use serversetup again.'); StdOutUtil_1.default.printError(`Instead, go to ${StdOutUtil_1.default.getColoredMachineUrl(this.machine.baseUrl)} and change your password on settings page.`); StdOutUtil_1.default.printError(`Then use <volxitcloud login> command to connect to your server.`); } SpinnerHelper_1.default.fail(); StdOutUtil_1.default.errorHandler(e); } }); } action(params) { return __awaiter(this, void 0, void 0, function* () { StorageHelper_1.default.get().saveMachine(this.machine); StdOutUtil_1.default.printGreenMessage(`VolxitCloud server setup completed: it is available as ${StdOutUtil_1.default.getColoredMachine(this.machine)}\n`); StdOutUtil_1.default.printMessage('For more details and docs see Volxit.com\n'); }); } } exports.default = ServerSetup; //# sourceMappingURL=serversetup.js.map