Awesome Web Blog

GCP:4 Deployment Using Mina

| Comments

Introduction

This is the last blog which deals with the GCP deployment, here our main area of topic is the mina “a blazing fast deployment tool”. Which is executed as a Rake.

Need to install and setup is pretty, no worries.Advantage is that we check-in the code to the github.

Only the place is the config/deploy.rb.Also we can implement DRY principle to the deployment script. Which means;we can use the same script to deploy the app to staging, QA and even Production.

mina github link

How the deployment process done ??

We are dividing the deployment to below section to easily understand and deployment.

  1. setup and install mina gem
  2. mina Rake
  3. script to deploy
  4. running the Rake.
  5. setup nginx and unicorn

1.Setup and install mina gem

Add the below gem to the development group.

1
  gem 'mina', '~> 1.2', '>= 1.2.3'

Now what we need to add is the mina-unicorn gem, this will used to deploy unicorn with mina.

1
  gem 'mina-unicorn', '~> 2.0'

then do

1
  bundle install

2.mina Rake

mina works like a rake task.There is a another command

1
  mina init

it will create the configuration file in the config/deploy.rb

Hooray, A major part is done, now we need to add the tasks we are going to use in the deploy.rb

You can use your own tasks, based on the taste, but i would like to follow the old practices like the setup and deploy

setup

In this rake, we are going to deals with the basic details like creating the folders, creating the temp folders, etc.

deploy

This rake mainly deals with clone the app and kill the pid, bundle install, migrate the app, migrate, precompile the assets.

3.mina script

deploy.rb is the heart of the mina tool. We can configure that based on our requirement.

I will give you basic explanation about the each of the commands here and updated the basic skeleton of the deploy.rb and also for verification, will updated with my own deploy.rb which is used in my blog_app.

skeleton of the deploy.rb

copy of my original deploy.rb file

Now our scripts are done.Copy this file into your app/config/deploy.rb file and

run the commands below,

4.Running the Rake

Before running the rake, ensure that you are added the mina gem and mina-unicorn and bundle installed.

first run the command

1
  mina setup

Now the setup execution is completion is success, there is nothing to worry in this, now we are moving to the deploy command.

1
  mina deploy

ah!!, We got the first error.

That issue is because we don’t specify the database and the connector, then user name and password in the path.In order to do that, go to our server terminal

Now we go to the app path , here it is blog_app/

Now go to the blog_app/ folder go to the shared/config/ there is a file called the database.yml and add the connection details there.

1
2
3
4
5
6
7
8
staging:
  adapter: postgresql
  encoding: unicode
  database: blog_app_staging
  pool: 5
  host: localhost
  username: blog_user
  password: password

ah!, we got another error.

That error comes because there is no database configured. It is possible to create the database from the mina tool;but we will do via console and we will explain that in another blog.

Now, we will create a database by logging to the server and using the below command

1
  sudo -i -u postgres

then using the below command, generate the database

1
  createdb databasename

Next is grant access to the user to the new database.

1
  grant all privileges on database databasename to username;

Please check the above image before executing the above command.

Now again will do the mina deploy

Awesome, we are good to go, our app is deployed,but it is not for accessing our browser, need to do some tweaks, Great working guys.

5.setup nginx and unicorn

Now, I want to check whether the nginx can be accessible via ip-address.

just go to ip-address in the browser,

Awesome, you got the output, now only some tweak is needed.

first update the nginx configuration with below configuration, it is normal config, only we need to know about the pid file and error logs.

now copy the config to the /etc/nginx/sites-enabled/default file.

then restart the nginx using the

1
  sudo service nginx restart

now nginx is okay, please check the browser we are hitting and also check the log if any errors are happening.Log will be in the below path

/var/log/nginx/error.log and /var/log/nginx/access.log

Awesome!!, it is hitting the nginx, but the page is not loaded, so the issue is with the unicorn, we will check the unicorn logs for this.

unicorn log will be in the blog_app/shared/log/unicorn.stderr.log

tailing this error will reveal the real issue (ie tail -f nicorn.stderr.log)

cool, the error is because, we have not set the secret key base, in the secrets.yml, need to set that and then proceed.

by running the below command from the current path in the server console,

1
  bundle exec rake secret

then update the command in the config/shared/secrets.yml with like below

the reason for staging, we are deployed to staging server.

Now, repeat the steps like we already done

  1. restart the nginx

  2. check the status of the nginx

  3. mina deploy from the project terminal(not from the GCP terminal!!!)

  4. tail -f /var/log/nginx/access.log to check it is hitting or not!!!

  5. go to the ip-adress in the browser,

hooray, we are all done, now we need to explore additional options like,how to store the images, login issues etc.

Please comment me if any issues happend

Comments