forked from rockswang/alispot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
259 lines (238 loc) · 9.32 KB
/
index.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
const path = require('path')
const fs = require('fs')
const crypto = require('crypto')
const Core = require('@alicloud/pop-core')
const dayjs = require('dayjs')
const bunyan = require('bunyan')
const ssh = require('ssh2')
const log = bunyan.createLogger({
name: 'alispot',
streams: [{
level: 'info',
stream: process.stdout
}, {
level: 'debug',
path: path.join(__dirname, 'alispot.log')
}]
})
const options = { method: 'POST', timeout: 20000 }
function sleep (ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
function _sshConnect (params) {
return new Promise((resolve, reject) => {
const conn = new ssh.Client()
conn.on('ready', () => {
resolve(conn)
}).on('error', err => {
reject(err)
}).connect(params)
})
}
async function sshConnect (params) {
for (let retryCount = 0; ; retryCount++) {
try {
return await _sshConnect(params)
} catch (err) {
if (retryCount >= 3) throw err
log.warn(`SSH连接失败,重试第${retryCount + 1}次...`)
await sleep(500)
}
}
}
function sshExec (conn, cmd) {
return new Promise((resolve, reject) => {
conn.exec(cmd, (err, stream) => {
if (err) {
reject(err)
} else {
let out = ''
stream.on('close', (code, signal) => {
out += `CLOSE: code=${code}, signal=${signal}`
log.debug(`CLOSE: code=${code}, signal=${signal}`)
resolve(out)
}).on('data', data => {
out += `STDOUT: ${data}\n`
log.debug(`STDOUT: ${data}`)
}).stderr.on('data', data => {
out += `STDERR: ${data}\n`
log.debug(`STDERR: ${data}`)
})
}
})
})
}
async function statusCheck (client, api, params, beforeStart, interval, times, check) {
if (beforeStart > 0) await sleep(beforeStart)
let retryCount = 0
while (retryCount < times) {
const result = await client.request(api, params, options)
if (check(result)) return result
await sleep(interval)
retryCount++
}
if (retryCount >= times) throw new Error('statusCheck timeout: ' + api)
}
async function main () {
try {
const confPath = process.argv[2] || path.join(__dirname, 'config.json')
const config = JSON.parse(fs.readFileSync(confPath, 'utf8'))
const { RAM, ECS } = config
const client = new Core({
accessKeyId: RAM.accessKeyId,
accessKeySecret: RAM.accessKeySecret,
apiVersion: '2014-05-26',
endpoint: 'https://ecs.aliyuncs.com'
})
let result = await client.request('DescribeRegions', {}, options)
log.debug({ result }, 'DescribeRegions')
client.endpoint = 'https://' + result.Regions.Region.find(o => o.RegionId === ECS.RegionId).RegionEndpoint
log.info('地域"%s"的API地址:"%s"', ECS.RegionId, client.endpoint)
let params = {
RegionId: ECS.RegionId,
NetworkType: 'vpc',
InstanceType: ECS.InstanceType
}
result = await client.request('DescribeSpotPriceHistory', params, options)
log.debug({ result }, 'DescribeSpotPriceHistory')
const ZoneId = result.SpotPrices.SpotPriceType.sort((p1, p2) => p1.SpotPrice - p2.SpotPrice)[0].ZoneId
log.info('地域"%s"抢占式实例价格最低的可用区:"%s"', ECS.RegionId, ZoneId)
params = {
RegionId: ECS.RegionId,
SecurityGroupName: 'alispotCreatedSecurityGroup'
}
let VpcId, SecurityGroupId
result = await client.request('DescribeSecurityGroups', params, options)
log.debug({ result }, 'DescribeSecurityGroups')
if (result.TotalCount === 0) {
log.info('创建VPC和安全组', ECS.RegionId)
params = {
RegionId: ECS.RegionId,
CidrBlock: '172.16.0.0/24',
VpcName: 'alispotCreatedVpc'
}
result = await client.request('CreateVpc', params, options)
log.debug({ result }, 'CreateVpc')
VpcId = result.VpcId
log.info('Vpc已创建,等待Vpc启用...')
params = { RegionId: ECS.RegionId, VpcId }
const start = Date.now()
result = await statusCheck(client, 'DescribeVpcs', params, 2000, 2000, 5, r => r.Vpcs.Vpc[0].Status === 'Available')
log.debug({ result }, 'DescribeVpcs')
log.info('Vpc已创建,耗时约%s ms', (Date.now() - start))
params = {
RegionId: ECS.RegionId,
VpcId,
SecurityGroupName: 'alispotCreatedSecurityGroup'
}
result = await client.request('CreateSecurityGroup', params, options)
log.debug({ result }, 'CreateSecurityGroup')
SecurityGroupId = result.SecurityGroupId
} else {
SecurityGroupId = result.SecurityGroups.SecurityGroup[0].SecurityGroupId
VpcId = result.SecurityGroups.SecurityGroup[0].VpcId
}
log.info(`VpcId: ${VpcId}, SecurityGroupId: ${SecurityGroupId}`)
params = {
RegionId: ECS.RegionId,
SecurityGroupId,
IpProtocol: 'tcp',
SourceCidrIp: '0.0.0.0/0'
}
const port = config.ssr_server ? config.ssr_server.port + '' : '33333'
const PortRange = port.slice(0, -1) + '0/' + port.slice(0, -1) + '9'
result = await Promise.all([
client.request('AuthorizeSecurityGroup', { ...params, IpProtocol: 'icmp', PortRange: '-1/-1' }, options), // enable ping
client.request('AuthorizeSecurityGroup', { ...params, PortRange: '22/22' }, options),
client.request('AuthorizeSecurityGroup', { ...params, PortRange: '80/80' }, options),
client.request('AuthorizeSecurityGroup', { ...params, PortRange: '443/443' }, options)
])
log.debug({ result }, 'AuthorizeSecurityGroup')
log.info(`为安全组${SecurityGroupId}开启端口`)
let VSwitchId
params = { RegionId: ECS.RegionId, VpcId, ZoneId }
result = await client.request('DescribeVSwitches', params, options)
log.debug({ result }, 'DescribeVSwitches')
if (result.TotalCount === 0) {
log.info('创建VSwitch', ECS.RegionId)
params = {
RegionId: ECS.RegionId,
CidrBlock: '172.16.0.0/24',
VpcId,
ZoneId,
VSwitchName: 'alispotCreatedVSwitch'
}
result = await client.request('CreateVSwitch', params, options)
log.debug({ result }, 'CreateVSwitch')
VSwitchId = result.VSwitchId
const start = Date.now()
params = { RegionId: ECS.RegionId, VpcId, ZoneId, VSwitchId }
result = await statusCheck(client, 'DescribeVSwitches', params, 500, 1000, 3, r => r.VSwitches.VSwitch[0].Status === 'Available')
log.debug({ result }, 'DescribeVSwitches')
log.info('VSwitch已创建,耗时约%s ms', (Date.now() - start))
} else {
VSwitchId = result.VSwitches.VSwitch[0].VSwitchId
}
log.info(`VSwitchId: ${VSwitchId}`)
let { AutoReleaseTime, Password } = ECS
if (AutoReleaseTime) {
let localTime
if (/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(AutoReleaseTime)) { // YYYY-MM-DD HH:mm:ss
localTime = AutoReleaseTime
} else if (/^\d{2}:\d{2}:\d{2}$/.test(AutoReleaseTime)) { // HH:mm:ss
localTime = dayjs().format('YYYY-MM-DD') + ' ' + AutoReleaseTime
} else {
throw new Error('AutoReleaseTime格式错误,必须是本地时间"YYYY-MM-DD HH:mm:ss"或"HH:mm:ss"格式')
}
localTime = dayjs(localTime)
// 如果设置的自动释放时间早于当前时刻,则将其向后顺延1天
while (localTime.isBefore(dayjs())) localTime = localTime.add(1, 'day')
const isoTime = localTime.toISOString()
AutoReleaseTime = isoTime.replace(/\.\d{3}Z$/, 'Z')
}
Password = Password || ('AliSpot@' + crypto.createHash('MD5').update('alispot' + Date.now()).digest('hex').substr(0, 13))
params = {
...ECS,
ZoneId,
SecurityGroupId,
VSwitchId,
AutoReleaseTime,
Password,
InstanceName: 'alispotCreatedInstance'
}
result = await client.request('RunInstances', params, options)
log.debug({ result }, 'RunInstances')
const InstanceId = result.InstanceIdSets.InstanceIdSet[0]
log.info('抢占式实例已经创建,实例ID: %s,等待实例启动...', InstanceId)
params = {
RegionId: ECS.RegionId,
InstanceIds: JSON.stringify([InstanceId])
}
let start = Date.now()
result = await statusCheck(client, 'DescribeInstances', params, 10000, 5000, 20, r => r.Instances.Instance[0].Status === 'Running')
log.debug({ result }, 'DescribeInstances')
log.info('实例已启动,耗时约%s ms', (Date.now() - start))
const IpAddress = result.Instances.Instance[0].PublicIpAddress.IpAddress[0]
log.info('实例SSH连接信息: IP:%s, 端口: 22, 账户: root, 密码: %s', IpAddress, Password)
log.info('SSH连接中...')
const sshParams = {
host: IpAddress,
port: 22,
username: 'root',
password: Password
}
let conn = await sshConnect(sshParams)
log.info('SSH已连接;开始启用GoogleBBR...')
start = Date.now()
await sshExec(conn, 'wget --no-check-certificate https://github.com/rockswang/alispot/raw/master/bbr.sh && chmod +x bbr.sh && ./bbr.sh')
try { conn.end() } catch (err) { }
log.info('GoogleBBR已启用,耗时约%s ms;系统重启...', (Date.now() - start))
start = Date.now()
result = await statusCheck(client, 'DescribeInstances', params, 10000, 5000, 20, r => r.Instances.Instance[0].Status === 'Running')
log.debug({ result }, 'DescribeInstances')
log.info('实例已重启,耗时约%s ms', (Date.now() - start))
} catch (error) {
log.fatal(error)
}
}
main()