Пример #1
0
def _copy_src(path):
    if os.path.isfile(path):
        os.makedirs(PACKAGE_DIR)
        shutil.copy(path, PACKAGE_DIR)
    else:
        shutil.copytree(path, PACKAGE_DIR)
    logger.info('source files copied into package')
Пример #2
0
def local_build(path, dependencies_list):
    # check if local build is running inside docker container
    if os.path.isfile('./.dockerenv'):
        logger.info(
            'starting build inside docker container using python {} installed in the container'
            .format(platform.python_version()))
    else:
        logger.info(
            'starting local build using python {} installed in the environment'
            .format(platform.python_version()))

    _clean()
    _copy_src(path)
    _install(dependencies_list)
    _zip()
Пример #3
0
def docker_build(path, dependencies_list, python_version):
    logger.info(
        'starting docker build using python {}. This will internally call local build inside '
        'a docker container'.format(python_version))

    cwd = os.getcwd()

    build_path_env = path
    dependencies_list_env = ','.join(dependencies_list)

    subprocess.call([
        'docker', 'run', '--rm', '-v', cwd + ':' + DOCKER_WORKDIR, '-e',
        'python_version=' + python_version, '-e',
        'build_path=' + build_path_env, '-e',
        'dependencies_list=' + dependencies_list_env, DOCKER_IMAGE
    ])
Пример #4
0
def deploy_lambda(function_name, aws_access_key, aws_secret_key, aws_region):
    if aws_access_key is not None and aws_secret_key is not None and aws_region is not None:
        logger.info(
            'using credentials and region passed as command line arguments')
        lambda_client = boto3.client('lambda',
                                     aws_access_key_id=aws_access_key,
                                     aws_secret_access_key=aws_secret_key,
                                     region_name=aws_region)

    elif (aws_access_key is None
          or aws_secret_key is None) and aws_region is not None:
        logger.info(
            'using credentials from default config and region passed as command line argument'
        )
        lambda_client = boto3.client('lambda', region_name=aws_region)

    elif aws_access_key is not None and aws_secret_key is not None and aws_region is None:
        logger.info(
            'using credentials passed as command line argument and region from default config'
        )
        lambda_client = boto3.client('lambda',
                                     aws_access_key_id=aws_access_key,
                                     aws_secret_access_key=aws_secret_key)

    else:
        logger.info('using credentials and region from default config')
        lambda_client = boto3.client('lambda')

    with open(PACKAGE_NAME + '.zip', 'rb') as zip_file:
        file_content = zip_file.read()

    logger.info('updating lambda function code...')
    response = lambda_client.update_function_code(FunctionName=function_name,
                                                  ZipFile=file_content)
    function_name = response['FunctionName']
    last_modified = response['LastModified']
    last_modified_local = __to_local_timezone__(last_modified)
    logger.info('deploy success - {} last modified at {}'.format(
        function_name, last_modified_local))
Пример #5
0
def _zip():
    shutil.make_archive(PACKAGE_NAME, 'zip', PACKAGE_DIR)
    logger.info(
        'package archived into a zip file, check the directory: {}'.format(
            PACKAGE_DIR))
Пример #6
0
def _install(dependencies_list):
    for dep in dependencies_list:
        subprocess.call(['pip', 'install', '--target', PACKAGE_DIR, dep])
    logger.info('dependencies installed into the package')
Пример #7
0
def _clean():
    shutil.rmtree(WORK_DIR, ignore_errors=True)
    logger.info('build directory {} cleaned'.format(WORK_DIR))
Пример #8
0
def deploy_lambda(function_name, aws_access_key, aws_secret_key, aws_region,
                  s3_bucket):
    if aws_access_key is not None and aws_secret_key is not None and aws_region is not None:
        logger.info(
            'using credentials and region passed as command line arguments')
        lambda_client = boto3.client('lambda',
                                     aws_access_key_id=aws_access_key,
                                     aws_secret_access_key=aws_secret_key,
                                     region_name=aws_region)
        s3_client = boto3.client('s3',
                                 aws_access_key_id=aws_access_key,
                                 aws_secret_access_key=aws_secret_key,
                                 region_name=aws_region)

    elif (aws_access_key is None
          or aws_secret_key is None) and aws_region is not None:
        logger.info(
            'using credentials from default config and region passed as command line argument'
        )
        lambda_client = boto3.client('lambda', region_name=aws_region)
        s3_client = boto3.client('s3', region_name=aws_region)

    elif aws_access_key is not None and aws_secret_key is not None and aws_region is None:
        logger.info(
            'using credentials passed as command line argument and region from default config'
        )
        lambda_client = boto3.client('lambda',
                                     aws_access_key_id=aws_access_key,
                                     aws_secret_access_key=aws_secret_key)
        s3_client = boto3.client('s3',
                                 aws_access_key_id=aws_access_key,
                                 aws_secret_access_key=aws_secret_key)

    else:
        logger.info('using credentials and region from default config')
        lambda_client = boto3.client('lambda')
        s3_client = boto3.client('s3')

    if s3_bucket is None:
        with open(PACKAGE_NAME + '.zip', 'rb') as zip_file:
            file_content = zip_file.read()

        logger.info('updating lambda function code by direct upload...')
        response = lambda_client.update_function_code(
            FunctionName=function_name, ZipFile=file_content)
    else:
        s3_key = PACKAGE_NAME + '.zip'
        logger.info('uploading file to s3 bucket {}'.format(s3_bucket))
        s3_transfer = S3Transfer(s3_client)
        s3_transfer.upload_file(PACKAGE_NAME + '.zip', s3_bucket,
                                PACKAGE_NAME + '.zip')

        logger.info('package uploaded to S3, updating lambda function...')
        response = lambda_client.update_function_code(
            FunctionName=function_name, S3Bucket=s3_bucket, S3Key=s3_key)

    function_name = response['FunctionName']
    last_modified = response['LastModified']
    last_modified_local = _to_local_timezone(last_modified)
    logger.info('deploy success - {} last modified at {}'.format(
        function_name, last_modified_local))