Jenkins+Python调用阿里云API实现发布自动修改SLB权重

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jenkins+Python调用阿里云API实现发布自动修改SLB权重相关的知识,希望对你有一定的参考价值。

  • 需求
    目前线上环境系统通过Jenkins发布时,需先将发布的ECS实例手动下线,即在阿里云SLB手动修改权重为0。然后在Jenkins中发布构建,发布完成后,又在SLB上手动修改权重。现为了省去每次登录阿里云SLB控制台,手动修改SLB权重这一繁琐的手动操作。现通过调用阿里云SLB的API结合Python程序去实现,通过API实现对SLB下的ECS实例的下线、移除、增加等操作,实际情况用的比较多的是对ECS下线操作,即修改SLB下对应的ECS实例权重。
    开始实战操作:
  • 环境
    Jenkins
    Python3.8
    CentOS7
    这里附上Python3.8的安装:
    #CentOS7 源码编译安装Python3.8
    安装依赖:
    yum install gcc openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel libffi-devel tk-devel wget curl-devel
    下载:
    wget https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tgz
    解压:
    tar -xzvf Python-3.8.0.tgz
    编译安装:
    mkdir /usr/local/python3
    /root/Python-3.8.0
    ./configure --prefix=/usr/local/python3/
    make && make install
    建立软链接:
    ln -s /usr/local/python3/bin/python3 /usr/bin/python3
    ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
  • 调用阿里云API
    需先安装sdk模块
    安装 aliyun-python-sdk-core-v3
    技术图片
    安装 aliyun-python-sdk-slb
    技术图片
    关键的python脚本如下:
#!/usr/bin/env python
#-*-coding:utf-8 -*-
#@Time     : 2020/06/16
#@Author   : Joey

import sys
import json

from aliyunsdkcore.client import AcsClient
from aliyunsdkslb.request.v20140515 import DescribeLoadBalancersRequest
from aliyunsdkslb.request.v20140515 import DescribeLoadBalancerAttributeRequest
from aliyunsdkslb.request.v20140515 import SetBackendServersRequest
from aliyunsdkslb.request.v20140515 import AddBackendServersRequest
from aliyunsdkslb.request.v20140515 import RemoveBackendServersRequest

#Access Key 信息
AccessKey = ‘ak‘
AccessKeySecret = ‘ak123‘
RegionId = ‘cn-shanghai‘

client = AcsClient(AccessKey, AccessKeySecret, RegionId)

Info = ‘‘‘
Basic Commands :
  get            获取SLB信息
  edit           配置SLB的后端权重
  add            添加SLB的后端实例
  remove         删除SLB的后端实例
Usage:
    获取所有实例的信息
        python slb.py get all
    获取某一实例的信息
        python slb.py get lb-uf6rb7g4a3op3f1qi19yx
    编辑单个权重
        python slb.py edit lb-uf6rb7g4a3op3f1qi19yx [{"ServerId":"i-ecs","Weight":"95","ServerIp":"10.10.10.10","Type":"ecs"}]
    编辑多个权重(最多20个)
        python slb.py edit lb-uf6rb7g4a3op3f1qi19yx [{"ServerId":"i-ecs1","Weight":"100"},{"ServerId":"i-ecs2","Weight":"100"}]
    添加单个后端ecs
        python slb.py add lb-uf6rb7g4a3op3f1qi19yx [{"ServerId":"i-ecs01","Weight":"100"}]
    添加多个后端ecs
        python slb.py add lb-uf6rb7g4a3op3f1qi19yx [{"ServerId":"i-ecs01","Weight":"100"},{"ServerId":"i-ecs02","Weight":"100"}]
    删除单个后端ecs
        python slb.py remove lb-uf6rb7g4a3op3f1qi19yx [{"ServerId":"i-ecs01","Weight":"100"}]
    删除多个后端ecs
        python slb.py remove lb-uf6rb7g4a3op3f1qi19yx [{"ServerId":"i-ecs01","Weight":"100"},{"ServerId":"i-ecs02","Weight":"100"}]
‘‘‘

def InfoSLB():
    print(Info)

def DescribeLoadBalancers(resource):
    request = DescribeLoadBalancersRequest.DescribeLoadBalancersRequest()   #SLB实例列表
    response = client.do_action_with_exception(request)    #发起请求
    SLBInfo = json.loads(response)  #处理 response  Json字符串对象
    LoadBalancerIdList = []
    for SLBInstance in SLBInfo[‘LoadBalancers‘][‘LoadBalancer‘]:    #LoadBalancers 返回负载均衡实例列表, LoadBalancer 负载均衡实例
        LoadBalancerIdList.append(SLBInstance[‘LoadBalancerId‘])    #通过 LoadBalancerId 得到 SLB实例
        print("SLBInstance实例详细信息: %s " % SLBInstance)

    if resource == ‘all‘:
        Ali_Slb_Info = {}
        for SLBInstance in SLBInfo[‘LoadBalancers‘][‘LoadBalancer‘]:    #SLB 实例
            request = DescribeLoadBalancerAttributeRequest.DescribeLoadBalancerAttributeRequest()   #SLB实例详细信息
            request.set_LoadBalancerId(SLBInstance[‘LoadBalancerId‘])
            #request.get_LoadBalancerId()
            response = client.do_action_with_exception(request)
            Ali_Slb_Info[SLBInstance[‘LoadBalancerId‘]] = json.loads(response.decode(‘utf-8‘))    #Json字符串 反序列化成 python对象, SLB实例列表 存储到set
        print(LoadBalancerIdList)
        # print(Ali_Slb_Info[SLBInstance[‘LoadBalancerId‘]])
    elif resource in LoadBalancerIdList:
        request = DescribeLoadBalancerAttributeRequest.DescribeLoadBalancerAttributeRequest()
        request.set_LoadBalancerId(resource)    #某一SLB实例详细信息
        response = client.do_action_with_exception(request)
        response = json.loads(response.decode(‘utf-8‘))
        print("SLB后端ECS服务器列表: %s " % response[‘BackendServers‘])    #SLB后端服务器列表
    else:
        print("输入错误,请输入 all 或 SLB实例ID !")

#设置SLB后端服务器权重等信息
def SetBackendServers(resource,BackendServers):
    request = SetBackendServersRequest.SetBackendServersRequest()    #调用SetBackendServers设置后端服务器权重、Port、ECS等信息
    request.set_accept_format(‘json‘)
    request.set_BackendServers(BackendServers)
    request.set_LoadBalancerId(resource)
    print("后端ECS信息: %s" % BackendServers)
    response = client.do_action_with_exception(request)
    print("SLB返回结果为: %s" % str(response, encoding=‘utf-8‘))

#增加SLB后端实例
def AddBackendServers(resource,BackendServers):
    request = AddBackendServersRequest.AddBackendServersRequest()    #调用 AddBackendServersRequest 调整增加后端实例
    request.set_accept_format(‘json‘)
    request.set_BackendServers(BackendServers)
    request.set_LoadBalancerId(resource)

    response = client.do_action_with_exception(request)
    print(str(response, encoding=‘utf-8‘))

#移除SLB后端实例
def RemoveBackendServers(resource,BackendServers):
    request = RemoveBackendServersRequest()
    request.set_accept_format(‘json‘)
    request.set_BackendServers(BackendServers)
    request.set_LoadBalancerId(resource)

    response = client.do_action_with_exception(request)
    print(str(response, encoding=‘utf-8‘))

if __name__ == ‘__main__‘:
    if len(sys.argv) == 1:
        main()
    else:
        userInput = sys.argv[1:]
        if userInput[0] == ‘get‘ and userInput[1]:    #userInput[1] LoadBalancerId or all
            resource = userInput[1]
            print("SLB实例ID: %s " % resource)
            DescribeLoadBalancers(resource)
        elif userInput[0] == ‘edit‘ and userInput[1] and userInput[2]:    #userInput[1] LoadBalancerId; userInput[2] BackendServers
            resource = userInput[1]
            BackendServers = userInput[2]
            print("编辑的SLB实例ID: %s " % resource)
            print("编辑后端ECS,传入的参数信息: %s " % BackendServers)
            SetBackendServers(resource, BackendServers)
        elif userInput[0] == ‘add‘ and userInput[1] and userInput[2]:
            resource = userInput[1]
            BackendServers = userInput[2]
            AddBackendServers(resource, BackendServers)
        elif userInput[0] == ‘remove‘ and userInput[1] and userInput[2]:
            resource = userInput[1]
            BackendServers = userInput[2]
            RemoveBackendServers(resource, BackendServers)
        else:
            print("错误的输入!")

以上是关于Jenkins+Python调用阿里云API实现发布自动修改SLB权重的主要内容,如果未能解决你的问题,请参考以下文章

各位大神,Python怎么调用阿里云API

python调用阿里云api导出到excel

[python][工具]阿里云平台短信验证功能

[python][工具]阿里云平台短信验证功能

Powershell 调用阿里云 云解析API 实现动态域名解析

调用阿里云API 的demo示例(java/python)