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

Add cdklocal package installer for lpm #10715

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 46 additions & 0 deletions localstack/packages/cdklocal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import os
from functools import lru_cache
from typing import List

from localstack.packages import InstallTarget, Package, PackageInstaller
from localstack.packages.core import NodePackageInstaller

DEFAULT_CDKLOCAL_VERSION = "2.18.0"


class CdkLocalPackage(Package):
def __init__(self, default_version: str = DEFAULT_CDKLOCAL_VERSION):
super().__init__(name="CDK Local", default_version=default_version)

@lru_cache
def _get_installer(self, version: str) -> PackageInstaller:
return CdkLocalInstaller(version)

def get_versions(self) -> List[str]:
return [DEFAULT_CDKLOCAL_VERSION]


class CdkLocalInstaller(NodePackageInstaller):
def __init__(self, version: str):
package_name = "aws-cdk-local"
super().__init__(
package_name=package_name,
package_spec=[f"{package_name}@{version}", "aws-cdk"],
version=version,
)

def _get_install_dir(self, target: InstallTarget) -> str:
# get the global node install path
return os.path.join(target.value, "node-packages")

def _get_install_marker_path(self, install_dir: str) -> str:
return os.path.join(
install_dir,
"node_modules",
self.package_name,
"bin",
"cdklocal",
)


cdklocal_package = CdkLocalPackage()
10 changes: 6 additions & 4 deletions localstack/packages/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
from abc import ABC
from functools import lru_cache
from sys import version_info
from typing import Optional, Tuple
from typing import List, Optional, Tuple, Union

import requests

from localstack import config

from ..constants import LOCALSTACK_VENV_FOLDER, MAVEN_REPO_URL
from ..utils.archives import download_and_extract
from ..utils.collections import ensure_list
from ..utils.files import chmod_r, chown_r, mkdir, rm_rf
from ..utils.http import download
from ..utils.run import is_root, run
Expand Down Expand Up @@ -193,7 +194,7 @@ def __init__(
self,
package_name: str,
version: str,
package_spec: Optional[str] = None,
package_spec: Optional[Union[List[str], str]] = None,
main_module: str = "main.js",
):
"""
Expand All @@ -207,23 +208,24 @@ def __init__(
super().__init__(package_name, version)
self.package_name = package_name
# If the package spec is not explicitly set (f.e. to a repo), we build it and pin the version
self.package_spec = package_spec or f"{self.package_name}@{version}"
self.package_spec = package_spec or [f"{self.package_name}@{version}"]
self.main_module = main_module

def _get_install_marker_path(self, install_dir: str) -> str:
return os.path.join(install_dir, "node_modules", self.package_name, self.main_module)

def _install(self, target: InstallTarget) -> None:
target_dir = self._get_install_dir(target)
package_spec = ensure_list(self.package_spec)

run(
[
"npm",
"install",
"--prefix",
target_dir,
self.package_spec,
]
+ package_spec,
)
# npm 9+ does _not_ set the ownership of files anymore if run as root
# - https://github.blog/changelog/2022-10-24-npm-v9-0-0-released/
Expand Down
7 changes: 7 additions & 0 deletions localstack/packages/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ def ffmpeg_package() -> Package:
from localstack.packages.ffmpeg import ffmpeg_package

return ffmpeg_package


@package(name="cdklocal")
def cdklocal_package() -> Package:
from localstack.packages.cdklocal import cdklocal_package

return cdklocal_package
12 changes: 11 additions & 1 deletion localstack/runtime/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,17 @@ class ShellScriptRunner(ScriptRunner):
"""

def run(self, path: str) -> None:
exit_code = subprocess.call(args=[], executable=path)
from localstack import config

# add special known paths to the system path
new_env = os.environ.copy()
for additional_path in [
# node modules dirs
os.path.join(config.dirs.var_libs, "node-packages", "node_modules", ".bin"),
os.path.join(config.dirs.static_libs, "node-packages", "node_modules", ".bin"),
]:
new_env["PATH"] = f"{new_env['PATH']}:{additional_path}"
exit_code = subprocess.call(args=[], executable=path, env=new_env)
if exit_code != 0:
raise OSError("Script %s returned a non-zero exit code %s" % (path, exit_code))

Expand Down