Note
This readme file is based off the master version of orats. If you want accurate documentation then make sure to select the branch for the latest gem version of orats.
What is orats and what problem does it solve?
It stands for opinionated rails application templates. The templates include solving tedious tasks that you would do for most projects. It handles creating a rails application with a bunch of opinions and optionally an ansible inventory/playbook so you can and provision your servers and deploy your apps effortlessly.
What version of Rails and Ruby are you targeting?
Rails 4.1.x and Ruby 2.1.x
Gems will also be updated once they are proven to work on the target rails/ruby versions. The gems are locked using the pessimistic operator ~> to ensure your installation works over time as long as rubygems.org's API is working.
Contents
System dependencies
Before running orats...
You must install
You should install
- Ansible
- If you plan to use the ansible features (optional)
- Imagemagick
- If you want favicons to be automatically created (optional)
You need these processes to be running
- Postgres
- Redis
Installation
gem install orats
Or if you already have orats then run gem update orats to upgrade to the latest version.
Commands
Here is an overview of the available commands. You can find out more information about each command and flag by running orats help <command name> from your terminal. You can also type orats on its own to see a list of all commands.
Create a new orats project:
orats project <TARGET_PATH> --pg-password foo- Configuration:
- Optionally takes:
--pg-location [localhost] - Optionally takes:
--pg-username [postgres] - Optionally takes:
--redis-location [localhost] - Optionally takes:
--redis-password []
- Optionally takes:
- Template:
- Optionally takes:
--template [] - Optionally takes:
--custom []
- Optionally takes:
- Project:
- Optionally takes:
--skip-ansible [false] - Optionally takes:
--skip-server-start [false]
- Optionally takes:
- Ansible:
- Optionally takes:
--sudo-password [] - Optionally takes:
--skip-galaxy [false]
- Optionally takes:
Create an ansible inventory:
orats inventory <TARGET_PATH>- Configuration:
- Optionally takes:
--sudo-password [] - Optionally takes:
--skip-galaxy [false]
- Optionally takes:
Create an ansible playbook:
orats playbook <TARGET_PATH>- Template:
- Optionally takes:
--custom []
- Optionally takes:
Delete a directory and optionally all data associated to it:
orats nuke <TARGET_PATH>- Optionally takes:
--skip-data [false]
Compare differences between orats versions:
orats diff [options]- Optionally takes:
--hosts [] - Optionally takes:
--inventory [] - Optionally takes:
--playbook []
Get a list of available orats templates:
orats templates
Project
The project command kicks off a new orats project. It will always use the
base template and optionally allows you to provide the --template foo flag
where foo would be an available template provided by orats.
You can also supply your own custom template which is explained in the custom template section.
Get started by checking out what the base template provides.
Inventory
The project command automatically creates an inventory for you but it also
has an optional flag to skip doing it by providing --skip-ansible.
In case you decided to --skip-ansible or decided to delete your inventory
from a really old project to let orats generate a new one for you then you can
generate a new inventory on its own.
You may also consider using this command if you happen to use ansible but are not interested in the orats project because here is what it does:
Features
- Create an
inventoryfolder- Create a
hostsfiles- Populate it with a few groups used by an orats project
- Create a
group_vars/all.ymlfile- Populate it with a bunch of configuration for an orats project
- Create a
- Create a
secretsfolder- Generate strong passwords for:
- Your postgres user
- Your redis server
- Your mail account
- Generate tokens for:
- Rails
- Devise
- Devise pepper
- Create a single private/public ssh keypair
- This could be used to send to your servers
- Create self signed ssl certificates to test/support ssl
- Create a monit pem file for its optional http interface over ssl
- Generate strong passwords for:
- Galaxy install the roles required by an orats project
- Optionally turned off with
--skip-galaxy
- Optionally turned off with
Try the inventory command
orats inventory myinventory
Inventory FAQ
What's with the sudo password flag?
Ansible can be installed in a number of ways. A common way is to build a package or use a package manager. When you install ansible this way it
gets installed to /etc/ansible.
By default ansible will download roles from the galaxy to
/etc/ansible/roles which will require sudo to write to.
If you installed ansible to your home directory then orats is smart enough not to use sudo. It will only try to use sudo when it detects a permission error.
You can also choose not to provide the --sudo-password flag but then you
will be prompted for a sudo password about 90% of the way through the
duration of the inventory command.
Playbook
Building your application is only one piece of the puzzle. If you want to ship your application you have to host it somewhere. You have a few options when it comes to managed hosts like Heroku but they tend to be very expensive if you fall out of their free tier.
The playbook template creates an ansible playbook that will provision a ubuntu 12.04 LTS server. It can be hosted anywhere as there are no hard requirements on any specific host.
Server breakdown
Everything is broken up into ansible roles so you can quickly scale out horizontally or by splitting up your server groups such that your database is on a separate server than your application.
- Security:
- Logging into the server is only possible with an ssh key
- Root login is disable
- fail2ban is setup
- ufw (firewall) is setup to block any ports not exposed by you
- All stack specific processes are running with less privileges than root
- User:
- A single deploy user is created
- Services and runtimes:
- Postgres
- Redis
- NodeJS
- Ruby
- Process management:
- Your rails app and sidekiq have
init.dscripts - Your rails app and sidekiq are monitored using
monit
- Your rails app and sidekiq have
Try the playbook command
orats playbook myplaybook
Ansible roles used
nickjj.userhttps://github.com/nickjj/ansible-usernickjj.securityhttps://github.com/nickjj/ansible-securitynickjj.postgreshttps://github.com/nickjj/ansible-postgresnickjj.rubyhttps://github.com/nickjj/ansible-rubynickjj.railshttps://github.com/nickjj/ansible-railsnickjj.wheneverhttps://github.com/nickjj/ansible-whenevernickjj.pumacornhttps://github.com/nickjj/ansible-pumacornnickjj.sidekiqhttps://github.com/nickjj/ansible-sidekiqnickjj.monithttps://github.com/nickjj/ansible-monitnickjj.nodejshttps://github.com/nickjj/ansible-nodejsnickjj.nginxhttps://github.com/nickjj/ansible-nginxDavidWittman.redishttps://github.com/DavidWittman/ansible-redis
All of the above roles will get installed and updated whenever you generate a new orats project.
Diff
The goal of the diff command is to provide you a way to compare your current orats gem to the latest orats gem. It can also compare the difference between the orats generated version of an inventory/playbook to an inventory/playbook that you generated and have customized.
This comes in handy when you want to upgrade orats and your project. You will be able to see if your inventory/playbook are missing any variables or roles and it will also detect custom variables/roles that you have added.
It allows you to make 2 different types of comparisons:
Latest stable version of orats vs your version
When doing this type of comparison it only compares the actual files contained in the orats source code, not your generated inventory/playbook.
This is the type of comparison that is made when you run the diff command
without any arguments. It is useful to run this from time to time to
see if you are missing out on any new features in the latest version.
Your orats version vs your custom project files
If you pass in the --hosts, --inventory and/or --playbook flags along
with a path to each of their files then it will compare the files contained
in the orats source code to your custom generated inventory/playbook.
If you stick with the orats naming convention and directory structure there
are a few quality of life enhancements. If you supply the path to the
inventory folder it will do a comparison on both the inventory and hosts file for you. If you supply the path to a
playbook folder it will automatically choose the site.yml playbook.
Try the diff command
orats diff
Templates
Return a list of available templates to choose from.
Try the templates command
orats templates
Available templates
Base
This is the starter template that every other template will append to. I feel like when I make a new project, 95% of the time it includes these features and when I do not want a specific thing it is much quicker to remove it than add it.
Changes vs the standard rails project
All of the changes have git commits to go with them. After generating a project you can type git reflog to get a list of changes.
- Core changes:
- Use
postgresas the primary SQL database - Use
redisas the cache backend - Use
pumaas the web server - Use
sidekiqas a background worker
- Use
- Features:
- Configure scheduled jobs and tasks using
whenever - Pagination and a route concern mapped to
/pageusingkaminari - Keep a sitemap up to date using
sitemap_generator - Add a
pagescontroller withhomeaction that has points of interest
- Configure scheduled jobs and tasks using
- Rake tasks:
- Daily backups using
backupandwhenever - Generate favicons for many devices based off a single source png
- Daily backups using
- Config:
- Extract a bunch of configuration to environment variables
- Rewrite the database.yml and secrets.yml files to be more dry
- Add a staging environment
- Development mode only:
- Use the
dotenvgem to manage environment variables - Use
foremanto manage the app's processes - Use
bullet,rack mini profilerandmeta_requestfor profiling/analysis - Set
scss/coffeeas the default generator engines
- Use the
- Production mode only:
- Setup log rotation
- Add popular file types to the assets precompile list
- Compress
css/jswhen runningrake assets:precompile
- Change validation errors to output inline on each element instead of a big list
- Helpers:
title,meta_description,headingto easily set those values per viewhumanize_booleanto convert true/false into Yes/Nocss_for_booleanto convert true/false into a css class success/danger
- Views:
- Use
sassandcoffeescript - Use
bootstrap 3.xandfont-awesome - Add a minimal and modern layout file
- Load
jquery1.10.x through a CDN - Conditionally load
html5shiv,json3andrespondjsfor IE < 9 support - Partials:
- Add navigation and navigation links
- Add flash message
- Add footer
- Add google analytics
- Add disqus
- Use
- Public:
- Add 404, 422, 500 and 502 pages so they can be served directly from your reverse proxy
- Add all of the favicons output by the favicon generator
Try the base template
orats project myapp --pg-password foo --skip-galaxy
Base FAQ
What is --pg-password?
Orats will automatically start your server (you can turn this off with a flag) and also run database migrations or generators depending on what you're doing.
In order to do this it must know your postgres location, username and password. By default it will use localhost for the location and postgres as the username but if you need to supply those values because yours are different you can use --pg-location foo and --pg-username bar.
What is --skip-galaxy?
By default the project command will generate ansible related files for you so that you can manage this app's "inventory". It also automatically downloads the ansible roles from the ansible galaxy.
This was done to ensure each app you create has the correct ansible role version to go with it. However, if you installed ansible through apt or somewhere outside of your home directory then you will get permissions errors when it tries to download the roles.
You can fix this by supplying --sudo-password foo to the above command if you know ansible is installed outside of your home directory or you can just wait while the command runs and it will prompt you for your sudo password when it gets to that point because orats will attempt to use sudo only after it fails trying to install the roles without sudo.
If you don't care about the ansible at all you could add --skip-ansible to not generate any ansible files.
Does your redis server use a password?
If your redis server is configured to use a password then you must also pass in --redis-password foo.
What's with the directory structure?
Let's say you were to generate a new project at ~/tmp/myapp, then you would get the following paths:
~/tmp/myapp/inventory
~/tmp/myapp/secrets
~/tmp/myapp/services
The inventory path contains the ansible inventory files for this project. This would be where your host addresses go along with configuration settings for this project.
The secrets path contains the passwords for various things as well as ssh keypairs and ssl certificates. This path should be kept out of version control. You could also go 1 extra step and encrypt this directory locally.
The services path contains your rails application. I like to call it services because you might have multiple services in 1 project.
If you run the command with --skip-ansible you will not get the inventory and services.
What do I need to configure for development?
Pretty much everything is contained within environment variables. They are stored in the .env file located in the root directory of the rails application. It should be self explanatory. This file is also added to .gitignore.
What do I need to configure for production?
If you are using ansible then you should open inventory/group_vars/all.yml and take a peek. Everything there has comments. Assuming you have everything hosted on 1 server then at minimum you will only need to change rails_deploy_git_url to get going.
The above variable is the repo where your code is contained. Ansible will clone that repo in an idempotent way.
You will also need to put the correct server IP(s) in inventory/hosts. At this point that's all you need to change to successfully provision a server.
There are many other variables that you would likely change too such as adding your google analytics UA, S3 keys and all of the mail settings.
You may also want to tinker with the following values for performance reasons based on your server(s).
DATABASE_POOL: 25
PUMA_THREADS_MIN: 0
PUMA_THREADS_MAX: 16
# ensure there are always at least 2 workers so puma can properly do phased restarts
PUMA_WORKERS: "{{ ansible_processor_cores if ansible_processor_cores > 1 else 2 }}"
SIDEKIQ_CONCURRENCY: 25
Auth
This is the auth template which gets merged into the base template. It contains a basic authentication setup using devise and pundit.
Changes vs the base template
All of the changes have git commits to go with them. After generating a project you can type git reflog to get a list of changes.
- Core:
- Handle authentication with
devise - Handle devise e-mails with
devise-async - Handle authorization with
pundit - Add
app/policieswith a basic pundit policy included
- Handle authentication with
- Config:
- Add devise related environment variables
- Set the session timeout to 2 hours
- Expire the auth token on timeout
- Enable account locking based on failed attempts (7 tries)
- Allow unlocking by e-mail or after 2 hours
- Inform users of their last login attempt when failing to login
- Add en-locale strings for authorization messages
- Add devise queue to the sidekiq config
- Add pundit related code to the application controller
- Routes:
- Protect the
/sidekiqend point so only logged in admins can see it - Enable/Disable users from publicly registering by commenting out a few lines
- Protect the
- Database:
- Add a seed user that you should change the details of ASAP once you deploy
- Models:
- Add
Accountdevise model with an extrarolefield- Add
adminandguestroles - Add
.is?method to compare roles - Add
generate_passwordmethod - Add a way to cache the
current_account
- Add
- Add
- Controllers:
- Alias
current_usertocurrent_account - Allow you to override devise's default sign in URL by uncommenting a few lines
- Alias
- Views:
- Use bootstrap for all of the devise views
- Add authentication links to the navbar
- Tests:
- Add
Accountfixtures - Add model tests for
Account
- Add
Try the auth template
orats project myauthapp --auth --pg-password foo --skip-galaxy
Auth FAQ
What do those flags do?
You should read the try the base template section to get an idea of what they do.
What do I need to configure for development?
You may want to change ACTION_MAILER_DEVISE_DEFAULT_FROM in .env.
What do I need to configure for production?
You will want to change ACTION_MAILER_DEVISE_DEFAULT_FROM in inventory/group_vars/all.yml.
Custom
You can pass custom templates into both the project and playbook commands
. It works exactly like passing a custom application template to rails new.
Pass in a custom template by providing the --custom flag along with either a local path or a URL.
Here is a simple example of a custom template:
# /tmp/foo.rb
file 'app/components/foo.rb', " class Foo\n end\n"
Try the custom template
orats project /tmp/customexample -p foo --custom /tmp/foo.rb
Custom FAQ
Any guides on how to make custom templates?
There's the official rails guide on custom application templates.
You can also view the orats project templates to use as inspiration. All of the template files are self contained.
