This repository was archived by the owner on Sep 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
backend: drop dvc-specific working-dir behavior #17
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| import asyncio | ||
| import os | ||
| import sys | ||
| from contextlib import contextmanager | ||
| from itertools import repeat | ||
| from typing import TYPE_CHECKING, Iterator, Optional, Union | ||
|
|
||
|
|
@@ -18,22 +17,14 @@ class TPIException(Exception): | |
|
|
||
|
|
||
| class TerraformBackend: | ||
| def __init__(self, tmp_dir: StrPath, **kwargs): | ||
| self.tmp_dir = tmp_dir | ||
| os.makedirs(self.tmp_dir, exist_ok=True) | ||
| """Class for managing a named TPI iterative-machine resource.""" | ||
|
|
||
| @contextmanager | ||
| def make_tf(self, name: str): | ||
| from tpi import TerraformProviderIterative, TPIError | ||
| def __init__(self, working_dir: StrPath, **kwargs): | ||
| from tpi import TerraformProviderIterative | ||
|
|
||
| try: | ||
| working_dir = os.path.join(self.tmp_dir, name) | ||
| os.makedirs(working_dir, exist_ok=True) | ||
| yield TerraformProviderIterative(working_dir=working_dir) | ||
| except TPIError: | ||
| raise | ||
| except Exception as exc: | ||
| raise TPIError("terraform failed") from exc | ||
| self.working_dir = working_dir | ||
| os.makedirs(self.working_dir, exist_ok=True) | ||
| self.tf = TerraformProviderIterative(working_dir=self.working_dir) | ||
|
|
||
| def create(self, name: Optional[str] = None, **config): | ||
| """Create and start an instance of the specified machine.""" | ||
|
|
@@ -42,29 +33,26 @@ def create(self, name: Optional[str] = None, **config): | |
| from tpi import render_json | ||
|
|
||
| assert name and "cloud" in config | ||
| with self.make_tf(name) as tf: | ||
| tf_file = os.path.join(tf.working_dir, "main.tf.json") | ||
| with open(tf_file, "w", encoding="utf-8") as fobj: | ||
| fobj.write(render_json(name=name, **config, indent=2)) | ||
| tf.cmd("init") | ||
| tf.cmd("apply", auto_approve=IsFlagged) | ||
| tf_file = os.path.join(self.working_dir, "main.tf.json") | ||
| with open(tf_file, "w", encoding="utf-8") as fobj: | ||
| fobj.write(render_json(name=name, **config, indent=2)) | ||
| self.tf.cmd("init") | ||
| self.tf.cmd("apply", auto_approve=IsFlagged) | ||
|
Comment on lines
+36
to
+40
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The one thing to note now is that since the init'ed A proper TPI release should parse the entire existing |
||
|
|
||
| def destroy(self, name: Optional[str] = None, **config): | ||
| """Stop and destroy all instances of the specified machine.""" | ||
| from python_terraform import IsFlagged | ||
|
|
||
| assert name | ||
|
|
||
| with self.make_tf(name) as tf: | ||
| if first(tf.iter_instances(name)): | ||
| tf.cmd("destroy", auto_approve=IsFlagged) | ||
| if first(self.tf.iter_instances(name)): | ||
| self.tf.cmd("destroy", auto_approve=IsFlagged) | ||
|
|
||
| def instances(self, name: Optional[str] = None, **config) -> Iterator[dict]: | ||
| """Iterate over status of all instances of the specified machine.""" | ||
| assert name | ||
|
|
||
| with self.make_tf(name) as tf: | ||
| yield from tf.iter_instances(name) | ||
| yield from self.tf.iter_instances(name) | ||
|
|
||
| def close(self): | ||
| pass | ||
|
|
@@ -73,7 +61,7 @@ def run_shell(self, name: Optional[str] = None, **config): | |
| """Spawn an interactive SSH shell for the specified machine.""" | ||
| from tpi import TerraformProviderIterative | ||
|
|
||
| resource = self._default_resource(name) | ||
| resource = self.default_resource(name) | ||
| with TerraformProviderIterative.pemfile(resource) as pem: | ||
| self._shell( | ||
| host=resource["instance_ip"], | ||
|
|
@@ -82,7 +70,7 @@ def run_shell(self, name: Optional[str] = None, **config): | |
| known_hosts=None, | ||
| ) | ||
|
|
||
| def _default_resource(self, name): | ||
| def default_resource(self, name): | ||
| from tpi import TPIError | ||
|
|
||
| resource = first(self.instances(name)) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feels like we should maybe rename this? The base/terraform backend naming is another leftover from dvc-machine and doesn't really have any meaning in TPI.