Initial commit

This commit is contained in:
alexpete
2021-03-05 11:26:34 -08:00
commit a10351f38d
27091 changed files with 5521199 additions and 0 deletions
+57
View File
@@ -0,0 +1,57 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
import boto3
import time
import logging
TIMEOUT = 300
def lambda_handler(event, context):
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
branch_name = event['detail']['referenceName']
ec2_client = boto3.resource('ec2')
response = ec2_client.volumes.filter(Filters=[
{
'Name': 'tag:BranchName',
'Values': [branch_name]
}
])
log.info(f'Deleting EBS volumes for remote-branch {branch_name}.')
for volume in response:
if volume.attachments:
ec2_instance_id = volume.attachments[0]['InstanceId']
try:
log.info(f'Detaching volume {volume.volume_id} from ec2 {ec2_instance_id}.')
volume.detach_from_instance(Device='xvdf',
Force=True,
InstanceId=ec2_instance_id,
VolumeId=volume.volume_id)
except Exception as e:
log.error(f'Failed to detach volume {volume.volume_id} from {ec2_instance_id}.')
log.error(e)
timeout_init = time.clock()
while len(volume.attachments) and volume.attachments[0]['State'] != 'detached':
time.sleep(1)
volume.load()
if (time.clock() - timeout_init) > TIMEOUT:
log.error('Timeout reached trying to detach EBS.')
try:
log.info(f'Deleting volume {volume.volume_id}')
volume.delete()
except Exception as e:
log.error(f'Failed to delete volume {volume.volume_id}.')
log.error(e)
lambda_handler(event, context)
@@ -0,0 +1,74 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
import boto3
import logging
import json
import os
import requests
from botocore.exceptions import ClientError
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
from requests.exceptions import RetryError
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
JENKINS_CREDENTIAL_STORE = os.environ['JENKINS_CREDENTIAL_STORE']
JENKINS_ENDPOINT = os.environ['JENKINS_ENDPOINT']
REGION = os.environ['REGION']
def get_secret(secret_name):
secrets = boto3.client(service_name='secretsmanager', region_name=REGION)
try:
response = secrets.get_secret_value(SecretId=secret_name)
return json.loads(response['SecretString'])
except ClientError as e:
log.error(f'Unable to get secret value: {e}')
return None
def lambda_handler(event, context):
event_record = event['Records'][0]
log.info(f'Received Event: {event_record}')
ref_prefix = "refs/heads/"
branch_name = event_record['codecommit']['references'][0]['ref'][len(ref_prefix):]
repo_name = event_record['eventSourceARN'].split(":")[-1]
credentials = get_secret(JENKINS_CREDENTIAL_STORE)
if credentials:
retries = 3
backoff = 30
status_list = [404] # Retry if the branch doesn't exist yet and provide time for Jenkins to discover it.
method_list = ['POST']
retry_config = Retry(total=retries, backoff_factor=backoff, status_forcelist=status_list, method_whitelist=method_list)
session = requests.Session()
session.mount('https://', HTTPAdapter(max_retries=retry_config))
encoded_branch_name = requests.utils.quote(branch_name, safe='')
build_path = f'/job/{repo_name}/job/{encoded_branch_name}/build'
jenkins_url = requests.compat.urljoin(JENKINS_ENDPOINT, build_path)
try:
response = session.post(jenkins_url, auth=(credentials['username'], credentials['apitoken']))
if response.status_code == 201:
log.info(f'Successfully triggered build on {repo_name}/{branch_name}')
elif response.status_code == 400:
log.info(f'Initial build already started for {repo_name}/{branch_name}. Parameters are already available.')
else:
log.error(f'Failed to start build on {repo_name}/{branch_name}. Status code: {response.status_code}')
except RetryError as e:
log.error(f'Pipeline for {repo_name}/{branch_name} does not exist in Jenkins: {e}')