UNPKG

@cto.ai/ops

Version:

💻 CTO.ai - The CLI built for Teams 🚀

147 lines (139 loc) 5.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const cli_sdk_1 = require("@cto.ai/cli-sdk"); const dockerode_1 = tslib_1.__importDefault(require("@cto.ai/dockerode")); const fs = tslib_1.__importStar(require("fs-extra")); const env_1 = require("./../constants/env"); const INSTALL_DOCKER_MSG = ` ${cli_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.")} ${cli_sdk_1.ux.colors.reset.green('→')} https://docs.docker.com/install/ ${cli_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 ${cli_sdk_1.ux.colors.reset.cyan("'Y'")}. We'll be waiting right here when you're ready 👍\n`; const UHOH_INSTALL_DOCKER_MSG = ` ${cli_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.")} ${cli_sdk_1.ux.colors.reset.green('→')} https://docs.docker.com/install/ ${cli_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 ${cli_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 = ` ${cli_sdk_1.ux.colors.reset.cyan("It looks like you have Docker installed, but it's not currently running.")} ${cli_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 ${cli_sdk_1.ux.colors.reset.cyan("'Y'")} We'll be waiting right here when you're ready 👍\n `; const DOCKER_STILL_NOT_RUNNING_MSG = ` ${cli_sdk_1.ux.colors.reset.cyan("Hmm. Docker still doesn't seem to be running.")} ${cli_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 = ` ${cli_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 cli_sdk_1.ux.prompt({ type: 'confirm', name: 'answer', message: `${cli_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 socketIndex = 0; let numRepeats = 0; const dockerConfigs = (0, 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' || dockerConfigs.OS === 'linux') { if (fs.existsSync(dockerConfigs.SOCKET_PATH[socketIndex])) { break; } } else if (dockerConfigs.OS === 'windows') { if (fs.existsSync(dockerConfigs.SOCKET_PATH[socketIndex])) { break; } } if (socketIndex === dockerConfigs.SOCKET_PATH.length - 1) { throw 'No Docker Found'; } socketIndex++; } catch (error) { 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 } socketIndex = socketIndex < dockerConfigs.SOCKET_PATH.length - 1 ? socketIndex++ : 0; 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[socketIndex], }); while (true) { try { await docker.ping(); // We don't need to retry if the ping succeeds break; } catch (_a) { 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;