Basic server setup - Reliably Deploying Rails Applications: Hassle free provisioning, reliable deployment (2014)

Reliably Deploying Rails Applications: Hassle free provisioning, reliable deployment (2014)

7.0 - Basic server setup

Overview

In this chapter we’ll cover:

· Installing some common packages to make your life easier

· Adding a visual queue when we login to a production environment

· Installing common Rails Gem dependencies

In our example repository, basic server setup is primarily contained in the server role roles/server.json with the rails specific elements in roles/rails-app.json.

This chapter will primarily focus on the look_and_feel-tlq cookbook and briefly touch on rails_gem_dependencies-tlq.

Basic Packages

There are some packages it’s worth installing on all servers by default because it’s highly likely they’ll be useful at some point. For this I maintain a simple chef cookbook called look_and_feel-tlq the first section of default.rb looks like this:

1 # htop is a prettier (but more resource intensive) alternative

2 # to top.

3 package 'htop'

4

5 # Vim because we're going to want to edit Rails config files

6 package 'vim'

7

8 # Because not everyone will send us nice .tar.gz files

9 package 'unzip'

10

11 # Add a banner to ssh login if we're in the production environment

This is fairly self explanatory, it simply installs a few admin tools I know I’m likely to need.

Adding a visual queue if you log into a production environment

The next section is more interesting:

1 if node[:environment] == 'production'

2

3 sshd_config = '/etc/ssh/sshd_config'

4

5 seds = []

6 echos = []

7

8 banner_path = '/etc/ssh_banner'

9

10 seds << 's/^Banner/#Banner/g'

11 echos << "Banner #{banner_path}"

12

13 template banner_path do

14 owner 'root'

15 group 'root'

16 mode '0644'

17 source 'production_ssh_banner.erb'

18 end

19

20 bash 'Adding visual flags for production environment' do

21 user 'root'

22 code <<-EOC

23 #{seds.map { |rx| "sed -i '#{rx}' #{sshd_config}" }.join("\n")}

24 #{echos.map { |e| %Q{echo "#{e}" >> #{sshd_config}} }.join("\n")}

25 EOC

26 end

27

28 service 'ssh' do

29 action :restart

30 end

31 end

This section looks to see if our node has:

1 "environment" : "production"

In the root node. If so it adds the contents of the production_ssh_banner.erb template as a banner which will be displayed when logging in via SSH. In my example repository this is simply the word “Production” in ASCI art.

The purpose of this is to minimise the possibility of accidentally SSHing into production and thinking we’re in a staging environment. This is surprisingly easy to do when flicking between multiple servers, particular if using something like zsh for hostname completion.

I recommend forking this recipe and creating your own version which includes the tools and visual tweaks you’re comfortable with.

Rails gem dependencies

A second recipe I install on almost every server is rails_gem_dependencies-tlq. This is a very simple recipe which installs packages often needed when installing rails gems, mine currently looks like this:

1 package 'curl'

2 package 'libcurl3'

3 package 'libcurl3-dev'

4 package 'libmagickwand-dev'

5 package 'imagemagick'

You’ll probably recognise most of these packages if you’ve ever tried to installs gems which do image processing or handle accessing web pages. This is my default version of this recipe which I tend to install on all servers intended to run Rails apps.

If an app contains unusual package dependencies I generally fork this gem and put a copy in site_cookbooks and add the app specific packages to that.

We’ll now move onto setting up default users and public keys on the remote machine.