Skip to content

Commit

Permalink
python version changes / os.walk fix
Browse files Browse the repository at this point in the history
  • Loading branch information
rmaynardap committed Jun 17, 2024
1 parent cf2f5fb commit 8268c03
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 70 deletions.
49 changes: 2 additions & 47 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ classifiers = [
]

[tool.poetry.dependencies]
python = "^3.10"
python = "^3.11"
boto3 = "^1.34"
click = "^8.1"
jinja2 = "^3.1"
Expand Down
12 changes: 6 additions & 6 deletions tests/util/test_util_terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def test_get_tf_version(
def mock_mirror_setup():
mock_mirror_settings = {
"providers": MagicMock(),
"terraform_bin": "/path/to/terraform",
'terraform_bin': "/path/to/terraform",
"working_dir": "/working/dir",
"cache_dir": "/cache/dir",
"temp_dir": "/temp/dir",
Expand Down Expand Up @@ -208,7 +208,7 @@ def test_mirror_providers(mock_mirror_setup):

result = mirror_providers(
providers=mock_mirror_settings["providers"],
terraform_bin=mock_mirror_settings["terraform_bin"],
terraform_bin=mock_mirror_settings['terraform_bin'],
working_dir=mock_mirror_settings["working_dir"],
cache_dir=mock_mirror_settings["cache_dir"],
)
Expand All @@ -220,7 +220,7 @@ def test_mirror_providers(mock_mirror_setup):
mock_mirror_settings["cache_dir"],
)
mock_pipe_exec.assert_called_once_with(
f"{mock_mirror_settings["terraform_bin"]} providers mirror {mock_mirror_settings['cache_dir']}",
f"{mock_mirror_settings['terraform_bin']} providers mirror {mock_mirror_settings['cache_dir']}",
cwd=mock_mirror_settings["temp_dir"],
stream_output=True,
)
Expand All @@ -243,7 +243,7 @@ def test_mirror_providers_tf_error(mock_mirror_setup):
with pytest.raises(SystemExit):
mirror_providers(
providers=mock_mirror_settings["providers"],
terraform_bin=mock_mirror_settings["terraform_bin"],
terraform_bin=mock_mirror_settings['terraform_bin'],
working_dir=mock_mirror_settings["working_dir"],
cache_dir=mock_mirror_settings["cache_dir"],
)
Expand All @@ -255,7 +255,7 @@ def test_mirror_providers_tf_error(mock_mirror_setup):
mock_mirror_settings["cache_dir"],
)
mock_pipe_exec.assert_called_once_with(
f"{mock_mirror_settings["terraform_bin"]} providers mirror {mock_mirror_settings['cache_dir']}",
f"{mock_mirror_settings['terraform_bin']} providers mirror {mock_mirror_settings['cache_dir']}",
cwd=mock_mirror_settings["temp_dir"],
stream_output=True,
)
Expand All @@ -273,7 +273,7 @@ def test_mirror_providers_all_in_cache(mock_mirror_setup):

mirror_providers(
providers=mock_mirror_settings["providers"],
terraform_bin=mock_mirror_settings["terraform_bin"],
terraform_bin=mock_mirror_settings['terraform_bin'],
working_dir=mock_mirror_settings["working_dir"],
cache_dir=mock_mirror_settings["cache_dir"],
)
Expand Down
3 changes: 3 additions & 0 deletions tfworker/commands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import click
import pathlib

from tfworker.authenticators import AuthenticatorsCollection
from tfworker.backends import BackendError, select_backend
Expand Down Expand Up @@ -50,6 +51,8 @@ def __init__(self, rootc, deployment="undefined", limit=tuple(), **kwargs):
rootc.load_config()

self._provider_cache = self._resolve_arg("provider_cache")
if self._provider_cache is not None:
self._provider_cache = pathlib.Path(self._provider_cache).resolve()

(self._tf_version_major, self._tf_version_minor) = self._resolve_arg(
"tf_version"
Expand Down
7 changes: 6 additions & 1 deletion tfworker/commands/terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,12 @@ def _run(

color_str = "-no-color" if self._use_colors is False else ""
params = {
"init": f"-input=false {color_str} -plugin-dir={plugin_dir} -lockfile=readonly",
"init": f"-input=false {color_str} -plugin-dir={plugin_dir}",
# -lockfile=readonly is ideal, but many of our modules are not
# only partially defining the required providers; they need to specify all
# required providers, or none, and let the worker generate the requirements
# based on the deployment_config.yaml.j2
# "init": f"-input=false {color_str} -plugin-dir={plugin_dir} -lockfile=readonly",
"plan": f"-input=false -detailed-exitcode {color_str}",
"apply": f"-input=false {color_str} -auto-approve",
"destroy": f"-input=false {color_str} -auto-approve",
Expand Down
19 changes: 10 additions & 9 deletions tfworker/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def fs_path(self):
@property
def provider_names(self):
try:
return list(find_required_providers(self.path).keys())
return list(find_required_providers(self.fs_path).keys())
except AttributeError:
return None

Expand Down Expand Up @@ -212,14 +212,15 @@ def _prep_terraform_lockfile(self):
if self._provider_cache is None:
return

with open(f"{self._target}/{TF_PROVIDER_DEFAULT_LOCKFILE}", "w") as lockfile:
lockfile.write(
generate_terraform_lockfile(
providers=self._providers,
included_providers=self.provider_names,
cache_dir=self._provider_cache,
)
)
result = generate_terraform_lockfile(
providers=self._providers,
included_providers=self.provider_names,
cache_dir=self._provider_cache,
)

if result is not None:
with open(f"{self._target}/{TF_PROVIDER_DEFAULT_LOCKFILE}", "w") as lockfile:
lockfile.write(result)

@staticmethod
def quote_str(some_string):
Expand Down
3 changes: 3 additions & 0 deletions tfworker/providers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ def __init__(self, config: ProviderConfig) -> None:
self.source = config.requirements.source or f"hashicorp/{self.tag}"
self._field_filter = []

def __str__(self):
return self.tag

@property
def gid(self) -> ProviderGID:
from tfworker.util.terraform import get_provider_gid_from_source
Expand Down
4 changes: 4 additions & 0 deletions tfworker/providers/providers_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import collections
import copy
import click
from typing import List

from tfworker.providers.generic import GenericProvider
Expand Down Expand Up @@ -49,6 +50,9 @@ def __getitem__(self, value):
def __iter__(self):
return iter(self._providers.values())

def __str__(self):
return str([f"{x.tag}: {str(x.gid)}" for x in self._providers.values()])

def keys(self):
return self._providers.keys()

Expand Down
2 changes: 0 additions & 2 deletions tfworker/util/terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,6 @@ def get_provider_gid_from_source(source: str) -> ProviderGID:

return ProviderGID(hostname=hostname, namespace=namespace, type=ptype)


@lru_cache
def find_required_providers(
search_dir: str,
) -> Union[None, Dict[str, [Dict[str, str]]]]:
Expand Down
15 changes: 11 additions & 4 deletions tfworker/util/terraform_helpers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import json
import os
import pathlib
from functools import lru_cache
from tempfile import TemporaryDirectory
from typing import Dict, List, Union

import click
import hcl2

from lark.exceptions import UnexpectedToken

from tfworker.providers.providers_collection import ProvidersCollection
from tfworker.types import ProviderGID
from tfworker.util.system import get_platform
Expand Down Expand Up @@ -79,10 +82,9 @@ def _write_mirror_configuration(
Raises:
IndexError: If there are no providers to mirror.
"""
includes = [x for x in providers if _not_in_cache(x.gid, x.version, cache_dir)]
includes = [x.tag for x in providers if _not_in_cache(x.gid, x.version, cache_dir)]
if len(includes) == 0:
raise IndexError("No providers to mirror")
click.secho(f"Mirroring providers: {includes}", fg="yellow")

mirror_configuration = _create_mirror_configuration(
providers=providers, includes=includes
Expand Down Expand Up @@ -166,13 +168,18 @@ def _parse_required_providers(content: dict) -> Union[None, Dict[str, Dict[str,
return providers


@lru_cache
def _find_required_providers(search_dir: str) -> Dict[str, [Dict[str, str]]]:
providers = {}
for root, _, files in os.walk(search_dir):
for root, _, files in os.walk(search_dir, followlinks=True):
for file in files:
if file.endswith(".tf"):
with open(f"{root}/{file}", "r") as f:
content = hcl2.load(f)
try:
content = hcl2.load(f)
except UnexpectedToken as e:
click.secho(f"skipping {root}/{file}: {e}", fg="blue")
continue
new_providers = _parse_required_providers(content)
if new_providers is not None:
providers.update(new_providers)
Expand Down

0 comments on commit 8268c03

Please sign in to comment.