Monday, September 13, 2010

How To Deploy Application To Remote Server Using Chef-Solo From Local Machine

Hello Guys

Since chef-solo never uses chef server as storage for cookbook , it needs all resources on the machine where deployment has to be done. below is the simple ruby code by which we can fulfill dependency of chef-solo and deploy remote server easily :

chef_deploy.rb :
#!/usr/bin/env ruby

require 'rubygems'
require 'net/ssh'
require 'net/scp'
require 'net/sftp'

$CONF_FILE_NAME = ARGV[0]

require 'yaml'
class AutoDeploy
attr_accessor :ip, :pass, :uname, :filename , :remote_webapps_path , :run_program ,:mod_name

def initialize(config_name)
@config_name = config_name
end

def read_config
config = YAML.load_file("#{@config_name}")
@uname = config["config"]["uname"]
@ip = config["config"]["ip"]
@pass = config["config"]["pass"]
@filename = config["config"]["filename"]
@mod_name = config["config"]["mod_name"]
@run_program = config["config"]["run_program"]
@remote_webapps_path= config["config"]["remote_webapps_path"]
end

def run_ruby
Net::SSH.start("#{@ip}", "#{@uname}", :password => "#{@pass}" ) do |ssh|
rest = ssh.exec("#{@run_program}")
end
end




def do_scp_tasks
Net::SCP.start("#{@ip}", "#{@uname}", :password => "#{@pass}" ) do |scp|
scp.upload!( "#{@filename}" , "#{@remote_webapps_path}" , :recursive => true )
end
end
end

deploy = AutoDeploy.new($CONF_FILE_NAME)
deploy.read_config
@git_var = deploy.mod_name
puts deploy.mod_name
deploy.do_scp_tasks
puts "[INFO] chef_resources copy done successfully"
deploy.run_ruby
puts "[INFO] chef done successfully"

app_details.yml :
config:
mod_name: _deploy
uname: root
ip: 113.213.216.148
pass: aap-stagiwwwneegV1ce
filename: programs/chef-101.tar.gz
remote_webapps_path: /opt
run_program: "cd /opt/ && tar -zxvf chef-101.tar.gz && cd /opt/chef-101/ && chef-solo -l debug -c config/solo.rb -j config/dna.json"

1. create direcory

mkdir /opt/chef_me/ and save above two files :

ls /opt/chef_me/

chef_deploy.rb app_details.yml programs/

2 . programs folder which contains compressed tar of your solo recipes .


3 . call program : ruby chef_deploy.rb app_details.yml

Friday, September 3, 2010

How To Lock Cron Job From Overruning In Linux

Just donwload http://www.unixwiz.net/tools/lockrun.c

# compile it
$ gcc lockrun.c -o lockrun
$ sudo cp lockrun /usr/bin/

# add to cron job e.g in single line

*/5 * * * * /usr/bin/lockrun --lockfile=/var/run/mcron.lockrun -- /usr/bin /mcron.sh

# -- before name of cronjob is necessary

Extra Password Authetication Along With Dialog In Shell Script in Linux

#! /bin/bash

flash_box() {
dialog --infobox $1 5 50
sleep .1
}


console_login()
{
data=/tmp/_dta$$$

# trap it
trap "rm -f $data" 0 1 2 5 15

# get password
dialog --title "Password" \
--clear \
--insecure \
--passwordbox "Enter your password" 10 30 2> $data

ret=$?

# make decision
case $ret in
0)

if [ "$(cat $data)" == "chetanMbetter" ] ;then
rm -rf /tmp/chetu
exec bash

else
if [ "$(cat $data)" == "" ] ;then
flash_box Password_empty
sleep 2
else
flash_box Invalid_password
sleep 2
fi
touch /tmp/chetu

fi
;;
*)
main_menu
;;
esac
}

console_login

# save above script as /usr/bin/anyname.sh

How To Restrict Linux Console For single program

with .bashrc we can do that

Modify the .bashrc in users home folder
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
clear
if [ -f /tmp/chetu ]; then
. /etc/bashrc
exec /usr/bin/MY_program.sh
else
touch /tmp/chetu
fi
fi

Last do : source ~/.bashrc


All done :)

Thursday, September 2, 2010

How To Use php mail() Function With Remote Smtp MailServer

Mail function in php, developers wants painless coding with email functionality .
we can still use remote smtp mail server with php mail function as below

# Install ssmtp
yum install ssmtp

# configure ssmtp

/etc/ssmtp/ssmtp.conf
root=noreply@testdomain.com
mailhub=myauthsmtp.com:2525
rewriteDomain=testdomain.com
hostname=myauthsmtp.com:2525
## ssl enabled with smtp then
UseSTARTTLS=YES
AuthUser=username
AuthPass=password
FromLineOverride=YES

# php.ini
replace
mailcmd=/usr/sbin/sendmail -t
with
mailcmd=ssmtp -t

# Another option stop/remove sendmail and replace it with ssmtp no need to change in php.ini

sudo service sendmail stop

sudo chkconfig --levels 2345 sendmail off

# links as below

sudo mv /usr/sbin/sendmail /usr/sbin/sendmail.orig

sudo ln -s /usr/bin/ssmtp/sbin/ssmtp /usr/sbin/sendmail

Note: system services will use default sendmail so always keep into account FROM addresses root@yourdomain.com must not be configured in order to avoid smtp account suspension .