jenkins+docker+git+etcd实现应用配置文件管理
两台机器:
一台机器安装gitlab: http://www.cnblogs.com/cjsblogs/p/8716932.html
另一台机器安装etcd+docker+jenkins
jenkins-docker: https://www.cnblogs.com/cjsblogs/p/8717602.html
防火墙端口启动;
firewall-cmd --zone=public --add-port=2379/tcp --permanent firewall-cmd --zone=public --add-port=2380/tcp --permanent firewall-cmd --reload
安装etcd:
yum install etcd -y systemctl start etcd
安装setuptools
wget --no-check-certificate https://pypi.python.org/packages/69/56/f0f52281b5175e3d9ca8623dadbc3b684e66350ea9e0006736194b265e99/setuptools-38.2.4.zip#md5=e8e05d4f8162c9341e1089c80f742f64 unzip setuptools-38.2.4.zip cd setuptools-38.2.4 python setup.py install cd ..
安装pip
wget --no-check-certificate https://github.com/pypa/pip/archive/1.5.5.tar.gz tar -xvf 1.5.5.tar.gz #解压文件 cd pip-1.5.5/ python setup.py install
安装python etcd模块:
pip install python-etcd
在git创建dev组,以及dev_etcd项目, 并且制作好的etcd脚本:
git目录如下:
etcdGet.py
#!/usr/bin/env python2.7 # -*- coding:utf-8 -*- from __future__ import print_function import etcd import sys import os import re etcdCli=etcd.Client(host=sys.argv[1],port=2379) f = open(sys.argv[3],'wb') f.write(etcdCli.get('%s' % sys.argv[2]).value.encode('utf-8')) f.close()
etcdPush.py
#!/usr/bin/env python2.7 # -*- coding:utf-8 -*- from __future__ import print_function import etcd import sys import time import os import re reload (sys) etcdCli=etcd.Client(host=sys.argv[1],port=2379) def push(local_dir, top_path='/', with_local_path=False): try: for root, dirs, files in os.walk(local_dir): if '.' not in dirs and '.' not in root: for name in files: file = os.path.join(root, name) f = open(file) content = f.read() f.close() if not with_local_path: etcd_path = '/' + top_path.strip('/') + '/' + file[re.match(local_dir, file).end():].strip( '/') else: etcd_path = '/' + top_path.strip('/') + '/' + file.strip('/') print(etcd_path) etcdCli.write(etcd_path, content) except Exception as e: etcd.EtcdException(e) push(sys.argv[2],top_path=sys.argv[3])
etcdRead.py
#!/usr/bin/env python import etcd import sys import time import os import re reload (sys) def getValue(host,port,location): etcdCli=etcd.Client(host=host,port=port) context=etcdCli.read(location).value return context envValue=getValue(host=sys.argv[1],port=int(sys.argv[2]),location=sys.argv[3]) print(envValue)
getRegistryTagList.py
#!/usr/bin/env python2.7 import requests import json import sys def getRegistryTagList(): registry="http://harbor.xxx.com" res=requests.get(registry+"/v2/") assert res.status_code == 200 res=requests.get(registry+"/v2/_catalog?n=1000") assert res.status_code == 200 repositories = res.json().get("repositories",[]) for repository in repositories: res = requests.get(registry + "/v2/{}/tags/list".format(repository)) tags = res.json().get("tags",None) if tags: for tag in tags: image = format(repository) tag = format(tag) if len(sys.argv) <2: print (image+":"+tag) else: AppTargetName=sys.argv[1] if image.endswith(AppTargetName): print (image+":"+tag) if __name__ == "__main__": getRegistryTagList()
此处需要修改对应的私有docker仓库地址
healthyCheck.py
#!/usr/bin/env python import requests,sys,os def healthyCheck(): if len(sys.argv) < 2: sys.exit("need 1 argument") url = sys.argv[1] try: code = requests.get(url,timeout=1).status_code if code >= 200 and code < 400: print "healthy check ok , status_code:%s" % code else: print "healthy check not ok, status_code:%s" % code sys.exit("healthy check not ok") except Exception,e: print "e" if __name__ == "__main__": healthyCheck()
import.sh
/opt/dev_etcd/etcdPush.py 172.16.5.113 appCfgs /xxx.com/instances
此处建立etcd推送的配置文件服务器以及路径
以上所有的脚本全部完毕
接下来需要在jenkins上创建job结合git的webhooks推送应用配置文件
jenkins创建job:
截图处创建git的webhooks能用到
以上git clone直接提交用户名和密码拉取git代码, 其中我的密码带有@符号, git clone无法识别@符号, 需要将@转义为%40故账号需要特殊处理:
最后在git上创建webhooks
综上全部创建完毕, 在jenkins上点击job立即构建测试吧