示例#1
0
文件: ecsidtoip.py 项目: yh777/aliyun
 def __init__(self, ecsid):
     Aliyunsdk.__init__(self)
     self.ecsid = ecsid
     self.resultFormat = resultFormat
     self.request = DescribeInstancesRequest()
     self.request.set_accept_format(self.resultFormat)
     self.request.set_InstanceIds([self.ecsid])
示例#2
0
    def __init__(self, address, name=None, resultFormat=resultFormat):
        Aliyunsdk.__init__(self)
        self.resultFormat = resultFormat
        rel = GetSlbIpAll()
        rel.run()
        if address not in rel.iplist:
            print('IP:  %s is not exists' % address)
            os._exit(17)

        self.slbip = address
        rel = SlbIpToId(self.slbip)
        rel.run()
        self.slbid = rel.slbid

        #如果不提供name参数则根据服务器名称生成name
        if not name:
            rel = GetSlbInfo(self.slbip)
            rel.run()
            if rel.backendServerId:
                name = rel.backendServerName.split('-')[-1]
                t = mydatetime()
                name = name + '-' + t['year'] + t['mon'] + t['day']
            else:
                name = 'tmp'
                t = mydatetime()
                name = name + t['year'] + t['mon'] + t['day']
        self.slbname = name
        self.request = SetLoadBalancerNameRequest()
        self.request.set_LoadBalancerId(self.slbid)
        self.request.set_LoadBalancerName(self.slbname)
示例#3
0
    def __init__(self, slbIp, listenPort, resultFormat=resultFormat):
        Aliyunsdk.__init__(self)
        self.listenPort = int(listenPort)  #要开放的端口
        self.resultFormat = resultFormat
        self.slbIp = slbIp  #要开放端口的SLB IP
        self.slballip = GetSlbIpAll()
        self.slballip.run()
        if not self.slbIp in self.slballip.iplist:  #检查SLB IP是否有错
            print('SLB IP: %s not exists' % self.slbIp)
            os._exit(17)

        self.slbinfo = GetSlbInfo(slbIp)  #获取SLB的相关信息
        self.slbinfo.run()

        if len(self.slbinfo.listenerPorts) == 50:  #开放端口达到50个,停止添加
            print('50  Ports')
            os._exit(17)
        if self.listenPort in self.slbinfo.listenerPorts:  #如果端口已经开放则跳过
            raise SkipPort(self.listenPort)

        #设置相关参数
        self.request = CreateLoadBalancerTCPListenerRequest()
        self.request.set_accept_format(self.resultFormat)
        self.request.set_LoadBalancerId(self.slbinfo.slbid)
        self.request.set_ListenerPort(self.listenPort)
        self.request.set_BackendServerPort(self.listenPort)  #后端服务器开放的端口
        self.request.set_Bandwidth(-1)  #设置带宽,-1为不限制
示例#4
0
 def __init__(self, publicIp):
     Aliyunsdk.__init__(self)
     self.publicIp = publicIp
     self.resultFormat = resultFormat
     self.request = DescribeInstancesRequest()
     self.request.set_accept_format(self.resultFormat)
     self.request.set_PublicIpAddresses([self.publicIp])
示例#5
0
    def handling(self):
        if u'RequestId' in self.result.keys():
            rel = SlbSetName(self.slbip)  #删除后端服务器后重新设置SLB名称
            rel.run()
            print('SLB: %s backendServer is deleted' % self.slbip)

        Aliyunsdk.handling(self)
示例#6
0
 def handling(self):
     self.iplist = self.result['LoadBalancers']['LoadBalancer']
     l = []
     #print('SLB IP:')
     #sum = len(self.iplist)
     for re in self.iplist:
         l.append(re['Address'])
     self.iplist = l[:]
     #for i in self.iplist:
     #    print(i)
     #print 'Total:',sum
     Aliyunsdk.handling(self)
示例#7
0
 def handling(self):
     try:
         self.ecsid = self.result['Instances']['Instance'][0]['InstanceId']
         self.innerIp = self.result['Instances']['Instance'][0][
             'InnerIpAddress']['IpAddress'][0]
         self.instanceName = self.result['Instances']['Instance'][0][
             'InstanceName']
         self.regionId = self.result['Instances']['Instance'][0]['RegionId']
         Aliyunsdk.handling(self)
     except IndexError:
         print('IP:  %s is not exist' % self.publicIp)
         os._exit(17)
示例#8
0
 def __init__(self, address, resultFormat=resultFormat):
     Aliyunsdk.__init__(self)
     self.resultFormat = resultFormat
     rel = GetSlbIpAll()
     rel.run()
     if address not in rel.iplist:
         print('IP:  %s is not exists' % address)
         os._exit(17)
     self.request = DescribeLoadBalancersRequest()
     self.request.set_accept_format(self.resultFormat)
     self.request.set_Address(address)
     self.slbid = ''
示例#9
0
    def __init__(self, slbip, resultFormat=resultFormat):
        Aliyunsdk.__init__(self)
        self.resultFormat = resultFormat
        self.slbip = slbip  #SLB  IP
        rel = GetSlbInfo(slbip)  #获得后端服务器的ID
        rel.run()
        if not rel.backendServerId:  #如果后端未设置服务器则打印信息并退出
            print("SLB: %s has not backendServer" % slbip)
            os._exit(17)

        #删除后端服务器
        self.slbid = rel.slbid
        self.ecsid = rel.backendServerId

        self.request = RemoveBackendServersRequest()
        self.request.set_LoadBalancerId(self.slbid)
        self.request.set_BackendServers([str(self.ecsid)])
示例#10
0
    def __init__(self, slbip, ecsip, resultFormat=resultFormat):
        Aliyunsdk.__init__(self)
        self.resultFormat = resultFormat
        self.slbip = slbip
        self.ecsip = ecsip

        rel = GetSlbInfo(slbip)
        rel.run()
        self.slbid = rel.slbid
        if rel.backendServerId:  #检查SLB  IP是否正确
            print('SLB: %s is used' % slbip)
            os._exit(17)

        rel = EcsIpToId(ecsip)  #通过ecs IP获得ID
        rel.run()
        self.ecsid = rel.ecsid

        self.request = AddBackendServersRequest()
        self.request.set_LoadBalancerId(self.slbid)
        self.request.set_BackendServers([{"ServerId": str(self.ecsid)}])
示例#11
0
    def __init__(self, slbIp, listenPort, resultFormat=resultFormat):
        Aliyunsdk.__init__(self)
        self.listenPort = int(listenPort)
        self.resultFormat = resultFormat
        self.slbIp = slbIp
        self.slballip = GetSlbIpAll()
        self.slballip.run()
        if not self.slbIp in self.slballip.iplist:  #检查SLB IP是否正确,否则退出
            print('SLB IP: %s not exists' % self.slbIp)
            os._exit(17)

        self.slbinfo = GetSlbInfo(slbIp)
        self.slbinfo.run()
        if self.listenPort not in self.slbinfo.listenerPorts:  #检查要删除的端口是否存在,否则跳过
            raise SkipPort(self.listenPort)

        self.request = DeleteLoadBalancerListenerRequest()
        self.request.set_accept_format(self.resultFormat)
        self.request.set_LoadBalancerId(self.slbinfo.slbid)
        self.request.set_ListenerPort(self.listenPort)
示例#12
0
 def handling(self):
     if u'RequestId' in self.result.keys():
         rel = SlbSetName(self.slbip)  #重新设置SLB的名称,根据ECS的名称(服务器ID-月日)
         rel.run()
         print('ecs: %s => slb: %s is added' % (self.ecsip, self.slbip))
     Aliyunsdk.handling(self)
示例#13
0
 def handling(self):
     re = self.result['LoadBalancers']['LoadBalancer'][0]
     self.slbid = re['LoadBalancerId']
     self.slbip = re['Address']
     Aliyunsdk.handling(self)
示例#14
0
 def run(self):  #重载run方法
     Aliyunsdk.run(self)  #调用父类的run依法
     sp = SlbStartPort(self.slbinfo.slbid, self.listenPort)  #激活端口
     sp.run()
示例#15
0
 def __init__(self, resultFormat=resultFormat):
     Aliyunsdk.__init__(self)
     self.resultFormat = resultFormat
     self.request = DescribeLoadBalancersRequest()
     self.request.set_accept_format(self.resultFormat)
示例#16
0
 def handling(self):
     if u'RequestId' in self.result.keys():
         print('Name:  %s is setted' % self.slbname)
     Aliyunsdk.handling(self)
示例#17
0
 def handling(self):
     if u'RequestId' in self.result.keys():
         print('Port %s opened' % self.listenPort)
     Aliyunsdk.handling(self)
示例#18
0
 def __init__(self, slbid, port, resultFormat=resultFormat):
     Aliyunsdk.__init__(self)
     self.resultFormat = resultFormat
     self.request = StartLoadBalancerListenerRequest()
     self.request.set_LoadBalancerId(slbid)
     self.request.set_ListenerPort(port)