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 local package sources #677

Merged
merged 1 commit into from
Nov 4, 2018
Merged
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
10 changes: 8 additions & 2 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,14 @@ For ``.tar.gz`` & ``zip`` archives on s3, specify a ``bucket`` & ``key``::
# last modified date on S3 changes
use_latest: false

Use the ``paths`` option when subdirectories of the repo/archive should be
added to Stacker's ``sys.path``.
Local directories can also be specified::

package_sources:
local:
- source: ../vpc

Use the ``paths`` option when subdirectories of the repo/archive/directory
should be added to Stacker's ``sys.path``.

Cloned repos/archives will be cached between builds; the cache location defaults
to ~/.stacker but can be manually specified via the **stacker_cache_dir** top
Expand Down
9 changes: 9 additions & 0 deletions stacker/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,13 @@ def not_empty_list(value):
class AnyType(BaseType):
pass

class LocalPackageSource(Model):
source = StringType(required=True)

paths = ListType(StringType, serialize_when_none=False)

configs = ListType(StringType, serialize_when_none=False)


class GitPackageSource(Model):
uri = StringType(required=True)
Expand Down Expand Up @@ -264,6 +271,8 @@ class S3PackageSource(Model):


class PackageSources(Model):
local = ListType(ModelType(LocalPackageSource))

git = ListType(ModelType(GitPackageSource))

s3 = ListType(ModelType(S3PackageSource))
Expand Down
27 changes: 23 additions & 4 deletions stacker/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,13 +628,28 @@ def create_cache_directories(self):

def get_package_sources(self):
"""Make remote python packages available for local use."""
# Checkout local modules
for config in self.sources.get('local', []):
self.fetch_local_package(config=config)
# Checkout S3 repositories specified in config
for config in self.sources.get('s3', []):
self.fetch_s3_package(config=config)
# Checkout git repositories specified in config
for config in self.sources.get('git', []):
self.fetch_git_package(config=config)

def fetch_local_package(self, config):
"""Make a local path available to current stacker config.

Args:
config (dict): 'local' path config dictionary

"""
# Update sys.path & merge in remote configs (if necessary)
self.update_paths_and_config(config=config,
pkg_dir_name=config['source'],
pkg_cache_dir=os.getcwd())

def fetch_s3_package(self, config):
"""Make a remote S3 archive available for local use.

Expand Down Expand Up @@ -773,21 +788,25 @@ def fetch_git_package(self, config):
self.update_paths_and_config(config=config,
pkg_dir_name=dir_name)

def update_paths_and_config(self, config, pkg_dir_name):
def update_paths_and_config(self, config, pkg_dir_name,
pkg_cache_dir=None):
"""Handle remote source defined sys.paths & configs.

Args:
config (dict): git config dictionary
pkg_dir_name (string): directory name of the stacker archive
pkg_cache_dir (string): fully qualified path to stacker cache
cache directory

"""
cached_dir_path = os.path.join(self.package_cache_dir, pkg_dir_name)
if pkg_cache_dir is None:
pkg_cache_dir = self.package_cache_dir
cached_dir_path = os.path.join(pkg_cache_dir, pkg_dir_name)

# Add the appropriate directory (or directories) to sys.path
if config.get('paths'):
for path in config['paths']:
path_to_append = os.path.join(self.package_cache_dir,
pkg_dir_name,
path_to_append = os.path.join(cached_dir_path,
path)
logger.debug("Appending \"%s\" to python sys.path",
path_to_append)
Expand Down