Tutorial

How To Deploy With Mina: Getting Started

Published on January 31, 2014
author

O.S. Tezer

How To Deploy With Mina: Getting Started

Status: Deprecated

This article covers a version of CentOS that is no longer supported. If you are currently operating a server running CentOS 6, we highly recommend upgrading or migrating to a supported version of CentOS.

Reason: CentOS 6 reached end of life (EOL) on November 30th, 2020 and no longer receives security patches or updates. For this reason, this guide is no longer maintained.

See Instead: This guide might still be useful as a reference, but may not work on other CentOS releases. If available, we strongly recommend using a guide written for the version of CentOS you are using.

Introduction


If you are looking for a way to facilitate some of the actions you perform daily, as a simple search will show, there are more than a handful of tools available. If you check them you’ll see that all of them claim to make the procedure of putting your application online (i.e. deploying it) easier for you. However, they hardly differ from one another - and they can be quite complicated to use as well.

Mina deployer and automation tool, which might appear no different than the rest, is actually quite special. In this DigitalOcean article we are going to learn a different way to automate remote server tasks, covering a large area from managing processes to actually deploying applications, using Mina: a Bash script generator and remote execution tool built on RAKE.

Glossary


1. Understanding Mina & Rake


  1. Mina Deployer And Automation Tool
  2. RAKE - Ruby Make

2. Getting Mina


  1. Preparing The System
  2. Setting Up Ruby Environment and Rails
  3. Installing Mina

3. Getting Started With Using Mina


  1. Initiating Mina
  2. Creating A Deployer User On The Deployment Server

4. Working With config/deploy.rb


  1. Defining The Server
  2. Deploying Applications
  3. Defining Deployment Tasks
  4. Working With Tasks & Sub-Tasks

5. Example: Deploying A Rails Application


Understanding Mina And RAKE


Fortunately, there is no “rocket science” behind Mina. It is a very simple to use Ruby/RAKE tool that helps you with automating execution of remote tasks - usually related to deployment.

Mina Deployer And Automation Tool


Mina is a Ruby based tool. It is there to help people with creating scripts which contain a list of tasks to perform, grouped under a name, to facilitate everyday operations. Mina converts these RAKE based programming files to Bash scripts, connects to a defined remote server, and executes them.

It’s actually pretty fast, and perhaps as fast as a tool of its genre can get, since all it does is connect to the server and run the Bash programmes.

Although some criticize Mina and call it primitive, with its excellent features (e.g. queuing of tasks, very handy!) a lot can be achieved very smoothly, without hiccups, with great speed.

To provide you a better understanding of not just Mina but such tools in general, here is a short list of what can be done and when Mina might come in handy:

  • Preparing and setting up a server: If you need to repeatedly create and destroy virtual servers, you will be spending a lot of time setting them up. Using Mina, you can automate the process.

  • Deploying applications: Whether you have HTTP server applications in place or not, you can use Mina to update your application’s source code on the deployment machine(s) and automatically bring the new version up and running without dealing with FTP or other tools.

  • Managing processes: This sounds very generic, but imagine just how many times (probably per day) you need to manage some process running on a server, such as an application server, a proxy, or your typical Nginx front-facing reverse proxy. With Mina’s tasks, you can facilitate this and gain a good amount of momentum – keeping everything organized at the same time.

  • Living in a Rails-less world: Mina can do all the above, and more, without having any strict reliance, dependence, or allegiance to Rails. With Mina, you can deploy almost everything.

Mina’s deploy.rb scripts are actually RAKE files with a bunch of grouped tasks. Everything is written in Ruby without dealing with other sorts of configurations – you do not need to get familiar with anything new that you’ll hardly ever use again.

RAKE - Ruby Make


Web developers, programmers, and anyone dealing with science or computers should have a good understanding of the tools they use. In Mina’s case, the magic comes from RAKE.

However, what is RAKE?

RAKE is a series of components, tools, and definitions similar in nature to GNU Make. Make is one of the most widespread and popular utilities, which has been built into Unix systems since its introduction in late 1970s. It became really popular for its system-independent nature and the ability it offers to combine commands and instructions into a single file, which are referred as “makefiles”. These files and Make as a tool are used to build applications (i.e. compile the source code and generate executable binaries).

Rake can be referred to as a build language. It is used for easily defining tasks in a structured way to be executed in the command line. By nature, these commands are usually system administration related, such as application deployment or managing servers. This structure of Rake can be referred as a domain specific language, written in Ruby, whereby everything is defined and set in a certain way within boundaries. It is called a build tool since, as described previously, its operations are usually related to constructing something (e.g. a library, an application, etc.)

RAKE can be obtained through RubyGems.

To visit the official RAKE project page, click here. To learn more about RAKE, check out this excellent, detailed article on the subject: Using the Rake Build Language.

Getting Mina


When using Mina, you need to make sure to have a stable Ruby environment with necessary dependencies installed and operating fine – such as gem.

In this section, we’ll begin with creating a Ruby environment on a CentOS 6 system. From thereon, you will be able to use Mina to automate procedures on your deployment servers.

Preparing The System


In order to install Ruby, we first need to prepare the minimally shipped CentOS server.

Run the following command to update the default tools of your CentOS based VPS:

yum -y update

# This command will update all the base applications
# that come with CentOS by default. Which are mostly
# reserved for use by the operating system. 

Install the bundle containing development tools by executing the following command:

yum groupinstall -y 'development tools'

# With more recent versions of CentOS, such as 6.5 in our case,
# you can simply run:
# yum groupinstall -y development
# instead.

# This bundle of applications contains various tools
# Such as: gcc, make, automake, binutils, git etc.

Some of the handy libraries and packages (e.g. libyaml-devel, nginx etc.) are not found within the official CentOS repository. To simplify things and not to deal with manually installing them, we will add the EPEL software repository for YUM package manager to use. This will enable the simple download of additional softare.

# Enable EPEL Repository
sudo su -c 'rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm'

# Update everything, once more.
yum -y update

Finally, we need to get curl-devel and several other tools and libraries for this tutorial (e.g. Rails needs sqlite-devel).

In order to install them, run the following:

yum install -y curl-devel nano sqlite-devel libyaml-devel

Setting Up Ruby Environment and Rails


Note: This section is a summary of our dedicated article How To Install Ruby 2.1.0 On CentOS 6.5.

We are going to be using Ruby Version Manager (RVM) to download and install a Ruby interpreter.

Run the following two commands to install RVM and create a system environment for Ruby:

curl -L get.rvm.io | bash -s stable
source /etc/profile.d/rvm.sh

Finally, to finish installing Ruby on our system, let’s get RVM to download and install Ruby version 2.1.0:

rvm reload
rvm install 2.1.0

Since Rails needs first and foremost a JavaScript interpreter to work, we will also need to set up Node.js. For this purpose, we will be using the default system package manager YUM.

Run the following to download and install nodejs using yum:

yum install -y nodejs

Execute the following command to download and install rails using gem:

gem install bundler rails

Installing Mina


There are a couple of ways to download Mina. The simplest way is to use RubyGems.

Run the following to download and install Mina:

gem install mina

Getting Started With Using Mina


When introducing mina to your project, the first things that is needed is to initiate mina inside the project directory.

Afterwards, you can edit the config/deploy.rb created by Mina to define your server and tasks to perform.

Initiating Mina


Enter your project directory and run the following command:

# Enter your project directory:
cd ~/project_dir

# Initiate Mina
mina init

# -----> Created ./config/deploy.rb
#        Edit this file, then run `mina setup` after.

Creating A Deployer User On The Deployment Server


It is a sane choice to create a user, other than root, to perform the actions of deployment which will be used and invoked by Mina. For this purpose, let’s create on the remote host a deployer user.

Create a new system user:

adduser deployer

Set up deployer’s password:

passwd deployer

# Enter a password
# Confirm the password

Edit /etc/sudoers using the text editor nano:

nano /etc/sudoers

Scroll down the file and find where root is defined:

..

## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere
root    ALL=(ALL)	ALL

..

Append the following right after root ALL=(ALL) ALL:

deployer ALL=(ALL) ALL

This section of the /etc/sudoers file should now look like this:

..

## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere
root     ALL=(ALL)	ALL
deployer ALL=(ALL) ALL

..

Press CTRL+X and confirm with Y to save and exit.

Working With config/deploy.rb


Next, open up the deploy.rb file, created by Mina, to define your deployment server and to set tasks.

nano config/deploy.rb

Defining The Server


The first step is to edit the :domain line to set your server’s address.

Find and amend the relevant configuration to match your virtual server’s:

# Basic settings:
#   domain	 - The hostname to SSH to.
#   deploy_to    - Path to deploy into.
#   repository   - Git repo to clone from. (needed by mina/git)
#   branch	 - Branch name to deploy. (needed by mina/git)

# Default:
# set :domain, 'foobar.com'

# Change the default to match your server's address
# Example:
set :domain, 'server.domain.tld'
# or
# set :domain, '162.243.74.190'

# Optional settings:
#   set :user, 'foobar'    # Username in the server to SSH to.
#   set :port, '30000'     # SSH port number.

set :user, 'deployer'
set :port, '22'

Deploying Applications


Using Mina, you can easily deploy your applications from a central and hosted repository such as Github. As mentioned in the previous section, you can achieve this with setting the deploy_to and repository parameters, as well as branch if necessary.

Example:

# Application's deployment location on the remote server
# Example:
set :deploy_to, '/var/www/my_app'

set :repository, 'git://...'
set :branch, 'master'

Defining Deployment Tasks


Mina’s deploy.rb file comes with some example tasks set, targeting Rails for mina deploy command.

Example:

desc "Deploys the current version to the server."
task :deploy => :environment do
  deploy do
    # Put things that will set up an empty directory into a fully set-up
    # instance of your project.
    invoke :'git:clone'
    invoke :'deploy:link_shared_paths'
    invoke :'bundle:install'
    invoke :'rails:db_migrate'
    invoke :'rails:assets_precompile'

    to :launch do
      queue "touch #{deploy_to}/tmp/restart.txt"
    end
  end
end

Working With Tasks & Sub-Tasks


You can define and chain tasks with Mina which can be really handy in many scenarios.

Below is an example of working with tasks and chaining them from the official documentation:

task :down do
  invoke :maintenance_on
  invoke :restart
end

task :maintenance_on
  queue 'touch maintenance.txt'
end

task :restart
  queue 'sudo service restart apache'
end

Example: Deploying A Rails Application


To learn about how to deploy an actual Rails application and how to work with deploy.rb accordingly, check out How To Use Mina to Deploy a Ruby on Rails Application in the DigitalOcean community articles section.

Note: Using the same principals, you can deploy any type of application with Mina.

<div class=“author”>Submitted by: <a href=“https://twitter.com/ostezer”>O.S. Tezer</a></div>

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
O.S. Tezer

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
1 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Excellent, thank you!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.