Skip to content

Commit

Permalink
feat: Many fixes and Refactors. Still in Initial Development (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
eieste authored Dec 12, 2024
2 parents fbc7e46 + b4f4e1c commit 6ddfaad
Show file tree
Hide file tree
Showing 24 changed files with 525 additions and 102 deletions.
1 change: 0 additions & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
FROM mcr.microsoft.com/devcontainers/python:1-3.12

COPY ./requirements.txt /

COPY ./.devcontainer/entrypoint.sh /entrypoint.sh

Expand Down
2 changes: 1 addition & 1 deletion .devcontainer/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/sh


pip install -e '.[dev]'
pip install -e .
pip3 install -r requirements-dev.txt


exec $@
17 changes: 17 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@


version: 2
updates:
- package-ecosystem: "docker" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "weekly"
- package-ecosystem: "pip" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "weekly"

# - package-ecosystem: "github-actions" # See documentation for possible values
# directory: "/" # Location of package manifests
# schedule:
# interval: "weekly"
6 changes: 5 additions & 1 deletion .github/release.bash
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ echo "Replace Version inside tfutility"
echo "__version__= \"$2\"" > src/tfutility/__init__.py

echo "Replace Docker image Reference inside README.md"
sed -i "s/tfutility:$1/tfutility:$1/" README.md
sed -i "s/tfutility:$1/tfutility:$2/" README.md


echo "Replace pre-commit Reference inside README.md"
sed -i "s/ref: $1/ref: $2/" README.md

echo "Replace Docker image Reference inside pre-commit-hook"
sed -i "s/tfutility:$1/tfutility:$1/" .pre-commit-hooks.yaml
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
run: mv .releaserc.prerelease.yaml .releaserc.yaml

- name: Semantic Release
uses: cycjimmy/semantic-release-action@v2
uses: cycjimmy/semantic-release-action@v4
id: semantic # Need an `id` for output variables
with:
semantic_version: 17
Expand Down
11 changes: 5 additions & 6 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,23 @@
- id: check-forcedremotesource-docker
name: forcedremotesource
description: Check if all modules hase remote sources
args: ["importdate"]
args: ["forcedremotesource"]
language: docker_image
entry: ghcr.io/eieste/tfutility:1.0.9 forcedremotesource
entry: ghcr.io/eieste/tfutility:1.1.1
files: (\.tf)$


- id: check-importdate-docker
name: importdate
description: Check if import block has decorated dates
args: ["importdate"]
language: docker_image
entry: ghcr.io/eieste/tfutility:1.0.9 importdate
entry: ghcr.io/eieste/tfutility:1.1.1
files: (\.tf)$

- id: check-movedate-docker
name: movedate
description: Check if moved block has decorated dates
args: ["importdate"]
args: ["movedate"]
language: docker_image
entry: ghcr.io/eieste/tfutility:1.0.9 movedate
entry: ghcr.io/eieste/tfutility:1.1.1
files: (\.tf)$
2 changes: 1 addition & 1 deletion .releaserc.prerelease.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ prepare:
assets:
- src/tfutility/__init__.py
- README.md
- CHANELOG.md
- CHANGELOG.md

publish:
- path: "@semantic-release/github"
Expand Down
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,35 @@ for detailed Examples visit the [Documentation](https://eieste.github.io/tfutili

### Use with Docker
```
docker run -it --rm -v $(pwd):/workspace ghcr.io/eieste/tfutility:1.0.9 forcedremotesource /workspace
docker run -it --rm -v $(pwd):/workspace ghcr.io/eieste/tfutility:1.1.1 forcedremotesource /workspace
```

### Use with pre-commit


Create a .pre-commit-config.yaml with the following content.
```yaml

repos:
- repo: https://github.com/eieste/tfutility/
rev: 1.1.1
hooks:
- id: check-forcedremotesource
- id: check-importdate
- id: check-moveddate

```

Its possible to attach the suffix `-docker` to each hook to use precommit docker hooks


### Autocompletion

Run the following command to setup a bash autocompletion for this command

```bash
register-python-argcomplete tfutilty > /etc/bash_completion.d/tfutilty
```

## Quick-Reference

Expand Down
1 change: 0 additions & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
exclude_patterns = []



# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

Expand Down
13 changes: 3 additions & 10 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
.. TF-Utils documentation master file, created by
sphinx-quickstart on Sun Nov 3 09:06:25 2024.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.

TF-Utils documentation
======================

Add your content using ``reStructuredText`` syntax. See the
`reStructuredText <https://www.sphinx-doc.org/en/master/usage/restructuredtext/index.html>`_
documentation for details.
tfutility documentation
=======================

tfutility allows performing commands on terraform/tofu files. This commands can be different things. Currently, there are possibilities to check if a module block has remote sources. Or import or moved blocks have creation dates on them.

.. toctree::
:maxdepth: 2
Expand Down
20 changes: 17 additions & 3 deletions docs/source/users/forcedremotesource.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ It is usefull in pre-commit hooks and development scenatios.



*Szenario:*
**Szenario:**

You are developing a large terraform/tofu project with different modules thre are pushed to an terraform module registry.
But for development purposes you linked the module localy together.
Expand All @@ -24,7 +24,7 @@ Write the following Decorator above modules to enforce this module must have a l
This decorator works only above `module` blocks. It has no additional parameters

Example usage
`# @forcedremotesource`
``# @forcedremotesource``


The following Code tells tfutility this module should have an remote source.
Expand All @@ -39,6 +39,17 @@ But as you can see the module has an local path
source = "../local/path"
}
shell:

.. code-block:: bash
:linenos:
tfutility forcedremotesource test.tf
ERROR: Module Block had no Version Defined in main.tf:4
ERROR: Module Block has no Remote Source in main.tf:4
so the following command loggs error and exit the whole application with an exit code 1

.. code-block:: hcl
Expand All @@ -52,11 +63,14 @@ so the following command loggs error and exit the whole application with an exit
This raises an version missing error


.. code-block:: sh
:linenos:
$ tfutility forcedremotesource test.tf
2024-11-18 23:05:25 ERROR: Module Block has no Remote Source in test.tf:4
Its possible to prevent error messages or exit 1 status codes with the --silent and --allow-failure arguments


Command Line Arguments
Expand Down
22 changes: 13 additions & 9 deletions docs/source/users/importdate.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,28 @@ This decorator must contain an date when the block was created.
It allows the detection of old import blocks which can be remoted at a certain point in time


**Szenario**

If you want to import already existing cloud resources into your terraform you can use import blocks in your code.
If this blocks are used it is a good idea to remove them after a specific time or after it could be ensured that these blocks are taken by terraform.
By using the first option ( after a specific time ) this Command can help you.

By executing ``tfutility importdate .`` it can be ensured every import block has an decorator which contains a create date.
If these create date are expired ( defined by an additional expire date or an duration command line argument ) these checks will be fail until this import block is removed


Terraform
=========


.. code-block:: hcl
:linenos:
#$ cat test.tf
import {
to = ""
id = ""
to = "resource.aws_s3_bucket.example"
id = "arandoms3bucket"
}
.. code-block:: hcl
:linenos:
Expand All @@ -30,8 +38,6 @@ Terraform
id = ""
}
.. code-block:: hcl
:linenos:
Expand All @@ -41,8 +47,6 @@ Terraform
id = ""
}
you can also overwrite the expire date with your own duration like:
```bash
tfutility moveddate --expire-after 60 /workspace
Expand Down
5 changes: 1 addition & 4 deletions docs/source/users/swapsource.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Swap Source




Terraform
=========

Expand All @@ -26,10 +27,6 @@ Terraform
version = ""
}
.. argparse::
:module: tfutility.main
:func: _get_parser_only
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ readme = { file = 'README.md', content-type = 'text/markdown' }
license = { file = 'LICENSE' }
authors = [{ name = "Stefan Eiermann", email = "stefan.eiermann@eneka.de" }]
dependencies = [
"python-hcl2==4.3.5"
"python-hcl2==4.3.5",
"argcomplete==3.5.1",
]
classifiers = [
"Development Status :: 4 - Beta",
Expand Down
22 changes: 13 additions & 9 deletions src/tfutility/controllers/blockdate.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def new_block(self, options, block):
self._error = True
block_name = self.get_block_name()
self.get_logger().error(
"Missing moveddate Decorator at block '{}' in file {} Line {}".format(
block_name, file_path, block.start
"Missing {} Decorator above block '{}' in file {}:{}".format(
self.get_command_name(), block_name, file_path, block.start
)
)
else:
Expand All @@ -54,32 +54,33 @@ def new_block(self, options, block):

if not options.expire_after:
if dec.parameter("expire"):
print("HIII")

dec_date_expire = datetime.strptime(
dec.parameter("expire"), "%d-%m-%Y"
)
if now > dec_date_expire:
print("FOOOHIII")

self._error = True
self.get_logger().error(
"Moved Block expired in file: {} Line {}".format(
file_path, block.start
"{} Block expired in file: {}:{}".format(
self.get_command_name(), file_path, block.start
)
)
else:
if now > dec_date_create + timedelta(days=options.expire_after):
self._error = True
self.get_logger().error(
"Moved Block expired in file: {} Line {}".format(
file_path, block.start
"{} Block expired in file: {}:{}".format(
self.get_command_name(), file_path, block.start
)
)

def handle(self, options):
self._error = False
results = super(BlockDateHandler, self).handle(options)

if not options.allow_failure and self._error:
sys.exit(1)

tf_files = self.get_file_list(options.paths)

for file in tf_files:
Expand All @@ -89,6 +90,9 @@ def handle(self, options):
if block.id.startswith(self.get_block_name()):
self.new_block(options, block)

if not options.allow_failure and self._error:
sys.exit(1)

return results


Expand Down
1 change: 1 addition & 0 deletions src/tfutility/controllers/forcedremotesource.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def new_decorator(self, options, block):

def handle(self, options):
self._error = False

if options.silent:
self.get_logger().setLevel(1000)

Expand Down
Loading

0 comments on commit 6ddfaad

Please sign in to comment.