Skip to content

mitre-cyber-academy/rails-app-cookbook

Repository files navigation

rails-app Cookbook

This is a cookbook that allows for plug-and-play creation of an application, using only a repository. The recipe takes care of creating the database, installing required gems via bundler, and creating an nginx stack to serve your site.

Requirements

This recipe has been tested on Ubuntu 12.04 and Ubuntu 14.04.

Attributes

rails-app::default attributes

Key Type Description Default
["rails-app"]["name"] String Application name "default"
["rails-app"]["address"] String IP address or domain name of the server. node['fqdn']
["rails-app"]["action"] String What to do with the app, options are :deploy and :force_deploy, for more info see the chef application cookbook. :deploy
["rails-app"]["path"] String Path where the application should be stored on the server. "/opt/#{node["rails-app"]["name"]}"
["rails-app"]["deploy_key"] String An optional string if you wish to pull your code from a private repository, make sure to replace all newlines with \n if you use this. nil
["rails-app"]["repository"] String Repository to pull your code from. nil
["rails-app"]["revision"] String Revision of your code to pull. "master"
["rails-app"]["environment"] String Rails environment to deploy. "production"
["rails-app"]["db_host"] String Tells rails where the database is located. "127.0.0.1"
["rails-app"]["db_adapter"] String Tells rails the database adapter to use. "postgresql"
["rails-app"]["db_name"] String Tells rails the database to use. "rails-app-#{default["rails-app"]["name"]}"
["rails-app"]["db_username"] String Tells rails who to login as. node["rails-app"]["name"]
["rails-app"]["db_password"] String Tells rails the database password. node["rails-app"]["db_password"] || ::SecureRandom.base64(24)
["rails-app"]["bundler"] Boolean Should I use bundler? true
["rails-app"]["bundle_command"] String The path to bundler on your system. "#{node["rbenv"]["root_path"]}/shims/bundle"
["rails-app"]["bundler_deployment"] Boolean Should bundler use deployment mode? true
["rails-app"]["migrate"] Boolean Should the rails app have migrations run on it? true
["rails-app"]["migration_command"] String What is the command to migrate the database? "#{node["rails-app"]["bundle_command"]} exec rake db:migrate"
["rails-app"]["gems"] Array Additional gems to install with this recipe. []
["rails-app"]["bundler_without_groups"] Array What groups should bundler ignore? []
["rails-app"]["ssl"] Boolean Should I use SSL? false
["rails-app"]["precompile_assets"] Boolean Shoudl assets be precompiled? true
["rails-app"]["delete_before_bundle"] Array What files should be deleted from the repo after fetching? [".rbenv-version", ".ruby-version"]

Usage

rails-app::default

Just include rails-app::default in your node's run_list and override attributes as needed. Generally you will need to override the name, host and repository to download from. An example of this is in the Vagrantfile in this repository.

Packer with Docker

This recipe includes a packer.json file that will build a Docker image and commit it to your local computer. In order to use it you will need to setup your environment first. (Note that this is only tested on Ubuntu 15.04. It should work on other versions of Ubuntu but has some bugs that cause it to not work on OS X, and Windows was not considered.)

  1. Perform the steps listed in the Developing section.
  2. git - sudo apt-get install git
  3. Docker - sudo apt-get install docker.io
  4. Download the temporary patched version of packer to work with docker 1.5 located here until the related github issue is fixed.
  5. wget https://bintray.com/artifact/download/alkersan/generic/packer_0.8.0_linux_amd64.zip
  6. sudo apt-get install unzip
  7. unzip packer_0.8.0_linux_amd64.zip
  8. sudo mv packer* /usr/local/bin/
  9. Install the docker post-processor onto packer. (steps below)
  10. Install go - sudo apt-get install golang-go
  11. Install mercurial - sudo apt-get install mercurial
  12. mkdir ~/go
  13. Add the next two lines to .bashrc
  14. export GOPATH=~/go
  15. export PATH=$PATH:$GOPATH/bin
  16. git clone https://github.com/avishai-ish-shalom/packer-post-processor-docker-dockerfile.git ~/packer-post-processor-docker-dockerfile
  17. cd ~/packer-post-processor-docker-dockerfile
  18. go get github.com/nitrous-io/goop && go build github.com/nitrous-io/goop
  19. goop install - if you get an error on this step, try the build step anyway, it may work.
  20. goop go build
  21. mv packer-post-processor-docker-dockerfile /usr/local/bin
  22. berks vendor cookbooks
  23. sudo su
  24. service docker start
  25. At this point you can run packer build packer.json in the root directory of this project.

Debugging Packer with Docker

If there is a problem happening with the Packer build, it is rather simply to turn on chef debugging output, however poorly documented. In the packer.json file, the provisioner block should be changed to include an override block like below. It will then spit out chef debugging output which is much more useful in the case of an error.

"provisioners": [
  {
    "type": "shell",
    "inline": ["apt-get -y update; apt-get install -y curl libjson-perl"]
  },
  {
    "type": "chef-solo",
    "cookbook_paths": ["cookbooks"],
    "run_list": ["recipe[rails-app::default]"],
    "prevent_sudo": true,
    "json": {        
      "rails-app": {
          "name": "registration-app",
          "ssl": false,
          "repository": "https://github.com/mitre-cyber-academy/registration-app.git",
          "revision": "moveToRails4"
      }},
    "override": {
      "docker": {
        "execute_command": "chef-solo -l debug --no-color -c {{.ConfigPath}} -j {{.JsonPath}}"
      }
    }
  }
]

Developing

  1. Install the ChefDK (https://downloads.chef.io/chef-dk/)
  2. If you are using rbenv, make sure you add add export PATH="/opt/chefdk/bin:$PATH" to your ~/.bash_profile after your rbenv init line.

Contributing

  1. Fork the repository on Github
  2. Create a named feature branch (like add_component_x)
  3. Write your change
  4. Submit a Pull Request using Github

To DO

  • Make recipe less fragile to different versions of ruby. Currently if the C compiler prefix (gems/2.1.0/gems) is changed then the recipe will break, that should not happen.

License and Authors

Authors: Robert Clark [email protected]

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published