import AWS from 'aws-sdk' const access_id = "AKIA5H7XHOJEGALILOFK" const access_key = "iMv0yuqI+qC1JkoWhsbBRYMILWU3jbxkCUdOHhU0" const minersInfo: any = { "美国1": { "instanceId": "i-0c6db8886f152e055", "region": "us-west-1", "intranetIP": "172.31.25.29" }, "美国2": { "instanceId": "i-0f6406d5c58fb9d25", "region": "us-east-2", "intranetIP": "172.31.8.127" }, "美国3": { "instanceId": "i-0d05384ff720eb8e4", "region": "us-east-2", "intranetIP": "172.31.8.239" }, "新加坡": { "instanceId": "i-02584f592239b40bd", "region": "ap-southeast-1", "intranetIP": "172.31.13.123" }, "德国": { "instanceId": "i-016b06151a42bb956", "region": "eu-central-1", "intranetIP": "172.31.27.3" }, "澳大利亚": { "instanceId": "i-0e1736378e35ad9f8", "region": "ap-southeast-2", "intranetIP": "172.31.4.242" }, "巴林": { "instanceId": "i-0096cd9cc30d628df", "region": "me-south-1", "intranetIP": "172.31.15.219" } } class UpdateIPResponse { oldIP: string newIP: string isSuccess: boolean msg: string intranetIP: string constructor(oldIP: string, newIP: string, isSuccess: boolean, msg: string, intranetIP: string) { this.oldIP = oldIP this.newIP = newIP this.isSuccess = isSuccess this.msg = msg this.intranetIP = intranetIP } } export default class AWSUtil { async updateIP(minerName: string): Promise { let promise = new Promise((resolve, reject) => { let miner = minersInfo[minerName] let region = miner["region"] let instanceId = miner["instanceId"] let intranetIP = miner["intranetIP"] AWS.config.update({ region: region, accessKeyId: access_id, secretAccessKey: access_key }) let msg: string[] = [] let oldIP = "" let newIP = "" const ec2 = new AWS.EC2({ apiVersion: '2016-11-15' }); // 先查询实例的地址信息 ec2.describeAddresses({ Filters: [ { Name: 'domain', Values: ['vpc'] }, { Name: 'instance-id', Values: [instanceId] } ] }, (err, data) => { if (err) { console.log(err) msg.push("获取实例信息失败") resolve(new UpdateIPResponse(oldIP, newIP, false, msg.join(","), "")) } else { if (data && data.Addresses) { let oldAssociationId: string | undefined let address = data.Addresses[0] if (address) { oldAssociationId = address.AllocationId if (address.PublicIp) { oldIP = address.PublicIp } } // 分配一个新的弹性IP ec2.allocateAddress({ Domain: 'vpc' }, function (err, data) { if (err) { msg.push("IP分配失败") resolve(new UpdateIPResponse(oldIP, newIP, false, msg.join(","), "")) } else { if (data.PublicIp) { newIP = data.PublicIp } let paramsAssociateAddress = { AllocationId: data.AllocationId, InstanceId: instanceId } // 将新分配到的弹性IP和实例绑定 ec2.associateAddress(paramsAssociateAddress, function (err, data) { if (err) { console.log(err) msg.push("IP绑定失败") resolve(new UpdateIPResponse(oldIP, newIP, false, msg.join(","), "")) } else { console.log(data.AssociationId) msg.push("IP绑定成功") // 删除旧的弹性IP if (oldAssociationId) { ec2.releaseAddress({ AllocationId: oldAssociationId }, (err, data) => { if (err) { msg.push("释放弹性ip失败") resolve(new UpdateIPResponse(oldIP, newIP, true, msg.join(","), "")) } else { msg.push("释放原有弹性ip成功") resolve(new UpdateIPResponse(oldIP, newIP, true, msg.join(","), intranetIP)) } }) } } }) } }) } } }) }) return promise } }