-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand-handlers.js
98 lines (86 loc) · 2.63 KB
/
command-handlers.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
var Promise = require('bluebird');
var chalk = require('chalk');
var inquirer = require('inquirer');
function serverUsage(api, server) {
console.log(chalk.blue(`======= ${server.host} =======`));
console.log(chalk.cyan('=> Overview'));
return api.runSSHCommand(server, 'df -h /')
.then((result) => {
console.log(result.output);
})
.then(() => {
console.log(chalk.cyan('=> Apps, MongoDB, and Reverse Proxy'));
return api.runSSHCommand(server, 'du -h --max-depth=2 /opt');
})
.then((result) => {
console.log(result.output);
})
.then((result) => {
console.log(chalk.cyan('=> Docker Usage'));
return api.runSSHCommand(server, 'docker system df --verbose');
})
.then((result) => {
console.log(result.output);
})
.catch(e => {
console.log(e);
})
}
function show(api, nodemiral) {
const servers = api.getConfig().servers;
Promise.each(Object.values(servers), (server) => {
return serverUsage(api, server);
});
}
function clean(api, nodemiral) {
console.log(`
This command will ${chalk.red('permenantly delete')} files and docker items, including:
- unused docker images
- stopped docker containers
- files created by your app before the last deploy/start
- unused docker volumes.
`);
inquirer.prompt([
{
type: 'confirm',
name: 'continue',
message: 'Are you sure you want to continue?',
default: false
}
]).then(answer => {
if (!answer.continue) {
return
}
const servers = api.getConfig().servers;
const appConfig = api.getConfig().app
const appName = appConfig ? appConfig.name : undefined;
const lastPath = `/opt/${appName}/last/bundle`;
// Should never happen, but there have been several issues and
// comments on Github reporting it.
const lastInCurrent = `/opt/${appName}/current/last`;
Promise.each(Object.values(servers), (server) => {
console.log(chalk.blue(`===== Cleaning ${server.host} =====`));
console.log(chalk.cyan('=> Docker'));
return api.runSSHCommand(server, 'docker system prune -af')
.then((result) => {
console.log(result.output);
console.log(chalk.cyan('=> Files from previous deploy'));
return api.runSSHCommand(server, `rm -rf ${lastPath}`);
})
.then((result) => {
console.log(result.output);
return api.runSSHCommand(server, `rm -rf ${lastInCurrent}`)
})
.then((result) => {
console.log(result.output);
})
.catch(e => {
console.log(e);
})
});
});
}
module.exports = {
show: show,
clean: clean
};