def lambda_handler(event, context): codecommit = boto3.client("codecommit") filepath = 'pipeline.yml' if 'detail' in event: if 'repositoryName' in event['detail'] and 'commitId' in event[ 'detail']: repo = event['detail']['repositoryName'] commit_id = event['detail']['commitId'] account = event['account'] retorno = get_commit_msg(codecommit, repo, commit_id) msg = retorno['msg'] if 'wasabi.run()' in msg: print("Rodando o Wasabi") f_body = get_files(repo, commit_id, filepath) f_json = change_yml_to_json(f_body) if f_json['status']: payload = {'payload': f_json, 'account': account} sqs_send(filas['payload'], payload) msg = "Arquivo validado e enviado para o Wasabi" add_comment(repo, commit_id, retorno['parents'], msg) else: msg = f_json['MessageError'] add_comment(repo, commit_id, retorno['parents'], msg) else: logging.warning("Arquivo pipeline.yml nao existe")
def save_s3_send_sqs(template_params, requestID, file_template, event): bucket = s3_bucket.split('.')[0].replace('https://', '') logger.info(f'Enviando template para o bucket {bucket}') if upload_file_s3(bucket, file_template): f_tp = file_template.split('/')[-1] f_template = f"{s3_bucket}{f_tp}" msg = { "url": f_template, "account": template_params['account'], "pipelinename": template_params['params']['Projeto'], "requestID": requestID } logger.info( f"Mensagem Enviada para Fila: {filas['deploy']}, RequestId: {msg['requestID']}" ) logger.info(f"Mensagem: {msg}") sqs_send(filas['deploy'], msg)
def test_carga_dos_templates_no_sqs(self): self.create_sqs() s3 = self.create_bucket() self.carga_do_template_da_pipeline_na_tabela_dynamodb() bucket = s3_bucket.split('.')[0].replace('https://', '') payloads = ['payload-ecs'] payload_files = [ 'payload_1.yml', 'payload_2.yml', 'payload_3.yml', 'payload_4.yml', 'payload_5.yml', 'payload_6.yml' ] for payload in payloads: for filename in payload_files: path_filename = f"tests/{payload}/{filename}" f_template = open(path_filename) yml_template = f_template.read() f_template.close() json_template = change_yml_to_json(yml_template) send_payload = { 'payload': json_template, 'requestID': 'xxxx-xxxx-xxxx', 'account': filename } sqs_send(filas['processing'], send_payload) # criar a pipeline template_name = engine.process_templates() # Verificando se o template foi criado no bucket lista_pipelines = [ 'Pipeline-Python-develop-payload_1.yml.json', 'Pipeline-Python-develop-payload_2.yml.json', 'Pipeline-Python-develop-payload_3.yml.json', 'Pipeline-Python-develop-payload_4.yml.json', 'Pipeline-Python-develop-payload_5.yml.json', 'Pipeline-Python-develop-payload_6.yml.json', 'Pipeline-Python-master-payload_1.yml.json' ] s3 = boto3.resource('s3') my_bucket = s3.Bucket(bucket) for object_summary in my_bucket.objects.filter(): print("=====>", object_summary.key) assert object_summary.key in lista_pipelines
def main(): while True: try: for event in sqs_receive(filas['payload']): print("Messagem consumida") payload = json.loads(event.body) #template = payload['payload'] #result = validate.check_template(template) #if result['status']: sqs_send(filas['processing'], payload) sqs_delete(event) #else: # pass #protocol = template_yml['callback']['protocolo'] #endpoint = template_yml['callback']['endpoint'] #pipelineName = template_yml['callback']['pipelineName'] #callback = CallBack(protocolo, endpoint, pipelineName) #callback.send(Msg) except: print('Erro ao validar') time.sleep(polling_time)
'source': 'aws.codecommit', 'account': '033921349789', 'time': '2020-03-25T12:25:58Z', 'region': 'us-east-1', 'resources': ['arn:aws:codecommit:us-east-1:033921349789:projeto'], 'detail': { 'callerUserArn': 'arn:aws:iam::033921349789:user/develop', 'commitId': '91cf0fe55b6b5efd4a4a1ca716836a4e6ca0511a', 'event': 'referenceUpdated', 'oldCommitId': '34baa66383b3493507e5c626cbe0846c7b55f19a', 'referenceFullName': 'refs/heads/master', 'referenceName': 'master', 'referenceType': 'branch', 'repositoryId': 'c83c41dd-d50e-4df5-ab90-45700663cea4', 'repositoryName': 'projeto' } } #lambda_handler(event, event) account = event['account'] filename = 'payload_8.yml' #filename = '../../tests/payload_5.yml' #filename = '../../tests/payload_6.yml' f_template = open(filename) yml_template = f_template.read() f_template.close() requestID = str(uuid.uuid1()) template = change_yml_to_json(yml_template) payload = {'payload': template, 'account': account, 'requestID': requestID} print(payload) sqs_send(filas['processing'], payload)