1 | const chalk = require( 'chalk' );
|
2 | const sh = require( 'shelljs' );
|
3 | const util = require( './util' );
|
4 | const config = require( './config' );
|
5 |
|
6 | const docker = {
|
7 |
|
8 | |
9 |
|
10 |
|
11 |
|
12 | isAvailable() {
|
13 | const isNotReachable = sh.exec( '(docker ps -q || printf true)', { silent: true });
|
14 |
|
15 | if ( isNotReachable.stdout === 'true' ) {
|
16 | return false;
|
17 | }
|
18 |
|
19 | return true;
|
20 | },
|
21 |
|
22 | |
23 |
|
24 |
|
25 |
|
26 | check() {
|
27 | if ( !this.isAvailable() ) {
|
28 | console.log( chalk`{red It doesn't look like you have Docker running.}` );
|
29 | process.exit( 1 );
|
30 | }
|
31 | },
|
32 |
|
33 | |
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | buildRunCmd( slug, url, container ) {
|
42 | const errors = [];
|
43 |
|
44 | const cmdParts = [
|
45 | 'docker run',
|
46 | '-d',
|
47 | ];
|
48 |
|
49 |
|
50 | if ( this.checkForOption( container.database ) ) {
|
51 |
|
52 | } else {
|
53 |
|
54 | const hostname = util.getProjectHostname( url );
|
55 | cmdParts.push( `-h ${hostname}` );
|
56 | }
|
57 |
|
58 |
|
59 | if ( this.checkForOption( container.database ) ) {
|
60 | cmdParts.push( `--name ${slug}_db` );
|
61 | } else {
|
62 | cmdParts.push( `--name ${slug}` );
|
63 | }
|
64 |
|
65 |
|
66 | if ( this.checkForOption( container.env_variables ) ) {
|
67 | cmdParts.push( `-e ${container.env_variables.join( ' -e ' )}` );
|
68 | }
|
69 |
|
70 |
|
71 | if ( this.checkForOption( container.volumes ) ) {
|
72 | cmdParts.push( `-v ${container.volumes.join( ' -v ' )}` );
|
73 | }
|
74 |
|
75 |
|
76 | if ( this.checkForOption( container.expose ) ) {
|
77 | cmdParts.push( `-p ${container.expose}` );
|
78 | }
|
79 |
|
80 |
|
81 | if ( !this.checkForOption( container.database ) && this.checkForOption( container.link ) ) {
|
82 | cmdParts.push( `--link ${container.link}` );
|
83 | }
|
84 |
|
85 |
|
86 | if ( this.checkForOption( container.image ) ) {
|
87 | cmdParts.push( container.image );
|
88 | } else {
|
89 | errors.push( chalk`Cannot find docker image.` );
|
90 | }
|
91 |
|
92 |
|
93 | if ( errors.length ) {
|
94 | errors.forEach( ( error ) => {
|
95 | console.log( chalk`{red Error:} ${error}` );
|
96 | });
|
97 | process.exit( 1 );
|
98 | }
|
99 |
|
100 | const cmd = cmdParts.join( ' ' );
|
101 |
|
102 | return cmd;
|
103 | },
|
104 |
|
105 | |
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 | buildContainerCmd( slug, container ) {
|
112 | const cmd = `docker exec ${slug} bash -c '${container.after_run_container_cmd}'`;
|
113 |
|
114 | return cmd;
|
115 | },
|
116 |
|
117 | |
118 |
|
119 |
|
120 |
|
121 |
|
122 | checkForOption( option ) {
|
123 | let optionExists = true;
|
124 |
|
125 | if ( typeof option === 'object' && util.isEmptyObject( option ) ) {
|
126 | optionExists = false;
|
127 | }
|
128 |
|
129 | if ( option === undefined || typeof option === 'undefined' ) {
|
130 | optionExists = false;
|
131 | }
|
132 |
|
133 | return optionExists;
|
134 | },
|
135 |
|
136 | |
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | setup( slug, url, typeInfo ) {
|
143 | const { containers } = typeInfo;
|
144 | const containerNames = [];
|
145 |
|
146 | containers.forEach( ( container ) => {
|
147 |
|
148 | if ( this.checkForOption( container.image ) ) {
|
149 | sh.exec( `docker pull ${container.image}` );
|
150 | } else {
|
151 | console.log( chalk`{red Error: Docker image is not defined.` );
|
152 | process.exit( 1 );
|
153 | }
|
154 |
|
155 | const runCmd = this.buildRunCmd( slug, url, container );
|
156 |
|
157 | let containerName;
|
158 |
|
159 | if ( this.checkForOption( container.database ) ) {
|
160 | containerName = `${slug}_db`;
|
161 | } else {
|
162 | containerName = `${slug}`;
|
163 | }
|
164 |
|
165 | let containerCmd = null;
|
166 |
|
167 | if ( this.checkForOption( container.after_run_container_cmd ) ) {
|
168 | containerCmd = this.buildContainerCmd( slug, container );
|
169 | }
|
170 |
|
171 |
|
172 | console.log( chalk`{blue ${runCmd}}` );
|
173 | sh.exec( runCmd );
|
174 |
|
175 |
|
176 | if ( containerCmd !== null ) {
|
177 | console.log( chalk`{blue ${containerCmd}}` );
|
178 | sh.exec( containerCmd );
|
179 | }
|
180 |
|
181 |
|
182 | if ( container.restart ) {
|
183 | sh.exec( `docker restart ${containerName}` );
|
184 | }
|
185 |
|
186 | containerNames.push( containerName );
|
187 | });
|
188 |
|
189 | config.updateContainerNames( slug, containerNames );
|
190 | },
|
191 |
|
192 | };
|
193 |
|
194 | module.exports = docker;
|