Friday, June 21, 2019

Vagrant: Built Customize Vagrant Box

Built Customize Vagrant Box

We can make customize box from our vagrant existed box. With this, we can change our environment in existing box and customize it according our need.




Scripting

// In your Vagrant Init file directory
vagrant up
// Get in to your vagrant environment
vagrant ssh

/*
 * Do your custumize in here
 * For example. I will remove my existing ruby version from vagrant box and change it to new one
 */

sudo apt-get update -y

sudo apt-get install build-essential zlib1g-dev libssl-dev libreadline-dev \
git-core curl libyaml-dev libcurl4-dev libsqlite3-dev apache2-dev -y

curl --remote-name http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p194.tar.gz

tar zxf ruby-1.9.3-p194.tar.gz

cd ruby-1.9.3-p194/

./configure

make

sudo make install

// Check if my new ruby version successfully installed
ruby -v

// If succeed quit from vagrant ssh
exit

// Adding your customize box to new box
vagrant package

// Name it
vagrant box add lucid64_with_ruby193 package.box

// Check for your new box
vagrant box list

- lucid32
- lucid64
- lucid64_with_ruby193

// Activate your vagrant 
vagrant init lucid64_with_ruby193





Share:

Tuesday, June 18, 2019

Breakman Rails: How to avoid Mass Assignment Warning

Breakman is static analysis security scanner for Ruby on Rails. It's open source vulnerability scanner specifically designed for Ruby on Rails applications. It statically analyzes Rails application code to find security issues at any stage of development.

I will share some trick to avoid Breakman Mass Assignment medium settings warning.
  1. Pay attention to your model relationship. My suggestion always use Nested Attributes .
  2. If necessary used attr_protected to relationship variable key in your model.
Below is some of example case I've solved:
  • Example Case 1 "Without Relationship" :
         Set relationship variable in attr_protected to avoid Breakman warning.
      attr_accessible :name, :description
      attr_protected : user_id
         Do this saving method:
            create :
         data_list = {:name => name, :description => description}
        saving_create = self.new(data_list)
        saving_create.user_id = user_id
        saving_create.save
             update :
        data_list = {:name => name, :description => description}
        user_data = self.find_by_id(1)
        user_data.attributes = data_list 
        user_data.user_id = user_id
        user_data.save

  • Example Case 2 "With Relationship" :
      attr_accessible :name, :description
      attr_protected : user_id

      has_one :category
      has_many :products

      accepted_nested_attributes :category, :products

            create :
        products = []
        data_list = {:name => name, :description => description}
        saving_create = self.new(data_list)
        saving_create.user_id = user_id
        all_data.each do |d|
          saving_process = saving_create.products.build
          saving_process.product_id = d[:product_id] 
          saving_process.product_name = d[:product_name]
        end
        saving_create.save
      update :
        products.each do |p|
        data_list = {:name => p.name, :description => p.description}
         update_process = self.find_by_id(p.user_id)
         update_process.attributes = data_list
         update_process.product_id = p.id
         update_process.product_name = p.id
         unless update_process.save
            raise ActiveRecord::Rollback 

         end
        end
      

Share:

Friday, June 14, 2019

Laravel 4 Scheduller with dispatcher


Dispatcher is a Laravel artisan command scheduling tool used to schedule artisan commands within your project so you don't need to touch your crontab when deploying.

For Futher Information go to this link Dispatcher.


  • Do you hate to touch crontab to add scheduller in laravel?
  • Do you hate with all crontab configuration when add scheduller in different servers?
Congratulations,  Laravel has package to handle this problem using Dispatcher. Everthing become simple and easy with this package. Doubt? Here we go!

Tool Kit

  • PHP 5.3+ or HHVM
  • Laravel 4

Configurations

In your application add this to your "require" :{} composer.json

    "indatus/dispatcher": "1.4.*@dev"

remember to do composer update.
In your app/config/app.php providers array add this line:

'Indatus\Dispatcher\ServiceProvider',

Usage

scheduled
  scheduled:make              Create a new scheduled artisan command
  scheduled:run               Run scheduled commands
  scheduled:summary           View a summary of all scheduled artisan commands

Build scheduled command

php artisan scheduled:make yourCommands

Register scheduled command

In Your app/start/artisan.php add this line depend on Your Command :


Artisan::add(new yourCommands);

Scripting

Go to your command app/command/yourCommands and change 

// my artisan command will be php artisan cron:mycommands
protected $name = 'cron:yourCommands';

// Configure this functions to change schedule time
public function schedule(Schedulable $scheduler)
{
  // Change this function to change time
  return $scheduler->daily();
}

This is Scheduler Changes time function example list:

//every day at 4:17am
return $scheduler->daily()->hours(4)->minutes(17);

//every Monday/Friday at 8:30am
return $scheduler->daysOfTheWeek([
                Scheduler::MONDAY,
                Scheduler::FRIDAY
            ])->hours(8)->minutes(30);

// Using Raw Commands
//every other day at 1:59am, 13:59pm and 23:59pm
return $scheduler->setSchedule(59, [1,13,23], '*/2', '*', '*');

// Every minutes
return $scheduler->everyMinutes();

// Every 20 minutes
return $scheduler->everyMinutes(10);

// Every hours
return $scheduler->everyHours();

Cron Setup

In console add

crontab -e
* * * * * php {{ path to your app}}/artisan scheduled:run 1>> /dev/null 2>&1

Debugging

If your cron not running check this

// Via console, check your mcrypt
php -i | mcrypt.

// Via console
php artisan scheduled:run --debug 
backup:avatars: No schedules were due
     command:name: No schedules were due
     myTestCommand:name: No schedules were due
     cache:clean: /usr/bin/env php /Users/myUser/myApp/artisan cache:clean > /dev/null &
     mail:subscribers: /usr/bin/env php /Users/myUser/myApp/artisan mail:subscribers > /dev/null &

// Via Console
php artisan scheduled:summary
//It works if output something like this:
+----------------+------------------+-----------+--------+------+--------------+-------+-------------+--------+
| Environment(s) | Name             | Args/Opts | Minute | Hour | Day of Month | Month | Day of Week | Run as |
+----------------+------------------+-----------+--------+------+--------------+-------+-------------+--------+
| *              | schedule:test    |           | */5    | *    | *            | *     | *           |        |
+----------------+------------------+-----------+--------+------+--------------+-------+-------------+--------+

Share: