-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
138 lines (116 loc) · 4.08 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* Change these to the relevant values for your project. */
const imageName = 'furkleindustries-homepage';
const containerName = 'furkleindustries-homepage';
const primaryPort = 3000;
const secondaryPort = 3001;
const letsEncryptDomain = 'furkleindustries.com';
/* */
/* Dependencies */
const {
exec,
} = require('child_process');
const {
copyFile,
mkdir,
} = require('fs');
const {
join,
} = require('path');
const {
promisify,
} = require('util');
/* */
const h2 = true;
const dockerBuild = async () => {
console.log(`Building ${imageName} image.`);
await promisify(exec)(`docker build -t ${imageName} ${__dirname}`);
console.log(`Built ${imageName} container.`);
console.log('dockerBuild task complete.');
};
module.exports.dockerBuild = dockerBuild;
const dockerClean = async () => {
console.log('Cleaning docker system files.');
await promisify(exec)('docker system prune -f');
console.log('Cleaned docker system files.');
console.log('dockerClean task complete.');
};
module.exports.dockerClean = dockerClean;
const dockerKill = async () => {
console.log(`Killing ${containerName} container.`);
await promisify(exec)(`docker kill ${containerName}`);
console.log(`Killed ${containerName} container.`);
console.log('dockerKill task complete.');
};
module.exports.dockerKill = dockerKill;
const dockerRun = async () => {
console.log(`Running ${containerName} container.`);
if (h2) {
const mk = promisify(mkdir);
try {
await mk(join(__dirname, 'secrets/'));
} catch (e) {
if (e.code !== 'EEXIST') {
throw e;
}
}
try {
await mk(join(__dirname, 'secrets', 'ssl/'));
} catch (e) {
if (e.code !== 'EEXIST') {
throw e;
}
}
const cp = promisify(copyFile);
const sslDir = `/etc/letsencrypt/live/${letsEncryptDomain}/`;
await Promise.all([
cp(`${sslDir}privkey.pem`, join(__dirname, 'secrets', 'ssl', 'privkey.pem',)),
cp(`${sslDir}fullchain.pem`, join(__dirname, 'secrets', 'ssl', 'fullchain.pem')),
]);
}
await promisify(exec)('docker run ' +
/* Run the process on a separate thread from the shell. */
'-d ' +
/* Call the container containerName. */
`--name ${containerName} ` +
(h2 ?
/* Expose port secondaryPort on the container as port 80 on the
* host machine, */
`-p 80:${secondaryPort} ` +
/* and port primaryPort on the container as port 443 on the
* host machine. */
`-p 443:${primaryPort} ` :
/* Or only use HTTP. */
`-p 80:${secondaryPort} `) +
/* Run from the imageName image. */
imageName);
console.log(`Ran ${containerName} container.`);
console.log('dockerRun task complete.');
};
module.exports.dockerRun = dockerRun;
const dockerUp = async () => {
await dockerClean();
await dockerBuild();
await dockerRun();
console.log('dockerUp task complete.');
};
module.exports.dockerUp = dockerUp;
const dockerRebuild = async () => {
await dockerKill();
await dockerUp();
console.log('dockerRebuild task complete.');
};
module.exports.dockerRebuild = dockerRebuild;
const dockerStart = async () => {
console.log(`Starting ${containerName} container.`);
await promisify(exec)(`docker start ${containerName}`);
console.log(`Started ${containerName} container.`);
console.log('dockerStart task complete.');
};
module.exports.dockerStart = dockerStart;
const dockerStop = async () => {
console.log(`Stopping ${containerName} container.`);
await promisify(exec)(`docker stop ${containerName}`);
console.log(`Stopped ${containerName} container.`);
console.log('dockerStop task complete.');
};
module.exports.dockerStop = dockerStop;