Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Bespoke Tool Path for dotnet-lambda Commands #127

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions aws_lambda_builders/workflows/dotnet_clipackage/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,22 @@ class GlobalToolInstallAction(BaseAction):
DESCRIPTION = "Install or update the Amazon.Lambda.Tools .NET Core Global Tool."
PURPOSE = Purpose.COMPILE_SOURCE

def __init__(self, subprocess_dotnet):
def __init__(self, subprocess_dotnet, tool_dir):
super(GlobalToolInstallAction, self).__init__()
self.subprocess_dotnet = subprocess_dotnet
self.tool_dir = tool_dir

def execute(self):
try:
LOG.debug("Installing Amazon.Lambda.Tools Global Tool")
self.subprocess_dotnet.run(
['tool', 'install', '-g', 'Amazon.Lambda.Tools'],
['tool', 'install', '--tool-path', self.tool_dir, 'Amazon.Lambda.Tools'],
Copy link
Contributor

@CoshUS CoshUS Mar 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would still be a global tool but with a custom path.
https://docs.microsoft.com/en-us/dotnet/core/tools/global-tools

As a global tool in a custom location (also known as a tool-path tool).

The tool binaries are installed in a location that you specify. You can invoke the tool from the installation directory or by providing the directory with the command name or by adding the directory to the PATH environment variable. One version of a tool is used for all directories on the machine.

Maybe we can use local tools instead and check whether a global Amazon.Lambda.Tools is already installed.

As a local tool (applies to .NET Core SDK 3.0 and later).

The tool binaries are installed in a default directory. You invoke the tool from the installation directory or any of its subdirectories. Different directories can use different versions of the same tool.

We should also check whether a global version of the Amazon.Lambda.Tools is already installed and skip over if needed.

)
except DotnetCLIExecutionError as ex:
LOG.debug("Error installing probably due to already installed. Attempt to update to latest version.")
try:
self.subprocess_dotnet.run(
['tool', 'update', '-g', 'Amazon.Lambda.Tools'],
['tool', 'update', '--tool-path', self.tool_dir, 'Amazon.Lambda.Tools'],
)
except DotnetCLIExecutionError as ex:
raise ActionFailedError("Error configuring the Amazon.Lambda.Tools .NET Core Global Tool: " + str(ex))
Expand All @@ -50,11 +51,12 @@ class RunPackageAction(BaseAction):
DESCRIPTION = "Execute the `dotnet lambda package` command."
PURPOSE = Purpose.COMPILE_SOURCE

def __init__(self, source_dir, subprocess_dotnet, artifacts_dir, options, mode, os_utils=None):
def __init__(self, source_dir, subprocess_dotnet, artifacts_dir, tool_dir, options, mode, os_utils=None):
super(RunPackageAction, self).__init__()
self.source_dir = source_dir
self.subprocess_dotnet = subprocess_dotnet
self.artifacts_dir = artifacts_dir
self.tool_dir = tool_dir
self.options = options
self.mode = mode
self.os_utils = os_utils if os_utils else OSUtils()
Expand All @@ -80,6 +82,7 @@ def execute(self):

self.subprocess_dotnet.run(
arguments,
tool_dir=self.tool_dir,
cwd=self.source_dir
)

Expand Down
23 changes: 22 additions & 1 deletion aws_lambda_builders/workflows/dotnet_clipackage/dotnetcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import sys
import logging
import os

from .utils import OSUtils

Expand Down Expand Up @@ -36,7 +37,7 @@ def __init__(self, dotnet_exe=None, os_utils=None):

self.dotnet_exe = dotnet_exe

def run(self, args, cwd=None):
def run(self, args, tool_dir=None, cwd=None):
if not isinstance(args, list):
raise ValueError('args must be a list')

Expand All @@ -47,9 +48,17 @@ def run(self, args, cwd=None):

LOG.debug("executing dotnet: %s", invoke_dotnet)

if tool_dir is None:
env = None
else:
env = SubprocessDotnetCLI._merge(
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm far from a Python expert, so if I've made a hash of this, please do let me know.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have dropped support for running Lambda builder in Python 2.
Let's directly use env.copy().update(...) instead.

os.environ,
PATH=os.pathsep.join([tool_dir, os.environ['PATH']]))

p = self.os_utils.popen(invoke_dotnet,
stdout=self.os_utils.pipe,
stderr=self.os_utils.pipe,
env=env,
cwd=cwd)

out, err = p.communicate()
Expand All @@ -61,3 +70,15 @@ def run(self, args, cwd=None):

if p.returncode != 0:
raise DotnetCLIExecutionError(message=err.decode('utf8').strip())

# The {**x, **y} syntax will not work in Python 2.
@staticmethod
def _merge(left, **right):
"""
Shallowly merges the elements of `right` onto a copy of `left`
and returns that copy.
"""

output = left.copy()
output.update(right)
return output
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ def __init__(self,

options = kwargs["options"] if "options" in kwargs else {}
subprocess_dotnetcli = SubprocessDotnetCLI(os_utils=OSUtils())
dotnetcli_install = GlobalToolInstallAction(subprocess_dotnet=subprocess_dotnetcli)
dotnetcli_install = GlobalToolInstallAction(subprocess_dotnet=subprocess_dotnetcli,
tool_dir=scratch_dir)
chrisoverzero marked this conversation as resolved.
Show resolved Hide resolved

dotnetcli_deployment = RunPackageAction(source_dir,
subprocess_dotnet=subprocess_dotnetcli,
artifacts_dir=artifacts_dir,
tool_dir=scratch_dir,
options=options,
mode=mode)
self.actions = [
Expand Down
37 changes: 22 additions & 15 deletions tests/unit/workflows/dotnet_clipackage/test_actions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from unittest import TestCase
from mock import patch
import os
import platform
from mock import patch

from aws_lambda_builders.actions import ActionFailedError
from aws_lambda_builders.workflows.dotnet_clipackage.dotnetcli import DotnetCLIExecutionError
Expand All @@ -13,26 +13,30 @@ class TestGlobalToolInstallAction(TestCase):
@patch("aws_lambda_builders.workflows.dotnet_clipackage.dotnetcli.SubprocessDotnetCLI")
def setUp(self, MockSubprocessDotnetCLI):
self.subprocess_dotnet = MockSubprocessDotnetCLI.return_value
self.scratch_dir = os.path.join('/scratch_dir')

def tearDown(self):
self.subprocess_dotnet.reset_mock()

def test_global_tool_install(self):
action = GlobalToolInstallAction(self.subprocess_dotnet)
action = GlobalToolInstallAction(self.subprocess_dotnet, self.scratch_dir)
action.execute()
self.subprocess_dotnet.run.assert_called_once_with(['tool', 'install', '-g', 'Amazon.Lambda.Tools'])
self.subprocess_dotnet.run.assert_called_once_with(
['tool', 'install', '--tool-path', self.scratch_dir, 'Amazon.Lambda.Tools'])

def test_global_tool_update(self):
self.subprocess_dotnet.run.side_effect = [DotnetCLIExecutionError(message="Already Installed"), None]
action = GlobalToolInstallAction(self.subprocess_dotnet)
action = GlobalToolInstallAction(self.subprocess_dotnet, self.scratch_dir)
action.execute()
self.subprocess_dotnet.run.assert_any_call(['tool', 'install', '-g', 'Amazon.Lambda.Tools'])
self.subprocess_dotnet.run.assert_any_call(['tool', 'update', '-g', 'Amazon.Lambda.Tools'])
self.subprocess_dotnet.run.assert_any_call(
['tool', 'install', '--tool-path', self.scratch_dir, 'Amazon.Lambda.Tools'])
self.subprocess_dotnet.run.assert_any_call(
['tool', 'update', '--tool-path', self.scratch_dir, 'Amazon.Lambda.Tools'])

def test_global_tool_update_failed(self):
self.subprocess_dotnet.run.side_effect = [DotnetCLIExecutionError(message="Already Installed"),
DotnetCLIExecutionError(message="Updated Failed")]
action = GlobalToolInstallAction(self.subprocess_dotnet)
action = GlobalToolInstallAction(self.subprocess_dotnet, self.scratch_dir)
self.assertRaises(ActionFailedError, action.execute)


Expand All @@ -54,21 +58,22 @@ def test_build_package(self):
mode = "Release"

options = {}
action = RunPackageAction(self.source_dir, self.subprocess_dotnet, self.artifacts_dir, options, mode,
self.os_utils)
action = RunPackageAction(self.source_dir, self.subprocess_dotnet, self.artifacts_dir, self.scratch_dir,
options, mode, self.os_utils)

action.execute()

zipFilePath = os.path.join('/', 'artifacts_dir', 'source_dir.zip')

self.subprocess_dotnet.run.assert_called_once_with(['lambda', 'package', '--output-package', zipFilePath],
tool_dir='/scratch_dir',
cwd='/source_dir')

def test_build_package_arguments(self):
mode = "Release"
options = {"--framework": "netcoreapp2.1"}
action = RunPackageAction(self.source_dir, self.subprocess_dotnet, self.artifacts_dir, options, mode,
self.os_utils)
action = RunPackageAction(self.source_dir, self.subprocess_dotnet, self.artifacts_dir, self.scratch_dir,
options, mode, self.os_utils)

action.execute()

Expand All @@ -79,28 +84,30 @@ def test_build_package_arguments(self):

self.subprocess_dotnet.run.assert_called_once_with(['lambda', 'package', '--output-package',
zipFilePath, '--framework', 'netcoreapp2.1'],
tool_dir='/scratch_dir',
cwd='/source_dir')

def test_build_error(self):
mode = "Release"

self.subprocess_dotnet.run.side_effect = DotnetCLIExecutionError(message="Failed Package")
options = {}
action = RunPackageAction(self.source_dir, self.subprocess_dotnet, self.artifacts_dir, options, mode,
self.os_utils)
action = RunPackageAction(self.source_dir, self.subprocess_dotnet, self.artifacts_dir, self.scratch_dir,
options, mode, self.os_utils)

self.assertRaises(ActionFailedError, action.execute)

def test_debug_configuration_set(self):
mode = "Debug"
options = None
action = RunPackageAction(self.source_dir, self.subprocess_dotnet, self.artifacts_dir, options, mode,
self.os_utils)
action = RunPackageAction(self.source_dir, self.subprocess_dotnet, self.artifacts_dir, self.scratch_dir,
options, mode, self.os_utils)

zipFilePath = os.path.join('/', 'artifacts_dir', 'source_dir.zip')

action.execute()

self.subprocess_dotnet.run.assert_called_once_with(
['lambda', 'package', '--output-package', zipFilePath, '--configuration', 'Debug'],
tool_dir='/scratch_dir',
cwd='/source_dir')