Showing posts with label aws. Show all posts
Showing posts with label aws. Show all posts

Thursday, November 26, 2015

Why I moved from capistrano to mina to deploy my app?


To be honest, I have just started to use deployment tools because doing the same thing over and over again was getting very tedious for me. I first started with capistrano and it was good I guess.

Why I prefer Mina over Capistrano?

While Capistrano is a really really great tool , it is terribly slow. Since time is money for every one of us, I wanted a tool that would not be so painfully slow and easy to use, you know , that get shits done. Then I met Mina and instantly fell in love with it when I browsed its homepage and read its slogan "Really fast deployer  and server automation tool" .

Before I was using any deployment tools I was manually ssh`ing into my server and then cloning the repo from the git then running bundle install , rake db:migrate and a lot of stuffs that is not very interesting to do repeatedly. If I had pushed any new updates to the repo I would have to ssh into the server and do the same thing which is time consuming as well as not very productive. So I highly reckon anyone who is not using deployment tools to use it and feel its power. But remember Great power comes with Great responsibility. :)

It is dead simple to get started with Mina. All you have to do is add a gem run the bundler, create a deploy.rb file , fill the right info in it and running mina deploy. The best part is you can write rake tasks in deploy.rb.

Either run gem install mina or add mina gem in your gemfile.
Run bundle install if you chose the later option.
run mina init in your working directory
Here is my deploy.rb file
require 'mina/bundler'
require 'mina/rails'
require 'mina/git'

# 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)

set :user, 'ubuntu'
set :domain, 'www.fuitter.com'
set :deploy_to, '/usr/share/nginx/html/fuitter'
set :repository, 'git@bitbucket.org:mc_cannibal/fuitter2.git'
set :branch, 'master'
set :forward_agent, true

# Manually create these paths in shared/ (eg: shared/config/database.yml) in your server.
# They will be linked in the 'deploy:link_shared_paths' step.
set :shared_paths, ['config/database.yml', 'config/secrets.yml', 'log']

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

# This task is the environment that is loaded for most commands, such as
# `mina deploy` or `mina rake`.
task :environment do
  ruby_version = File.read('.ruby-version').strip
  raise "Couldn't determine Ruby version: Do you have a file .ruby-version in your project root?" if ruby_version.empty?
 queue %{
   source /home/ubuntu/.rvm/bin/rvm
   rvm use #{ruby_version} || exit 1
 }
end

task :setup => :environment do
  queue! %[mkdir -p "#{deploy_to}/#{shared_path}/log"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/log"]

  queue! %[mkdir -p "#{deploy_to}/#{shared_path}/config"]

  # Add the repository server to .ssh/known_hosts
  if repository
    repo_host = repository.split(%r{@|://}).last.split(%r{:|\/}).first
    repo_port = /:([0-9]+)/.match(repository) && /:([0-9]+)/.match(repository)[1] || '22'

    queue! %[
      if ! ssh-keygen -H  -F #{repo_host} &>/dev/null; then
        ssh-keyscan -t rsa -p #{repo_port} -H #{repo_host} >> ~/.ssh/known_hosts
      fi
    ]
  end

  # Create database.yml for Postgres if it doesn't exist
  path_database_yml = "#{deploy_to}/#{shared_path}/config/database.yml"
  database_yml = %[production:
  database: fuitter
  adapter: postgresql
  pool: 5
  timeout: 5000]
  queue! %[ test -e #{path_database_yml} || echo "#{database_yml}" > #{path_database_yml} ]

  # Create secrets.yml if it doesn't exist
  path_secrets_yml = "#{deploy_to}/#{shared_path}/config/secrets.yml"
  secret =
  secrets_yml = %[production:
  secret_key_base:
    #{`rake secret`.strip}]
  queue! %[ test -e #{path_secrets_yml} || echo "#{secrets_yml}" > #{path_secrets_yml} ]

  queue! %[chmod g+rx,u+rwx,o-rwx "#{deploy_to}/#{shared_path}/config"]

end

desc "Deploys the current version to the server."
task :deploy => :environment do
  to :before_hook do
    # Put things to run locally before ssh
  end
  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'
    invoke :'deploy:cleanup'

    to :launch do
      # queue "mkdir -p #{deploy_to}/#{current_path}/tmp/"
      # queue "service #{user} restart"
    end
  end
end

# For help in making your deploy script, see the Mina documentation:
#
#  - http://nadarei.co/mina
#  - http://nadarei.co/mina/tasks
#  - http://nadarei.co/mina/settings
#  - http://nadarei.co/mina/helpers

then run mina setup and finally mina deploy.
That is all there is to it.

Thursday, October 29, 2015

How maggie worn me out ?


Maggie is a facebook app, that lets you add filter with your name and change your profile picture. I would not have bothered building an app that was already built by many talented developers out there. But we offered something different than the rest of those apps. A friend of mine proposed that we add the name of the user in the image because it would make our app a little different than what is out there. I loved the idea. Another friend of mine told me to finish this app as soon as possible, 24 hrs, urgent. When a programmers hear the word "URGENT", they loose control and screw things up. I am no different than them.

I started developing the app, while the login with facebook and getting the logged in users data was finished in a matter of minutes , the main problem was merging the user`s image with the overlay and to make it even worse adding text to the image with a different font style.

Day 1

Finished login the user using fb, getting users data, adding users to database. Then I had to merge those images, so I tried couple of different techniques. The first was using the user`s image as a background and the overlay image in the img tag. But that was a noob mistake. Then I tried canvas. It worked but since I needed to download the image to send it to the graph api, I could not convert the base64 into the real image. So I had to drop that and go with rmagick. After a couple of hours of hair pulling, what seemed impossible was done. So the next step was to add post to facebook and make it profile picture. After a while of googling, it was also solved. Seriously, why do not we call programming, stackoverflowing and we are the stackoverflow bots that rely on the very first result we get on stackoverlfow without even understaing the problem or the system we are working on.

Day2

As my friend wanted, text on the image, I had no idea how to do that. But why fear when stackoverflow is there? Some one already had the problem I had and it was solved too. Ctrl c+ v, god I need to change this habit.  Text was added , fonts was working perfect until , until I had to push it to production. Then it was just hell.  Fonts were not working as excepted. This time no google or no stackoverflow could help me but I was very determined. So I wrote a simple ruby script with the same image manipulation code and ran it and surprisingly it worked. Then I tried it on my rails app but it did not work. I had to google the whole night.

Day3

It was raining, cold and the right excuse to cancel my morning walk. Got my laptop and started working on it again from 6 am. Little did I knew, it was already 10 am and still I had no luck plus I forgot to take my medications. Is that determination?? haha I hope so. Then I made the rails app to execute the ruby script I wrote to manipulate image but again it did not worked. I still have no idea why? Why was it not working? I finally gave up and settled for the default font. Now even though there are some problems with the app , I feel like I have accomplished something that I thought I was incapable to develop.

There is something I have learned though.
If a certain tool that you are using, does not make your life easier, then stop using it.
Brace yourself while pushing the code to production mode and pray to Buddha (God).
Never push security keys to VC`s.
Premature optimization can be hell.
First get the functionality that your app depends upon then work on the rest.
TDD development is for those who have still plenty of time left to code.