1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | const sdk_1 = require("@cto.ai/sdk");
|
5 | const dockerode_1 = tslib_1.__importDefault(require("dockerode"));
|
6 | const fs = tslib_1.__importStar(require("fs-extra"));
|
7 | const env_1 = require("../constants/env");
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | function 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 |
|
85 |
|
86 | async 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 |
|
95 |
|
96 |
|
97 |
|
98 | async function getDocker(self, type) {
|
99 |
|
100 | |
101 |
|
102 |
|
103 |
|
104 | let numRepeats = 0;
|
105 | let isDockerInstalled = false;
|
106 | while (!isDockerInstalled) {
|
107 | try {
|
108 | const stats = fs.statSync(env_1.DOCKER_SOCKET);
|
109 |
|
110 | isDockerInstalled = stats.isSocket();
|
111 | }
|
112 | catch (_a) {
|
113 | logError(self, numRepeats, `${type}-docker-not-installed`);
|
114 |
|
115 | let { answer } = await confirmReadyContinue();
|
116 |
|
117 | if (!answer)
|
118 | process.exit(0);
|
119 | numRepeats++;
|
120 | }
|
121 | }
|
122 | |
123 |
|
124 |
|
125 |
|
126 | numRepeats = 0;
|
127 | let isDockerRunning = false;
|
128 | const docker = new dockerode_1.default({ socketPath: env_1.DOCKER_SOCKET });
|
129 | while (!isDockerRunning) {
|
130 |
|
131 | try {
|
132 | await docker.ping();
|
133 |
|
134 | isDockerRunning = true;
|
135 | }
|
136 | catch (_b) {
|
137 | logError(self, numRepeats, `${type}-docker-stopped`);
|
138 |
|
139 | let { answer } = await confirmReadyContinue();
|
140 |
|
141 | if (!answer)
|
142 | process.exit(0);
|
143 | numRepeats++;
|
144 | }
|
145 | }
|
146 | return docker;
|
147 | }
|
148 | exports.default = getDocker;
|