def make_image(image,redis_key): try: Redis.lpush(redis_key, 'start build image %s......' % image) _flow_log('start build image %s......' % image) project = image.split('/')[-1].split(':')[0] dockerfile = "%s/%s" %(dockerfile_path,project) if os.path.exists(dockerfile): try: client = docker.APIClient(base_url=docker_base_url) response = [line for line in client.build(path=dockerfile, rm=True, tag=image)] result = eval(response[-1]) if 'Successfully' in str(result): Redis.lpush(redis_key,"docker build %s success!" %image) _flow_log("docker build %s success!" %image) else: Redis.lpush(redis_key,'fail:%s'%result) _flow_log('fail:%s'%result) return False except Exception as e: logging.error(e) if 'BaseException' not in str(e): Redis.lpush(redis_key, 'fail:%s' % e) _flow_log('fail:%s' % e) else: try: Files = tools.get_k8s_packages() response = [line for line in client.push(image, stream=True,auth_config={'username':docker_user,'password':docker_password})] result = eval(response[-1])['aux']['Tag'] version = image.split(':')[-1] if version == result: #删除代码包 for file in os.listdir(dockerfile): if Files[project].split('.')[0] in file: try: os.remove('%s/%s' % (dockerfile,file)) except: shutil.rmtree('%s/%s' % (dockerfile,file)) Redis.lpush(redis_key,"docker push %s success!" % image) _flow_log("docker push %s success!" % image) return True else: Redis.lpush(redis_key, 'fail:%s' %result) _flow_log('fail:%s' %result) return False except Exception as e: logging.error(e) Redis.lpush(redis_key, 'fail:%s' %e) _flow_log('fail:%s' %e) return False else: Redis.lpush(redis_key,'dockerfile %s path not exists!' %dockerfile, 'fail') _flow_log('dockerfile %s path not exists!' %dockerfile, 'fail') return False except Exception as e: logging.error(e) if 'BaseException' not in str(e): Redis.lpush(redis_key, 'fail:%s' % e) _flow_log('fail:%s' % e) return False
redis_password = app.config.get('REDIS_PASSWORD') docker_user = app.config.get('USER') docker_password = app.config.get('PASSWORD') docker_base_url = app.config.get('BASE_URL') dockerfile_path = app.config.get('DOCKERFILE_PATH') ops_token = app.config.get('OPS_TOKEN') oss_id = app.config.get('OSS_ID') oss_key = app.config.get('OSS_KEY') oss_url = app.config.get('OSS_URL') Redis = redis.StrictRedis(host=redis_host, port=redis_port, decode_responses=True) logging = loging.Error() config, contexts, config_file = tools.k8s_conf() flow_number = time.strftime('%Y%m%d%H%M%S', time.localtime()) Files = tools.get_k8s_packages() #流水日志记录 def _flow_log(Msg): try: logpath = "/opt/k8s/flow_logs" tm = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) if not os.path.exists(logpath): os.system("/bin/mkdir -p %s" % logpath) flow_log_ptah = "%s/%s.log" % (logpath, flow_number) with open(flow_log_ptah, 'a+') as f: f.write("%s %s\n" % (tm, str(Msg))) except Exception as e: logging.error(e)
def download_war(object, version, run_args, redis_key): #下载对应项目的最新代码包 try: #包名需要规范 Files = tools.get_k8s_packages() project_file = object object = object.split('.') dm_name = object[0] if dm_name in Files: project_file = Files[dm_name] dm_type = project_file.split('.')[-1] if len(project_file.split('.')) > 2: dm_type = '.'.join(project_file.split('.')[1:]) package = '%s-%s.%s' % (dm_name, version, dm_type) project_path = '%s/%s/%s' % (dockerfile_path, dm_name, project_file) if not os.path.exists(project_path): try: Redis.lpush(redis_key, '%s package download from oss ......' % package) _flow_log('%s package download from oss ......' % package) auth = oss2.Auth(oss_id, oss_key) bucket = oss2.Bucket(auth, oss_url, 'mojiops') oss_project_path = None try: if not os.path.exists('%s/%s' % (dockerfile_path, dm_name)): os.mkdir('%s/%s' % (dockerfile_path, dm_name)) for obj in oss2.ObjectIterator(bucket): if obj.key.endswith('.war') or obj.key.endswith( '.tar.gz') or obj.key.endswith('.jar'): if obj.key.split('/')[-1].startswith( dm_name) and version in obj.key.split( '/')[-1]: oss_project_path = obj.key break except Exception as e: logging.error(e) if oss_project_path: #尝试3次下载 for i in range(3): try: oss2.resumable_download(bucket, oss_project_path, project_path) break except: continue else: Redis.lpush(redis_key, '%s package not fond!' % package) _flow_log('%s package not fond!' % package) return False except Exception as e: logging.error(e) if os.path.exists(project_path): try: if project_file.endswith('.tar.gz'): project_file = project_file.split('.')[0] os.chdir('%s/%s/' % (dockerfile_path, dm_name)) tar = tarfile.open(project_path.split('/')[-1], 'r') tar.extractall() tar.close() for file in os.listdir('./'): if dm_name in file and not file.endswith('.tar.gz'): shutil.move(file, project_file) if os.path.exists(dm_name): os.remove(project_path.split('/')[-1]) #生成dockerfile文件 dockerfile = '%s/%s/Dockerfile' % (dockerfile_path, dm_name) if os.path.exists(dockerfile): os.remove(dockerfile) with open(dockerfile, 'a+') as f: with open('%s/../conf/dockerfile_%s.template' % (app.root_path, dm_type)) as F: for line in F: if '<PROJECT>' in line: line = line.replace('<PROJECT>', project_file) f.write('%s\n' % line) #生成docker_run启动脚本文件 if run_args: runfile = '%s/%s/run.sh' % (dockerfile_path, dm_name) if os.path.exists(runfile): os.remove(runfile) with open(runfile, 'a+') as f: with open('%s/../conf/docker_run.template' % app.root_path) as F: for line in F: f.write('%s\n' % line) for line in run_args: f.write('%s\n' % line) Redis.lpush(redis_key, '%s package download success!' % package) _flow_log('%s package download success!' % package) return package except Exception as e: logging.error(e) else: Redis.lpush(redis_key, '%s package download fail!' % package) _flow_log('%s package download fail!' % package) return False except Exception as e: logging.error(e)