Friday, October 23, 2015

YAML Loader - Ruby

Sample YAML Loader:


Assume your yaml file 'email_credential.yml' exists in same directory and in the format below:

email:
  smtp_server: smtp.gmail.com
  smtp_port: 587


Here is the simple snippet to load YAML and collect data:

begin
  yml = YAML.load_file("email_credentials.yml")
  
  email = yml['email']['smtp_server']
  eport = yml['email']['smtp_port']
  
rescue Exception => e
  puts "Email credential YAML loading issue \n" + e.to_s
end

Website Monitor - Ruby Script

Simple Ruby Script file to check the web site and post an email using gmail service.

This script contains 2 main parts

  1. Checking website - for response OK and content
  2. Email to admin on failure [using gmail]

Checking Website:

    url = URI.parse(web_url)
  net_conn = Net::HTTP.new(url.host, url.port)
  net_conn.use_ssl = url.scheme == "https"
  res = net_conn.get('/')

   We can check res.code == 200 and verify res.body includes the content we wanted to see.

Email [Using gmail]

  NOTE: Gmail needs TLS encryption; so enable it before start sending msg
   
    smtp = Net::SMTP.new("smtp.gmail.com", 587)
    smtp.enable_starttls #gmail needs this encryption
    smtp.start("DOMAIN_FILLER", <gmail_acc>, <gmail_pwd>, :login)  do |s| 
      s.send_message(<msg>, <gmail_acc>, <to>)
    end  

  Sample GMAIL Msg format:
   msg = [
    "From: WebMonitor <from_addr>",
    "To: Admin <to_addr>",
    "Subject: <subject>",
    "",
    "Hi,",
    "!!MORE DETAILED MSG!!"
  ].join("\r\n");


Gist: https://gist.github.com/sivajankan/8278d75c39bc5a8260ae

The script can be added to crontab to do automated testing

Thursday, August 20, 2015

Selenium - IE remote browser setup

Setup Selenium -  IE remote browser


  1. Install Java. If you are setting up from a base level testing VM, you may need to first install Java.
  2. Download following files from https://code.google.com/p/selenium/downloads/list into your windows machine (This page mostly has latest versions. Pick them)
    • a. selenium-server-standalone-2.39.0.jar
    • b. IEDriverServer _Win32_2.39.0.zip (There is version for 64 bit; pick appropriate version)
  3. Place the jar file and extracted exe file from zip, into C:\SeleniumServer folder
  4. In Command Prompt and navigate to 'C:\SeleniumServer'
  5. Run the command to start the standalone selenium server
java -jar selenium-server-standalone-2.35.0.jar -Djava.util.logging.Level=OFF -Dwebdriver.ie.driver="C:\SeleniumServer\IEDriverServer.exe"

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*