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

Hcl2 and Azure support (plus some additional additions/fixes) #92

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 8 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
ARG TF_VERSION=0.12.12
ARG PYTHON_VERSION=3.7
ARG TF_VERSION=0.12.30
ARG PYTHON_VERSION=3.9

FROM hashicorp/terraform:$TF_VERSION AS terraform

FROM python:$PYTHON_VERSION-alpine
RUN pip install -U pip ply \
&& apk add --update --no-cache graphviz ttf-freefont
&& apk add --update --no-cache graphviz ttf-freefont \
gcc make openssl-dev musl-dev libffi-dev \
&& apk upgrade \
&& pip install azure-cli \
&& apk del gcc make openssl-dev musl-dev libffi-dev \
&& rm -rf /var/cache/apk/*

COPY --from=terraform /bin/terraform /bin/terraform
COPY ./docker-entrypoint.sh /bin/docker-entrypoint.sh
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,26 @@ $ docker run --rm -it -p 5000:5000 \
28mm/blast-radius --serve stacks/beef
```

## Additional notes for Docker usage

You can specify `DO_TF_INIT=true` in the docker command line as an environment variable (`-e DO_TF_INIT=true`) if you want `terraform init` to be applied (default is not to run `terraform init`).

You can salso pecify `TF_DATA_DIR` in the docker command line as an environment variable (`-e TF_DATA_DIR=$(pwd)/.terraform`) if you want terraform to use an alternate state repository.

If you have several other variables to specify for your terraform command, like `AWS_PROFILE` or dedicated variable files, you can do so as follows:

```sh
$ docker run --rm -it -p 5000:5000 \
-v $(pwd):/data:ro \
--security-opt apparmor:unconfined \
--cap-add=SYS_ADMIN \
-e TF_DATA_DIR=/data-rw/.terraform.dev \
-e AWS_PROFILE=dev \
-v $HOME/.aws/:/root/.aws/:ro,Z \
-v $w/$s/terraform-dev.tfvars:/data-rw/terraform.tfvars:Z \
28mm/blast-radius
```

## Embedded Figures

You may wish to embed figures produced with *Blast Radius* in other documents.
Expand Down
4 changes: 2 additions & 2 deletions blastradius/handlers/terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import re

# 3rd party libraries
import hcl # hashicorp configuration language (.tf)
import hcl2 # hashicorp configuration language (.tf)

class Terraform:
"""Finds terraform/hcl files (*.tf) in CWD or a supplied directory, parses
Expand All @@ -23,7 +23,7 @@ def __init__(self, directory=None, settings=None):
with open(fname, 'r', encoding='utf-8') as f:
self.config_str += f.read() + ' '
config_io = io.StringIO(self.config_str)
self.config = hcl.load(config_io)
self.config = hcl2.load(config_io)

# then any submodules it may contain, skipping any remote modules for
# the time being.
Expand Down
9 changes: 5 additions & 4 deletions blastradius/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@

@app.route('/')
def index():
tf_data_dir = os.getenv('TF_DATA_DIR')
# we need terraform, graphviz, and an init-ed terraform project.
if not which('terraform') and not which('terraform.exe'):
return render_template('error.html')
return render_template('error.html', error='No terraform executable found')
elif not which('dot') and not which('dot.exe'):
return render_template('error.html')
elif not os.path.exists('.terraform'):
return render_template('error.html')
return render_template('error.html', error='No dot executable found')
elif not (tf_data_dir is not None and os.path.exists(tf_data_dir)) and not os.path.exists('.terraform'):
return render_template('error.html', error='No .terraform or TF_DATA_DIR={} directory found'.format(tf_data_dir))
else:
return render_template('index.html', help=get_help())

Expand Down
1 change: 1 addition & 0 deletions blastradius/server/templates/error.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ <h3>Something has gone wrong. Please check the following:</h3>
<li>Is Terraform installed?</li>
<li>Is this an init-ed Terraform project?</li>
</ul>
<h4>{{ error }}</h4>
</body>

<script>
Expand Down
10 changes: 6 additions & 4 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ mount -t overlay overlay -o lowerdir=/data,upperdir=/tmp/overlay/upper,workdir=/
# change to the overlayFS
cd /data-rw

# Is Terraform already initialized? Ensure modules are all downloaded.
[ -d '.terraform' ] && terraform get
if [ -n "${DO_TF_INIT}" ]; then
# Is Terraform already initialized? Ensure modules are all downloaded.
[ [ -d '.terraform' ] || [ -d ${TF_DATA_DIR:-""} ] ] && terraform get

# Reinitialize for some reason
terraform init
# Reinitialize for some reason
terraform init
fi

# it's possible that we're in a sub-directory. leave.
cd /data-rw
Expand Down
12 changes: 6 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
setuptools==41.0.1
requests==2.22.0
Jinja2==2.10.1
Flask==1.0.3
beautifulsoup4==4.7.1
setuptools==51.0.0
requests==2.25.0
Jinja2==2.11.2
Flask==1.1.2
beautifulsoup4==4.9.3
ply>=3.11
pyhcl==0.3.12
python-hcl2==2.0.0