diff --git a/Dockerfile b/Dockerfile index 2d35634..f93b042 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/README.md b/README.md index d475fd7..80f7ce5 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/blastradius/handlers/terraform.py b/blastradius/handlers/terraform.py index 00b41d9..3178eee 100644 --- a/blastradius/handlers/terraform.py +++ b/blastradius/handlers/terraform.py @@ -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 @@ -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. diff --git a/blastradius/server/server.py b/blastradius/server/server.py index aae9278..9d588fc 100644 --- a/blastradius/server/server.py +++ b/blastradius/server/server.py @@ -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()) diff --git a/blastradius/server/templates/error.html b/blastradius/server/templates/error.html index ea97c1f..1326379 100644 --- a/blastradius/server/templates/error.html +++ b/blastradius/server/templates/error.html @@ -13,6 +13,7 @@

Something has gone wrong. Please check the following:

  • Is Terraform installed?
  • Is this an init-ed Terraform project?
  • +

    {{ error }}