def _download(url, file_path, login, password): status = url_download(url, file_path, login, password) if status == 404: raise BobTheBuilderException('{status}: Could find download "{url}"'.format(url=url, status=status)) if status == 401: raise BobTheBuilderException('{status}: Could download unauthorized "{url}"'.format(url=url, status=status)) if status >= 400: raise BobTheBuilderException('{status}: Could download "{url}"'.format(url=url, status=status))
def _get_tag(auth_username, auth_password, repo, tag_name): status, tags = url_get_json('https://api.github.com/repos/{0}/tags'.format(repo), auth_username, auth_password) if not tags: raise BobTheBuilderException('github tag {0}:{1} not found'.format(repo, tag_name)) _check_response(status, tags) for tag in tags: if not 'name' in tag or tag_name != tag.get('name'): continue return status, tag raise BobTheBuilderException('github tag {0}:{1} not found'.format(repo, tag_name))
def download_tag_source(repo, tag_name, output_path, auth_username, auth_password): """ downloads and unzip the source for the given git repo's release. :param repo_owner_name: the git repo owner. :param tag_name: the git release tag name e.g. 'v1.0.2'. :param output_path: the directory where the logs and source are to be saved. :param auth_username: git username / login. :param auth_password: git password. :return: the directory path to source. """ status, tag = _get_tag(auth_username, auth_password, repo, tag_name) if not tag: raise BobTheBuilderException('Git tags request failed status:{0}'.format(status)) with open(os.path.join(output_path, 'git-tag.json'), 'w') as f: f.write(json.dumps(tag, indent=2)) download_url = tag.get('zipball_url') if not download_url: raise BobTheBuilderException('Could find a download url') release_file = os.path.join(output_path, 'src.zip') source_path = os.path.join(output_path, 'src') status = _download(download_url, release_file, auth_username, auth_password) os.system('unzip {0} -d {1}'.format(release_file, source_path)) result = glob(os.path.join(source_path, '*')) if len(result) == 1 and os.path.isdir(result[0]): source_path = result[0] if source_path and not source_path.endswith('/'): source_path += '/' os.remove(release_file) print(source_path) return source_path
def do_download_git_repo(task, build_path, created_at_str): print('do_download_git_repo') settings = load_settings() if task.git_tag and task.git_tag != 'latest': source_path = download_tag_source(task.git_repo, task.git_tag, build_path, settings['git_hub']['login'], settings['git_hub']['password']) else: source_path = download_branch_source(task.git_repo, build_path, task.git_branch, settings['git_hub']['login'], settings['git_hub']['password']) with open(os.path.join(source_path, 'bob-the-builder.yml'), 'r') as f: build = yaml.load(f) if not ('docker_compose' in build): raise BobTheBuilderException( 'Invalid "bob-the-builder.yml" file, could not find "docker_compose" att' ) if not ('services_to_push' in build['docker_compose']): raise BobTheBuilderException( 'Invalid "bob-the-builder.yml" file, could not find "services_to_push" att' ) docker_compose_file = build['docker_compose'].get( 'file', 'docker-compose.yml') services_to_push = build['docker_compose']['services_to_push'] test_service = build['docker_compose'].get('test_service') notification_emails = build.get('notification_emails', []) return (rename_basedir(source_path, created_at_str), docker_compose_file, services_to_push, test_service, notification_emails)
def find_settings_file(filename): path = os.path.join(expanduser("~/.bob"), filename) if os.path.exists(path): return path path = os.path.join('/opt/bob/etc', filename) if os.path.exists(path): return path path = os.path.join('/usr/local/etc/bob', filename) if os.path.exists(path): return path path = os.path.join('/etc/bob', filename) if os.path.exists(path): return path raise BobTheBuilderException( 'cannot find settings file {0} in\r\n~/.bob\r\n/opt/bob/etc\r\n/usr/local/etc/bob\r\n/etc/bob' .format(filename))
def _check_response(status, response): if 'message' in response: raise BobTheBuilderException('github error: {0}'.format(response['message']))
def do_push_dockers(task, build_path, source_path, services_to_push): print('do_push_dockers') local_images = [] for image in get_recent_images(): local_images.append({'Id': image['Id'], 'RepoTags': image['RepoTags']}) images_to_push = _map_services_to_images(source_path, services_to_push, local_images) msg = 'local images found:\n{0}'.format(json.dumps(local_images, indent=2)) msg += '\n\nimages matched for push:\n{0}'.format( json.dumps(images_to_push, indent=2)) print(msg) _write_log_file_and_db(msg, _get_image_matching_log(build_path), task) if not images_to_push and len(images_to_push) == 0: raise BobTheBuilderException( 'Could not match any images to push to docker hub:\r\n{0}'.format( services_to_push)) settings = load_settings() if 'docker_hub' in settings and 'login' in settings[ 'docker_hub'] and 'password' in settings['docker_hub']: execute('docker login -u {login} -p {password}'.format( login=settings['docker_hub']['login'], password=settings['docker_hub']['password'])) if task.git_tag and len(task.git_tag) and task.git_tag != 'latest': tag = task.git_tag else: tag = task.git_branch if tag == 'master': tag = 'latest' for docker_hub_image in images_to_push: local_image_name = images_to_push[docker_hub_image] docker_hub_tag = tag #dirty dirty prefix hack for Tom.D! if ':' in docker_hub_image: if docker_hub_tag == 'latest' or not docker_hub_tag or len( docker_hub_tag) == 0: value = docker_hub_image else: value = docker_hub_image + '-' + docker_hub_tag value = value.split(':', 1) docker_hub_image = value[0] docker_hub_tag = value[1] print('pushing docker image: {0} {1}:{2}'.format( local_image_name, docker_hub_image, docker_hub_tag)) execute_with_logging('docker tag {0} {1}:{2}'.format( local_image_name, docker_hub_image, docker_hub_tag), log_filename=_get_tag_log(build_path), tail_callback=_write_log_to_db, tail_callback_obj=task) execute_with_logging('docker push {0}:{1}'.format( docker_hub_image, docker_hub_tag), log_filename=_get_push_log(build_path), tail_callback=_write_log_to_db, tail_callback_obj=task)