Skip to content
This repository has been archived by the owner on Jan 7, 2022. It is now read-only.

Commit

Permalink
Refactor the "push TC parameter" functionality to make it more genera…
Browse files Browse the repository at this point in the history
…l. Update Readme
  • Loading branch information
sdomme committed Nov 13, 2015
1 parent e42b89d commit b160bbd
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
9 changes: 9 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ This will upload the files to the following files:
- `my_lambda/v123/projectname.zip`
- `my_lambda/latest/projectname.zip`

In an TeamCity Environment (teamcity_output = True) you can use the property
``teamcity_parameter`` to push en ``##teamcity[setParameter name='' value='']``
event to TeamCity and rewrite this parameter with the name of the uploaded
zip file. For example:

.. code:: python
project.set_property('teamcity_parameter', 'my_tc_parameter')
@Task: upload_cfn_to_s3
-----------------------
ATTENTION: This task is currently only available for Python 2.7. No 2.6 and no
Expand Down
18 changes: 5 additions & 13 deletions src/main/python/pybuilder_aws_lambda_plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@
# -*- coding: utf-8 -*-

import os
import boto3
import zipfile
import subprocess
import sys
import zipfile

from pybuilder.core import depends, init, task
from pybuilder.plugins.python.install_dependencies_plugin import (
as_pip_argument)
from pybuilder.ci_server_interaction import flush_text_line

from .helpers import upload_helper
from .helpers import upload_helper, teamcity_helper


def zip_recursive(archive, directory, folder=''):
Expand Down Expand Up @@ -56,6 +53,7 @@ def initialize_plugin(project):
project.set_property(
'template_file_access_control', 'bucket-owner-full-control')
project.set_property('template_key_prefix', '')
project.set_property('teamcity_parameter', '')


@task('package_lambda_code',
Expand All @@ -82,13 +80,6 @@ def package_lambda_code(project, logger):
logger.info('Lambda zip is available at: "{0}".'.format(path_to_zipfile))


def teamcity_helper(keyname_version):
flush_text_line(
"##teamcity[setParameter name='crassus_filename' value='{0}']".format(
keyname_version
))


@task('upload_zip_to_s3', description='Upload a packaged lambda-zip to S3')
@depends('package_lambda_code')
def upload_zip_to_s3(project, logger):
Expand All @@ -103,8 +94,9 @@ def upload_zip_to_s3(project, logger):
acl = project.get_property('lambda_file_access_control')
upload_helper(logger, bucket_name, keyname_version, data, acl)
upload_helper(logger, bucket_name, keyname_latest, data, acl)
if project.get_property("teamcity_output"):
teamcity_helper(keyname_version)
tc_param = project.get_property('teamcity_parameter')
if project.get_property("teamcity_output") and tc_param:
teamcity_helper(tc_param, keyname_version)

if sys.version_info[0:2] == (2, 7):
from upload_task import upload_cfn_to_s3
Expand Down
8 changes: 7 additions & 1 deletion src/main/python/pybuilder_aws_lambda_plugin/helpers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import boto3

from pybuilder.ci_server_interaction import flush_text_line

def upload_helper(logger, bucket_name, keyname, data, acl):
s3 = boto3.resource('s3')
logger.info(
'Uploading lambda-zip to bucket: "{0}" as key: "{1}"'
.format(bucket_name, keyname))
s3.Bucket(bucket_name).put_object(Key=keyname, Body=data, ACL=acl)

def teamcity_helper(tc_param, keyname):
flush_text_line(
"##teamcity[setParameter name='{0}' value='{1}']".format(
tc_param, keyname
))
7 changes: 4 additions & 3 deletions src/unittest/python/pybuilder_aws_lambda_plugin_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,16 @@ def test_if_file_was_uploaded_to_s3_with_bucket_prefix(self):
self.assertEqual(s3_object_list[0].key, 'palp/latest/palp.zip')
self.assertEqual(s3_object_list[1].key, 'palp/v123/palp.zip')

@mock.patch("pybuilder_aws_lambda_plugin.flush_text_line")
@mock.patch("pybuilder_aws_lambda_plugin.helpers.flush_text_line")
def test_teamcity_output_if_set(self, flush_text_line_mock):
self.project.set_property('teamcity_output', True)
self.project.set_property('teamcity_parameter', 'palp_keyname')

upload_zip_to_s3(self.project, mock.MagicMock(Logger))

flush_text_line_mock.assert_called_with("##teamcity[setParameter name='crassus_filename' value='v123/palp.zip']")
flush_text_line_mock.assert_called_with("##teamcity[setParameter name='palp_keyname' value='v123/palp.zip']")

@mock.patch("pybuilder_aws_lambda_plugin.flush_text_line")
@mock.patch("pybuilder_aws_lambda_plugin.helpers.flush_text_line")
def test_teamcity_output_if_not_set(self, flush_text_line_mock):

upload_zip_to_s3(self.project, mock.MagicMock(Logger))
Expand Down

0 comments on commit b160bbd

Please sign in to comment.