Reviewing Core Plans example - scaffoldings

Example - testing a change to core/scaffolding-node

If I want to test a change to the node scaffolding, I would do this:

(build a new version of the core/scaffolding-node package)
$ git clone https://github.com/habitat-sh/sample-node-app
$ cd sample-node-app
$ cp path/to/new/core/scaffolding-node.hart .

Open up the habitat/plan.sh file. Change this line

pkg_scaffolding="core/scaffolding-node"

to

pkg_scaffolding="core/scaffolding-node/new-version/new-release"

Write and close the file.

Now run:

$ hab studio enter
(studio) $ hab install <new-core-scaffolding-node.hart>
(studio) $ build
(studio) $ hab pkg export docker ./results/<newly-build-hart.hart>
(studio) $ exit

Now that you are out of the studio, start a docker container with the new build of the sample-node-app (which using the new build of scaffolding-node).

$ docker run -it -p 8000:8000 your_origin/sample-node-app

Example: testing a change to core/scaffolding-ruby

(build a new version of the core/scaffolding-ruby package)

Create an ultra-simple Rails application

$ rails new widget_world --database=postgresql
$ cd widget_world
$ cp path/to/new/core/scaffolding-ruby.hart .
$ vim Gemfile

Navigate to this line in the Gemfile

gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

And change it to this line:

gem 'tzinfo-data'

Save and close the file, then regenerate the Gemfile.lock file (otherwise bundle install will fail with an error), and create a scaffold for a Widget table.

$ rm Gemfile.lock
$ bundle install
$ rails generate scaffold Widget name:string

Now let’s Habitize this application!

$ hab plan init -s ruby
$ vim habitat/plan.sh

Add this line to habitat/plan.sh

pkg_binds_optional=( [database]="port" )

There’s one more thing we need to do to use the new build of scaffolding-ruby.

Change this line:

pkg_scaffolding="core/scaffolding-ruby"

To

pkg_scaffolding="core/scaffolding-ruby/new-version/new-release"

Save and close habitat/plan.sh

Now, we need to generate a secret key for the rails application. Luckily for us, Rails makes this super easy. Run:

$ rails secret

Now, we need to provide that value to our package. Open up the habitat/default.toml file:

$ vim habitat/default.toml

And add in this content:
~/widget_world/habitat/default.toml

secret_key_base = "secret_key_you_just_generated"
 
rails_env = 'production'
 
[db]
user = "admin"
password = "admin"

Next, let’s enter the Habitat studio, install the new scaffolding-ruby package, and then build this application’s package:

$ hab studio enter
(studio) $ build
(studio) $ hab pkg export docker ./results/<your-new hart-package.hart>
(studio) $ hab pkg export docker core/postgresql
(studio) $ exit

Because we need to use two containers that need to talke to each other, we’ll use docker-compose. Create a docker-compose.yml file and include this content:

version: '3'
services:
  db:
    environment:
      - HAB_LICENSE=accept-no-persist
    image: core/postgresql
    volumes:
      - "./habitat/default.toml:/default.toml"
  railsapp:
    environment:
      - HAB_LICENSE=accept-no-persist
    image: your_origin/widget_world
    ports:
      - 8000:8000
    links:
    - db
    command: --peer db --bind database:postgresql.default

Save and close that file, now let’s bring up those containers!

$ docker-compose up

Currently, the ruby scaffolding does not automatically set up the database for you, this still needs to be done manually.

docker-compose exec railsapp hab pkg exec your-origin/widget_world widget_world-rake db:setup

to create the database

docker-compose exec railsapp hab pkg exec your-origin/widget_world widget_world-rake db:migrate

to setup the database schema

And now we have a running Rails app and database running in Docker containers! Head on over to http://localhost:8000/widgets to check it out! If this is working properly, chances are the new core-ruby package works as well!

Hi there

I noticed today working through this example that the build fails with a rake aborted! due to missing ‘secret_key_base’ for ‘production’ environment (rails/5.2.3 ruby/2.6.3)

widget_world: Detected and running Rake ‘assets:precompile’
rake aborted!
ArgumentError: Missing secret_key_base for ‘production’ environment, set this string with rails credentials:edit

Should these be read from the default.toml and set as environment var’s or should the instructions be updated to create a secrets yml file? not sure what’s best?

Thanks
Gary