ppctl.py11 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3. from __future__ import print_function
  4. import os
  5. import commands
  6. import httplib, urllib
  7. import json
  8. import time
  9. from kubernetes.client.rest import ApiException
  10. from pprint import pprint
  11. from kubernetes import client, config
  12. import sys, getopt, random
  13. from aliyunsdkcore.client import AcsClient
  14. from aliyunsdkcore.acs_exception.exceptions import ClientException
  15. from aliyunsdkcore.acs_exception.exceptions import ServerException
  16. from aliyunsdkalidns.request.v20150109.AddDomainRecordRequest import AddDomainRecordRequest
  17. from aliyunsdkalidns.request.v20150109.DeleteDomainRecordRequest import DeleteDomainRecordRequest
  18. from aliyunsdkalidns.request.v20150109.DescribeDomainRecordsRequest import DescribeDomainRecordsRequest
  19. JNLP_ENV = os.getenv('JNLP_ENV')
  20. JNLP_TAG = os.getenv('JNLP_TAG')
  21. JNLP_CONTAINER_PORT = os.getenv('JNLP_CONTAINER_PORT')
  22. # JNLP_WAR = os.getenv('JNLP_WAR')
  23. JNLP_INGRESS = os.getenv('JNLP_INGRESS')
  24. JNLP_IMAGE = os.getenv('JNLP_IMAGE')
  25. JNLP_CONTROL_NAME = os.getenv('DRONE_REPO_NAME')
  26. JNLP_STORAGE_CLASS = os.getenv('JNLP_STORAGE_CLASS')
  27. JNLP_DOMAIN = os.getenv('JNLP_DOMAIN')
  28. JNLP_CONTROL = os.getenv('JNLP_CONTROL')
  29. JNLP_REPLICAS = os.getenv('JNLP_REPLICAS')
  30. JNLP_STORAGE_CAPACITY = os.getenv('JNLP_STORAGE_CAPACITY')
  31. JNLP_MOUNT_PATH = os.getenv('JNLP_MOUNT_PATH')
  32. JNLP_SVC_MODE = os.getenv('JNLP_SVC_MODE')
  33. NGINX_SVC = str(JNLP_ENV) + '/' + str(JNLP_CONTROL_NAME) + '-svc:' + str(JNLP_CONTAINER_PORT)
  34. NGINX_PORT = os.getenv('JNLP_CONTAINER_PORT')
  35. RECORD_NAME = os.getenv('JNLP_ENV') + "-" + os.getenv('DRONE_REPO_NAME')
  36. INGRESS_LIST_IN = ["10.26.32.137"]
  37. INGRESS_LIST_PUB = ["120.192.70.105"]
  38. NGINX_RECORD_IP = random.sample(INGRESS_LIST_PUB, 1)[0]
  39. DOMAINNAME = os.getenv('JNLP_DOMAIN').strip('.')
  40. # 生成deploy.yaml文件
  41. def deployYamlCreate():
  42. tmpctl = '''
  43. config=`env|grep JNLP`
  44. template=`cat ./deploy-tmp.yaml`
  45. printf "$config\ncat << EOF\n$template\nEOF" | bash > ./deploy.yaml
  46. '''
  47. f1 = open("tempshell.sh","w")
  48. f1.writelines(tmpctl)
  49. f1.close()
  50. status,output = commands.getstatusoutput("sh tempshell.sh && rm -f tempshell.sh")
  51. print("code:" + str(status) + str(output) + "; create deploy.yaml success !!!")
  52. return status,output
  53. # 生成Dockerfile文件
  54. def DockerfileCreate():
  55. tmp = '''
  56. onfig=`env|grep JNLP`
  57. template=`cat ./Dockerfile`
  58. printf "$config\ncat << EOF\n$template\nEOF" | bash > ./Dockerfile
  59. '''
  60. f2 = open("tmp.sh","w")
  61. f2.writelines(tmp)
  62. f2.close()
  63. status,output = commands.getstatusoutput("sh tmp.sh && rm -f tmp.sh")
  64. print("code:" + str(status) + str(output) + "; create Dockerfile success !!!")
  65. return
  66. # 更新haproxy configmap.json配置文件,生成TCP服务转发记录
  67. def updateConfigMap(file):
  68. if JNLP_SVC_MODE == "tcp":
  69. data = {}
  70. data[NGINX_PORT] = NGINX_SVC
  71. status, output = commands.getstatusoutput("kubectl get cm tcp-services -n ingress-nginx -o=json")
  72. tempdict = eval(output)
  73. tempdict['data'][NGINX_PORT] = NGINX_SVC
  74. newdata = tempdict['data']
  75. print(newdata,status)
  76. old_str = "CMDATA"
  77. new_str = str(newdata).replace("'",'"')
  78. with open(file, "r") as f1,open("%s.bak" % file, "w" ) as f2:
  79. for line in f1:
  80. if old_str in line:
  81. line = line.replace(old_str, new_str)
  82. f2.write(line)
  83. os.remove(file)
  84. os.rename("%s.bak" % file, file)
  85. cmStatus = "1"
  86. else:
  87. print("****** Nginx Tcp Services Configmap.json Do Not Update ******")
  88. cmStatus = "0"
  89. return cmStatus
  90. # 部署应用到kubernetes平台
  91. def deployToKube():
  92. cmStatus = updateConfigMap("configmap.json")
  93. appStatus = commands.getoutput("kubectl get sts $JNLP_CONTROL_NAME -n $JNLP_NAMESPACE|grep $JNLP_CONTROL_NAME|wc -l")
  94. if appStatus == "1" and cmStatus == "1":
  95. print("Warning:" + JNLP_CONTROL_NAME + " is already, i will update it and update nginx tcp proxy !!!")
  96. apply_out = commands.getoutput("kubectl apply -f ./deploy.yaml && kubectl apply -f ./configmap.json")
  97. print("===>>> kubectl apply output <<<===")
  98. print(apply_out)
  99. elif appStatus == "1" and cmStatus == "0":
  100. print("Warning:" + JNLP_CONTROL_NAME + " is already, i will update it !!!")
  101. apply_out = commands.getoutput("kubectl apply -f ./deploy.yaml")
  102. print("===>>> kubectl apply output <<<===")
  103. print(apply_out)
  104. elif appStatus != "1" and cmStatus == "1":
  105. apply_out = commands.getoutput("kubectl apply -f ./deploy.yaml && kubectl apply -f ./configmap.json")
  106. print("===>>> kubectl apply output <<<===")
  107. print(apply_out)
  108. print("===>>> apply is successfull <<<===")
  109. else:
  110. apply_out = commands.getoutput("kubectl apply -f ./deploy.yaml")
  111. print("===>>> kubectl apply output <<<===")
  112. print(apply_out)
  113. print("===>>> apply app successfull <<<===")
  114. return
  115. # 部署应用状态获取
  116. def deployStatus():
  117. kubeconfig = config.kube_config.load_kube_config(config_file="/root/.kube/config")
  118. appsv1_api_instance = client.AppsV1Api(kubeconfig)
  119. namespace = JNLP_ENV
  120. stsname = JNLP_CONTROL_NAME
  121. pretty = "true"
  122. for x in range(20):
  123. try:
  124. sts_response = appsv1_api_instance.read_namespaced_stateful_set_status(stsname, namespace, pretty=pretty)
  125. if sts_response.status.replicas == sts_response.status.ready_replicas :
  126. print("===>>> " +stsname + " is RUNNING <<<===")
  127. chk_status = 1
  128. break
  129. else:
  130. if x < 40:
  131. time.sleep(15)
  132. else:
  133. chk_status = 0
  134. print(stsname + " status check fail !!! , Please contact kubernetes systen administrator")
  135. except ApiException as e:
  136. print("Exception when calling CoreV1Api->list_namespaced_pod: %s\n" % e)
  137. return chk_status
  138. # DNS解析
  139. # 域名由jenkins项目名+命名空间+dns域组合而成
  140. # 如果域名已存在,不会再更新
  141. def dnsAllocation():
  142. if JNLP_INGRESS == "nginx":
  143. httpClient = httplib.HTTPConnection("opsadmin.evbj.easou.com", 80, timeout=30)
  144. headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
  145. params = urllib.urlencode({'method': 'add', 'type': 'record', 'domain_id': 24, 'record_type': 'A', 'record_name': RECORD_NAME ,'record_ip': NGINX_RECORD_IP , 'record_ttl': 180})
  146. httpClient.request("POST", "/dns/dnsapi/?token=c2d1856be235a3207cd03b6f4d3c4094", params, headers)
  147. response = httpClient.getresponse().read()
  148. jstr = json.loads(response)
  149. nsStatus = jstr['status']
  150. print(NGINX_RECORD_IP)
  151. if nsStatus == "-501":
  152. print(JNLP_ENV + "-" + JNLP_CONTROL_NAME + JNLP_DOMAIN + " is already exist")
  153. elif nsStatus == "0":
  154. httpClient = httplib.HTTPConnection("opsadmin.evbj.easou.com", 80, timeout=30)
  155. httpClient.request("POST", "/dns/mkconfig/?token=c2d1856be235a3207cd03b6f4d3c4094")
  156. response = httpClient.getresponse().read()
  157. jstr = json.loads(response)
  158. upStatus = jstr['status']
  159. print(JNLP_ENV + "-" + JNLP_CONTROL_NAME + JNLP_DOMAIN + " " +upStatus + " add successfull !!!")
  160. else:
  161. print("*****Please contact DNS systen administrator*****")
  162. else :
  163. print(">>> Ingress error, Only nginx !!! <<<")
  164. return
  165. def cloudDns():
  166. if JNLP_INGRESS == "nginx":
  167. client = AcsClient('LTAI4LibOCW5ilIa', 'NiA9OumXJDHp4TwLwXWcCKGoDgDjms', 'cn-hangzhou')
  168. # describe record
  169. request = DescribeDomainRecordsRequest()
  170. request.set_accept_format('json')
  171. request.set_DomainName(DOMAINNAME)
  172. request.set_RRKeyWord(RECORD_NAME)
  173. response = client.do_action_with_exception(request)
  174. jsondata = json.loads(response)
  175. # print(response)
  176. if jsondata['TotalCount'] != 0:
  177. print(RECORD_NAME + "." + DOMAINNAME + " is already exists <<<")
  178. else :
  179. # add record
  180. request = AddDomainRecordRequest()
  181. request.set_accept_format('json')
  182. request.set_Value(NGINX_RECORD_IP)
  183. request.set_Type("A")
  184. request.set_RR(RECORD_NAME)
  185. request.set_DomainName(DOMAINNAME)
  186. response = client.do_action_with_exception(request)
  187. # print(response)
  188. print(RECORD_NAME + "." + DOMAINNAME + " add sucess <<<")
  189. else :
  190. print(">>> Ingress error, Only nginx !!! <<<")
  191. return
  192. def main(argv=None):
  193. if argv is None:
  194. args = sys.argv[1:]
  195. for opt_name in args :
  196. if opt_name in ('-a','--apply'):
  197. print("===>>> deploy app to kubernetes <<<===")
  198. deployToKube()
  199. time.sleep(10)
  200. sys.exit()
  201. if opt_name in ('-b','--build'):
  202. print("===>>> create deploy.yaml <<<===")
  203. deployYamlCreate()
  204. sys.exit()
  205. if opt_name in ('-c','--check'):
  206. print("===>>> app status check <<<===")
  207. deployStatus()
  208. sys.exit()
  209. if opt_name in ('-d','--dns'):
  210. print("===>>> add/update dns resolve <<<===")
  211. cloudDns()
  212. sys.exit()
  213. return
  214. if __name__ == "__main__":
  215. main()