传统运维 - apollo批量发布
2022-11-03
###
# coding=utf-8
import requests
import json
import datetime
import sys
class ApolloAPI(object):
def __init__(self, appid, env, cluster, namespace=None):
self.appid = appid
self.env = env
self.cluster = cluster
self.namespace = namespace if namespace else 'all'
self.baseurl = 'http://apollo.xx.com:8070'
self.user = 'apollo'
self.token = 'xxxxxxx'
self.headers = {
'Authorization': self.token,
'Content-Type': 'application/json;charset=UTF-8'
}
def get_namespace_list(self):
url = f'{self.baseurl}/openapi/v1/envs/{self.env}/apps/{self.appid}/clusters/{self.cluster}/namespaces'
result = []
try:
res = requests.get(url=url, headers=self.headers)
if res.status_code == 200:
response = json.loads(res.text)
for item in response:
result.append(item.get('namespaceName'))
except Exception:
pass
return result
def release_items(self):
if self.namespace == 'all':
namespace_list = self.get_namespace_list()
else:
namespace_list = [self.namespace]
if len(namespace_list) == 0:
print(f'AppId: {self.appid}, ENV: {self.env}, Cluster: {self.cluster} namespace为空')
for namespace in namespace_list:
url = f'{self.baseurl}/openapi/v1/envs/{self.env}/apps/{self.appid}/clusters/{self.cluster}/namespaces/{namespace}/releases'
try:
params = {
'releaseTitle': '{}-release'.format(datetime.datetime.now().strftime("%Y%m%d%H%M%S")),
'releaseComment': '批量发布脚本提交',
'releasedBy': self.user
}
res = requests.post(url, data=json.dumps(params), headers=self.headers)
if res.status_code == 200:
'AppId:30000, ENV:FAT, Cluster:qa, Namespace:application'
print(
f'AppId: {self.appid}, ENV: {self.env}, Cluster: {self.cluster}, '
f'Namespace: {namespace} 发布成功'
)
else:
print(
f'AppId: {self.appid}, ENV: {self.env}, Cluster: {self.cluster}, '
f'Namespace: {namespace} 发布失败, HttpCode: {res.status_code}'
)
except Exception as e:
print(
f'AppId: {self.appid}, ENV: {self.env}, Cluster: {self.cluster}, '
f'Namespace: {namespace} 发布失败, 信息: {str(e)}'
)
if __name__ == '__main__':
appid = sys.argv[1]
env = sys.argv[2]
cluster = sys.argv[3]
a = ApolloAPI(appid, env, cluster)
a.release_items()
执行脚本
> python /opt/qa_apollo_release.py 12000 fat default
批量执行
cat list |awk '{print "python /opt/qa_apollo_release.py "$1" fat default "}'
为token绑定所有应用最大权限
#!/bin/bash
# 授权
consumerid=$(mysql -N -hmysql-ops.qa-pub.yqn.corp -uroot -pxxxx -A apolloportaldb -e "SELECT ConsumerId FROM consumertoken where Token='54662b9b8722b7dcf36bfaa'");
roleids=$(mysql -N -hmysql-ops.qa-pub.yqn.corp -uroot -pxxxx -A apolloportaldb -e "select id from role where RoleName like 'Master%';")
for roleid in ${roleids}
do
res=$(mysql -hmysql-ops.qa-pub.yqn.corp -uroot -pxxxx -A apolloportaldb -e "select * from consumerrole where ConsumerId=${consumerid} and RoleId=${roleid}")
if [[ -z ${res} ]];then
echo insert ${roleid}
mysql -hmysql-ops.qa-pub.yqn.corp -uroot -pxxxx -A apolloportaldb -e "insert into consumerrole(ConsumerId,RoleId) values (${consumerid},${roleid});"
fi
done
mysql -N -h mysql-ops.qa-pub.yqn.corp -uroot -pxxxx -A qa4_apolloconfigdb -e "select appid from namespace group by appid;" > appids
查看token权限
SELECT a.Token,b.appid,c.RoleId,d.RoleName FROM `consumertoken` a, `consumer` b, `consumerrole` c, `role` d where a.ConsumerId = b.Id and a.ConsumerId=c.ConsumerId and a.Token='3e6854662b9b8722ba5e9b' and c.RoleId=d.Id
批量变更
update item set value=replace(value,'192.168.10.179','mongo-common.qa-pub.yqn.corp') where value like '%192.168.10.179%';
查询
select b.AppId, b.ClusterName, a.`value` from item a, namespace b where a.NamespaceId= b.id and a.`value` like '%aliyuncs.com%'
批量发布
#!/bin/bash
# appids来源 select appid from namespace group by appid
basepath=$(cd `dirname $0`; pwd)
declare -A dict
dict=([qa4]="default" [fat]="default qa qa2 qa3")
envs="fat qa4"
cat ${basepath}/appids | while read appid
do
for env in ${envs}
do
clusters=${dict["${env}"]}
for cluster in ${clusters}
do
python /opt/qa_apollo_release.py ${appid} ${env} ${cluster}
done
done
done