Authored by chunhua.zhang

提交脚本

  1 +yoho.lb.switch
  2 +=========
  3 +
  4 +切换load balancer (负载均衡器,简称lb)
  5 +
  6 +
  7 +Requirements
  8 +------------
  9 +
  10 +Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required.
  11 +
  12 +Role Variables
  13 +--------------
  14 +
  15 +A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well.
  16 +
  17 +Dependencies
  18 +------------
  19 +
  20 +A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles.
  21 +
  22 +Example Playbook
  23 +----------------
  24 +
  25 +Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too:
  26 +
  27 + - hosts: servers
  28 + roles:
  29 + - { role: username.rolename, x: 42 }
  30 +
  31 +License
  32 +-------
  33 +
  34 +BSD
  35 +
  36 +Author Information
  37 +------------------
  38 +
  39 +An optional section for the role authors to include contact information, or a website (HTML is not allowed).
  1 +---
  2 +# defaults file for yoho.lb.switch
  1 +---
  2 +# handlers file for yoho.lb.switch
  1 +galaxy_info:
  2 + author: your name
  3 + description: your description
  4 + company: your company (optional)
  5 +
  6 + # If the issue tracker for your role is not on github, uncomment the
  7 + # next line and provide a value
  8 + # issue_tracker_url: http://example.com/issue/tracker
  9 +
  10 + # Some suggested licenses:
  11 + # - BSD (default)
  12 + # - MIT
  13 + # - GPLv2
  14 + # - GPLv3
  15 + # - Apache
  16 + # - CC-BY
  17 + license: license (GPLv2, CC-BY, etc)
  18 +
  19 + min_ansible_version: 1.2
  20 +
  21 + # If this a Container Enabled role, provide the minimum Ansible Container version.
  22 + # min_ansible_container_version:
  23 +
  24 + # Optionally specify the branch Galaxy will use when accessing the GitHub
  25 + # repo for this role. During role install, if no tags are available,
  26 + # Galaxy will use this branch. During import Galaxy will access files on
  27 + # this branch. If Travis integration is configured, only notifications for this
  28 + # branch will be accepted. Otherwise, in all cases, the repo's default branch
  29 + # (usually master) will be used.
  30 + #github_branch:
  31 +
  32 + #
  33 + # platforms is a list of platforms, and each platform has a name and a list of versions.
  34 + #
  35 + # platforms:
  36 + # - name: Fedora
  37 + # versions:
  38 + # - all
  39 + # - 25
  40 + # - name: SomePlatform
  41 + # versions:
  42 + # - all
  43 + # - 1.0
  44 + # - 7
  45 + # - 99.99
  46 +
  47 + galaxy_tags: []
  48 + # List tags for your role here, one per line. A tag is a keyword that describes
  49 + # and categorizes the role. Users find roles by searching for tags. Be sure to
  50 + # remove the '[]' above, if you add tags to this list.
  51 + #
  52 + # NOTE: A tag is limited to a single word comprised of alphanumeric characters.
  53 + # Maximum 20 tags per role.
  54 +
  55 +dependencies: []
  56 + # List your role dependencies here, one per line. Be sure to remove the '[]' above,
  57 + # if you add dependencies to this list.
  1 +---
  2 +# tasks file for yoho.lb.switch
  1 +---
  2 +- hosts: localhost
  3 + remote_user: root
  4 + roles:
  5 + - yoho.lb.switch
  1 +---
  2 +# vars file for yoho.lb.switch
No preview for this file type
@@ -7,6 +7,7 @@ import base64 @@ -7,6 +7,7 @@ import base64
7 import hashlib 7 import hashlib
8 import time 8 import time
9 import requests 9 import requests
  10 +import json
10 11
11 12
12 class QcloudApi: 13 class QcloudApi:
@@ -45,7 +46,8 @@ class QcloudApi: @@ -45,7 +46,8 @@ class QcloudApi:
45 sign = self.signature(params, req_url) 46 sign = self.signature(params, req_url)
46 params['Signature'] = sign 47 params['Signature'] = sign
47 response_dict = requests.post("https://" + req_url, data=params).json() 48 response_dict = requests.post("https://" + req_url, data=params).json()
48 - print("Query qcloud api success. params: %s, result: %s" % (params, response_dict)) 49 + print("Query qcloud api success. params:\n %s \n" % (params))
  50 + print json.dumps(response_dict, indent=4, sort_keys=True)
49 return response_dict 51 return response_dict
50 52
51 @staticmethod 53 @staticmethod
No preview for this file type
  1 +#!/usr/bin/python
  2 +# -*- coding: UTF-8 -*-
  3 +
  4 +"""
  5 +修改腾讯云七层负载均衡器的权重
  6 +
  7 +@see doc: https://cloud.tencent.com/document/product/214/8978
  8 +
  9 +"""
  10 +
  11 +from qcloud.qcloud_api import QcloudApi
  12 +import time, sys
  13 +
  14 +def modify_alb_weight(SecretId, SecretKey, lb_id, domain, ip_weight_dict):
  15 + """
  16 + 修改应用型负载均衡器上服务器的权重
  17 + @:param lb_id: 应用型负载均衡器ID
  18 + @:param domain: 应用型负载均衡器的domain
  19 + @:param ip_weight_dict: 需要设置权重的dict, 格式为 {'ip': weight }
  20 + @return list
  21 + """
  22 +
  23 + instance_weight = {} # instance_id --> weight
  24 + instance_port = {} # instance_id --> port
  25 + listener_ids = [] # all linsters
  26 +
  27 + api = QcloudApi(secretId=SecretId, secretKey=SecretKey)
  28 + lb_info = api.do_query(params={'Action': 'DescribeForwardLBBackends', 'loadBalancerId': lb_id}, req_url=QcloudApi.URLS_lb)
  29 +
  30 + # listener : [http , https]
  31 + for listener in lb_info['data']:
  32 + for rule in listener['rules']:
  33 + if rule['domain'] == domain: # domain match
  34 + listener_ids.append(listener['listenerId'])
  35 + for backend in rule['backends']:
  36 + instance_port[backend['unInstanceId']] = backend['port']
  37 + if backend['lanIp'] in ip_weight_dict.keys():
  38 + # setup the instance_id --> weight
  39 + instance_weight[backend['unInstanceId']] = ip_weight_dict[backend['lanIp']]
  40 + else:
  41 + instance_weight[backend['unInstanceId']] = 0
  42 +
  43 + # 检查至少有一个weight
  44 + normal = False
  45 + for v in instance_weight.values():
  46 + if v != 0:
  47 + normal = True
  48 +
  49 + if not normal:
  50 + sys.exit(" instance weight: %s is all zero. please check again!" % instance_weight)
  51 +
  52 + # 修改七层负载均衡器的权重: https://cloud.tencent.com/document/product/214/8978
  53 + for lis_id in listener_ids:
  54 + params = {'Action': 'ModifyForwardSeventhBackends', 'loadBalancerId': lb_id, 'listenerId': lis_id, 'domain': domain}
  55 + i = 1
  56 + for ins_id, ins_weight in instance_weight.iteritems():
  57 + params['backends.%i.instanceId' % i] = ins_id
  58 + params['backends.%i.weight' % i] = ins_weight
  59 + params['backends.%i.port' % i] = instance_port[ins_id]
  60 + i = i + 1
  61 +
  62 + rsp = api.do_query(params, req_url=QcloudApi.URLS_lb)
  63 + print(u"response code: %s . success change domain: %s for lis_id : %s. wait for 10s" % (rsp['code'], domain, lis_id))
  64 + time.sleep(10)
  65 +
  66 +
  67 +
  68 +
  69 +
  70 +
  71 +
  72 +
  73 +modify_alb_weight('AKID6dwpKadiQgbDpXDtyNhppIHPO5qPv5GK', 'ACJkH9mg0DBA1PYpf0E7f3g534wBsQaW', lb_id='lb-990aeqw1', domain='*.yoho.cn', ip_weight_dict={'10.66.202.5': 10, '10.66.202.12':10})