boltops-tools / terraspace

Terraspace: The Terraform Framework
https://terraspace.cloud
Apache License 2.0
678 stars 46 forks source link

Running Terraspace in Alpine Ruby Docker Container #52

Closed josephlim75 closed 3 years ago

josephlim75 commented 3 years ago

Hi,

I'm trying to test out terraspace inside an alpine 3.12 docker container with ruby runtime installed. I'm able to install terraspace and validate the installation and everything seems to be fine. When I try to execute terraspace build demo or terraspace up demo I got the following error. I'm not familiar with ruby, I'm not sure if this is even possible to run in alpine docker. I've also tried in a Debian container and I get the same error. Containers were downloaded from https://hub.docker.com/_/ruby

Is good if you can include some documentation on how to install and execute inside a docker container, I believe this will help a lot of people who want to try out this awesome tool without having the need to install Ruby runtime components into their current workspace.

Error

root@ebf18855f463:/infra# terraspace up demo
cannot load such file -- /usr/local/bundle/gems/terraspace_plugin_google-0.2.2/lib/terraspace_plugin_google/interfaces
WARNING: Unable to require "bundler/setup"
There may be something funny with your ruby and bundler setup.
You can try upgrading bundler and rubygems:

    gem update --system
    gem install bundler

Here are some links that may be helpful:

* https://bundler.io/blog/2019/01/03/announcing-bundler-2.html

Also, running bundle exec in front of your command may remove this message.

Traceback (most recent call last):
    11: from /usr/local/bundle/bin/terraspace:23:in `<main>'
    10: from /usr/local/bundle/bin/terraspace:23:in `load'
     9: from /usr/local/bundle/gems/terraspace-0.4.4/exe/terraspace:12:in `<top (required)>'
     8: from /usr/local/bundle/gems/terraspace-0.4.4/exe/terraspace:12:in `require'
     7: from /usr/local/bundle/gems/terraspace-0.4.4/lib/terraspace/cli.rb:1:in `<top (required)>'
     6: from /usr/local/bundle/gems/terraspace-0.4.4/lib/terraspace/cli.rb:2:in `<module:Terraspace>'
     5: from /usr/local/bundle/gems/terraspace-0.4.4/lib/terraspace/cli.rb:41:in `<class:CLI>'
     4: from /usr/local/bundle/gems/terraspace-0.4.4/lib/terraspace/cli.rb:41:in `require'
     3: from /usr/local/bundle/gems/terraspace-0.4.4/lib/terraspace/cli/cloud.rb:1:in `<top (required)>'
     2: from /usr/local/bundle/gems/terraspace-0.4.4/lib/terraspace/cli/cloud.rb:2:in `<class:CLI>'
     1: from /usr/local/bundle/gems/terraspace-0.4.4/lib/terraspace/cli/cloud.rb:3:in `<class:Cloud>'
/usr/local/bundle/gems/terraspace-0.4.4/lib/terraspace/cli/cloud.rb:3:in `require': cannot load such file -- /usr/local/bundle/gems/terraspace-0.4.4/lib/terraspace/terraform (LoadError)

Environment

root@ebf18855f463:/usr/local/bundle/gems/terraspace-0.4.4/lib/terraspace# bundle -v
Bundler version 2.1.4
root@ebf18855f463:/usr/local/bundle/gems/terraspace-0.4.4/lib/terraspace# ruby -v
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-linux]
root@ebf18855f463:/usr/local/bundle/gems/terraspace-0.4.4/lib/terraspace# gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 3.1.4
  - RUBY VERSION: 2.7.2 (2020-10-01 patchlevel 137) [x86_64-linux]
  - INSTALLATION DIRECTORY: /usr/local/bundle
  - USER INSTALLATION DIRECTORY: /root/.gem/ruby/2.7.0
  - RUBY EXECUTABLE: /usr/local/bin/ruby
  - GIT EXECUTABLE: /usr/bin/git
  - EXECUTABLE DIRECTORY: /usr/local/bundle/bin
  - SPEC CACHE DIRECTORY: /root/.gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /usr/local/etc
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-linux
  - GEM PATHS:
     - /usr/local/bundle
     - /root/.gem/ruby/2.7.0
     - /usr/local/lib/ruby/gems/2.7.0
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
     - "install" => "--no-document"
     - "update" => "--no-document"
  - REMOTE SOURCES:
     - https://rubygems.org/
  - SHELL PATH:
     - /usr/local/bundle/bin
     - /usr/local/sbin
     - /usr/local/bin
     - /usr/sbin
     - /usr/bin
     - /sbin
     - /bin
gnomeria commented 3 years ago

Was having the same issue for the aws plugin too cannot load such file -- ~/home/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/terraspace_plugin_aws-0.2.2/lib/terraspace_plugin_aws/interfaces

eugeneromancenko commented 3 years ago

I was getting a similar error when running on macOS (10.15.7). For me, works only when running 'bundle exec' in front of the command. for example => bundle exec terraspace all up

So, I have this sh to simplify my run, where I also added a few more variables like env and region

#!/bin/bash

if [ $# -le "3" ]
  then
    tput setaf 1; echo "No all arguments supplied, Please supply command (e.g. plan), scope (e.g. demo), enviroment (e.g. dev), region (e.g. eu-west-1)"
    tput setaf 1; echo "for example => ./run.sh plan demo dev eu-west-1"

  else
    command=$1
    scope=$2
    env=$3
    region=$4

    export TS_ENV=$3
    export AWS_REGION=$4

    tput setaf 2; echo "Set Enviroment => $env; Running Terraspace $command for $scope stack(s)"
fi
if [ "$scope" = "all" ];
    then
     bundle exec terraspace all $command
    else
     bundle exec terraspace $command $scope
fi
tongueroo commented 3 years ago

@eugeneromancenko Thanks for the script. Wondering which version terraspace you're using. In v0.5.x terraspace loads bundler/setup internally earlier and believe that should get rid of the need to call bundle exec. Thanks!

l1ahim commented 3 years ago

Managed to get it work inside an alpine container: Building 86.3s

FROM ruby:2.7.2-alpine3.12

RUN apk add ruby-unf_ext cmake build-base git terraform aws-cli

RUN gem install terraspace

env

/ # env
RUBY_MAJOR=2.7
HOSTNAME=34aca7e1e102
SHLVL=1
HOME=/root
BUNDLE_APP_CONFIG=/usr/local/bundle
RUBY_VERSION=2.7.2
TERM=xterm
PATH=/usr/local/bundle/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
LANG=C.UTF-8
GEM_HOME=/usr/local/bundle
RUBY_DOWNLOAD_SHA256=1b95ab193cc8f5b5e59d2686cb3d5dcf1ddf2a86cb6950e0b4bdaae5040ec0d6
PWD=/
BUNDLE_SILENCE_ROOT_WARNING=1

After configuring the aws cli I was able to create the demo stack. Regarding the aws credentials those could be mounted or used from the env file.. or other options

terraspace up demo
Building .terraspace-cache/us-east-1/dev/stacks/demo
Creating bucket: terraform-state-<aws_account_id>-us-east-1-dev
Creating dynamodb table: terraform_locks
Waiting for dynamodb table to finish creating...
Built in .terraspace-cache/us-east-1/dev/stacks/demo
Current directory: .terraspace-cache/us-east-1/dev/stacks/demo
=> terraform init -get -input=false >> /tmp/terraspace/log/init/demo.log
=> terraform apply -input=false
Acquiring state lock. This may take a few moments...

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # random_pet.this will be created
  + resource "random_pet" "this" {
      + id        = (known after apply)
      + length    = 2
      + separator = "-"
    }......
tongueroo commented 3 years ago

@l1ahim Thanks for the help with the alpine dockerfile 👍

Docker images with terraspace are now on Dockerhub: https://hub.docker.com/r/boltops/terraspace 🎉 They are built and published on a daily basis. Here are docs: Terraspace with Docker

docker run -ti boltops/terraspace:alpine
docker run -ti boltops/terraspace:amzn2
docker run -ti boltops/terraspace:ubuntu

In addition to the Docker images, there is now a standalone package installer. Also built daily.