|
@@ -167,6 +167,8 @@ namespace molilian.core
|
|
|
|
|
|
|
|
public async Task<(bool, string, string)> ChangePublicIpAsync(string privateIp)
|
|
public async Task<(bool, string, string)> ChangePublicIpAsync(string privateIp)
|
|
|
{
|
|
{
|
|
|
|
|
+ LoggerLibrary log = new LoggerLibrary("ChangePublicIp");
|
|
|
|
|
+ log.Info($"=====更改公网IP:{privateIp}=====");
|
|
|
try
|
|
try
|
|
|
{
|
|
{
|
|
|
var regions = _account.regions?.Split(',');
|
|
var regions = _account.regions?.Split(',');
|
|
@@ -188,6 +190,7 @@ namespace molilian.core
|
|
|
var taraget = privateIps?.PrivateIpSet.Where(e => e.PrivateIpAddress.Equals(privateIp)).FirstOrDefault();
|
|
var taraget = privateIps?.PrivateIpSet.Where(e => e.PrivateIpAddress.Equals(privateIp)).FirstOrDefault();
|
|
|
if (taraget == null) continue;
|
|
if (taraget == null) continue;
|
|
|
|
|
|
|
|
|
|
+
|
|
|
//找到当前公网IP
|
|
//找到当前公网IP
|
|
|
string currentPublicIp = taraget?.AssociatedPublicIp?.PublicIpAddress;
|
|
string currentPublicIp = taraget?.AssociatedPublicIp?.PublicIpAddress;
|
|
|
DescribeEipAddressesResponseBodyEipAddressesEipAddress currentEip;
|
|
DescribeEipAddressesResponseBodyEipAddressesEipAddress currentEip;
|
|
@@ -222,10 +225,12 @@ namespace molilian.core
|
|
|
await Task.Delay(500);
|
|
await Task.Delay(500);
|
|
|
currentEip = DescribeEipAddresse(region, currentPublicIp) ?? throw new Exception("没有找到 EIP");
|
|
currentEip = DescribeEipAddresse(region, currentPublicIp) ?? throw new Exception("没有找到 EIP");
|
|
|
}
|
|
}
|
|
|
|
|
+ log.Info($"找到当前公网IP:{currentPublicIp}");
|
|
|
if (currentEip == null) throw new Exception("没有找到 EIP");
|
|
if (currentEip == null) throw new Exception("没有找到 EIP");
|
|
|
|
|
|
|
|
//第1步、获取当前的公网EIP
|
|
//第1步、获取当前的公网EIP
|
|
|
var currentAllocationId = currentEip.AllocationId;
|
|
var currentAllocationId = currentEip.AllocationId;
|
|
|
|
|
+ log.Info($"获取当前的公网EIP:{currentAllocationId}");
|
|
|
|
|
|
|
|
//第2步、申请新的公网ip
|
|
//第2步、申请新的公网ip
|
|
|
await Task.Delay(500);
|
|
await Task.Delay(500);
|
|
@@ -233,12 +238,27 @@ namespace molilian.core
|
|
|
var allocationId = eip.Body.AllocationId;
|
|
var allocationId = eip.Body.AllocationId;
|
|
|
string eipAddress = eip.Body.EipAddress;
|
|
string eipAddress = eip.Body.EipAddress;
|
|
|
|
|
|
|
|
|
|
+ if (string.IsNullOrEmpty(eipAddress))
|
|
|
|
|
+ {
|
|
|
|
|
+ log.Info("申请新的公网ip 可能出现异常", eip.Convert2Json());
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ log.Info($"申请新的公网ip:{eipAddress}");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
//第3步、解绑当前的公网ip
|
|
//第3步、解绑当前的公网ip
|
|
|
await Task.Delay(500);
|
|
await Task.Delay(500);
|
|
|
plus.UnassociateEipAddress(region, currentAllocationId);
|
|
plus.UnassociateEipAddress(region, currentAllocationId);
|
|
|
|
|
|
|
|
|
|
+ log.Info("解绑当前的公网ip");
|
|
|
|
|
+
|
|
|
//第4步、检查公网ip是否成功解绑
|
|
//第4步、检查公网ip是否成功解绑
|
|
|
string unPublicIpAddress = currentPublicIp;
|
|
string unPublicIpAddress = currentPublicIp;
|
|
|
|
|
+
|
|
|
|
|
+ log.Info("检查公网ip是否成功解绑");
|
|
|
|
|
+
|
|
|
while (!string.IsNullOrEmpty(unPublicIpAddress))
|
|
while (!string.IsNullOrEmpty(unPublicIpAddress))
|
|
|
{
|
|
{
|
|
|
await Task.Delay(500);
|
|
await Task.Delay(500);
|
|
@@ -247,19 +267,25 @@ namespace molilian.core
|
|
|
|
|
|
|
|
var currentPrivateIpAddress = currentPrivateIpSet.Where(e => e.PrivateIpAddress.Equals(privateIp)).FirstOrDefault();
|
|
var currentPrivateIpAddress = currentPrivateIpSet.Where(e => e.PrivateIpAddress.Equals(privateIp)).FirstOrDefault();
|
|
|
unPublicIpAddress = currentPrivateIpAddress?.AssociatedPublicIp?.PublicIpAddress;
|
|
unPublicIpAddress = currentPrivateIpAddress?.AssociatedPublicIp?.PublicIpAddress;
|
|
|
|
|
+
|
|
|
|
|
+ log.Info($"{unPublicIpAddress} 还没解绑");
|
|
|
|
|
+
|
|
|
if (string.IsNullOrEmpty(unPublicIpAddress)) break;
|
|
if (string.IsNullOrEmpty(unPublicIpAddress)) break;
|
|
|
await Task.Delay(500);
|
|
await Task.Delay(500);
|
|
|
}
|
|
}
|
|
|
|
|
+ log.Info("公网ip成功解绑");
|
|
|
|
|
+
|
|
|
|
|
|
|
|
await Task.Delay(1500);
|
|
await Task.Delay(1500);
|
|
|
//第5步、绑定新的公网ip
|
|
//第5步、绑定新的公网ip
|
|
|
var AssociateEipAddress =
|
|
var AssociateEipAddress =
|
|
|
plus.AssociateEipAddress(region, allocationId, networkInterfaceId, EIPInstanceType.NetworkInterface, privateIp);
|
|
plus.AssociateEipAddress(region, allocationId, networkInterfaceId, EIPInstanceType.NetworkInterface, privateIp);
|
|
|
|
|
|
|
|
|
|
+ log.Info($"绑定新的公网ip", AssociateEipAddress.Convert2Json());
|
|
|
await Task.Delay(500);
|
|
await Task.Delay(500);
|
|
|
//第5步、释放当前的公网IP
|
|
//第5步、释放当前的公网IP
|
|
|
plus.ReleaseEipAddress(region, currentAllocationId);
|
|
plus.ReleaseEipAddress(region, currentAllocationId);
|
|
|
-
|
|
|
|
|
|
|
+ log.Info($"释放当前的公网IP");
|
|
|
return (true, currentPublicIp, eipAddress);
|
|
return (true, currentPublicIp, eipAddress);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -268,7 +294,13 @@ namespace molilian.core
|
|
|
}
|
|
}
|
|
|
catch (Exception ex)
|
|
catch (Exception ex)
|
|
|
{
|
|
{
|
|
|
- throw new Exception(ex.Message);
|
|
|
|
|
|
|
+ log.Info($"发生异常:{ex.Message}");
|
|
|
|
|
+ throw;
|
|
|
|
|
+ }
|
|
|
|
|
+ finally
|
|
|
|
|
+ {
|
|
|
|
|
+ log.Info($"=====结束更改公网IP:{privateIp}=====");
|
|
|
|
|
+ log.Save();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|