UNPKG

@cto.ai/ops-rc

Version:

💻 CTO.ai Ops - The CLI built for Teams 🚀

143 lines (135 loc) 5.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const sdk_1 = require("@cto.ai/sdk"); const dockerode_1 = tslib_1.__importDefault(require("dockerode")); const fs = tslib_1.__importStar(require("fs-extra")); const env_1 = require("./../constants/env"); const INSTALL_DOCKER_MSG = ` ${sdk_1.ux.colors.reset.cyan("We're almost there! You'll just need to install Docker for CTO.ai ops to run properly - go here to install it now.")} ${sdk_1.ux.colors.reset.green('→')} https://docs.docker.com/install/ ${sdk_1.ux.colors.reset.grey("(You'll create an account with Docker in order to start the download)")} Once installed, make sure you start the Docker app, then come back to this terminal and type ${sdk_1.ux.colors.reset.cyan("'Y'")}. We'll be waiting right here when you're ready 👍\n`; const UHOH_INSTALL_DOCKER_MSG = ` ${sdk_1.ux.colors.reset.cyan("Uh-oh! You'll just need to install Docker for CTO.ai ops to run properly - go here to install it now.")} ${sdk_1.ux.colors.reset.green('→')} https://docs.docker.com/install/ ${sdk_1.ux.colors.reset.grey("(You'll create an account with Docker in order to start the download)")} Once installed, make sure you start the Docker app, then come back to this terminal and type ${sdk_1.ux.colors.reset.cyan("'Y'")}. We'll be waiting right here when you're ready 👍\n`; // TODO: This message is Mac-specific const DOCKER_NOT_RUNNING_MSG = ` ${sdk_1.ux.colors.reset.cyan("It looks like you have Docker installed, but it's not currently running.")} ${sdk_1.ux.colors.reset.cyan('Please start the Docker app to continue (You can find it in the MacOS → Applications folder)')} Once Docker is running, come back to this terminal and type ${sdk_1.ux.colors.reset.cyan("'Y'")} We'll be waiting right here when you're ready 👍\n `; const DOCKER_STILL_NOT_RUNNING_MSG = ` ${sdk_1.ux.colors.reset.cyan("Hmm. Docker still doesn't seem to be running.")} ${sdk_1.ux.colors.reset.cyan('Please check again, or run, "ops account support" and we\'ll be happy to help you out.')}`; const PLEASE_CHECK_AGAIN_MSG = ` ${sdk_1.ux.colors.reset.cyan('Please check that Docker is running again and come back here.')}`; /** * Helper function to display appropriate error message to the user * @param self Is the instance of 'this' * @param numRepeats Checks the number of retries the user has attempted * @param type The type of error message we want to display */ function logError(self, numRepeats, type) { if (numRepeats >= 3) { self.log(DOCKER_STILL_NOT_RUNNING_MSG); return; } if (numRepeats) { self.log(PLEASE_CHECK_AGAIN_MSG); return; } // If we haven't retried we'll need specific messaging switch (type) { case 'account-create-docker-not-installed': { self.log(INSTALL_DOCKER_MSG); break; } case 'account-create-docker-stopped': { self.log(DOCKER_NOT_RUNNING_MSG); break; } default: if (type.endsWith('docker-not-installed')) { self.log(UHOH_INSTALL_DOCKER_MSG); } else { self.log(DOCKER_NOT_RUNNING_MSG); } } } /** * Helper function to display the prompt to the user if they want to retry */ async function confirmReadyContinue() { return sdk_1.ux.prompt({ type: 'confirm', name: 'answer', message: `${sdk_1.ux.colors.reset.cyan('Ready to continue?')}`, }); } /** * Helper function to check the existence of docker * @param self Is the instance of 'this' * @param type Is the context of the caller, so we know which error to log */ async function getDocker(self, type) { let numRepeats = 0; const dockerConfigs = env_1.GetDockerSocketConfigs(); // Check whether docker is installed on the machine // Keep repeating until the socket is available while (true) { try { // Check the validity of the socket, which is only available if docker is installed if (dockerConfigs.OS === 'mac') { if (fs.statSync(dockerConfigs.SOCKET_PATH).isSocket()) { break; } } else if (dockerConfigs.OS === 'windows') { if (fs.statSync(dockerConfigs.SOCKET_PATH).isFile()) { break; } } else if (dockerConfigs.OS === 'linux') { if (fs.statSync(dockerConfigs.SOCKET_PATH).isSocket()) { break; } } } catch (_a) { logError(self, numRepeats, `${type}-docker-not-installed`); let { answer } = await confirmReadyContinue(); if (!answer) { process.exit(0); // Exit safely if there is no docker without an error } numRepeats++; } } // Check whether docker is running // At this point, we can assume docker has already been installed numRepeats = 0; const docker = new dockerode_1.default({ socketPath: dockerConfigs.SOCKET_PATH }); while (true) { try { await docker.ping(); // We don't need to retry if the ping succeeds break; } catch (_b) { logError(self, numRepeats, `${type}-docker-stopped`); let { answer } = await confirmReadyContinue(); if (!answer) { process.exit(0); // Exit safely if there is no docker without an error } numRepeats++; } } return docker; } exports.default = getDocker;