def tasks_ps(db=get_boto3_resource('dynamodb'), git_repo=None, git_branch=None, git_tag=None): table = db.Table(_task_table_name) db_filter = (Attr('state').eq(State.pending) | Attr('state').eq(State.downloading) | Attr('state').eq(State.building) | Attr('state').eq(State.testing) | Attr('state').eq(State.pushing)) if git_branch: db_filter = db_filter & Attr('git_branch').eq(git_branch) if git_tag: db_filter = db_filter & Attr('git_tag').eq(git_tag) if db_filter and git_repo: response = table.query( FilterExpression=db_filter, KeyConditionExpression=Key('git_repo').eq(git_repo)) else: response = table.scan() if 'Items' in response: for task in response['Items']: yield Task.from_dict(task)
def _queue_build(repo, branch='master', tag='latest', created_by=None): task = Task(git_repo=repo, git_branch=branch, git_tag=tag, created_by=created_by) db.save_task(task) queues.enqueue_task(task)
def run(): db.create_task_table() queues.create_task_queue() print('removing all docker networks') remove_all_docker_networks() print('removing all docker images') remove_all_docker_images() task_queue = queues.get_task_queue() terminate = False while not terminate: messages = task_queue.receive_messages(MaxNumberOfMessages=1, WaitTimeSeconds=1) if not messages or len(messages) == 0: continue task = Task.from_json(messages[0].body) if not task: continue # clean up old dockers sitting here before. _remove_all_docker_networks_blocking() _remove_all_docker_images_blocking() process = Process(target=_run_build, args=( task.git_repo, task.git_branch, task.git_tag, task.created_at,)) try: process.start() # delete message for queue now we are actually processing it. messages[0].delete() while process.is_alive(): process.join(2) if process.is_alive(): task = db.reload_task(task) if task.state == State.cancel: _cancel_task(task, process) except KeyboardInterrupt: terminate = True if process and process.is_alive(): _cancel_task(db.reload_task(task), process) _remove_all_docker_networks_blocking() _remove_all_docker_images_blocking()
def reload_task(task, db=get_boto3_resource('dynamodb')): table = db.Table(_task_table_name) response = table.get_item( Key={ 'git_repo': task.git_repo, 'key': '{0}:{1}:{2}'.format(task.created_at.isoformat(), task.git_branch, task.git_tag) }) return Task.from_dict(response['Item'])
def load_task(git_repo, git_branch, git_tag, created_at, db=get_boto3_resource('dynamodb')): table = db.Table(_task_table_name) response = table.get_item( Key={ 'git_repo': git_repo, 'key': '{0}:{1}:{2}'.format(created_at.isoformat(), git_branch, git_tag) }) if 'Item' in response: return Task.from_dict(response['Item']) return None
def _build(repo, branch='master', tag=None, args=[]): if not repo: print('failed: are you in a git repo?') return task = Task(git_repo=repo, git_branch=branch, git_tag=tag, build_args=args, created_by=_get_username()) db.save_task(task) queues.enqueue_task(task) msg = 'ok - building: ' + repo if branch: msg += ' - ' + branch if tag: msg += ' - ' + tag print(msg)
def tasks_list(db=get_boto3_resource('dynamodb'), git_repo=None, git_branch=None, git_tag=None): table = db.Table(_task_table_name) db_filter = None if git_branch: db_filter = db_filter & Attr('git_branch').eq( git_branch) if db_filter else Attr('git_branch').eq(git_branch) if git_tag: db_filter = db_filter & Attr('git_tag').eq( git_tag) if db_filter else Attr('git_tag').eq(git_tag) if db_filter and git_repo: response = table.query( FilterExpression=db_filter, KeyConditionExpression=Key('git_repo').eq(git_repo)) else: response = table.scan() if 'Items' in response: for task in response['Items']: yield Task.from_dict(task)
def load_all_tasks(db=get_boto3_resource('dynamodb')): table = db.Table(_task_table_name) response = table.scan() if 'Items' in response: for task in response['Items']: yield Task.from_dict(task)