forked from kuzzleio/kourou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkill.ts
128 lines (113 loc) · 3.26 KB
/
kill.ts
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
import { flags } from '@oclif/command'
import inquirer from 'inquirer'
import cli from 'cli-ux'
import { ChildProcess, spawn } from 'child_process'
import chalk from 'chalk'
import emoji from 'node-emoji'
import { Kommand } from '../../common'
import { execute } from '../../support/execute'
export class InstanceLogs extends Kommand {
static initSdk = false
static description =
'Stop and remove all the containers of a running kuzzle instance'
static flags = {
instance: flags.string({
char: 'i',
description: 'Kuzzle instance name [ex: stack-0]'
}),
all: flags.boolean({
char: 'a',
description: 'Kill all instances'
})
}
async runSafe() {
let instance: string = this.flags.instance
const all: boolean = this.flags.all
const instancesList = await this.getInstancesList()
if (instancesList.length === 0) {
throw new Error('There are no Kuzzle running instances')
}
if (all) {
for (const i of instancesList) {
await this.killInstance(i)
}
return
}
if (!instance) {
const responses: any = await inquirer.prompt([
{
name: 'instance',
message: 'Which kuzzle instance do you want to kill',
type: 'list',
choices: instancesList
}
])
if (!responses.instance) {
throw new Error('A running instance must be selected');
}
instance = responses.instance
}
else if (!instancesList.includes(instance)) {
throw new Error(`The instance parameter you setted ${instance} isn't running`)
}
await this.killInstance(instance)
}
private async killInstance(instanceName: string) {
const docoFilename = `/tmp/kuzzle-${instanceName}.yml`
cli.action.start(
`${emoji.get('boom')} Killing Kuzzle instance ${instanceName}`,
undefined,
{
stdout: true
}
)
const instanceKill: ChildProcess = spawn('docker-compose', [
'-f',
docoFilename,
'-p',
instanceName,
'down'
])
return new Promise(resolve => instanceKill.on('close', code => {
if (code === 0) {
cli.action.stop(
chalk.green(
`\n${emoji.get(
'thumbsup'
)} Instance ${instanceName} successfully killed.`
)
)
} else {
cli.action.stop(
chalk.red(
`\n${emoji.get(
'thumbsdown'
)} Something went wrong whilde killing instance ${instanceName}.`
)
)
}
resolve(undefined)
}))
}
private async getInstancesList(): Promise<string[]> {
let containersListProcess
try {
containersListProcess = await execute('docker', 'ps', '--format', '"{{.Names}}"')
}
catch (error) {
this.warn(`Something went wrong while getting kuzzle running instances list: ${error.message}`)
return []
}
const containersList: string[] = containersListProcess.stdout
.replace(/"/g, '')
.split('\n')
return containersList
.filter(
containerName =>
containerName.includes('kuzzle') &&
!containerName.includes('redis') &&
!containerName.includes('elasticsearch')
)
.map(containerName => containerName.split('_')[0])
}
}