def push_special_image_to_project(project_name, registry, username, password, image, tags=None, size=1, expected_login_error_message=None, expected_error_message = None): _docker_api = DockerAPI() _docker_api.docker_login(registry, username, password, expected_error_message = expected_login_error_message) time.sleep(2) if expected_login_error_message != None: return return _docker_api.docker_image_build(r'{}/{}/{}'.format(registry, project_name, image), tags = tags, size=int(size), expected_error_message=expected_error_message)
def push_self_build_image_to_project(project_name, registry, username, password, image, tag, size=2, expected_login_error_message=None, expected_error_message=None): _docker_api = DockerAPI() _docker_api.docker_login( registry, username, password, expected_error_message=expected_login_error_message) time.sleep(2) if expected_login_error_message in [None, ""]: push_special_image_to_project( project_name, registry, username, password, image, tags=[tag], size=size, expected_login_error_message=expected_login_error_message, expected_error_message=expected_error_message) return r'{}/{}'.format(project_name, image), tag
def pull_harbor_image(registry, username, password, image, tag, expected_login_error_message = None, expected_error_message = None): _docker_api = DockerAPI() _docker_api.docker_login(registry, username, password, expected_error_message = expected_login_error_message) if expected_login_error_message != None: return time.sleep(2) ret = _docker_api.docker_image_pull(r'{}/{}'.format(registry, image), tag = tag, expected_error_message = expected_error_message) print("Docker pull image return message: {}".format(ret))
def push_image_to_project(project_name, registry, username, password, image, tag, expected_login_error_message=None, expected_error_message=None): _docker_api = DockerAPI() _docker_api.docker_login( registry, username, password, expected_error_message=expected_login_error_message) time.sleep(2) if expected_login_error_message != None: return _docker_api.docker_image_pull(image, tag=tag) time.sleep(2) new_harbor_registry, new_tag = _docker_api.docker_image_tag( r'{}:{}'.format(image, tag), r'{}/{}/{}'.format(registry, project_name, image)) time.sleep(2) _docker_api.docker_image_push( new_harbor_registry, new_tag, expected_error_message=expected_error_message) return r'{}/{}'.format(project_name, image), new_tag
def webhook(): print("Hook Route: " + os.environ['WEB_HOOK_ROUTE']) input_json = request.get_json() # print("json --> " + str(json)) print("Incoming hook detected!") event = str(request.headers['X-GitHub-Event']) print("X-GitHub-Event: " + event) print("X-GitHub-Delivery: " + str(request.headers['X-GitHub-Delivery'])) # print("Json: " + str(json.dumps(input_json))) if event == "ping": return "ping detected" repository_name = input_json['repository']['full_name'] curr_commit = input_json['after'] is_master = input_json['ref'].endswith("master") print("Repository:" + repository_name) print("Curr commit: " + curr_commit) print("is_master: " + str(is_master)) bw = bws[repository_name] if is_master: print("Commit on master detected!") try: DockerAPI.login(username=bw.secrets.docker_hub_u, password=bw.secrets.docker_hub_p) OutputGenerator.generate_loading_markup(bw) pipeline_results = ActionBuilder.start_pipeline(bw) print("Pipeline results | Number of runned steps:" + str(pipeline_results.__len__())) for result in pipeline_results: if result.has_failed(): print("[Failed] Namespace: " + str(result.namespace)) print("Error:\n" + str(result.get_error_msg())) else: print("[Successed] Namespace: " + str(result.namespace)) print("Message:\n" + result.get_message()) bw.count += 1 finally: bw.erase_repo() return 'Hook handled'
def build_test_image(bw): dockerfile = bw.test_image.dockerfile context = ActionBuilder.get_context_path(bw) tag = bw.test_image.tag return DockerAPI.build(dockerfile=dockerfile, tag=tag, context_path=context)
def push_image_to_project(project_name, registry, username, password, image, tag): _docker_api = DockerAPI() _docker_api.docker_login(registry, username, password) time.sleep(2) _docker_api.docker_image_pull(image, tag = tag) time.sleep(2) new_harbor_registry, new_tag = _docker_api.docker_image_tag(image, r'{}/{}/{}'.format(registry, project_name, image)) time.sleep(2) _docker_api.docker_image_push(new_harbor_registry, new_tag) return r'{}/{}'.format(project_name, image), new_tag
def push_image_to_project(project_name, registry, username, password, image, tag, expected_login_error_message=None, expected_error_message=None, profix_for_image=None, new_image=None): print("Start to push image {}/{}/{}:{}".format(registry, project_name, image, tag)) _docker_api = DockerAPI() _docker_api.docker_login( registry, username, password, expected_error_message=expected_login_error_message) time.sleep(2) if expected_login_error_message != None: return _docker_api.docker_image_pull(image, tag=tag) time.sleep(2) original_name = image image = new_image or image if profix_for_image == None: new_harbor_registry, new_tag = _docker_api.docker_image_tag( r'{}:{}'.format(original_name, tag), r'{}/{}/{}'.format(registry, project_name, image), tag=tag) else: new_harbor_registry, new_tag = _docker_api.docker_image_tag( r'{}:{}'.format(original_name, tag), r'{}/{}/{}/{}'.format(registry, project_name, profix_for_image, image), tag=tag) time.sleep(2) _docker_api.docker_image_push( new_harbor_registry, new_tag, expected_error_message=expected_error_message) return r'{}/{}'.format(project_name, image), new_tag
def pull_harbor_image(registry, username, password, image, tag, expected_error_message=None): _docker_api = DockerAPI() _docker_api.docker_login(registry, username, password) time.sleep(2) _docker_api.docker_image_pull( r'{}/{}'.format(registry, image), tag=tag, expected_error_message=expected_error_message)
def run_tests(bw): tag = bw.test_image.tag return DockerAPI.run(image=tag)
def push_prod_image(bw): DockerAPI.push(bw.prod_image, dhub_u=bw.secrets.docker_hub_u, dhub_p=bw.secrets.docker_hub_p) return StepResult()
def push_image_to_project(project_name, registry, username, password, image, tag, expected_login_error_message=None, expected_error_message=None, profix_for_image=None, new_image=None): print("Start to push image {}/{}/{}:{}".format(registry, project_name, image, tag)) _docker_api = DockerAPI() _docker_api.docker_login("docker", DOCKER_USER, DOCKER_PWD) _docker_api.docker_image_pull(image, tag=tag, is_clean_all_img=False) _docker_api.docker_login( registry, username, password, expected_error_message=expected_login_error_message) time.sleep(2) if expected_login_error_message != None: return time.sleep(2) original_name = image image = new_image or image if profix_for_image == None: target_image = r'{}/{}/{}'.format(registry, project_name, image) else: target_image = r'{}/{}/{}/{}'.format(registry, project_name, profix_for_image, image) new_harbor_registry, new_tag = _docker_api.docker_image_tag( r'{}:{}'.format(original_name, tag), target_image, tag=tag) time.sleep(2) _docker_api.docker_image_push( new_harbor_registry, new_tag, expected_error_message=expected_error_message) docker_api.docker_image_clean_all() return r'{}/{}'.format(project_name, image), new_tag
from random import randint from subprocess import call from action_builder import ActionBuilder from docker_api import DockerAPI from builder_wrapper import BuilderWrapper from builder_wrapper import GCloudConfig from google_cloud import GCloudAPI import json from results_output_generator import OutputGenerator import os app = Flask(__name__) # app.run(threaded=True) bws = {} DockerAPI.init() route_suffix = str(randint(0, 1000000)) # route_suffix = "934644" # route_suffix = "" os.environ[ 'WEB_HOOK_ROUTE'] = os.environ['WEB_HOOK_HOST'] + "/webhook" + route_suffix print("Hook Route: " + os.environ['WEB_HOOK_ROUTE']) GCLOUD_PATH = "google_cloud_keys" call(["mkdir", "-p", GCLOUD_PATH]) OutputGenerator.init_outputs_folder() @app.route('/webhook' + route_suffix, methods=['POST'])