Monday, July 6, 2015

Cap Deploy [Capistrano]

Capistrano

Ref:
http://capistranorb.com/
https://github.com/capistrano/ # Shows list of all capistrano repos
https://github.com/capistrano/capistrano
https://github.com/capistrano/rails
https://github.com/capistrano/passenger



Gemfile


gem 'capistrano'
gem 'capistrano-rails'
gem 'capistrano-passenger'
gem 'capistrano-rvm'  #if your preference is RVM



$> bundle install
$> bundle exec cap install #Make sure files are created
$> bundle exec cap install STAGES=production  #add additional env seperated by ','

Limited gem installation [selected env]
$> bundle install --without development test


Capfile


require 'capistrano/rails'

#or
require 'capistrano/rvm'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
require 'capistrano/passenger'

#For rbenv
require 'capistrano/rbenv'
require 'capistrano/chruby'



Ref: http://capistranorb.com/documentation/getting-started/configuration/

config/deploy.rb


set :application, '<app_name>'
set :repo_url, 'git@.... <git repo>'

set :linked_files, fetch(:linked_files, []).push('config/database.yml')
set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system')

set :keep_releases, 5



config/deploy/production.rb

server '<dest_server>', user: '<dest_server_user>', roles: %w{web db app}
set :deploy_to, "/var/www/html/<dir>"

set :passenger_restart_command, 'passenger-config restart-app'

set :passenger_restart_options, -> { "#{deploy_to} --ignore-app-not-running" }

Adding tasks

Ref: http://capistranorb.com/documentation/getting-started/cold-start/

in create lib/capistrano/tasks/access_check.rake file, and add

  namespace :check do
    desc "Check that we can access everything"
    task :write_permissions do
      on roles(:all) do |host|
        if test("[ -w #{fetch(:deploy_to)} ]")
          info "#{fetch(:deploy_to)} is writable on #{host}"
        else
          error "#{fetch(:deploy_to)} is not writable on #{host}"
        end
      end
    end
  end

$> bundle exec cap -T #to check the task existence
$> bundle exec cap production check:write_permissions #to verify the write permission


[Still working on Cap Scripting]

Basic Commands

cap production deploy:check
cap production deploy 
cap production deploy:rollback



Advance Comments [from capistrano Git page]


# list all available tasks
$ bundle exec cap -T

# deploy to the staging environment
$ bundle exec cap staging deploy

# deploy to the production environment
$ bundle exec cap production deploy

# simulate deploying to the production environment
# does not actually do anything
$ bundle exec cap production deploy --dry-run

# list task dependencies
$ bundle exec cap production deploy --prereqs

# trace through task invocations

$ bundle exec cap production deploy --trace


Advance Learning

https://github.com/capistrano/sshkit


Deploy issue with git

When you get: git stdout: permission denied (publickey).

$> eval $(ssh-agent)
$> ssh-add



Rails Life

Ruby Self Help Steps

Installation required Ruby setups!

http://code.tutsplus.com/tutorials/how-to-install-ruby-on-a-mac--net-21664
https://gorails.com/setup/osx/10.10-yosemite
http://railsapps.github.io/installrubyonrails-mac.html

Xcode installation if mac
RVM installation
Ruby installation
git installation
PostgreSQL or SQLite3


RVM Ruby version Preparation

rvm current #shows current default
rvm list #lists local installation

rvm get stable #get latest stable version
rvm reload # reload rvm
rvm list remote #all available to install
rvm install 2.2.5
rvm reinstall 2.2.0 --disable-binary

rvm --default use 2.1.5 #to make 2.1.5 as default

gem setup
$ echo 'gem: --no-document' >> ~/.gemrc

To create gemset
$ rvm use 2.2.1@gemset-2.2.1 --create

Install rails before the next step
$ gem install rails

Good start for first project
Ref: https://github.com/RailsApps/rails-composer/

#sudo gem install rails
rails new myapp -m https://raw.github.com/RailsApps/rails-composer/master/composer.rb



#Standard way
$rails new <project_name>

use .ruby-version  file to specify ruby version for the project
Example: 2.2.1@gemset_2_2_1

rspec
https://relishapp.com/rspec/rspec-rails/docs/gettingstarted

in Gemfile, development and test section
gem "rspec-rails"

bundle install
rails generate rspec:install

Remember: rake db:test:prepare

rake spec #to run tests


Cucumber
in Gemfile - development 

gem 'cucumber-rails', :require => false
gem 'database_cleaner'

  
bundle install
rails generate cucumber:install


Haml

add
gem "haml-rails", "~> 0.9" #Check version from https://github.com/indirect/haml-rails


rails generate haml:application_layout convert

rake haml:erb2haml # to convert existing erb files to haml



Devise
Add gem "devise" in Gemfile 
bundle install

rails generate devise:install
Follow suggestions


Registeration Email issue

Google account access blocking
SMTPAuthenticationError

http://stackoverflow.com/questions/18124878/netsmtpauthenticationerror-when-sending-email-from-rails-app-on-staging-envir

Add gem 'sass-rails' in Gemfile





Pry
http://pryrepl.org/


pry -r ./config/environment.rb





Passenger for Rails : CentOS

System Info

CentOS Linux release 7.0.1406 (Core) 
3.7GB Memory 
50GB diskspace
DUAL Core - Intel(R) Xeon(R) CPU E5-2680 v2 @ 2.80GHz
X86_64 - Architecture

Ref: 
https://www.digitalocean.com/community/tutorials/how-to-use-capistrano-to-automate-deployments-getting-started
https://www.digitalocean.com/community/tutorials/how-to-setup-a-rails-4-app-with-apache-and-passenger-on-centos-6

Root access:
yum -y update
yum groupinstall -y 'development tools'
curl -L get.rvm.io | bash -s stable  
  -- didnt work; and suggested next step in error
  >gpg2 --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

curl -L get.rvm.io | bash -s stable
source /etc/profile.d/rvm.sh 
rvm reload
rvm get stable
rvm requirements
rvm reload
rvm install 2.1 #installed 2.1.5p273
rvm --default use 2.1.5

CONTINUE
https://www.rosehosting.com/blog/install-ruby-on-rails-with-apache-and-passenger-on-centos-6/

install rvm for newuser


adduser newuser
passwd newuser 
usermod -G wheel newuser #pick a fair name for new user
visudo #uncomment next line
  #%wheel ALL=(ALL) NOPASSWD:ALL
vim /etc/ssh/sshd_config #add next line to avoid ssh access using new user name
  DenyUsers newuser
service sshd restart
su - newuser

sudo yum -y update
sudo yum -y install curl curl-devel httpd-devel httpd mod_ssl
sudo curl -L get.rvm.io | bash -s stable  

  gpg2 --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
#then sudo curl -L get.rvm.io | bash -s stable  
rvm get stable
rvm reload
rvm install 2.1.5
rvm use 2.1.5 --default
vim ~/.gemrc #put following lines to avoid lot of ri doc installations

---
:backtrace: false
:benchmark: false
:bulk_threshold: 1000
:sources:
- http://rubygems.org/
- http://gemcutter.org
:update_sources: true
:verbose: true
gem:
  --no-document
  --no-ri
  --no-rdoc

gem install rails
gem install passenger
passenger-install-apache2-module #follow the suggestions


PostgreSQL Installation

CentOS

#Postgres installation
ref: https://wiki.postgresql.org/wiki/YUM_Installation
ref2: http://karolgalanciak.com/blog/2013/07/19/centos-6-4-server-setup-with-ruby-on-rails-nginx-and-postgresql/

#as root
vim /etc/yum.repos.d/CentOS-Base.repo
 #add following line into [base] and [updates] section
 exclude=postgresql*

yum localinstall http://yum.postgresql.org/9.4/redhat/rhel-6-x86_64/pgdg-centos94-9.4-1.noarch.rpm
yum install postgresql94-server

yum install postgresql94 postgresql94-devel postgresql94-server postgresql94-libs postgresql94-contrib


Installation is complete: next setup
/usr/pgsql-9.4/bin/postgresql94-setup initdb
  #service postgresql-9.4 init db - failed
  #systemctl initdb postgresql-9.4.service - didnt work

chkconfig postgresql-9.4 on #postgres start on startup

#CONFIG 
vi /var/lib/pgsql/9.3/data/pg_hba.conf

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5


service postgresql-9.4 restart


#db setup
https://wiki.postgresql.org/wiki/First_steps

$>su - postgres
$>psql
psql>alter user postgres with password 'postgres-user-password';
psql>ctrl+d
exit

$> su - newuser #user in rails installation
$> psql postgres -U postgres #postgres password from previous step

psql>create user username with password 'secretPassword'; #username - new db account user
psql>create database testdb owner=username;


To delete postgres:
yum erase postgresql94*




Sunday, July 5, 2015

Git

Git server
http://git-scm.com/book/en/v2/Git-on-the-Server-Setting-Up-the-Server

cd ~

git config --global merge.ff false

mkdir repos
cd repos
mkdir ur_project.git
cd ur_project.git
git init --bare


Local machine

git remote add <urid>  git@your_ip:repos/ur_project.git
git remote -v


#To see the added url

git remote show origin


Git Cheat Sheet
ref: http://git.or.cz

Remember: git command --help

Global Git configuration is stored in $HOME/.gitconfig (git config --help)

Create
From existing data
cd ~/projects/myproject
git init
git add .

From existing repo
git clone ~/existing/repo ~/new/repo
git clone git://host.org/project.git
git clone ssh://you@host.org/proj.git

Show

Files changed in working directory
git status

Changes to tracked files
git diff

What changed between $ID1 and $ID2
git diff $id1 $id2

History of changes
git log

History of changes for file with diffs
git log -p $file $dir/ec/tory

Who changed what and when in a file
git blame $file

A commit identified by $ID
git show $id

A specific file from a specific $ID
git show $id:$file

All local branches
git branch

List remote
git remote -v

git remote --help


Git Repo Browser
git instaweb --httpd webrick

Log
git log
git reflog

Youtube video:
  https://www.youtube.com/watch?v=sevc6668cQ0


http://stackoverflow.com/questions/6565357/git-push-requires-username-and-password
A common mistake is cloning using the default (HTTPS) instead of SSH. You can correct this by going to your repository, clicking the ssh button left to the URL field and updating the URL of your origin remote like this:
git remote set-url origin git@github.com:username/repo.git