Wednesday, May 16, 2018

Vagrant Rails Puppet



Vagrant & Puppet to easily provision virtual machines that can be used as isolated development environments for your projects. You'll keep your development machine fast, your environments isolated, and any developer will be able to get up and running on a new project in less than 5 minutes.

Puppet automates server provisioning by formalizing a server’s configuration into manifests. Puppet’s manifests are text files that contain declarations written in Puppet’s domain-specific language (DSL). When we’ve defined our configuration using this DSL, Puppet will ensure that the machine’s files, packages, and services match the settings we’ve specified


Configuration


// Initialize vagrant
vagrant up

// Enter Vagrant SSH
vagrant ssh
// Built Git repository for manifest. built it if not exist
ls -l /etc/puppet/.git/
// First we need to create a user and group for Puppet to use
sudo useradd --comment "Puppet" --no-create-home \ --system --shell /bin/false puppet

// Installing puppet as ruby gem. This will install puppet and facter
sudo gem install puppet -v 2.7.12 --no-rdoc --no-ri

facter is the tool that Puppet uses to gather information about the system in order to populate variables and determine which parts of our.

// Run Facter
facter

// Built manifests and site.pp in /etc/puppet
cd /etc/puppet
mkdir manifests
cd manifests
sudo nano site.pp

write this configuration:
Exec {
  path => "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
}

// Commit this change
git add .
git commit -m "Init Puppet manifests"

Puppet Configurations


puppet help apply

// Run puppet
sudo puppet apply --verbose manifests/site.pp


When developing a Puppet configuration, it’s helpful to pass the --verbose flag to Puppet so that it displays any extended information that might be available if an error happens. We’ll see output like this from puppet apply:

 info: Applying configuration version '1319427535' info: Creating state file /var/lib/puppet/state/state.yaml notice: Finished catalog run in 0.02 seconds We’ll also see a few lines of output like the following ones.

This is because the Puppet developers are still bringing the codebase up-to-date for Ruby 1.9.3; these should go away in future Puppet releases.

 Could not load confine test 'operatingsystem': cannot load such file \ --  puppet/provider/confine/operatingsystem

 We’ve successfully built a tiny Puppet repository and executed Puppet on our VM. Now we’ll build out the VM with a standard Rails application stack. That is, we’ll configure Apache, MySQL, and Passenger, and we’ll put the Rails application directory tree into place. Along the way, we’ll learn all about how Puppet helps us manage our VM’s services and configuration.

Study Case

Installing Apache  on a server

In our site.pp. add this conf:
Exec {
path => "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
}
package {
"apache2":
ensure => present
}
service {
"apache2":
ensure => true,
enable => true
}


This package declaration is a fine example of the Puppet DSL. There’s a type declaration, package, followed by a block delimited by curly braces. That block contains a title for the instance of the type that we’re configuring, apache2.
That title is associated with various parameters that affect how Puppet will act on the type. In this case, we’ve declared an ensure parameter that’s set to present. This declaration is our way to tell Puppet that it should ensure there is a package called apache2 installed on the system.

service apache ensure true it means setting Puppet to attempt the service if it’s not running. And enable means ensures the service will start on boot


// Apply puppet
sudo puppet apply --verbose manifests/site.pp

If there's error message: "Could not load confine test 'operatingsystem' ". That's just update issue and harmless. If affecting your process just update your RoR versions.




Share:

Sunday, April 8, 2018

Vagrant in Ubuntu Linux

As a developers, we have a lot of development environment depends on our projects, sometimes each project has different environment specifications and it's difficult to re adjust.

With Vagrant we can make development environment for PHP 5.3 and PHP 5.4, MySQL, SQLite, MongoDB, Postgres, PEAR, PHPUnit, Rails 3.1, Memcached, Redis, Gearman, NodeJS in instance. It means, we doesn't need to remove and install different development environment for each of our projects. We just need  Vagrant  to manage our instance virtual machines.




First Step
     Installing VirtualBox to your machine:

  • sudo apt-get install virtualbox
  • sudo apt-get install virtualbox-dkms
   Or you can download VirtualBox from https://www.virtualbox.org/wiki/Downloads, then install that package in your local machine.

Second Step
     Installing Vagrant:
  • sudo apt-get install vagrant
Third Step
     Go to Your directory projects or create new directory projects.
  • mkdir <project_name> && cd <project_name>
  • echo “Hello! with sync to Virtual Machine” > Readme.md
Fourth Step
     Initialize projects with Vagrant project Environment
  • vagrant init
   This will create "Vagrantfile" file.

Five Step
     Open "Vagrantfile" file, it will contain something like this:

  VAGRANTFILE_API_VERSION = "<no>"
  Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    config.vm.box = "<name_your_box>"
    config.vm.box_url = "<path_to_package.box>"
    config.vm.network :private_network, ip: "<the.ip.add.number>"
    config.vm.provision :puppet do |puppet|
      puppet.manifests_path = "<path/to/manifests/folder>"
      puppet.manifest_file = "<name_file_puppet.pp>"
    end
  end

     This is sample of Vagrant configuration file:

  VAGRANTFILE_API_VERSION = "2"
  Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    config.vm.box = "trusty64"
    config.vm.box_url = "https://oss-binaries.phusionpassenger.com/vagrant/boxes/latest/ubuntu-14.04-amd64-vbox.box"
    config.vm.network :private_network, ip: "192.168.0.33"
    config.vm.provision :puppet do |puppet|
      puppet.manifests_path = "manifests"
      puppet.manifest_file = "default.pp"
    end
  end


     Descriptions:
  • config.vm.box = This configures what box the machine will be brought up against. The value here should be the name of an installed box or a shorthand name of a box in HashiCorp's Atlas.
  • config.vm.box_url = for setup source of package vagrant box, for complete list of vagrant box you can found here : http://www.vagrantbox.es/ .If not specified, no checksum comparison will be done. If specified, Vagrant will compare the checksum of the downloaded box to this value and error if they do not match. Checksum checking is only done when Vagrant must download the box.
  • config.vm.network = ip that use for communication between VM and local.
  • config.vm.provision = Configures provisioners on the machine, so that software can be automatically installed and configured when the machine is created. Please see the page on provisioners for more information on how this setting works.
  • puppet.manifests_path = path to folder manifests where to put file manifest configuration. (required by puppet)
  • puppet.manifest_file = name of file manifests configuration.

Six Step
     Setup Puppet Configurations:
  • mkdir manifests && cd manifests
  • touch default.pp
Seven Step
     Next open file <project_directory>/manifests/default.pp, add :

class system-update {
  # execute 'apt-get update'
  exec { 'apt-update': # exec resource named 'apt-update'
    command => '/usr/bin/apt-get update --fix-missing' # command this resource will run
  }
}
include system-update
class apache-setup {
  # install apache2 package
  package { 'apache2':
    require => Exec['apt-update'], # require 'apt-update' before installing
    ensure => installed,
  }
  # ensure apache2 service is running
  service { 'apache2':
    ensure => running,
  }
}
include apache-setup
# install php5 package
package { 'php5':
  require => Exec['apt-update'], # require 'apt-update' before installing
  ensure => installed,
}
# ensure info.php file exists
file { '/var/www/html/info.php':
  ensure => file,
  content => '<?php phpinfo(); ?>', # phpinfo code
  require => Package['apache2'], # require 'apache2' package before creating
}

Eight Step

  • vagrant up

Sample of working Local configurations.

Notes

  • To Login to Vagrant Box:  vagrant ssh
  • cd vagrant/
  • Congratulations, you're in VagrantBox!






Share:

Friday, October 24, 2014

Build Background Job in Laravel Using Cron Job

Cron Job with Laravel running with Artisan Command. So we have to make command using Artisan.

Generate Class.
php artisan command:make FooCommand 
php artisan command:make [[your command]]
This command will generate a class in app/commands.

Write Your Function
Write your functions in your generated command fire() method.

Register Command
Write: Artisan::add(new [[your command]]);
In app/start/artisan.php

Runs Cron Job
Write in terminal : crontab -e
Write:
* * * * * php [[path to application]]/[[your application]]/artisan background:process
* * * * * : it means this cron job will be executed every minutes
Share:

Wednesday, July 23, 2014

Record Scenario With Jmeter

Many of Us has difficulties to Record Scenario with Jmeter. Most problems appears in configurations and browser connection. I will try to explain step by step to setup Jmeter for recording scenario.


  1.  Right Click in Test Plan > Add > Thread Groups.
  2.  Right Click in Thread Groups > Add > HTTP Request Defaults. And filled your Server name or IP to be recorded by Jmeter.

  3. Right Click in Workbench > Non Test Elements > HTTP Proxy Servers. And fill available port, I used 8080.

  4. Drag HTTP Proxy Servers to below HTTP Request Defaults. Use "Insert Before" options. It should become like this:

  5. Right Click in Thread Groups > Logic Controller > Recording Controller.
  6. Right Click in Thread Groups > Listener > View Result Tree
  7. Right Click in Thread Groups > Listener > View Result in Table
  8. Go to File > Save As Test Plan. Save it.
  9. Open your FireFox browser. Go to Edit > Preferences > Advanced > Network > Settings

  10. Clik OK. And Open Jmeter.
  11. Go to HTTP Proxy Servers and click Start.
  12. Go to Your Browser, Type URL, and do the scenario.
  13. In Recording Controller it will appear some result, Expand it for further details.


            (Example)
  14. To watch Generation Time and more advanced recording, In your Jmeter go to Run start and open View Result Tree, it will collected all data in previous scenarios.
Share: