示例#1
0
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
示例#2
0
def make_image(image, redis_key):
    try:
        Redis.lpush(redis_key, 'start build image %s......' % image)
        dockerfile = "%s/%s" % (dockerfile_path,
                                image.split('/')[-1].split(':')[0])
        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)
                else:
                    Redis.lpush(redis_key, '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)
            else:
                try:
                    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:
                        Redis.lpush(redis_key,
                                    "docker push %s success!" % image)
                        return True
                    else:
                        Redis.lpush(redis_key, 'fail:%s' % result)
                        return False
                except Exception as e:
                    logging.error(e)
                    Redis.lpush(redis_key, 'fail:%s' % e)
                    return False

        else:
            Redis.lpush(redis_key,
                        '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)
        return False