Skip to content

Commit 01f3564

Browse files
committed
fix: add ruby pathresolver and validator
1 parent 84e1a79 commit 01f3564

File tree

5 files changed

+96
-2
lines changed

5 files changed

+96
-2
lines changed

aws_lambda_builders/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class UnsupportedManifestError(LambdaBuilderError):
1818
class MisMatchRuntimeError(LambdaBuilderError):
1919
MESSAGE = "{language} executable found in your path does not " \
2020
"match runtime. " \
21-
"\n Expected version: {required_runtime}, Found version: {found_runtime}. " \
21+
"\n Expected version: {required_runtime}, Found version: {runtime_path}. " \
2222
"\n Possibly related: https://github.com/awslabs/aws-lambda-builders/issues/30"
2323

2424

aws_lambda_builders/workflows/python_pip/validator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Supported Runtimes and their validations.
2+
Python Runtime Validation
33
"""
44

55
import logging
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
Ruby Path Resolver that looks for the executable by runtime first, before proceeding to 'ruby' in PATH.
3+
"""
4+
5+
from whichcraft import which
6+
7+
8+
class RubyPathResolver(object):
9+
10+
def __init__(self, runtime):
11+
self.language = 'ruby'
12+
self.runtime = runtime
13+
self.executables = [self.runtime, self.language]
14+
15+
def _which(self):
16+
for executable in self.executables:
17+
path = which(executable)
18+
if path:
19+
return path
20+
raise ValueError("Path resolution for runtime: {} of language: "
21+
"{} was not successful".format(self.runtime, self.language))
22+
23+
@property
24+
def path(self):
25+
return self._which()
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""
2+
Supported Runtimes and their validations.
3+
"""
4+
5+
import logging
6+
import os
7+
import subprocess
8+
9+
from aws_lambda_builders.exceptions import MisMatchRuntimeError
10+
11+
LOG = logging.getLogger(__name__)
12+
13+
14+
class RubyRuntimeValidator(object):
15+
SUPPORTED_RUNTIMES = [
16+
"ruby2.5"
17+
]
18+
19+
def __init__(self, runtime, runtime_path):
20+
self.language = "ruby"
21+
self.runtime = runtime
22+
self.runtime_path = runtime_path
23+
24+
def has_runtime(self):
25+
"""
26+
Checks if the runtime is supported.
27+
:param string runtime: Runtime to check
28+
:return bool: True, if the runtime is supported.
29+
"""
30+
return self.runtime in self.SUPPORTED_RUNTIMES
31+
32+
def validate_runtime(self):
33+
"""
34+
Checks if the language supplied matches the required lambda runtime
35+
:param string runtime_path: runtime to check eg: /usr/bin/python3.6
36+
:raises MisMatchRuntimeError: Version mismatch of the language vs the required runtime
37+
"""
38+
if not self.has_runtime():
39+
LOG.warning("'%s' runtime is not "
40+
"a supported runtime", self.runtime_path)
41+
return
42+
cmd = self._validate_ruby(self.runtime_path)
43+
44+
p = subprocess.Popen(cmd,
45+
cwd=os.getcwd(),
46+
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
47+
p.communicate()
48+
if p.returncode != 0:
49+
raise MisMatchRuntimeError(language=self.language,
50+
required_runtime=self.runtime,
51+
runtime_path=self.runtime_path)
52+
53+
def _validate_ruby(self, runtime_path):
54+
major, minor = self.runtime.replace(self.language, "").split('.')
55+
cmd = [
56+
runtime_path,
57+
"-e",
58+
"unless RUBY_VERSION.match(/{major}\.{minor}\.\d/); exit(1); end".format(
59+
major=major,
60+
minor=minor)]
61+
return cmd

aws_lambda_builders/workflows/ruby_bundler/workflow.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
from aws_lambda_builders.workflow import BaseWorkflow, Capability
66
from aws_lambda_builders.actions import CopySourceAction
7+
from aws_lambda_builders.workflows.ruby_bundler.path_resolver import RubyPathResolver
8+
from aws_lambda_builders.workflows.ruby_bundler.validator import RubyRuntimeValidator
79
from .actions import RubyBundlerInstallAction, RubyBundlerVendorAction
810
from .utils import OSUtils
911
from .bundler import SubprocessBundler
@@ -53,3 +55,9 @@ def __init__(self,
5355
bundle_install,
5456
bundle_deployment,
5557
]
58+
59+
def get_executable(self):
60+
return RubyPathResolver(runtime=self.runtime).path
61+
62+
def get_validator(self):
63+
return RubyRuntimeValidator(runtime=self.runtime, runtime_path=self.get_executable())

0 commit comments

Comments
 (0)