UNPKG

8.11 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const sdk_1 = require("@cto.ai/sdk");
5const dockerode_1 = tslib_1.__importDefault(require("dockerode"));
6const fs = tslib_1.__importStar(require("fs-extra"));
7const env_1 = require("../constants/env");
8/**
9 * Helper function to display appropriate error message to the user
10 * @param self Is the instance of 'this'
11 * @param numRepeats Checks the number of retries the user has attempted
12 * @param type The type of error message we want to display
13 */
14function logError(self, numRepeats, type) {
15 switch (type) {
16 case 'account-create-docker-not-installed': {
17 if (!numRepeats) {
18 self.log(`\n${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.")}`);
19 self.log(`\n${sdk_1.ux.colors.reset.green('→')} https://docs.docker.com/install/`);
20 self.log(`${sdk_1.ux.colors.reset.grey("(You'll create an account with Docker in order to start the download)")}`);
21 self.log(`\nOnce installed, make sure you start the Docker app, then come back to this terminal and type ${sdk_1.ux.colors.reset.cyan("'Y'")}.`);
22 self.log("We'll be waiting right here when you're ready 👍\n");
23 }
24 else if (numRepeats >= 3) {
25 self.log(`\n${sdk_1.ux.colors.reset.cyan("Hmm. Docker still doesn't seem to be running.")}`);
26 self.log(`${sdk_1.ux.colors.reset.cyan('Please check again, or run, "ops account support" and we\'ll be happy to help you out.')}`);
27 }
28 else {
29 self.log(`\n${sdk_1.ux.colors.reset.cyan('Please check that Docker is running again and come back here.')}`);
30 }
31 break;
32 }
33 case 'account-create-docker-stopped': {
34 if (!numRepeats) {
35 self.log(`\n${sdk_1.ux.colors.reset.cyan("It looks like you have Docker installed, but it's not currently running.")}`);
36 self.log(`${sdk_1.ux.colors.reset.cyan('Please start the Docker app to continue (You can find it in the MacOS → Applications folder)')}`);
37 self.log(`\nOnce Docker is running, come back to this terminal and type ${sdk_1.ux.colors.reset.cyan("'Y'")}`);
38 self.log("We'll be waiting right here when you're ready 👍\n");
39 }
40 else if (numRepeats >= 3) {
41 self.log(`\n${sdk_1.ux.colors.reset.cyan("Hmm. Docker still doesn't seem to be running.")}`);
42 self.log(`${sdk_1.ux.colors.reset.cyan('Please check again, or run, "ops account support" and we\'ll be happy to help you out.')}`);
43 }
44 else {
45 self.log(`\n${sdk_1.ux.colors.reset.cyan('Please check that Docker is running again and come back here.')}`);
46 }
47 break;
48 }
49 default:
50 if (type.endsWith('docker-not-installed')) {
51 if (!numRepeats) {
52 self.log(`\n${sdk_1.ux.colors.reset.cyan("Uh-oh! You'll need to install and run Docker for CTO.ai ops to work properly - go here to install it now.")}`);
53 self.log(`\n${sdk_1.ux.colors.reset.green('→')} https://docs.docker.com/install/`);
54 self.log(`${sdk_1.ux.colors.reset.grey("(You'll create an account with Docker in order to start the download)")}`);
55 self.log(`\nOnce installed, make sure you start the Docker app, then come back to this terminal and type ${sdk_1.ux.colors.reset.cyan("'Y'")}.`);
56 self.log("We'll be waiting right here when you're ready 👍\n");
57 }
58 else if (numRepeats >= 3) {
59 self.log(`\n${sdk_1.ux.colors.reset.cyan("Hmm. Docker still doesn't seem to be running.")}`);
60 self.log(`${sdk_1.ux.colors.reset.cyan('Please check again, or run, "ops account support" and we\'ll be happy to help you out.')}`);
61 }
62 else {
63 self.log(`\n${sdk_1.ux.colors.reset.cyan('Please check that Docker is running again and come back here.')}`);
64 }
65 }
66 else {
67 if (!numRepeats) {
68 self.log(`\n${sdk_1.ux.colors.reset.cyan("It looks like you have Docker installed, but it's not currently running.")}`);
69 self.log(`${sdk_1.ux.colors.reset.cyan('Please start the Docker app to continue (You can find it in the MacOS → Applications folder)')}`);
70 self.log(`\nOnce Docker is running, come back to this terminal and type ${sdk_1.ux.colors.reset.cyan("'Y'")}`);
71 self.log("We'll be waiting right here when you're ready 👍\n");
72 }
73 else if (numRepeats >= 3) {
74 self.log(`\n${sdk_1.ux.colors.reset.cyan("Hmm. Docker still doesn't seem to be running.")}`);
75 self.log(`${sdk_1.ux.colors.reset.cyan('Please check again, or run, "ops account support" and we\'ll be happy to help you out.')}`);
76 }
77 else {
78 self.log(`\n${sdk_1.ux.colors.reset.cyan('Please check that Docker is running again and come back here.')}`);
79 }
80 }
81 }
82}
83/**
84 * Helper function to display the prompt to the user if they want to retry
85 */
86async function confirmReadyContinue() {
87 return sdk_1.ux.prompt({
88 type: 'confirm',
89 name: 'answer',
90 message: `${sdk_1.ux.colors.reset.cyan('Ready to continue?')}`,
91 });
92}
93/**
94 * Helper function to check the existence of docker
95 * @param self Is the instance of 'this'
96 * @param type Is the context of the caller, so we know which error to log
97 */
98async function getDocker(self, type) {
99 // Point to the docker socket
100 /**
101 * Checks whether docker is installed on the machine
102 * Keep repeating until the socket is available
103 */
104 let numRepeats = 0; // Flag to set whether the prompt has been repeated or not
105 let isDockerInstalled = false; // Flag to indicate whether docker is installed or not
106 while (!isDockerInstalled) {
107 try {
108 const stats = fs.statSync(env_1.DOCKER_SOCKET); // Resets the status of the socket
109 // Check the validity of the socket, which is only available if docker is installed
110 isDockerInstalled = stats.isSocket();
111 }
112 catch (_a) {
113 logError(self, numRepeats, `${type}-docker-not-installed`); // Log the error message
114 // Ask the user for prompt
115 let { answer } = await confirmReadyContinue();
116 // Return if user doesn't want to repeat
117 if (!answer)
118 process.exit(0); // Exit safely if there is no docker without an error
119 numRepeats++; // Adds the number of errors the user has repeated
120 }
121 }
122 /**
123 * Checks whether docker is running
124 * At this point, assume docker has already been installed
125 */
126 numRepeats = 0; // Re-initialize the flag for repeat
127 let isDockerRunning = false; // Flag to indicate whether docker is running or not
128 const docker = new dockerode_1.default({ socketPath: env_1.DOCKER_SOCKET });
129 while (!isDockerRunning) {
130 // Instantiate a new docker client
131 try {
132 await docker.ping(); // Try to ping the docker daemon
133 // If successful, set flag to true to exit the while loop
134 isDockerRunning = true;
135 }
136 catch (_b) {
137 logError(self, numRepeats, `${type}-docker-stopped`);
138 // Ask the user for prompt
139 let { answer } = await confirmReadyContinue();
140 // Return if user doesn't want to repeat
141 if (!answer)
142 process.exit(0); // Exit safely if there is no docker without an error
143 numRepeats++; // Adds the number of errors the user has repeated
144 }
145 }
146 return docker;
147}
148exports.default = getDocker;