Wednesday, December 17, 2014

Sending emails from Linux with ssmtp

Install SSMTP

apt-get install ssmtp

Configure the file

nano /etc/ssmtp/ssmtp.conf

#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=thetnswe@gmail.com

# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=smtp.gmail.com:587

# Where will the mail seem to come from?
#rewriteDomain=thetnswe@gmail.com

# The full hostname
hostname=thetnswe@gmail.com

UseSTARTTLS=YES
AuthUser=thetnswe@gmail.com
AuthPass=password

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES

Add the user to allow to send emails

nano /etc/ssmtp/revaliases

# sSMTP aliases
#
# Format:       local_account:outgoing_address:mailhub
#
# Example: root:your_login@your.domain:mailhub.your.domain[:port]
# where [:port] is an optional port number that defaults to 25.

root:thetnswe@gmail.com:smtp.gmail.com:587
localusername:thetnswe@gmail.com:smtp.gmail.com:587

Sending email

sudo ssmtp thetnswe@gmail.com

You will then type your message, hit enter and ‘ctrl+d‘

Now that you have a simple outgoing email server setup, you can do all sorts of neat things:

Creating email Form

nano /emails/email_test1.txt
sudo ssmtp thetnswe@gmail.com < /emails/email_test1.txt

Email send form might looks like this

Subject: Terminal Email Send

Email Content line 1
Email Content line 2



Setting up CRON jobs on shell scripts in Linux (Ubutu)

Open the CRON job editor

crontab -e

After that you can format the job as below

*     *     *     *     *  YOURCOMMAND
-     -     -     -     -
|     |     |     |     |
|     |     |     |     +----- Day in Week (0 to 7) (Sunday is 0 and 7)
|     |     |     +------- Month (1 to 12)
|     |     +--------- Day in Month (1 to 31)
|     +----------- Hour (0 to 23)
+------------- Minute (0 to 59)
There are some shorts, too (if you don´t want the *):
@reboot --> only once at startup
@daily ---> once a day
@midnight --> once a day at midnight
@hourly --> once a hour
@weekly --> once a week
@monthly --> once a month
@annually --> once a year
@yearly --> once a year
If you want to use the shorts as cron (because they don´t work or so):
@daily --> 0 0 * * *
@midnight --> 0 0 * * *
@hourly --> 0 * * * *
@weekly --> 0 0 * * 0
@monthly --> 0 0 1 * *
@annually --> 0 0 1 1 *
@yearly --> 0 0 1 1 *

# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/

//Every 4 hours
This will run the job every 4 hours, on the hours of 00:00, 04:00, 08:00 12:00, 16:00, 20:00.

0 0,4,8,12,16,20 * * * /scripts/test.sh

More info @http://serverfault.com/questions/39004/what-is-the-correct-syntax-to-run-cron-every-4-hours

Start/Stop cron service


sudo service cron restart




Tuesday, December 16, 2014

Some useful linux commands

Give read/write permission to the specific folder

chmod -775 folder_name

//Give read/write permission to all the sub-folders
chmod -775 -R folder_name

//Check rails server
ps aux|grep rails

//Check Ubuntu version
lsb_release -a

Mongodb locations

//Config file
/etc/mongodb.conf

//Actual Storage location file
cd var/lib/mongodb/

/etc/init.d/mongod

Change the owner

sudo chown -R mongodb:mongodb /var/lib/mongodb/

Repair the mongodb is nothing can't be fixed

sudo -u mongodb mongod --repair --dbpath /var/lib/mongodb/

Check all ports

 netstat -tulpn

Show all running processes

ps aux | less

Checking free memory

free -m

Stop Bitnami mysql

sudo ./ctlscript.sh stop mysql

Start the default mysql server

/etc/init.d/mysql start
/etc/init.d#

Sometimes if the Bitnami stack is installed and local mysql is running, it can cause the problem

//End the default mysql
cd /etc/init.d
sudo service mysql stop

Check haproxy log

vim /var/log/haproxy.log

Use HAProxy to loadbalance the ruby processes

Installing HAProxy

apt-get install haproxy

We need to enable HAProxy to be started by the init script. 

nano /etc/default/haproxy

Set the ENABLED option to 1 

ENABLED=1

Test whether it's installed and enabled

sudo service haproxy status

Configuring HAProxy

We'll move the default configuration file and create our own one.

mv /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.backup
Create and edit a new configuration file:

nano /etc/haproxy/haproxy.cfg
Let us begin by adding configuration block by block to this file:

global
        log /dev/log    local0
        log /dev/log    local1 notice
        chroot /var/lib/haproxy
        user haproxy
        group haproxy
        daemon

defaults
        option http-server-close # for slowloris like attacks
        mode http
        timeout http-request 5s # for slowloris like attacks, wait only 5sec for                                                                                                                                                              header
        timeout connect 5000ms
        timeout client 50000ms
        timeout server 50000ms

        log     global
        mode    http
        option  httplog
        option  dontlognull

frontend WebServer-In
    bind *:801
    default_backend Web-Servers

backend Web-Servers
    balance url_param user_id
    server web1 127.0.0.1:3000 maxconn 12 check
    server web2 127.0.0.1:3001 maxconn 12 check
    server web3 127.0.0.1:3002 maxconn 12 check

frontend API-In
    bind *:802
    default_backend API-Servers

backend API-Servers
    balance url_param user_id
    server api1 127.0.0.1:4567 maxconn 6 check
    server api2 127.0.0.1:4568 maxconn 6 check
    server api3 127.0.0.1:4569 maxconn 6 check

listen Stats ip_address:port
    mode http
    stats enable
    stats hide-version
    stats realm Haproxy\ Statistics
    stats uri /
    stats auth username:password

#This include both frontend and backend
listen appname 0.0.0.0:80
    mode http
    stats enable
    stats uri /haproxy?stats
    stats realm Strictly\ Private
    stats auth A_Username:YourPassword
    stats auth Another_User:passwd
    balance roundrobin
    option httpclose
    option forwardfor
    server lamp1 10.0.0.1:80 check
    server lamp2 10.0.0.2:80 check

#Use subdomain and re-route the clusters
frontend http-in
        bind *:80

        # Define hosts
        acl host_bacon hdr(host) -i ilovebacon.com
        acl host_milkshakes hdr(host) -i bobsmilkshakes.com

        ## figure out which one to use
        use_backend bacon_cluster if host_bacon
        use_backend milshake_cluster if host_milkshakes

backend baconcluster
        balance leastconn
        option httpclose
        option forwardfor
        cookie JSESSIONID prefix
        server node1 10.0.0.1:8080 cookie A check
        server node1 10.0.0.2:8080 cookie A check
        server node1 10.0.0.3:8080 cookie A check


backend milshake_cluster
        balance leastconn
        option httpclose
        option forwardfor
        cookie JSESSIONID prefix
        server node1 10.0.0.4:8080 cookie A check
        server node1 10.0.0.5:8080 cookie A check
        server node1 10.0.0.6:8080 cookie A check

Save and close the file
sudo service haproxy start

multiple Subdomain setups

To keep performance at a maximum (avoiding a regex every hit) but still cleaning up the config, I'd use an external file for your ACLs here. For example let's say you had a file called /etc/haproxy/sub1urls, which was exactly this:

apple.gamma.com
banana.gamma.com
cherry.gamma.com
Then in your config the ACL could simply be:

acl is_sub1 hdr(host) -i -f /etc/haproxy/sub1urls
Putting the other hosts in a sub2urls file the same way reduces your config down to:

frontend http-in
    bind *:80

    acl alpha     hdr(host) -i alpha.com
    acl beta      hdr(host) -i beta.com
    acl is_sub1   hdr(host) -i -f /etc/haproxy/sub1urls
    acl is_sub2   hdr(host) -i -f /etc/haproxy/sub2urls
    acl gamma     hdr(host) -i gamma.com

    use_backend a if alpha
    use_backend b if beta
    use_backend sub1 if is_sub1
    use_backend sub2 if is_sub2
    use_backend g if gamma

    default_backend default
This makes it very easy to maintain those other files, since they're just lists of hosts. It opens up the list of who can edit them and exposes less risk as well. For example, we have people editing these ACL lists like this in puppet who don't have to know the HAProxy config syntax at all.

Wednesday, October 1, 2014

Configuring mongo id for rails

Add the following gems to your Gem file of your rails app

gem 'mongoid', github: 'mongoid/mongoid'
gem 'bson'
# gem 'bson_ext', '~> 1.9.2'

make bundle install

rails g mongoid:config

First in config/application.rb comment out the line and add the following to enable mongodb

# require 'rails/all'
require "action_controller/railtie"
require "action_mailer/railtie"
# require "active_resource/railtie"

require "sprockets/railtie"

In config/environments/development.rb, comment out this line
# config.active_record.migration_error = :page_load

Let’s generate our Post first, it will have 3 fields: a title, a body and it can be starred.
rails g scaffold post title:string body:string starred:boolean

Add fields to your model
class Post
  include Mongoid::Document
  include Mongoid::Timestamps

  field :title, type: String
  field :body, type: String
  field :starred, type: Boolean

  index({ starred: 1 })

end

Now we have a command to enforce indexes on the database
rake db:mongoid:create_indexes

if you've the database connection error, try doing the following two things 
uncomment 127.0.0.1 host line in C:\Windows\System32\drivers\etc\hosts
or
in mongoid.yml file, change the hosts to the following
hosts:

        - 127.0.0.1:27017

Read more at
http://requiremind.com/riding-rails-4-along-with-mongoid-and-ruby-2-dot-0/


Adding devise on Mongoid

Configure ActionMailer

Set up action_mailer in your development environment in the file
config/environments/development.rb
by commenting out the line in the file:

# Don't care if the mailer can't send
# config.action_mailer.raise_delivery_errors = false
and adding:
# ActionMailer Config
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
# A dummy setup for development - no deliveries, but logged
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = false
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default :charset => "utf-8"

Set up action_mailer in your production environment in the file
config/environments/production.rb
by adding:

config.action_mailer.default_url_options = { :host => 'yourhost.com' }
# ActionMailer Config
# Setup for production - deliveries, no errors raised
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default :charset => "utf-8"

Installing devise

$ rails generate devise:install
which installs a configuration file:
config/initializers/devise.rb
and a localization file.
Devise will recognize that you already have Mongoid installed and it will set its ORM configuration in the config/initializers/devise.rb file to include:
require 'devise/orm/mongoid'

Adding devise users

Generate a Model and Routes for Users
Devise can manage users and administrators separately, allowing two (or more) roles to be implemented differently. For this example, we just implement Users.

Use Devise to generate models and routes for a User:

$ rails generate devise User
Devise will recognize that Mongoid is installed and set up the User model with

include Mongoid::Document
which must precede all other statements in the model.

Devise will modify the config/routes.rb file to add:
devise_for :users


Add these manually in developer.rb model class to generate routes

  # traditional devise need to manually place in mongodb generated devise
  devise :database_authenticatable, :registerable,

         :recoverable, :rememberable, :trackable, :validatable

Modify the file models/user.rb and add:

field :name
validates_presence_of :name
validates_uniqueness_of :name, :email, :case_sensitive => false
attr_accessible :name, :email, :password, :password_confirmation, :remember_me

rails g devise:views to add view files


Set Up a Demonstration of Devise


$ rails generate controller users show


Note that “users” is plural when you create the controller.

Modify the file app/controllers/users_controller.rb and add:

before_filter :authenticate_user!

def show
  @user = User.find(params[:id])
end
The file config/routes.rb has already been modified to include:

get "users/show"

Remove that and change the routes to:

root :to => "home#index"
devise_for :users
resources :users
Important note: The devise_for :users route must be placed above resources :users.

This line needs to be commented out in config/nitialiser/cookies_serialiser.rb or you will get cookies errors while logging in

# Rails.application.config.action_dispatch.cookies_serializer = :json


Comment out the following line to stop conflict with previous active records

# Do not dump schema after migrations.

  # config.active_record.dump_schema_after_migration = false

Add production config in config/mongoid.yml file

production:
  # Configure available database sessions. (required)
  sessions:
    # Defines the default session. (required)
    default:
      database: analytics_server_rails_production
      # Provides the hosts the default session can connect to. Must be an array
      # of host:port pairs. (required)
      hosts:
        - 127.0.0.1:27017
      options:
        # Change the default write concern. (default = { w: 1 })
        # write:
        # w: 1

        # Change the default consistency model to primary, secondary.
        # 'secondary' will send reads to secondaries, 'primary' sends everything
        # to master. (default: primary)
        # read: secondary_preferred

        # How many times Moped should attempt to retry an operation after
        # failure. (default: The number of nodes in the cluster)
        # max_retries: 20

        # The time in seconds that Moped should wait before retrying an
        # operation on failure. (default: 0.25)
        # retry_interval: 0.25
  # Configure Mongoid specific options. (optional)

  options:



Saturday, September 27, 2014

Using Mongoid with devise for authentication

Install mongoid gem

gem install moped
gem install mongoid

Generating a new rails app

rails new rails3-mongoid-devise -m https://github.com/RailsApps/rails-composer/composer-Rails3_2.rb -T -O

and you will be asked which one you want to generate as a template project

question  Install an example application?
      1)  I want to build my own application
      2)  membership/subscription/saas
      3)  rails-prelaunch-signup
      4)  rails3-bootstrap-devise-cancan
      5)  rails3-devise-rspec-cucumber
      6)  rails3-mongoid-devise
      7)  rails3-mongoid-omniauth
      8)  rails3-subdomains

Choose rails3-mongoid-devise as preferred environment

The application generator template will ask you for additional preferences:

 question  Web server for development?
       1)  WEBrick (default)
       2)  Thin
       3)  Unicorn
       4)  Puma
 question  Web server for production?
       1)  Same as development
       2)  Thin
       3)  Unicorn
       4)  Puma
 question  Template engine?
       1)  ERB
       2)  Haml
       3)  Slim
   extras  Set a robots.txt file to ban spiders? (y/n)
   extras  Use or create a project-specific rvm gemset? (y/n)
   extras  Create a GitHub repository? (y/n)

Configure Devise


You can modify the configuration file for Devise if you want to use something other than the defaults:

config/initializers/devise.rb
Configuration File

The application uses the figaro gem to set environment variables. Credentials for your administrator account and email account are set in the config/application.yml file. The .gitignore file prevents the config/application.yml file from being saved in the git repository so your credentials are kept private. See the article Rails Environment Variables for more information.

Modify the file config/application.yml:


# Add account credentials and API keys here.
# See http://railsapps.github.io/rails-environment-variables.html
# This file should be listed in .gitignore to keep your settings secret!
# Each entry sets a local environment variable and overrides ENV variables in the Unix shell.
# For example, setting:
# GMAIL_USERNAME: Your_Gmail_Username
# makes 'Your_Gmail_Username' available as ENV["GMAIL_USERNAME"]
# Add application configuration variables here, as shown below.
#
GMAIL_USERNAME: Your_Username
GMAIL_PASSWORD: Your_Password
ADMIN_NAME: First User
ADMIN_EMAIL: user@example.com
ADMIN_PASSWORD: changeme
If you are planning to customize the application to send email using a Gmail account, you can add the user name and password needed for the application to send email. See the article Send Email with Rails.

If you wish, set your name, email address, and password for the first user’s account. If you prefer, you can use the default to sign in to the application and edit the account after deployment. It is always a good idea to change the password after the application is deployed.

All configuration values in the config/application.yml file are available anywhere in the application as environment variables. For example, ENV["GMAIL_USERNAME"] will return the string “Your_Username”.

If you prefer, you can delete the config/application.yml file and set each value as an environment variable in the Unix shell.

Set Up a Database Seed File


The db/seeds.rb file initializes the database with default values. To keep some data private, and consolidate configuration settings in a single location, we use the config/application.yml file to set environment variables and then use the environment variables in the db/seeds.rb file.

puts 'DEFAULT USERS'
user = User.create! :name => ENV['ADMIN_NAME'].dup, :email => ENV['ADMIN_EMAIL'].dup, :password => ENV['ADMIN_PASSWORD'].dup, :password_confirmation => ENV['ADMIN_PASSWORD'].dup
puts 'user: ' << user.name

Set the Database


Prepare the database and add the default user to the database by running the commands:

$ rake db:seed
Use rake db:reseed if you want to empty and reseed the database. Or you can use rake db:drop and rake db:setup. The equivalent task for Rails with ActiveRecord is rake db:reset which will be available in Mongoid 4.0.

Set the database for running tests:

$ rake db:test:prepare
If you’re not using rvm, the Ruby Version Manager, you should preface each rake command with bundle exec. You don’t need to use bundle exec if you are using rvm version 1.11.0 or newer.

Change your Application’s Secret Token


If you’ve used the Rails Composer tool to generate the application, the application’s secret token will be unique, just as with any Rails application generated with the rails new command.

However, if you’ve cloned the application directly from GitHub, it is crucial that you change the application’s secret token before deploying your application in production mode. Otherwise, people could change their session information, and potentially access your site without your permission. Your secret token should be at least 30 characters long and completely random.

Get a unique secret token:


rake secret
Edit your config/initializers/secret_token.rb file to add the secret token:

Rails3MongoidDevise::Application.config.secret_token = '...some really long, random string...'
Test the App

You can check that your app runs properly by entering the command

$ rails server

To see your application in action, open a browser window and navigate to http://localhost:3000/. You should see the default user listed on the home page. When you click on the user’s name, you should be required to log in before seeing the user’s detail page.

To sign in as the default user, (unless you’ve changed it) use

email: user@example.com
password: changeme
You should delete or change the pre-configured logins before you deploy your application.

If you test the app by starting the web server and then leave the server running while you install new gems, you’ll have to restart the server to see any changes. The same is true for changes to configuration files in the config folder. This can be confusing to new Rails developers because you can change files in the app folders without restarting the server. Stop the server each time after testing and you will avoid this issue.

Testing MongoDB from sinatra

gem install mongo_kerberos

require 'mongo'
# require 'bson_ext'

include Mongo

MONGO_CLIENT = MongoClient.new # defaults to localhost:27017
# mongo_client = MongoClient.new("localhost", 27017)
# db = client['analytics-learning']

# coll = db['collection-scene-abc']

# CRUD
10.times { |i| coll.insert({ :count => i+1 }) }
puts “There are #collcoll.count total documents. Here they are:” coll.find.each { |doc| puts doc.inspect }
coll.update({ :count => 5 }, { :count => ‘foobar’ })

coll.remove({ :count => 8 }) coll.remove ```

Working with Mongodb on Rails

Installing and running on Windows

Download and installl the mogodb for windows from the following link. If you're using 64bit, choose the 64bit ones.

http://www.mongodb.org/downloads

Now you need to install related gem files

gem install bson
gem install mongo
gem install bson_ext

if the installing of bson_ext failed, trying updating the gems and then t
gem update  --system
gem install bson_ext

if it still doesn't work, try installing the lower version
gem install bson_ext -v=1.9.2

Now set the database path to any place, in this example, I set it to c:/mongodb/data/

mongod.exe --dbpath C:\mongodb\data\
or
mongod.exe if the above command is already run

and by now, the data path will create and you will start seeing the waiting for connections

Running the mongodb shell for testing

C:\mongodb\bin\mongo.exe

Now try running the following command and your mongodb environment is set up successfully.

db.test.save( { a: 1 } )
db.test.find()

Setting up mongodb as a windows service


Configure the System

md C:\mongodb\log
echo logpath=C:\mongodb\log\mongo.log > C:\mongodb\mongod.cfg

Running as service

C:\mongodb\bin\mongod.exe --config C:\mongodb\mongod.cfg --install
net start MongoDB

Stop/Removing the service

net stop MongoDB
C:\mongodb\bin\mongod.exe --remove

Installing and running on Linux/Ununtu

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
Create a /etc/apt/sources.list.d/10gen.list file using the following command

echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/10gen.list
Now you can update your repository

sudo apt-get update
And then install the package

sudo apt-get install mongodb-10gen
To check that it works, just issue a mongo in your shell and you should see something like

MongoDB shell version: x.x.x
connecting to: test
>

Download and install the mongodb for linux
curl -O http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2.6.4.tgz
curl -O http://downloads.mongodb.org/linux/mongodb-linux-i686-2.6.4.tgz (32Bit)
tar -zxvf mongodb-linux-x86_64-2.6.4.tgz

(Copy to target directory - Optional)
mkdir -p mongodb 
cp -R -n mongodb-linux-i686-2.6.4/ mongodb

Configure Search path (Optional)

To ensure that the downloaded binaries are in your PATH, you can modify your PATH and/or create symbolic links to the MongoDB binaries in your /usr/local/bin directory (/usr/local/bin is already in your PATH). You can find the MongoDB binaries in the bin/ directory within the archive.

Running the MongoDB

mkdir -p /data/db
chown mongodb /data/db
Note : You must create the user manually

Starting the MongoDB

mongod or 
mongod --dbpath <some alternate directory>

Ctrl + C to stop the server as usual