/
controller.ts
148 lines (131 loc) · 4.41 KB
/
controller.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import NodeSSH from 'node-ssh'
import path from 'path'
import Logger from '../../logger'
import { INodeDeployConfig, NodeDeployer } from './deployer'
import chalk from 'chalk'
const logger = new Logger('NodeController')
const GET_VEMO_ENTRY = 'npm run vemo -- main | tail -n 1'
const PM2_OPTIONS = '-o out.log -e err.log'
export class NodeController {
ssh: any
config: INodeDeployConfig
deployer: NodeDeployer
constructor(config: INodeDeployConfig) {
this.config = {
username: 'root',
port: 22,
remotePath: `/data/tcb-service/${config.name}`,
...config
}
this.ssh = new NodeSSH()
this.deployer = new NodeDeployer(this.config)
}
async connect() {
const { host, username, port, password } = this.config
await this.ssh.connect({ host, username, port, password })
}
async deploy() {
let { vemo } = await this.deployer.deploy()
await this.start({ vemo })
}
async start({ vemo }) {
await this.connect()
await this.installDependencies()
logger.log('Starting application...')
const secret = this.injectSecret()
const { remotePath, name } = this.config
// 清理pm2进程
await this.ssh.execCommand('pm2 delete all')
if (vemo) {
logger.log('start vemo')
const { stdout, stderr } = await this.ssh.execCommand(
secret +
`pm2 start $(${GET_VEMO_ENTRY}) ${PM2_OPTIONS} --name ${name}`,
{
cwd: remotePath
}
)
console.log(stdout || stderr)
} else {
const entryPath = path.posix.resolve(remotePath, 'index.js')
logger.log(`start ${entryPath}`)
const { stdout, stderr } = await this.ssh.execCommand(
secret + `pm2 start ${entryPath} ${PM2_OPTIONS} --name ${name}`,
{
cwd: remotePath
}
)
console.log(stdout || stderr)
}
this.ssh.dispose()
}
async installDependencies() {
const { remotePath } = this.config
logger.log('Installing dependencies...')
await this.ssh.execCommand('rm -rf node_modules', {
cwd: remotePath
})
const installResult = await this.ssh.execCommand(
'npm install --production',
{
cwd: remotePath
}
)
console.log(installResult.stdout || installResult.stderr)
}
injectSecret() {
const { secretId, secretKey, token } = this.config
let authSecret = `export TENCENTCLOUD_SECRETID=${secretId} && export TENCENTCLOUD_SECRETKEY=${secretKey} && `
return token
? authSecret + `export TENCENTCLOUD_SESSIONTOKEN=${token} && `
: authSecret
}
async logs({ lines }) {
await this.connect()
const { remotePath } = this.config
const {
stdout: logContent,
stderr: logFail
} = await this.ssh.execCommand(`tail -n ${lines} out.log`, {
cwd: remotePath
})
const {
stdout: errContent,
stderr: errFail
} = await this.ssh.execCommand(`tail -n ${lines} err.log`, {
cwd: remotePath
})
console.log(chalk.gray(`${remotePath}/out.log last ${lines} lines:`))
console.log(logContent || logFail)
console.log('\n')
console.log(chalk.gray(`${remotePath}/err.log last ${lines} lines:`))
console.log(errContent || errFail)
this.ssh.dispose()
}
async reload() {
await this.connect()
const { remotePath, name } = this.config
const { stdout, stderr } = await this.ssh.execCommand(
`pm2 reload ${name}`,
{ cwd: remotePath }
)
console.log(stdout || stderr)
this.ssh.dispose()
}
async delete() {
await this.connect()
const { remotePath, name } = this.config
const { stdout, stderr } = await this.ssh.execCommand(
`pm2 delete ${name}`,
{ cwd: remotePath }
)
console.log(stdout || stderr)
this.ssh.dispose()
}
async show() {
await this.connect()
const { stdout, stderr } = await this.ssh.execCommand('pm2 list')
console.log(stdout || stderr)
this.ssh.dispose()
}
}