class CodeBuild: def __init__(self, project_name, role_name): self.codebuild = Session().client('codebuild') self.iam = IAM(role_name=role_name) self.project_name = project_name return def _invoke_via_paginator(self, method, field_id, use_paginator, **kwargs): paginator = self.codebuild.get_paginator(method) for page in paginator.paginate(**kwargs): for id in page.get(field_id): yield id if use_paginator is False: return def all_builds_ids(self, use_paginator=False): return self._invoke_via_paginator('list_builds', 'ids', use_paginator) def build_info(self, build_id): builds = self.codebuild.batch_get_builds(ids=[build_id]).get('builds') return Misc.array_pop(builds, 0) def build_start(self): kvargs = {'projectName': self.project_name} return self.codebuild.start_build(**kvargs).get('build').get('arn') def build_wait_for_completion(self, build_id, sleep_for=0.5, max_attempts=20, log_status=False): for i in range(0, max_attempts): build_info = self.build_info(build_id) build_status = build_info.get('buildStatus') current_phase = build_info.get('currentPhase') if log_status: Dev.pprint("[{0}] {1} {2}".format(i, build_status, current_phase)) if build_status != 'IN_PROGRESS': return build_info sleep(sleep_for) return None def policies_create(self, policies): return self.iam.policies_create(policies, self.project_name) def project_builds(self, ids): return self.codebuild.batch_get_builds(ids=ids) def project_create(self, project_repo, service_role): kvargs = { 'name': self.project_name, 'source': { 'type': 'GITHUB', 'location': project_repo }, 'artifacts': { 'type': 'NO_ARTIFACTS' }, 'environment': { 'type': 'LINUX_CONTAINER', 'image': 'aws/codebuild/python:3.7.1-1.7.0', 'computeType': 'BUILD_GENERAL1_SMALL' }, 'serviceRole': service_role } return self.codebuild.create_project(**kvargs) def project_delete(self): if self.project_exists() is False: return False self.codebuild.delete_project(name=self.project_name) return self.project_exists() is False def project_exists(self): return self.project_name in self.projects() def project_info(self): projects = Misc.get_value( self.codebuild.batch_get_projects(names=[self.project_name]), 'projects', []) return Misc.array_pop(projects, 0) def project_builds_ids(self, project_name, use_paginator=False): if use_paginator: kwargs = {'projectName': project_name} else: kwargs = {'projectName': project_name, 'sortOrder': 'DESCENDING'} return self._invoke_via_paginator('list_builds_for_project', 'ids', use_paginator, **kwargs) def projects(self): return self.codebuild.list_projects().get('projects')
class IAM_Role: def __init__(self, role_name=None): self.role_name = role_name or f"osbot_temp_role_{random_string()}" self.iam = IAM(role_name=self.role_name) def add_policy_for__lambda(self): temp_policy_name = 'policy_{0}'.format(self.role_name) cloud_watch_arn = f'arn:aws:logs:{AWS_Config().aws_session_region_name()}:{AWS_Config().aws_session_account_id()}:log-group:/aws/lambda/*' iam_policy = IAM_Policy(temp_policy_name) policy_arn = iam_policy.add_cloud_watch(cloud_watch_arn).create().get( 'policy_arn') self.iam.role_policy_attach(policy_arn) return policy_arn def arn(self): return self.iam.role_arn() def attach_policy(self, policy_name, policy_document): self.delete_policy(policy_name=policy_name) result_create = self.iam.policy_create(policy_name=policy_name, policy_document=policy_document) policy_arn = result_create.get('policy_arn') self.iam.role_policy_attach(policy_arn=policy_arn) return policy_arn def create(self, policy_document, skip_if_exists=True): self.iam.role_create(policy_document=policy_document, skip_if_exists=skip_if_exists) return self.exists() def create_for__lambda(self): result = self.create_for_service__assume_role('lambda.amazonaws.com') if result.get('status') == 'ok': self.add_policy_for__lambda() return result def create_for__code_build(self): return self.create_for_service__assume_role('codebuild.amazonaws.com') def create_for_service__assume_role(self, service): statement = { 'Action': 'sts:AssumeRole', 'Effect': 'Allow', 'Principal': { 'Service': service } } return self.create_from_statement(statement) def create_for_service(self, service, statement): statement['Principal'] = {'Service': service} return self.create_from_statement(statement) def create_for_service_with_policies(self, service, policies, project_name, recreate_policy=False): role = self.create_for_service__assume_role(service) role_arn = role.get('role_arn') policies_arns = self.iam.policies_create(policies, project_name, recreate_policy) self.iam.role_policies_attach(policies_arns) return {"role_arn": role_arn, "policies_arns": policies_arns} def create_from_statement(self, statement): return self.create_from_statements([statement]) def create_from_statements(self, statement): role_arn = self.iam.role_arn() if role_arn: return { 'status': 'warning', 'data': 'role already exists', 'role_name': self.iam.role_name, 'role_arn': role_arn } else: policy_document = {'Statement': statement} data = self.iam.role_create(policy_document) return { 'status': 'ok', 'data': data, 'role_name': self.iam.role_name, 'role_arn': data.get('Arn') } def delete(self): return self.iam.role_delete() def delete_policy(self, policy_arn=None, policy_name=None): return self.iam.policy_delete(policy_arn=policy_arn, policy_name=policy_name) def exists(self): return self.iam.role_exists() def info(self): return self.iam.role_info() def not_exists(self): return self.iam.role_not_exists() def policies_statements(self): return self.iam.role_policies_statements()