Merge branch 'wap-node-activity' into 'master'
支持containers的IP地址获取 See merge request !85
Showing
3 changed files
with
87 additions
and
16 deletions
@@ -13,6 +13,7 @@ from jinja2 import Environment, FileSystemLoader | @@ -13,6 +13,7 @@ from jinja2 import Environment, FileSystemLoader | ||
13 | import collections | 13 | import collections |
14 | import time | 14 | import time |
15 | import sys | 15 | import sys |
16 | +import get_iplist | ||
16 | 17 | ||
17 | # clone deploy project to local | 18 | # clone deploy project to local |
18 | HOME = os.path.expanduser("~") | 19 | HOME = os.path.expanduser("~") |
@@ -26,7 +27,7 @@ call("git clone --recursive git@git.yoho.cn:yohoops/auto_deploy.git %s" % PATH | @@ -26,7 +27,7 @@ call("git clone --recursive git@git.yoho.cn:yohoops/auto_deploy.git %s" % PATH | ||
26 | 27 | ||
27 | 28 | ||
28 | # get iptables info from git files. | 29 | # get iptables info from git files. |
29 | -azs_mappings = {"qcloud": "az1", "aws": "aws", "t6": "t6"} | 30 | +azs_mappings = {"qcloud": "az1", "aws": "aws", "t6": "t6", "containers": "containers"} |
30 | host_group = { "nginx": "java-nginx", | 31 | host_group = { "nginx": "java-nginx", |
31 | "yoho_gateway": "java-gateway", | 32 | "yoho_gateway": "java-gateway", |
32 | "yoho_users": "java-users", | 33 | "yoho_users": "java-users", |
@@ -62,26 +63,43 @@ host_group = { "nginx": "java-nginx", | @@ -62,26 +63,43 @@ host_group = { "nginx": "java-nginx", | ||
62 | "yoho_reviewed": "java-review", | 63 | "yoho_reviewed": "java-review", |
63 | "yoho_message_controller": "java-messagecontroller" | 64 | "yoho_message_controller": "java-messagecontroller" |
64 | } | 65 | } |
66 | +containers_host_group = { "media-containers": "media-containers", | ||
67 | + "web-containers": "web-containers", | ||
68 | + "yoho-java-container": "yoho-java-container", | ||
69 | + "yoho-ops-container": "yoho-ops-container" | ||
70 | + } | ||
65 | # all available zone groups | 71 | # all available zone groups |
66 | all_groups = {} | 72 | all_groups = {} |
73 | +ClusterIds = {"yoho-java-container": "cls-rzb97zht","yoho-ops-container": "cls-gscif1o5","media-containers": "cls-qzkxrhg7","web-containers": "cls-ro6kl3cp"} | ||
67 | for _az in azs_mappings.keys(): | 74 | for _az in azs_mappings.keys(): |
68 | groups = {} | 75 | groups = {} |
69 | all_groups[azs_mappings[_az]] = groups | 76 | all_groups[azs_mappings[_az]] = groups |
70 | - file = open(os.path.join(PATH, "scripts/iptable/", _az, "app-iptable.sh"), 'r') | ||
71 | - for line in file: | ||
72 | - for deploy_prj_name in host_group.keys(): | ||
73 | - if line.strip().startswith(deploy_prj_name + "_" + _az): | ||
74 | - g_hosts = [] | ||
75 | - groups[host_group[deploy_prj_name]] = g_hosts | ||
76 | - i_1 = line.index("(") | ||
77 | - i_2 = line.index(")") | ||
78 | - sub = line[i_1 + 1 : i_2] | ||
79 | - for user_ip in sub.split("master@"): | ||
80 | - if user_ip.strip() != '': | ||
81 | - g_hosts.append(user_ip.strip()) | ||
82 | - | ||
83 | - | ||
84 | - file.close() | 77 | + if _az == "containers": |
78 | + for line in ClusterIds.keys(): | ||
79 | + for deploy_prj_name in containers_host_group.keys(): | ||
80 | + if line == deploy_prj_name: | ||
81 | + g_hosts = [] | ||
82 | + groups[containers_host_group[deploy_prj_name]] = g_hosts | ||
83 | + result = get_iplist.ip_inqure(ClusterIds[line]) | ||
84 | + ip_s = result['data']['data']['nodes'] | ||
85 | + for ip in ip_s: | ||
86 | + g_hosts.append(ip['lanIp'].strip()) | ||
87 | + else: | ||
88 | + file = open(os.path.join(PATH, "scripts/iptable/", _az, "app-iptable.sh"), 'r') | ||
89 | + for line in file: | ||
90 | + for deploy_prj_name in host_group.keys(): | ||
91 | + if line.strip().startswith(deploy_prj_name + "_" + _az): | ||
92 | + g_hosts = [] | ||
93 | + groups[host_group[deploy_prj_name]] = g_hosts | ||
94 | + i_1 = line.index("(") | ||
95 | + i_2 = line.index(")") | ||
96 | + sub = line[i_1 + 1 : i_2] | ||
97 | + for user_ip in sub.split("master@"): | ||
98 | + if user_ip.strip() != '': | ||
99 | + g_hosts.append(user_ip.strip()) | ||
100 | + | ||
101 | + | ||
102 | + file.close() | ||
85 | 103 | ||
86 | print("get all inventory info success. \n") | 104 | print("get all inventory info success. \n") |
87 | print(all_groups) | 105 | print(all_groups) |
scripts/inventory/get_iplist.py
0 → 100644
1 | +#!/usr/bin/env python | ||
2 | +# -*- coding: UTF-8 -*- | ||
3 | + | ||
4 | +import json | ||
5 | +import commands | ||
6 | +import sys,os | ||
7 | +reload(sys) | ||
8 | +sys.setdefaultencoding('utf8') | ||
9 | + | ||
10 | +base_dir=os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | ||
11 | +base_dir = base_dir + "/playbooks/module_utils" | ||
12 | +sys.path.append(base_dir) | ||
13 | +from qcloud_api import QcloudApi | ||
14 | + | ||
15 | +# 获取SecretId和SecretKey值 | ||
16 | +def get_secrets(): | ||
17 | + (status, output) = commands.getstatusoutput( | ||
18 | + 'curl -s -H "X-Vault-Token: s.0pbNAI6Oy94m2svlEFBadl74" -X GET http://10.66.107.2:8200/v1/yoho/ops/qcloud') | ||
19 | + result = {} | ||
20 | + result['status'] = status | ||
21 | + if status == 0: | ||
22 | + output = json.loads(output) | ||
23 | + result['data'] = output['data'] | ||
24 | + return result | ||
25 | + | ||
26 | +# 调用腾讯云查询集群节点列表接口 ( DescribeClusterInstances ) 获取容器集群节点内网IP | ||
27 | +def ip_inqure(clusterId): | ||
28 | + ret = {} | ||
29 | + URLS = 'ccs.api.qcloud.com/v2/index.php' | ||
30 | + try: | ||
31 | + datas = get_secrets() | ||
32 | + if datas['status'] != 0: | ||
33 | + ret["code"] = datas['status'] | ||
34 | + ret["err"] = "未获取到正确的SecretId和SecretKey值" | ||
35 | + else: | ||
36 | + SecretId = datas['data']['SecretId'].encode('utf-8') | ||
37 | + SecretKey = datas['data']['SecretKey'].encode('utf-8') | ||
38 | + ips_query = QcloudApi(secretId=SecretId, secretKey=SecretKey) | ||
39 | + ip_list = ips_query.do_query(params={'Action': 'DescribeClusterInstances', 'clusterId': clusterId, 'Region': 'ap-beijing'}, req_url=URLS) | ||
40 | + if ip_list['code'] == 0: | ||
41 | + ret['code'] = 0 | ||
42 | + ret["data"] = ip_list | ||
43 | + else: | ||
44 | + ret['code'] = ip_list['code'] | ||
45 | + except Exception,e: | ||
46 | + ret["code"] = 500 | ||
47 | + ret["err"] = e.message | ||
48 | + return ret | ||
49 | + |
-
Please register or login to post a comment