diff --git a/Dockerfile b/Dockerfile index 02793ba..2c2a5cb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,9 +17,6 @@ FROM ubuntu:18.04 -#define default terraform version in environment var -ENV TF_VERSION "0.12.0" - #expose blast-radius port EXPOSE 5000 @@ -31,21 +28,25 @@ RUN apt-get update \ graphviz \ python3 \ python3-pip \ + python3-ply \ jq \ unzip \ && rm -rf /var/lib/apt/lists/* -#install terraform WORKDIR /src COPY . . -RUN chmod +x ./docker-build.sh \ - && ./docker-build.sh /src #create blast-radius package from source -RUN pip3 install -e . - #set up entrypoint script -RUN chmod +x ./docker-entrypoint.sh +RUN pip3 install -e . \ + && chmod +x ./docker-entrypoint.sh + +#define default terraform version in environment var +ENV TF_VERSION "0.11.14" + +#install terraform +RUN chmod +x ./docker-build.sh \ + && ./docker-build.sh /src #set up runtime workdir for tf files WORKDIR /workdir diff --git a/README.md b/README.md index 00f891e..04eff88 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,12 @@ Point *Blast Radius* at an `init-ed` *Terraform* project, and connect with your *Additional note*: If you organised your terraform directories with stacks and modules, please call *Blast Radius* from the root directory and give the stack's directory as argument (plus the `--serve` argument). +*Additional note*: +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 + +*Additional note*: +You can specify 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 + ```bash [...]$ tree -d /-- project diff --git a/blastradius/server/server.py b/blastradius/server/server.py index 662d584..0ca0d41 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()) @@ -70,9 +71,9 @@ def graph_json(): return dot.json() def run_tf_graph(): - completed = subprocess.run(['terraform', 'graph'], stdout=subprocess.PIPE) + completed = subprocess.run(['terraform', 'graph'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) if completed.returncode != 0: - raise + raise Exception('Execution error', completed.stderr) return completed.stdout.decode('utf-8') def get_help(): diff --git a/blastradius/server/templates/error.html b/blastradius/server/templates/error.html index ea97c1f..a5e1122 100644 --- a/blastradius/server/templates/error.html +++ b/blastradius/server/templates/error.html @@ -13,10 +13,11 @@

Something has gone wrong. Please check the following:

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

    {{ error }}

    - \ No newline at end of file + diff --git a/docker-build.sh b/docker-build.sh index 8b53388..383c388 100644 --- a/docker-build.sh +++ b/docker-build.sh @@ -14,6 +14,3 @@ curl -o terraform.zip $terraform_url unzip terraform.zip mv terraform /usr/local/bin rm terraform.zip - - - diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 01a164e..b5bfec8 100644 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -25,14 +25,16 @@ cd /workdir-rw cd $2 } -# is terraform already initialized? -[ -d '.terraform' ] && terraform get --update=true +if [ -n "${DO_TF_INIT}" ]; then + # is terraform already initialized? + [ [ -d '.terraform' ] || [ -d ${TF_DATA_DIR:-""} ] && terraform get --update=true -# re-initialize anyway. -terraform init -input=false + # re-initialize anyway. + terraform init -input=false +fi # it's possible that we're in a sub-directory. leave. cd /workdir-rw # okay, we should be good to go. -blast-radius $1 $2 $3 \ No newline at end of file +blast-radius $1 $2 $3